diff --git a/src/Main.cpp b/src/Main.cpp index 2e692ca..3c406b7 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -34,7 +34,7 @@ class MainState : public State { title.scale.y = sin(elaped); - title.y = (480.0/2) - (title.h*title.scale.y/2); + title.centerSelf(); elaped += 0.01; } diff --git a/src/Render.cpp b/src/Render.cpp index 299477c..e0799fe 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -79,6 +79,21 @@ void Render::Object::Draw(float dt) { _sc_h = _h-cam_rect.h; } +void Render::Object::centerSelf(AXIS axis) { + switch (axis) { + case X: + x = (WINDOW_WIDTH/2) - (w*scale.x/2); + break; + case Y: + y = (WINDOW_HEIGHT/2) - (h*scale.y/2); + break; + case XY: + x = (WINDOW_WIDTH/2) - (w*scale.x/2); + y = (WINDOW_HEIGHT/2) - (h*scale.y/2); + break; + } +} + bool Render::Init(string window_name) { consoleD = GetConsoleWindow(); SetWindowTextA(consoleD, "Logging window"); diff --git a/src/Render.hpp b/src/Render.hpp index 5de3da9..1f51e32 100644 --- a/src/Render.hpp +++ b/src/Render.hpp @@ -24,6 +24,12 @@ struct Vector2 float y = 1; }; +enum AXIS { + X, + Y, + XY +}; + namespace Render { extern SDL_Window* window; extern SDL_Renderer* renderer; @@ -54,6 +60,8 @@ namespace Render { int _sc_x, _sc_y, _sc_w, _sc_h; SDL_Texture* _tex = nullptr; void set_property(string name, bool value); + + void centerSelf(AXIS axis = XY); private: int _x, _y, _w, _h; int _ori_w, _ori_h;