New UITextComponent implemented

This commit is contained in:
2022-02-03 17:59:50 -05:00
parent 40095cb8b7
commit 72a941810d
8 changed files with 212 additions and 141 deletions

109
src/ecs/UITextComponent.h Normal file
View File

@ -0,0 +1,109 @@
/*
* UITextComponent.h
*
* Created on: Feb 22, 2020
* Author: ayoungblood
*/
#ifndef SRC_ECS_UITEXTCOMPONENT_H_
#define SRC_ECS_UITEXTCOMPONENT_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 "../assetmgr/AssetManager.h"
#include <stdio.h>
#include <string>
#include <iostream>
#include <tuple>
// Should include <iostream> and <tuple> to make an array of tuples with two SDL_Rect type vars to feed to the draw function.
class UITextComponent : public Component
{
private:
TransformComponent *transform;
SDL_Texture *texture;
SDL_Rect srcRect, destRect;
// char letter;
std::string text;
int letterWidth, letterHeight;
int scale = 1;
SDL_RendererFlip spriteFlip = SDL_FLIP_NONE;
std::tuple <SDL_Rect, SDL_Rect> letter;
public:
UITextComponent(std::string id, std::string textToPrint, int letterW, int letterH, int letterScale)
{
setTex(id);
text = textToPrint;
letterWidth = letterW;
letterHeight = letterH;
scale = letterScale;
// destRect.w = letterW*letterScale;
// destRect.h = letterH*letterScale;
}
~UITextComponent()
{
SDL_DestroyTexture(texture);
}
void setTex(std::string id)
{
texture = Game::assets->GetTexture(id);
}
void init() override
{
transform = &entity->getComponent<TransformComponent>();
}
void update() override
{
destRect.x = static_cast<int>(transform->position.x);
destRect.y = static_cast<int>(transform->position.y);
}
void draw() override
{
// This should be updated to iterate through an array of each letter's srcRect and destRect
for (int l = 0; l < text.length(); l++)
{
std::tuple<SDL_Rect, SDL_Rect> lttr = getLetterTexture(text[l],l);
TextureManager::Draw(texture, std::get<0>(lttr), std::get<1>(lttr), spriteFlip);
}
}
std::tuple<SDL_Rect, SDL_Rect> getLetterTexture(char currentLetter, int i)
{
std::tuple<SDL_Rect, SDL_Rect> letterTuple;
srcRect.x = ((currentLetter-ASCII_START_IDX) % ASCII_ROW_COUNT)*letterWidth;
srcRect.y = ((currentLetter-ASCII_START_IDX)/ASCII_ROW_COUNT)*letterHeight;
srcRect.w = letterWidth;
srcRect.h = letterHeight;
destRect.x = static_cast<int>(transform->position.x)*scale+i*letterWidth*scale;
destRect.y = static_cast<int>(transform->position.y)*scale;
destRect.w = letterWidth*scale;
destRect.h = letterHeight*scale;
letterTuple = std::make_tuple(srcRect,destRect);
return letterTuple;
}
void updateString(std::string newString)
{
text = newString;
}
};
#endif /* SRC_ECS_UITEXTCOMPONENT_H_ */