diff --git a/build/BeagleRescue b/build/BeagleRescue index 2d45efb..66f7a2b 100755 Binary files a/build/BeagleRescue and b/build/BeagleRescue differ diff --git a/src/config/config.json b/src/config/config.json index c8a053d..7185755 100644 --- a/src/config/config.json +++ b/src/config/config.json @@ -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 } diff --git a/src/ecs/Components.h b/src/ecs/Components.h index dbbcb67..4e9dba6 100644 --- a/src/ecs/Components.h +++ b/src/ecs/Components.h @@ -16,6 +16,6 @@ #include "PlayerController.h" #include "ProjectileComponent.h" #include "TileComponent.h" -#include "UIFontComponent.h" +#include "UITextComponent.h" #endif /* SRC_COMPONENTS_H_ */ diff --git a/src/ecs/UIFontComponent.h b/src/ecs/UIFontComponent.h deleted file mode 100644 index 5ab52de..0000000 --- a/src/ecs/UIFontComponent.h +++ /dev/null @@ -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_ */ diff --git a/src/ecs/UITextComponent.h b/src/ecs/UITextComponent.h new file mode 100644 index 0000000..1d88865 --- /dev/null +++ b/src/ecs/UITextComponent.h @@ -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 +#include +#include +#include + +// Should include and 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 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(); + + } + + void update() override + { + destRect.x = static_cast(transform->position.x); + destRect.y = static_cast(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 lttr = getLetterTexture(text[l],l); + TextureManager::Draw(texture, std::get<0>(lttr), std::get<1>(lttr), spriteFlip); + } + } + + std::tuple getLetterTexture(char currentLetter, int i) + { + std::tuple 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(transform->position.x)*scale+i*letterWidth*scale; + destRect.y = static_cast(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_ */ + diff --git a/src/game/Game.cpp b/src/game/Game.cpp index 9f1a3d9..3322d76 100644 --- a/src/game/Game.cpp +++ b/src/game/Game.cpp @@ -18,20 +18,20 @@ #include #include #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(160,120,64,32,1); + uiInfo.addComponent("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().collider; Vector2D playerPos = player.getComponent().position; + uiInfo.getComponent().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().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(); } } diff --git a/src/ui/UIText.cpp b/src/ui/UIText.cpp index 67b384d..4900b44 100644 --- a/src/ui/UIText.cpp +++ b/src/ui/UIText.cpp @@ -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(xpos*lttrScale, ypos*lttrScale, letterWidth, letterHeight, 1); - letter.addComponent("font", SpriteComponent::spriteText, crnt, letterWidth, letterHeight, lttrScale); - letter.setTag(tag); - letter.addGroup(groupLabel); +// auto& letter(manager.addEntity()); +// letter.addComponent(xpos*lttrScale, ypos*lttrScale, letterWidth, letterHeight, 1); +// letter.addComponent("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(""); -} diff --git a/src/ui/UIText.h b/src/ui/UIText.h index 4577734..153006d 100644 --- a/src/ui/UIText.h +++ b/src/ui/UIText.h @@ -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 #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_ */