New UITextComponent implemented
This commit is contained in:
@ -16,6 +16,6 @@
|
||||
#include "PlayerController.h"
|
||||
#include "ProjectileComponent.h"
|
||||
#include "TileComponent.h"
|
||||
#include "UIFontComponent.h"
|
||||
#include "UITextComponent.h"
|
||||
|
||||
#endif /* SRC_COMPONENTS_H_ */
|
||||
|
@ -1,81 +0,0 @@
|
||||
/*
|
||||
* 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(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_ */
|
109
src/ecs/UITextComponent.h
Normal file
109
src/ecs/UITextComponent.h
Normal 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_ */
|
||||
|
Reference in New Issue
Block a user