diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-30 15:22:17 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-30 15:22:17 +0100 |
commit | 4159c7187e17d6318e33d8a0c014ba042ae8261b (patch) | |
tree | 7653a3b933ee074eabeab8d47ebbd01c9da8ff12 | |
parent | 7ae597ac9268e031927f94dd89c20ab6c034f5de (diff) |
first version of new viewport floats without the use of sdl functions
-rw-r--r-- | src/crepe/api/Sprite.h | 2 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 60 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 2 | ||||
-rw-r--r-- | src/example/rendering_particle.cpp | 9 |
4 files changed, 42 insertions, 31 deletions
diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 5a7a1d9..7d9c14b 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -71,7 +71,7 @@ public: * cannot be const because if Animator component is addded then ratio becomes scuffed and * does it need to be calculated again in the Animator */ - double aspect_ratio; + float aspect_ratio; private: //! Reads the mask of sprite diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 56ba61a..0ba1db4 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -108,22 +108,30 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { .h = sprite.mask.h, }; } -SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, + +vec2 bar_size; +vec2 scale; + +SDL_FRect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, const vec2 & cam_pos, const double & img_scale) const { - double width = sprite.height * sprite.aspect_ratio; - double height = sprite.height; - width *= img_scale * cam.zoom; - height *= img_scale * cam.zoom; + vec2 size = { (sprite.height * sprite.aspect_ratio), sprite.height}; + + size *= scale * img_scale * cam.zoom ; - return SDL_Rect{ - .x - = static_cast<int>(round(pos.x - cam_pos.x + (cam.viewport_size.x / 2) - width / 2)), - .y - = static_cast<int>(round(pos.y - cam_pos.y + (cam.viewport_size.y / 2) - height / 2)), - .w = static_cast<int>(width), - .h = static_cast<int>(height), + vec2 screen_pos = (pos - cam_pos + cam.viewport_size / 2) * scale - size / 2 + bar_size; + + cout << size.x << " " << size.y << endl; + cout << pos.x << " " << pos.y << endl; + cout << scale.x << " " << scale.y << endl; + cout << screen_pos.x << " " << screen_pos.y << endl; + + return SDL_FRect{ + .x = screen_pos.x, + .y = screen_pos.y, + .w = size.x, + .h = size.y, }; } @@ -134,12 +142,12 @@ void SDLContext::draw(const RenderContext & ctx) { | (SDL_FLIP_VERTICAL * ctx.sprite.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(ctx.sprite); - SDL_Rect dstrect + SDL_FRect dstrect = this->get_dst_rect(ctx.sprite, ctx.pos, ctx.cam, ctx.cam_pos, ctx.scale); cout << dstrect.x << " " << dstrect.y << " " << dstrect.w << " " << dstrect.h << endl; - SDL_RenderCopyEx(this->game_renderer.get(), ctx.sprite.sprite_image.texture.get(), + SDL_RenderCopyExF(this->game_renderer.get(), ctx.sprite.sprite_image.texture.get(), &srcrect, &dstrect, ctx.angle, NULL, render_flip); } @@ -155,7 +163,7 @@ void SDLContext::set_camera(const Camera & cam) { double screen_aspect = static_cast<double>(cam.screen.x) / cam.screen.y; double viewport_aspect = cam.viewport_size.x / cam.viewport_size.y; - cout << screen_aspect << " " << viewport_aspect << endl; + SDL_FRect black_bars[2]; // calculate black bars if (screen_aspect > viewport_aspect) { @@ -164,22 +172,26 @@ void SDLContext::set_camera(const Camera & cam) { float render_scale = cam.screen.y / cam.viewport_size.y; float adj_width = cam.viewport_size.x * render_scale; float bar_width = (cam.screen.x - adj_width) / 2; - black_bars[0] = {0, 0, bar_width, (float)cam.screen.y}; - black_bars[1] = {(cam.screen.x - bar_width), 0, bar_width, (float)cam.screen.y}; - cout << render_scale << " " << adj_width << " " << bar_width << " " << cam.screen.y - << " " << cam.viewport_size.y << endl; + black_bars[0] = {0, 0, bar_width, (float) cam.screen.y}; + black_bars[1] = {(cam.screen.x - bar_width), 0, bar_width, (float) cam.screen.y}; + + bar_size = {bar_width,0}; + scale.x = scale.y = cam.screen.y / cam.viewport_size.y; } else { // letterboxing cout << "letterboxing" << endl; - float render_scale = cam.screen.y / cam.viewport_size.y; - float adj_width = cam.viewport_size.x * render_scale; - float bar_height = (cam.screen.x - adj_width) / 2; + float render_scale = cam.screen.x / cam.viewport_size.x; + float adj_height = cam.viewport_size.y * render_scale; + float bar_height = (cam.screen.y - adj_height) / 2; black_bars[0] = {0, 0, (float) cam.screen.x, bar_height}; black_bars[1] = {0, (cam.screen.y - bar_height), (float) cam.screen.x, bar_height}; - cout << render_scale << " " << adj_width << " " << bar_height << " " << cam.screen.y - << " " << cam.viewport_size.y << endl; + + bar_size = {0,bar_height}; + scale.x = scale.y = render_scale; } + cout << "BARS" << endl; + for (int i = 0; i < 2; ++i) { cout << black_bars[i].x << " " << black_bars[i].y << " " << black_bars[i].w << " " << black_bars[i].h << endl; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 6030a6e..ead0c72 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -158,7 +158,7 @@ private: * \param img_scale the image multiplier for increasing img size * \return sdl rectangle to draw a dst image to draw on the screen */ - SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, + SDL_FRect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, const vec2 & cam_pos, const double & img_scale) const; private: diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 5896c81..f5b9ab3 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -32,14 +32,12 @@ int main(int argc, char * argv[]) { auto img = Texture("asset/texture/test_ap43.png"); - /* Sprite & test_sprite = game_object.add_component<Sprite>( - img, color, Sprite::FlipSettings{true, true}, 1, 1, 500); + img, color, Sprite::FlipSettings{true, true}, 1, 1, 3000); //game_object.add_component<Animator>(test_sprite, 4, 1, 0).active = true; game_object.add_component<Animator>(test_sprite, 1, 1, 0).active = true; - */ /* auto & test = game_object.add_component<ParticleEmitter>(ParticleEmitter::Data{ .position = {0, 0}, @@ -62,8 +60,9 @@ int main(int argc, char * argv[]) { }); */ - auto & cam = game_object.add_component<Camera>(Color::WHITE, ivec2{1600, 900}, - vec2{800,800}, 1.0f); + auto & cam = game_object.add_component<Camera>(Color::WHITE, ivec2{1280, 720}, + vec2{8000,6000}, 1.0f); + cam.offset.x = 4000; /* game_object |