Semi-finalize? camera system

This commit is contained in:
/nick haya 2022-02-08 10:20:45 +08:00
parent a4aa47bf0e
commit 317d1e8f2c
3 changed files with 63 additions and 7 deletions

View file

@ -13,20 +13,40 @@ class MainState : public State {
AnimatedObject objs; AnimatedObject objs;
TextObject text; TextObject text;
Object title; Object title;
Object title2;
Object title3;
Object title4;
SDL_Rect camera = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
virtual void Create() { virtual void Create() {
title.create(0, 0, "data/bg.png"); title.create(0, 0, "data/bg.png");
AddObject(&title); AddObject(&title);
objs.create(50, 50, "data/smile.png");
title2.create(640, 0, "data/bg.png");
AddObject(&title2);
title3.create(640, 480, "data/bg.png");
AddObject(&title3);
title4.create(0, 480, "data/bg.png");
AddObject(&title4);
objs.create(0, 0, "data/smile.png");
AddObject(&objs); AddObject(&objs);
objs.AddAnimation("idle", {{0, 0, 50, 50}, {50, 0, 50, 50}}); objs.AddAnimation("idle", {{0, 0, 50, 50}, {50, 0, 50, 50}});
objs.PlayAnimation("idle"); objs.PlayAnimation("idle");
objs.framerate = 1; objs.framerate = 1;
objs.setCamera(&camera);
title.setCamera(&camera);
title2.setCamera(&camera);
title3.setCamera(&camera);
title4.setCamera(&camera);
title.scale.y = 0; title.scale.y = 0;
objs.centerSelf();
title.center.x = WINDOW_WIDTH/2; title.center.x = WINDOW_WIDTH/2;
title.center.y = WINDOW_HEIGHT/2; title.center.y = WINDOW_HEIGHT/2;
text.create(50, 480 - 100, "Welcome to the funny application\nMake yourself at home :)", "data/monogram.ttf", {255, 255, 255, 255}, TTF_STYLE_NORMAL, 40); text.create(50, 480 - 100, "Welcome to the funny application\nMake yourself at home :)\n- haya", "data/monogram.ttf", {255, 255, 255, 255}, TTF_STYLE_NORMAL, 40);
AddObject(&text); AddObject(&text);
} }
int yvel = 0; int yvel = 0;
@ -43,6 +63,8 @@ class MainState : public State {
title.scale.y = sin(elaped); title.scale.y = sin(elaped);
title.scale.x = cos(elaped); title.scale.x = cos(elaped);
pointTo(&camera, objs);
title.centerSelf(); title.centerSelf();
text.centerSelf(); text.centerSelf();

View file

@ -75,13 +75,17 @@ vector<Object*> Render::State::get_obj() {
void Render::Object::Draw(float dt) { void Render::Object::Draw(float dt) {
_x = x; _x = x;
_sc_x = x-cam_rect.x; _sc_x = x-cam_rect->x;
_y = y; _y = y;
_sc_y = y-cam_rect.y; _sc_y = y-cam_rect->y;
_w = w*scale.x; _w = w*scale.x;
_sc_w = _w-cam_rect.w; _sc_w = _w;
_h = h*scale.y; _h = h*scale.y;
_sc_h = _h-cam_rect.h; _sc_h = _h;
}
void Render::Object::setCamera(SDL_Rect* cam_p) {
cam_rect = cam_p;
} }
void Render::AnimatedObject::Draw(float dt) { void Render::AnimatedObject::Draw(float dt) {
@ -287,3 +291,25 @@ bool Render::playMusic(string path) {
return true; return true;
} }
void Render::pointTo(SDL_Rect* camera, Object object) {
camera->x = ( object.x + (object.w / 2)/2 ) - WINDOW_WIDTH / 2;
camera->y = ( object.y + (object.h / 2) ) - WINDOW_HEIGHT / 2;
if( camera->x < 0 )
{
camera->x = 0;
}
if( camera->y < 0 )
{
camera->y = 0;
}
if( camera->x > camera->w )
{
camera->x = camera->w;
}
if( camera->y > camera->h )
{
camera->y = camera->h;
}
}

View file

@ -82,10 +82,13 @@ namespace Render {
* Center object on the center of the screen on a certain axis. Defaults to both X and Y. * Center object on the center of the screen on a certain axis. Defaults to both X and Y.
*/ */
void centerSelf(AXIS axis = XY); void centerSelf(AXIS axis = XY);
void setCamera(SDL_Rect* cam_p);
private: private:
int _x, _y, _w, _h; int _x, _y, _w, _h;
int _ori_w, _ori_h; int _ori_w, _ori_h;
SDL_Rect cam_rect = {0, 0, 0, 0}; SDL_Rect du = {0, 0, 0, 0};
SDL_Rect* cam_rect = &du;
map<string, bool> _properties; map<string, bool> _properties;
}; };
@ -208,5 +211,10 @@ namespace Render {
* Passing a blank string (e.g. "") will stop the current playing music. * Passing a blank string (e.g. "") will stop the current playing music.
*/ */
bool playMusic(string path); bool playMusic(string path);
/*
* Make the camera center itself on an object.
*/
void pointTo(SDL_Rect* camera, Object object);
} }
#endif #endif