easier centering

This commit is contained in:
/nick haya 2022-02-07 13:44:09 +08:00
parent 6ffff01ac7
commit ad034794d6
3 changed files with 24 additions and 1 deletions

View file

@ -34,7 +34,7 @@ class MainState : public State {
title.scale.y = sin(elaped); title.scale.y = sin(elaped);
title.y = (480.0/2) - (title.h*title.scale.y/2); title.centerSelf();
elaped += 0.01; elaped += 0.01;
} }

View file

@ -79,6 +79,21 @@ void Render::Object::Draw(float dt) {
_sc_h = _h-cam_rect.h; _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) { bool Render::Init(string window_name) {
consoleD = GetConsoleWindow(); consoleD = GetConsoleWindow();
SetWindowTextA(consoleD, "Logging window"); SetWindowTextA(consoleD, "Logging window");

View file

@ -24,6 +24,12 @@ struct Vector2
float y = 1; float y = 1;
}; };
enum AXIS {
X,
Y,
XY
};
namespace Render { namespace Render {
extern SDL_Window* window; extern SDL_Window* window;
extern SDL_Renderer* renderer; extern SDL_Renderer* renderer;
@ -54,6 +60,8 @@ namespace Render {
int _sc_x, _sc_y, _sc_w, _sc_h; int _sc_x, _sc_y, _sc_w, _sc_h;
SDL_Texture* _tex = nullptr; SDL_Texture* _tex = nullptr;
void set_property(string name, bool value); void set_property(string name, bool value);
void centerSelf(AXIS axis = XY);
private: private:
int _x, _y, _w, _h; int _x, _y, _w, _h;
int _ori_w, _ori_h; int _ori_w, _ori_h;