diff --git a/bin/data/monogram.ttf b/bin/data/monogram.ttf new file mode 100644 index 0000000..4d4a390 Binary files /dev/null and b/bin/data/monogram.ttf differ diff --git a/src/Main.cpp b/src/Main.cpp index d2f050d..cda78df 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -11,6 +11,7 @@ using namespace Render; class MainState : public State { AnimatedObject objs; + TextObject text; Object title; virtual void Create() { title.create(0, 0, "data/bg.png"); @@ -24,6 +25,11 @@ class MainState : public State { title.center.x = WINDOW_WIDTH/2; title.center.y = WINDOW_HEIGHT/2; + + text.create(50, 480 - 100, "funny"); + text.scale.x = 2; + text.scale.y = 2; + AddObject(&text); } int yvel = 0; int xvel = 0; @@ -46,7 +52,7 @@ class MainState : public State { }; int main() { - Init("lmfao"); + Init("SDLflixel :)))))"); MainState m; diff --git a/src/Render.cpp b/src/Render.cpp index 2b0c16d..e771b70 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -114,6 +114,40 @@ void Render::AnimatedObject::PlayAnimation(string anim_name) { current_framename = anim_name; } +void Render::TextObject::create(int x, int y, string text, string font_name) { + Render::Object::create(x,y,"data/smile.png"); // dummy + + font = TTF_OpenFont(font_name.c_str(), 25); + + SDL_Surface* temp = TTF_RenderText_Solid(font, text.c_str(), {255, 255, 255, 255}); + + this->text = text; + + this->_tex = SDL_CreateTextureFromSurface(renderer, temp); + SDL_FreeSurface(temp); + int w_, h_; + SDL_QueryTexture(this->_tex, NULL, NULL, &w_, &h_); + _sc_w = w_; + _sc_h = h_; + this->x = x; + this->y = y; + w = w_; + h = h_; +} + +void Render::TextObject::changeText(string text) { + SDL_Surface* temp = TTF_RenderText_Solid(font, text.c_str(), {255, 255, 255, 255}); + this->text = text; + this->_tex = SDL_CreateTextureFromSurface(renderer, temp); + SDL_FreeSurface(temp); + int w_, h_; + SDL_QueryTexture(this->_tex, NULL, NULL, &w_, &h_); + _sc_w = w_; + _sc_h = h_; + w = w_; + h = h_; +} + void Render::Object::centerSelf(AXIS axis) { switch (axis) { case X: diff --git a/src/Render.hpp b/src/Render.hpp index b6cba32..146d483 100644 --- a/src/Render.hpp +++ b/src/Render.hpp @@ -7,6 +7,7 @@ #include #include #include "SDL2/SDL.h" +#include "SDL2/SDL_ttf.h" #include "BASS/audio_out.h" using namespace std; @@ -47,7 +48,7 @@ namespace Render { /* * Create a new Object instance. */ - void create(int x = 0, int y = 0, string path = ""); + virtual void create(int x = 0, int y = 0, string path = ""); virtual void Draw(float dt); @@ -84,7 +85,7 @@ namespace Render { /* * Create a new AnimatedObject instance. */ - void create(int x = 0, int y = 0, string path = ""); + virtual void create(int x = 0, int y = 0, string path = ""); /* * Add an animation to said object. Uses SDL_Rects for frames. */ @@ -104,6 +105,22 @@ namespace Render { int startTime; }; + class TextObject : public Object { + public: + /* + * Create a new TextObject instance. + */ + virtual void create(int x = 0, int y = 0, string text = "", string font_name = "data/monogram.ttf"); + /* + * Change current text. !! TO AVOID MEMORY LEAKS, DO NOT RUN THIS EVERY FRAME! !! + */ + virtual void changeText(string text = ""); + + TTF_Font* font; + private: + string text = ""; + }; + /* * A state is where you would contain said objects. */