diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-18 20:12:31 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-18 20:12:31 +0100 |
commit | 88f613cbb2e4aaf3ed55ac0c6490706dd6f6f19d (patch) | |
tree | 8db09f0a0f27dc552356a6c5513589245e9ff7b7 /src/crepe | |
parent | 9288e4964526f1ce6b7d0aca0f075a04f56ede32 (diff) |
rendering based on world unites instead of pixels
Diffstat (limited to 'src/crepe')
-rw-r--r-- | src/crepe/api/Animator.cpp | 2 | ||||
-rw-r--r-- | src/crepe/api/Camera.h | 39 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 36 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 2 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 3 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.h | 3 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 1 |
7 files changed, 36 insertions, 50 deletions
diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 464b0fd..f3d809c 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -18,7 +18,7 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a animator_rect.h /= col; animator_rect.w /= row; animator_rect.x = 0; - animator_rect.y = col_animator * animator_rect.h; + animator_rect.y = (col_animator - 1)* animator_rect.h; this->active = false; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index 73d4ef4..dfccd24 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -28,32 +28,25 @@ public: //! Background color of the camera view. Color bg_color; - Vector2 pos = {0,0}; - - Vector2 screen = {640,480}; - - Vector2 viewport = {500,500}; - - /* - //! screen width in pixel coordinates - double screen_w = 480; - - //! screen height in pixel coordinates - double screen_h = 640; - - //! screen widht in world units - double viewport_w = 500.0f; - - //! screen height in world units - double viewport_h = 500.0f; + /** + * \pos The position of the camera in world units + */ + Vector2 pos = {0, 0}; - //! X-coordinate of the camera position. in world space - double x = 0.0; + /** + * \screen the display size in pixels ( output resolution ) + */ + Vector2 screen = {640, 480}; - //! Y-coordinate of the camera position. in world space - double y = 0.0; + /** + * \viewport is the area of the world visible through the camera (in world units) + */ + Vector2 viewport = {500, 500}; - */ + /** + * \scale scaling factor from world units to pixel coordinates + */ + Vector2 scale = {0,0}; //! Zoom level of the camera view. double zoom = 1.0f; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 5185adc..785b285 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -111,21 +111,10 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); - double screen_aspect = cam.screen.x / cam.screen.y; - double viewport_aspect = cam.viewport.x / cam.viewport.y; - Vector2 scale; - if (screen_aspect > viewport_aspect) { - scale.x = scale.y = cam.screen.x / cam.viewport.x; - } else { - scale.y = scale.x = cam.screen.y / cam.viewport.y; - } - - Vector2 zoomed_viewport = cam.viewport * cam.zoom; - Vector2 pixel_coord = (transform.position - cam.pos) * scale; - - double pixel_w = sprite.sprite_rect.w * transform.scale * scale.x; - double pixel_h = sprite.sprite_rect.h * transform.scale * scale.y; + Vector2 pixel_coord = (transform.position - cam.pos) * cam.scale; + double pixel_w = sprite.sprite_rect.w * transform.scale * cam.scale.x; + double pixel_h = sprite.sprite_rect.h * transform.scale * cam.scale.y; // decides which part of the sprite will be drawn SDL_Rect srcrect = { @@ -147,7 +136,18 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const &dstrect, transform.rotation, NULL, render_flip); } -void SDLContext::camera(const Camera & cam) { +void SDLContext::camera(Camera & cam) { + + double screen_aspect = cam.screen.x / cam.screen.y; + double viewport_aspect = cam.viewport.x / cam.viewport.y; + Vector2 zoomed_viewport = cam.viewport * cam.zoom; + + if (screen_aspect > viewport_aspect) { + cam.scale.x = cam.scale.y = cam.screen.x / zoomed_viewport.x; + } else { + cam.scale.y = cam.scale.x = cam.screen.y / zoomed_viewport.y; + } + if (this->viewport.w != cam.screen.x && this->viewport.h != cam.screen.y) { this->viewport.w = cam.screen.x; @@ -155,12 +155,6 @@ void SDLContext::camera(const Camera & cam) { SDL_SetWindowSize(this->game_window.get(), cam.screen.x , cam.screen.y); } - /* - this->viewport.x = static_cast<int>(cam.x) - (SCREEN_WIDTH / 2); - this->viewport.y = static_cast<int>(cam.y) - (SCREEN_HEIGHT / 2); - - */ - SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g, cam.bg_color.b, cam.bg_color.a); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 007092b..b69e509 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -137,7 +137,7 @@ private: * \brief Sets the current camera for rendering. * \param camera Reference to the Camera object. */ - void camera(const Camera & camera); + void camera(Camera & camera); private: //! sdl Window diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 9d18873..ceb5bfd 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -3,8 +3,8 @@ #include <vector> #include "api/Animator.h" -#include "facade/SDLContext.h" +#include "facade/SDLContext.h" #include "AnimatorSystem.h" #include "ComponentManager.h" @@ -19,6 +19,7 @@ void AnimatorSystem::update() { uint64_t tick = SDLContext::get_instance().get_ticks(); for (Animator & a : animations) { if (a.active) { + // (10 frames per second) a.curr_row = (tick / 100) % a.row; a.animator_rect.x = (a.curr_row * a.animator_rect.w) + a.curr_col; a.spritesheet.sprite_rect = a.animator_rect; diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h index 56cc7b3..f8179a9 100644 --- a/src/crepe/system/AnimatorSystem.h +++ b/src/crepe/system/AnimatorSystem.h @@ -21,12 +21,11 @@ public: /** * \brief Updates the Animator components. * - * This method is called periodically (likely every frame) to update the state of all + * This method is called to update the state of all * Animator components, moving the animations forward and managing their behavior (e.g., * looping). */ void update() override; - // FIXME: never say "likely" in the documentation lmao }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index fa3d0de..989a82f 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -5,7 +5,6 @@ #include "../api/Sprite.h" #include "../api/Transform.h" #include "../facade/SDLContext.h" -#include "../util/Log.h" #include "RenderSystem.h" |