first commit
This commit is contained in:
216
src/ecs/SpriteComponent.h
Normal file
216
src/ecs/SpriteComponent.h
Normal file
@ -0,0 +1,216 @@
|
||||
/*
|
||||
* SpriteComponent.h
|
||||
*
|
||||
* Created on: Feb 22, 2020
|
||||
* Author: ayoungblood
|
||||
*/
|
||||
|
||||
#ifndef SRC_ECS_SPRITECOMPONENT_H_
|
||||
#define SRC_ECS_SPRITECOMPONENT_H_
|
||||
|
||||
#define ASCII_START_IDX 32
|
||||
#define ASCII_COUNT 96
|
||||
#define ASCII_ROW_COUNT 16
|
||||
|
||||
#include "Components.h"
|
||||
#include "SDL2/SDL.h"
|
||||
#include "../assetmgr/TextureManager.h"
|
||||
#include "Animation.h"
|
||||
#include <map>
|
||||
#include "../assetmgr/AssetManager.h"
|
||||
#include "../cjson/cJSON.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
|
||||
class SpriteComponent : public Component
|
||||
{
|
||||
private:
|
||||
TransformComponent *transform;
|
||||
SDL_Texture *texture;
|
||||
SDL_Rect srcRect, destRect;
|
||||
char letter;
|
||||
int frames = 0;
|
||||
int speed = 100;
|
||||
int letterWidth, letterHeight;
|
||||
int scale = 1;
|
||||
|
||||
public:
|
||||
enum SpriteType : std::size_t
|
||||
{
|
||||
spriteUIL0,
|
||||
spriteUIL1,
|
||||
spriteText,
|
||||
spriteObject,
|
||||
spriteTileMap,
|
||||
spriteActor,
|
||||
spriteAnimation,
|
||||
spriteAtlas
|
||||
};
|
||||
SpriteType spriteType;
|
||||
int animIndex = 0;
|
||||
bool ui = false;
|
||||
std::map<const char*, Animation> animations;
|
||||
|
||||
SDL_RendererFlip spriteFlip = SDL_FLIP_NONE;
|
||||
|
||||
// SpriteComponent() = default;
|
||||
SpriteComponent(std::string id, SpriteType sType)
|
||||
{
|
||||
spriteType = sType;
|
||||
setTex(id);
|
||||
}
|
||||
|
||||
// One initializer to rule them all
|
||||
SpriteComponent(std::string id, SpriteType sType, std::string json)
|
||||
{
|
||||
spriteType = sType;
|
||||
if(sType == spriteAnimation)
|
||||
{
|
||||
std::ifstream jsonText(json);
|
||||
std::ostringstream tmp;
|
||||
tmp << jsonText.rdbuf();
|
||||
std::string aJson = tmp.str();
|
||||
cJSON * animJson = cJSON_Parse(aJson.c_str());
|
||||
cJSON * meta = cJSON_GetObjectItem(animJson, "meta");
|
||||
cJSON * frameTags = cJSON_GetObjectItem(meta,"frameTags");
|
||||
// printf("frameTags:\n%s\n",cJSON_Print(frameTags));
|
||||
int tagsCount = cJSON_GetArraySize(frameTags);
|
||||
// printf("number of tags: \n%d\n",tagsCount);
|
||||
// cJSON * arrItem = cJSON_GetArrayItem(frameTags,0);
|
||||
// cJSON * animItem = cJSON_GetArrayItem(frameTags, 0);
|
||||
// printf("Animation item: \n%s\n",cJSON_Print(animItem));
|
||||
// printf("arrItem: \n%s\n",cJSON_Print(arrItem));
|
||||
for (int t = 0; t < tagsCount; t++)
|
||||
{
|
||||
// printf("Tag: \n%d\n",t);
|
||||
// printf("tag number: \n%d\n",t);
|
||||
cJSON * animItem = cJSON_GetArrayItem(frameTags,t);
|
||||
// printf("Animation item: \n%s\n",cJSON_Print(animItem));
|
||||
cJSON * nameJson = cJSON_GetObjectItem(animItem, "name");
|
||||
const char * name = cJSON_Print(nameJson);
|
||||
int fromFrame = cJSON_GetObjectItem(animItem, "from")->valueint;
|
||||
int toFrame = cJSON_GetObjectItem(animItem, "to")->valueint;
|
||||
Animation anim = Animation(fromFrame,toFrame,100);
|
||||
animations.emplace(name, anim);
|
||||
Play(name);
|
||||
// printf("Animation name: \n%s\n",name);
|
||||
// printf("animation.frames: %d\n", animations[name].frames);
|
||||
// printf("animations.index: %d\n", animations[name].index);
|
||||
// printf("animations.speed: %d\n", animations[name].speed);
|
||||
}
|
||||
|
||||
// if(!animations.empty()){
|
||||
// printf("animations found!\n");
|
||||
// }else{
|
||||
// printf("No animations\n");
|
||||
// }
|
||||
// Play("S-Fly");
|
||||
setTex(id);
|
||||
}
|
||||
}
|
||||
|
||||
SpriteComponent(std::string id, SpriteType sType, char fontLetter, int letterW, int letterH, int letterScale)
|
||||
{
|
||||
spriteType = sType;
|
||||
setTex(id);
|
||||
// text = isText;
|
||||
letter = fontLetter;
|
||||
letterWidth = letterW;
|
||||
letterHeight = letterH;
|
||||
scale = letterScale;
|
||||
destRect.w = letterW*letterScale;
|
||||
destRect.h = letterH*letterScale;
|
||||
}
|
||||
|
||||
SpriteComponent(std::string id, SpriteType sType, SDL_Rect srcR, SDL_Rect destR)
|
||||
{
|
||||
spriteType = sType;
|
||||
setTex(id);
|
||||
srcRect = srcR;
|
||||
destRect = destR;
|
||||
}
|
||||
|
||||
~SpriteComponent()
|
||||
{
|
||||
SDL_DestroyTexture(texture);
|
||||
}
|
||||
|
||||
void setTex(std::string id)
|
||||
{
|
||||
texture = Game::assets->GetTexture(id);
|
||||
}
|
||||
|
||||
void init() override
|
||||
{
|
||||
transform = &entity->getComponent<TransformComponent>();
|
||||
|
||||
switch(spriteType)
|
||||
{
|
||||
case spriteText:
|
||||
srcRect.x = ((letter-ASCII_START_IDX) % ASCII_ROW_COUNT)*letterWidth;
|
||||
srcRect.y = ((letter-ASCII_START_IDX)/ASCII_ROW_COUNT)*letterHeight;
|
||||
srcRect.w = letterWidth;
|
||||
srcRect.h = letterHeight;
|
||||
destRect.w = letterWidth*scale;
|
||||
destRect.h = letterHeight*scale;
|
||||
break;
|
||||
case spriteUIL0: case spriteUIL1:
|
||||
break;
|
||||
case spriteActor: case spriteAnimation: case spriteAtlas: case spriteObject: case spriteTileMap:
|
||||
srcRect.x = srcRect.y = 0;
|
||||
srcRect.w = transform->width;
|
||||
srcRect.h = transform->height;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void update() override
|
||||
{
|
||||
switch(spriteType)
|
||||
{
|
||||
case spriteUIL0: case spriteUIL1: case spriteText:
|
||||
destRect.x = static_cast<int>(transform->position.x);
|
||||
destRect.y = static_cast<int>(transform->position.y);
|
||||
break;
|
||||
case spriteActor: case spriteObject: case spriteAtlas:
|
||||
srcRect.y = animIndex * transform->height;
|
||||
destRect.x = static_cast<int>(transform->position.x) - Game::camera.x;
|
||||
destRect.y = static_cast<int>(transform->position.y) - Game::camera.y;
|
||||
destRect.w = transform->width * transform->scale;
|
||||
destRect.h = transform->height * transform->scale;
|
||||
break;
|
||||
case spriteTileMap:
|
||||
break;
|
||||
case spriteAnimation:
|
||||
srcRect.x = srcRect.w * static_cast<int>((SDL_GetTicks()/speed) % frames);
|
||||
srcRect.y = animIndex * transform->height;
|
||||
destRect.x = static_cast<int>(transform->position.x) - Game::camera.x;
|
||||
destRect.y = static_cast<int>(transform->position.y) - Game::camera.y;
|
||||
destRect.w = transform->width * transform->scale;
|
||||
destRect.h = transform->height * transform->scale;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void draw() override
|
||||
{
|
||||
TextureManager::Draw(texture, srcRect, destRect, spriteFlip);
|
||||
}
|
||||
|
||||
void Play(const char* animName)
|
||||
{
|
||||
frames = animations[animName].frames;
|
||||
animIndex = animations[animName].index;
|
||||
speed = animations[animName].speed;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif /* SRC_ECS_SPRITECOMPONENT_H_ */
|
Reference in New Issue
Block a user