KaijuSaveEarth/src/ecs/UIFontComponent.h
2021-01-29 21:14:20 -05:00

84 lines
1.6 KiB
C++

/*
* UIFontComponent.h
*
* Created on: May 14, 2020
* Author: ayoungblood
*/
#ifndef SRC_ECS_UIFONTCOMPONENT_H_
#define SRC_ECS_UIFONTCOMPONENT_H_
#define ASCII_START_IDX 32
#define ASCII_COUNT 96
#define ASCII_ROW_COUNT 16
#include "Components.h"
#include "SDL2/SDL.h"
#include "../assetmgr/AssetManager.h"
class UIFontComponent : public Component
{
private:
// SDL_Texture * texture;
// Font dimensions
int _LetterWidth;
int _LetterHeight;
// Track current letter on texture
SDL_Rect _LetterClips[ASCII_COUNT];
public:
// SDL_RendererFlip spriteFlip = SDL_FLIP_NONE;
SDL_Texture* texture;
SDL_Rect srcRect, destRect;
Vector2D position;
// UIFontComponent() = default;
UIFontComponent(std::string id, int letterW, int letterH, int xpos, int ypos, int scale)
{
texture = Game::assets->GetTexture(id);
srcRect = UIFontComponent::SetCharClips(texture, xpos, ypos);
position.x = xpos;
position.y = ypos;
destRect.x = xpos;
destRect.y = ypos;
destRect.w = letterW * scale;
destRect.h = letterH * scale;
}
~UIFontComponent()
{
SDL_DestroyTexture(texture);
}
void update() override
{
}
void draw() override
{
TextureManager::Draw(texture, srcRect, destRect, SDL_FLIP_NONE);
}
SDL_Rect SetCharClips(SDL_Texture* fontTex, int x, int y)
{
SDL_Rect letterClip;
for (int i = 0; i < ASCII_COUNT; ++i)
{
_LetterClips[i].x = x + ((i % ASCII_ROW_COUNT) * _LetterWidth);
_LetterClips[i].y = y + ((i / ASCII_ROW_COUNT) * _LetterHeight);
_LetterClips[i].w = _LetterWidth;
_LetterClips[i].h = _LetterHeight;
letterClip = _LetterClips[i];
}
return letterClip;
}
};
#endif /* SRC_ECS_UIFONTCOMPONENT_H_ */