diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-29 21:20:48 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-29 21:20:48 +0100 |
commit | 7ae597ac9268e031927f94dd89c20ab6c034f5de (patch) | |
tree | ee04ad307c70d9007b7e2589c33b4ebcf2fbb287 /src | |
parent | 85aa57e33d493502b05984ce44db6c68f627a6f2 (diff) |
working floating point black bars
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/api/Config.h | 2 | ||||
-rw-r--r-- | src/crepe/api/Sprite.cpp | 2 | ||||
-rw-r--r-- | src/crepe/api/Sprite.h | 4 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 71 | ||||
-rw-r--r-- | src/example/rendering_particle.cpp | 7 |
5 files changed, 55 insertions, 31 deletions
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 225e9b9..200a3b0 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -67,7 +67,7 @@ public: //! default window settings struct { //TODO make this constexpr because this will never change - ivec2 default_size = {1080, 720}; + ivec2 default_size = {1280, 720}; std::string window_title = "Jetpack joyride clone"; } window_settings; diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 8647794..1d57b53 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -11,7 +11,7 @@ using namespace std; using namespace crepe; Sprite::Sprite(game_object_id_t id, Texture & image, const Color & color, - const FlipSettings & flip, int sort_layer, int order_layer, int height) + const FlipSettings & flip, int sort_layer, int order_layer, float height) : Component(id), color(color), flip(flip), diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index a0e90a0..5a7a1d9 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -41,7 +41,7 @@ public: * \param height the height of the image in game units */ Sprite(game_object_id_t id, Texture & image, const Color & color, - const FlipSettings & flip, int sort_layer, int order_layer, int height); + const FlipSettings & flip, int sort_layer, int order_layer, float height); /** * \brief Destroys the Sprite instance. @@ -63,7 +63,7 @@ public: const int order_in_layer; //! height in world units - const int height; + const float height; /** * \aspect_ratio ratio of the img so that scaling will not become weird diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 9f60285..56ba61a 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -8,6 +8,7 @@ #include <cmath> #include <cstddef> #include <functional> +#include <iostream> #include <memory> #include <stdexcept> @@ -94,7 +95,7 @@ void SDLContext::handle_events(bool & running) { } void SDLContext::clear_screen() { - SDL_SetRenderDrawColor(this->game_renderer.get(), 0, 0, 0, 255); + SDL_SetRenderDrawColor(this->game_renderer.get(), 255, 255, 255, 255); SDL_RenderClear(this->game_renderer.get()); } void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } @@ -110,17 +111,19 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, const vec2 & cam_pos, const double & img_scale) const { - int width = sprite.height * sprite.aspect_ratio; - int height = sprite.height; + double width = sprite.height * sprite.aspect_ratio; + double height = sprite.height; width *= img_scale * cam.zoom; height *= img_scale * cam.zoom; return SDL_Rect{ - .x = static_cast<int>((pos.x - cam_pos.x + (cam.viewport_size.x / 2) - width / 2)), - .y = static_cast<int>((pos.y - cam_pos.y + (cam.viewport_size.y / 2) - height / 2)), - .w = width, - .h = height, + .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), }; } @@ -134,6 +137,8 @@ void SDLContext::draw(const RenderContext & ctx) { SDL_Rect 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(), &srcrect, &dstrect, ctx.angle, NULL, render_flip); } @@ -147,30 +152,44 @@ void SDLContext::set_camera(const Camera & cam) { SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y); } - double screen_aspect = cam.screen.x / cam.screen.y; + double screen_aspect = static_cast<double>(cam.screen.x) / cam.screen.y; double viewport_aspect = cam.viewport_size.x / cam.viewport_size.y; - SDL_Rect view; + cout << screen_aspect << " " << viewport_aspect << endl; + SDL_FRect black_bars[2]; // calculate black bars if (screen_aspect > viewport_aspect) { - // letterboxing - view.h = cam.screen.y / cam.zoom; - view.w = cam.screen.y * viewport_aspect; - view.x = (cam.screen.x - view.w) / 2; - view.y = 0; - } else { // pillarboxing - view.h = cam.screen.x / viewport_aspect; - view.w = cam.screen.x / cam.zoom; - view.x = 0; - view.y = (cam.screen.y - view.h) / 2; + cout << "pillarboxing" << endl; + 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; + } 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; + 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; } - // set drawing area - SDL_RenderSetViewport(this->game_renderer.get(), &view); - SDL_RenderSetLogicalSize(this->game_renderer.get(), static_cast<int>(cam.viewport_size.x), - static_cast<int>(cam.viewport_size.y)); + 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; + } + + SDL_SetRenderDrawColor(this->game_renderer.get(), 0, 0, 0, 255); + SDL_RenderFillRectF(this->game_renderer.get(), &black_bars[0]); + SDL_RenderFillRectF(this->game_renderer.get(), &black_bars[1]); + /* // set bg color SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g, cam.bg_color.b, cam.bg_color.a); @@ -178,11 +197,13 @@ void SDLContext::set_camera(const Camera & cam) { SDL_Rect bg = { .x = 0, .y = 0, - .w = static_cast<int>(cam.viewport_size.x), - .h = static_cast<int>(cam.viewport_size.y), + .w = (int)cam.viewport_size.x, + .h = (int)cam.viewport_size.y, }; // fill bg color SDL_RenderFillRect(this->game_renderer.get(), &bg); + + */ } uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 3a12144..5896c81 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -31,12 +31,15 @@ int main(int argc, char * argv[]) { Color color(255, 255, 255, 255); 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); //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}, @@ -59,8 +62,8 @@ int main(int argc, char * argv[]) { }); */ - auto & cam = game_object.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, - vec2{2000, 2000}, 1.0f); + auto & cam = game_object.add_component<Camera>(Color::WHITE, ivec2{1600, 900}, + vec2{800,800}, 1.0f); /* game_object |