diff options
Diffstat (limited to 'src/crepe')
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 101 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 26 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 10 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.h | 5 |
4 files changed, 70 insertions, 72 deletions
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 6d79c73..1f729b2 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -94,11 +94,13 @@ void SDLContext::handle_events(bool & running) { */ } -void SDLContext::clear_screen() { - SDL_SetRenderDrawColor(this->game_renderer.get(), 255, 255, 255, 255); - SDL_RenderClear(this->game_renderer.get()); +void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); } +void SDLContext::present_screen() { + 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]); + SDL_RenderPresent(this->game_renderer.get()); } -void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { return SDL_Rect{ @@ -109,23 +111,16 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { }; } -vec2 bar_size; -vec2 scale; +SDL_FRect SDLContext::get_dst_rect(const DstRect & ctx) const { -SDL_FRect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, - const vec2 & cam_pos, const double & img_scale) const { + vec2 size = {(ctx.sprite.height * ctx.sprite.aspect_ratio), ctx.sprite.height}; + const CameraValues & cam = ctx.cam; - vec2 size = { (sprite.height * sprite.aspect_ratio), sprite.height}; - - size *= scale * img_scale; + size *= cam.render_scale * ctx.img_scale; - vec2 screen_pos = (pos - cam_pos + (cam.viewport_size / cam.zoom ) / 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; + vec2 screen_pos = (ctx.pos - cam.cam_pos + (cam.zoomed_viewport) / 2) * cam.render_scale + - size / 2 + cam.bar_size; return SDL_FRect{ .x = screen_pos.x, @@ -142,16 +137,18 @@ void SDLContext::draw(const RenderContext & ctx) { | (SDL_FLIP_VERTICAL * ctx.sprite.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(ctx.sprite); - 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_FRect dstrect = this->get_dst_rect(SDLContext::DstRect{ + .sprite = ctx.sprite, + .cam = ctx.cam, + .pos = ctx.pos, + .img_scale = ctx.scale, + }); SDL_RenderCopyExF(this->game_renderer.get(), ctx.sprite.sprite_image.texture.get(), - &srcrect, &dstrect, ctx.angle, NULL, render_flip); + &srcrect, &dstrect, ctx.angle, NULL, render_flip); } -void SDLContext::set_camera(const Camera & cam) { +void SDLContext::set_camera(const Camera & cam, CameraValues & ctx) { // resize window int w, h; @@ -160,62 +157,50 @@ void SDLContext::set_camera(const Camera & cam) { SDL_SetWindowSize(this->game_window.get(), 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.zoom ) / (cam.viewport_size.y / cam.zoom); + vec2 & zoomed_viewport = ctx.zoomed_viewport; + vec2 & bar_size = ctx.bar_size; + vec2 & render_scale = ctx.render_scale; + zoomed_viewport = cam.viewport_size * cam.zoom; + double screen_aspect = static_cast<double>(cam.screen.x) / cam.screen.y; + double viewport_aspect = zoomed_viewport.x / zoomed_viewport.y; - SDL_FRect black_bars[2]; - // calculate black bars + // calculate black bars if (screen_aspect > viewport_aspect) { // pillarboxing - cout << "pillarboxing" << endl; - float render_scale = cam.screen.y / (cam.viewport_size.y / cam.zoom); - float adj_width = (cam.viewport_size.x / cam.zoom )* render_scale; + float scale = cam.screen.y / zoomed_viewport.y; + float adj_width = zoomed_viewport.x * 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}; + this->black_bars[0] = {0, 0, bar_width, (float) cam.screen.y}; + this->black_bars[1] = {(cam.screen.x - bar_width), 0, bar_width, (float) cam.screen.y}; - bar_size = {bar_width,0}; - scale.x = scale.y = render_scale; + bar_size = {bar_width, 0}; + render_scale.x = render_scale.y = scale; } else { // letterboxing - cout << "letterboxing" << endl; - float render_scale = cam.screen.x / (cam.viewport_size.x * cam.zoom); - float adj_height = cam.viewport_size.y * render_scale; + float scale = cam.screen.x / (cam.viewport_size.x * cam.zoom); + float adj_height = cam.viewport_size.y * 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}; - - bar_size = {0,bar_height}; - scale.x = scale.y = render_scale; - } + this->black_bars[0] = {0, 0, (float) cam.screen.x, bar_height}; + this->black_bars[1] + = {0, (cam.screen.y - bar_height), (float) cam.screen.x, bar_height}; - 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; + bar_size = {0, bar_height}; + render_scale.x = render_scale.y = scale; } - 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); SDL_Rect bg = { .x = 0, .y = 0, - .w = (int)cam.viewport_size.x, - .h = (int)cam.viewport_size.y, + .w = cam.screen.x, + .h = cam.screen.y, }; + // fill bg color SDL_RenderFillRect(this->game_renderer.get(), &bg); - - */ } uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index ead0c72..285eee2 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -29,15 +29,23 @@ typedef SDL_Keycode CREPE_KEYCODES; */ class SDLContext { public: + //! data that the camera component cannot hold + struct CameraValues { + vec2 zoomed_viewport; + vec2 render_scale; + vec2 bar_size; + vec2 cam_pos; + }; + struct RenderContext { const Sprite & sprite; - const Camera & cam; - const vec2 & cam_pos; + const CameraValues & cam; const vec2 & pos; const double & angle; const double & scale; }; + public: /** * \brief Gets the singleton instance of SDLContext. @@ -137,9 +145,15 @@ private: * \brief sets the background of the camera (will be adjusted in future PR) * \param camera Reference to the Camera object. */ - void set_camera(const Camera & camera); + void set_camera(const Camera & camera, CameraValues & ctx); private: + struct DstRect { + const Sprite & sprite; + const CameraValues & cam; + const vec2 & pos; + const double & img_scale; + }; /** * \brief calculates the sqaure size of the image * @@ -158,8 +172,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_FRect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, - const vec2 & cam_pos, const double & img_scale) const; + SDL_FRect get_dst_rect(const DstRect & ctx) const; private: //! sdl Window @@ -167,6 +180,9 @@ private: //! renderer for the crepe engine std::unique_ptr<SDL_Renderer, std::function<void(SDL_Renderer *)>> game_renderer; + + //! black bars rectangle to draw + SDL_FRect black_bars[2]; }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index c196bb1..944ac86 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -32,8 +32,8 @@ const Camera & RenderSystem::update_camera() { if (!cam.active) continue; const Transform & transform = mgr.get_components_by_id<Transform>(cam.game_object_id).front().get(); - this->context.set_camera(cam); - this->cam_pos = transform.position + cam.offset; + this->context.set_camera(cam, this->cam_ctx); + this->cam_ctx.cam_pos = transform.position + cam.offset; return cam; } throw std::runtime_error("No active cameras in current scene"); @@ -79,8 +79,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, this->context.draw(SDLContext::RenderContext{ .sprite = sprite, - .cam = cam, - .cam_pos = this->cam_pos, + .cam = this->cam_ctx, .pos = p.position, .angle = p.angle, .scale = scale, @@ -93,8 +92,7 @@ void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam, const Transform & tm) { this->context.draw(SDLContext::RenderContext{ .sprite = sprite, - .cam = cam, - .cam_pos = this->cam_pos, + .cam = this->cam_ctx, .pos = tm.position, .angle = tm.rotation, .scale = tm.scale, diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index e70831e..9ff015e 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -76,9 +76,8 @@ private: private: SDLContext & context = SDLContext::get_instance(); - - //! camera postion in the current scene - vec2 cam_pos; + + SDLContext::CameraValues cam_ctx; }; } // namespace crepe |