New UITextComponent implemented

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

Binary file not shown.

View File

@ -1,7 +1,7 @@
{
"GameName":"Beagle Rescue",
"WindowName":"Beagle Rescue",
"WindowSize":{"w":640,"h":360},
"WindowSize":{"w":320,"h":240},
"WindowFullScreen": 0,
"GlobalScale": 3
"GlobalScale": 1
}

View File

@ -16,6 +16,6 @@
#include "PlayerController.h"
#include "ProjectileComponent.h"
#include "TileComponent.h"
#include "UIFontComponent.h"
#include "UITextComponent.h"
#endif /* SRC_COMPONENTS_H_ */

View File

@ -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
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_ */

View File

@ -18,20 +18,20 @@
#include <fstream>
#include <sstream>
#include "../assetmgr/AssetManager.h"
#include "../ui/UIText.h"
// #include "../ui/UIText.h"
#include "../ui/UINineSlice.h"
#include "../cjson/cJSON.h"
Map* map;
Manager manager;
UIText* text;
// UIText* text;
UINineSlice* my9Slice;
UIText* scoreboardText;
// UIText* scoreboardText;
UINineSlice* scoreboard9Slice;
UINineSlice* debugBox;
UIText* debugStaticText;
UIText* debugdynamicText;
UIText* debugJumpText;
// UIText* debugStaticText;
// UIText* debugdynamicText;
// UIText* debugJumpText;
GameStateManager* Game::gsm = new GameStateManager();
@ -141,8 +141,8 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
// std::string myText = "Find lost puppies!\nThey need your help!";
std::string myText = "Press U to Start";
text = new UIText(myText, "font", 0, 0, 8, 12, globalScale);
text->ParseString(myText, 12, 22, globalScale, "text",Game::groupUI_Layer1);
// text = new UIText(myText, "font", 0, 0, 8, 12, globalScale, "start instructions", Game::groupUI_Layer1);
// text->ParseString(myText, 12, 22, globalScale, "text",Game::groupUI_Layer1);
SDL_Rect myDestRect = SDL_Rect();
myDestRect.x = 12;
myDestRect.y = 8;
@ -151,39 +151,42 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
my9Slice = new UINineSlice("textBox");
my9Slice->MakeSlices("textBox",32,32,14,16,14,16,myDestRect,globalScale,Game::groupUI_Layer0);
uiInfo.addComponent<TransformComponent>(160,120,64,32,1);
uiInfo.addComponent<UITextComponent>("font", "UI Text works again!", 8, 12, 1);
uiInfo.addGroup(groupUI_Layer3);
// debug UI text
UIText* debugStaticText2;
/* UIText* debugStaticText2;
UIText* debugStaticText3;
UIText* debugStaticText4;
UIText* debugStaticText5;
UIText* debugStaticText6;
UIText* debugStaticText7;
UIText* debugStaticText7; */
std::string debugStaticString = "Collision";
debugStaticText = new UIText(debugStaticString, "font", 0,0,8,12,1);
debugStaticText->ParseString(debugStaticString,camera.w-100*globalScale,14*gScale,1,"debug",Game::groupUI_Layer3);
std::string debugStaticString2 = "Hori:";
debugStaticText2 = new UIText(debugStaticString2, "font", 0,0,8,12,1);
debugStaticText2->ParseString(debugStaticString2,camera.w-100*globalScale,24*gScale,1,"debug",Game::groupUI_Layer3);
std::string debugStaticString3 = "Vert:";
debugStaticText3 = new UIText(debugStaticString3, "font", 0,0,8,12,1);
debugStaticText3->ParseString(debugStaticString3,camera.w-100*globalScale,34*gScale,1,"debug",Game::groupUI_Layer3);
std::string debugStaticString4 = "Jump:";
debugStaticText4 = new UIText(debugStaticString4, "font", 0,0,8,12,1);
debugStaticText4->ParseString(debugStaticString4,camera.w-100*globalScale,44*gScale,1,"debug",Game::groupUI_Layer3);
std::string debugStaticString5 = "P.y :";
debugStaticText5 = new UIText(debugStaticString5, "font", 0,0,8,12,1);
debugStaticText5->ParseString(debugStaticString5,camera.w-100*globalScale,54*gScale,1,"debug",Game::groupUI_Layer3);
std::string debugStaticString6 = "P.dy:";
debugStaticText6 = new UIText(debugStaticString6, "font", 0,0,8,12,1);
debugStaticText6->ParseString(debugStaticString6,camera.w-100*globalScale,64*gScale,1,"debug",Game::groupUI_Layer3);
std::string debugStaticString7 = "YVec:";
debugStaticText7 = new UIText(debugStaticString7, "font", 0,0,8,12,1);
debugStaticText7->ParseString(debugStaticString7,camera.w-100*globalScale,74*gScale,1,"debug",Game::groupUI_Layer3);
debugJumpText = new UIText(Game::BoolToString(playerIsJumping), "font", 0,0,8,12,1);
debugJumpText->ParseString(Game::BoolToString(playerIsJumping),camera.w-50*globalScale,44*gScale,1,"debugJumpText",Game::groupUI_Layer3);
// std::string debugStaticString = "Collision";
// debugStaticText = new UIText(debugStaticString, "font", 0,0,8,12,1,"debug text",Game::groupUI_Layer3);
// debugStaticText->ParseString(debugStaticString,camera.w-100*globalScale,14*gScale,1,"debug",Game::groupUI_Layer3);
// std::string debugStaticString2 = "Hori:";
// debugStaticText2 = new UIText(debugStaticString2, "font", 0,0,8,12,1,"debug text",Game::groupUI_Layer3);
// debugStaticText2->ParseString(debugStaticString2,camera.w-100*globalScale,24*gScale,1,"debug",Game::groupUI_Layer3);
// std::string debugStaticString3 = "Vert:";
// debugStaticText3 = new UIText(debugStaticString3, "font", 0,0,8,12,1,"debug text",Game::groupUI_Layer3);
// debugStaticText3->ParseString(debugStaticString3,camera.w-100*globalScale,34*gScale,1,"debug",Game::groupUI_Layer3);
// std::string debugStaticString4 = "Jump:";
// debugStaticText4 = new UIText(debugStaticString4, "font", 0,0,8,12,1,"debug text",Game::groupUI_Layer3);
// debugStaticText4->ParseString(debugStaticString4,camera.w-100*globalScale,44*gScale,1,"debug",Game::groupUI_Layer3);
// std::string debugStaticString5 = "P.y :";
// debugStaticText5 = new UIText(debugStaticString5, "font", 0,0,8,12,1,"debug text",Game::groupUI_Layer3);
// debugStaticText5->ParseString(debugStaticString5,camera.w-100*globalScale,54*gScale,1,"debug",Game::groupUI_Layer3);
// std::string debugStaticString6 = "P.dy:";
// debugStaticText6 = new UIText(debugStaticString6, "font", 0,0,8,12,1,"debug text",Game::groupUI_Layer3);
// debugStaticText6->ParseString(debugStaticString6,camera.w-100*globalScale,64*gScale,1,"debug",Game::groupUI_Layer3);
// std::string debugStaticString7 = "YVec:";
// debugStaticText7 = new UIText(debugStaticString7, "font", 0,0,8,12,1,"debug text",Game::groupUI_Layer3);
// debugStaticText7->ParseString(debugStaticString7,camera.w-100*globalScale,74*gScale,1,"debug",Game::groupUI_Layer3);
//
// debugJumpText = new UIText(Game::BoolToString(playerIsJumping), "font", 0,0,8,12,1,"debug text",Game::groupUI_Layer3);
// debugJumpText->ParseString(Game::BoolToString(playerIsJumping),camera.w-50*globalScale,44*gScale,1,"debugJumpText",Game::groupUI_Layer3);
// debug UI box
SDL_Rect debugBoxRect = SDL_Rect();
@ -194,7 +197,7 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
debugBox = new UINineSlice("textBox");
debugBox->MakeSlices("textBox",32,32,14,16,14,16,debugBoxRect,1,Game::groupUI_Layer2);
printf("camera.w: %d \n",camera.w);
// printf("camera.w: %d \n",camera.w);
//ecs implementation
@ -255,6 +258,8 @@ void Game::update()
SDL_Rect playerCol = player.getComponent<ColliderComponent>().collider;
Vector2D playerPos = player.getComponent<TransformComponent>().position;
uiInfo.getComponent<UITextComponent>().updateString("New Text Works!");
// if (gsm->currentState == GameStateManager::ST_INIT)
// {
// const char* initText = "Loading...";
@ -308,6 +313,7 @@ void Game::update()
// Gravity
if (gravityOnPlayer){
player.getComponent<TransformComponent>().position.y += 3*gScale;
// debugJumpText->ParseString(Game::BoolToString(playerIsJumping),camera.w-50*gScale,44*gScale,1,"debugJumpText",Game::groupUI_Layer3);
}
// for(auto& p: projectiles)
// {
@ -387,9 +393,9 @@ void Game::render()
{
guiElement->draw();
}
for (auto& letter : uiText)
for (auto& text : uiText)
{
letter->draw();
text->draw();
}
}
if (debugMenu)
@ -398,9 +404,9 @@ void Game::render()
{
guiElement->draw();
}
for (auto& letter : debugText)
for (auto& text : debugText)
{
letter->draw();
text->draw();
}
}

View File

@ -16,7 +16,7 @@
extern Manager manager;
UIText::UIText(std::string text, std::string texId, int x, int y, int letterW, int letterH, int lScale)
UIText::UIText(std::string text, std::string texId, int x, int y, int letterW, int letterH, int lScale, std::string tag, Game::groupLabels group)
{
inputText = text;
textureID = texId;
@ -25,6 +25,10 @@ UIText::UIText(std::string text, std::string texId, int x, int y, int letterW, i
letterWidth = letterW;
letterHeight = letterH;
scale = lScale;
// gameGroup = Game::groupLabels::groupUI_Layer3;
auto& uiLetters(manager.addEntity());
uiLetters.setTag(tag);
uiLetters.addGroup(group);
}
UIText::~UIText()
@ -33,12 +37,16 @@ UIText::~UIText()
void UIText::ParseString(std::string inputText, int x, int y, int letterScale, std::string tag, Game::groupLabels group)
{
// gameGroup = group;
//Parse input text into an array of char
int posX = x;
int posY = y;
int i = 0;
char current = inputText[i];
int charsNumber = inputText.length();
// printf("Counting string \'%s\': \n",inputText.c_str());
// printf("%d\n",charsNumber);
do
{
++i;
@ -60,15 +68,24 @@ void UIText::ParseString(std::string inputText, int x, int y, int letterScale, s
void UIText::AddLetter(int xpos, int ypos, char crnt, std::string tag, int lttrScale, Game::groupLabels groupLabel)
{
// =======THIS NEEDS TO BE REFACTORED TO NOT USE INDIVIDUAL ENTITIES FOR EACH LETTER============
auto& letter(manager.addEntity());
letter.addComponent<TransformComponent>(xpos*lttrScale, ypos*lttrScale, letterWidth, letterHeight, 1);
letter.addComponent<SpriteComponent>("font", SpriteComponent::spriteText, crnt, letterWidth, letterHeight, lttrScale);
letter.setTag(tag);
letter.addGroup(groupLabel);
// auto& letter(manager.addEntity());
// letter.addComponent<TransformComponent>(xpos*lttrScale, ypos*lttrScale, letterWidth, letterHeight, 1);
// letter.addComponent<SpriteComponent>("font", SpriteComponent::spriteText, crnt, letterWidth, letterHeight, lttrScale);
// letter.setTag(tag);
// letter.addGroup(groupLabel);
SDL_Texture* letterTexture;
letterTexture = Game::assets->GetTexture(textureID);
SDL_Rect srcRect,destRect;
srcRect.x = ((crnt-ASCII_START_IDX) % ASCII_ROW_COUNT)*letterWidth;
srcRect.y = ((crnt-ASCII_START_IDX)/ASCII_ROW_COUNT)*letterHeight;
srcRect.w = letterWidth;
srcRect.h = letterHeight;
destRect.x = xpos;
destRect.y = ypos;
destRect.w = letterWidth*scale;
destRect.h = letterHeight*scale;
TextureManager::Draw(letterTexture,srcRect,destRect,SDL_FLIP_NONE);
}
void UIText::RemoveAllLetters()
{
manager.getEntitiesByTag("");
}

View File

@ -8,27 +8,47 @@
#ifndef SRC_UITEXT_H_
#define SRC_UITEXT_H_
#define ASCII_START_IDX 32
#define ASCII_COUNT 96
#define ASCII_ROW_COUNT 16
#include "SDL2/SDL.h"
#include <iostream>
#include "../game/Game.hpp"
#include "../assetmgr/TextureManager.h"
#include "../assetmgr/AssetManager.h"
class UIText
{
private:
// SDL_Texture *texture;
// SDL_Rect srcRect, destRect;
char letter;
// int frames = 0;
// int speed = 100;
int letterWidth, letterHeight;
int scale = 1;
public:
std::string inputText;
int letterHeight;
int letterWidth;
// int letterHeight;
// int letterWidth;
// virtual void init() {}
// virtual void update() {}
// virtual void draw() {}
Game::groupLabels gameGroup;
int posX;
int posY;
std::string textureID;
UIText(std::string inputText, std::string texID, int x, int y, int letterW, int letterH, int lScale);
UIText(std::string inputText, std::string texID, int x, int y, int letterW, int letterH, int lScale, std::string tag, Game::groupLabels group);
~UIText();
void init() {}
void update() {}
void draw() {}
void AddLetter(int xpos, int ypos, char crnt, std::string tag, int lttrScale, Game::groupLabels groupLabel);
void ParseString(std::string inputText, int x, int y, int letterScale, std::string tag, Game::groupLabels group);
void RemoveAllLetters();
void UpdateString(std::string newInputText);
int scale;
// void setTex(std::string id);
// int scale;
};
#endif /* SRC_UITEXT_H_ */