diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-28 09:18:13 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-28 09:18:13 +0100 |
commit | 71be3e36dbb402c3e84d87ea0255c08cb2a1b7ca (patch) | |
tree | 25a26be197fe64d2e4411ee0db1568fb6eb0b6f5 /src/crepe | |
parent | 5fa57ab2d8f809b8cb6bd72f54567a01f3e63b95 (diff) |
implemented feedback to have draw struct
Diffstat (limited to 'src/crepe')
-rw-r--r-- | src/crepe/api/Camera.h | 4 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 35 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 22 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 25 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.h | 6 | ||||
-rw-r--r-- | src/crepe/types.h | 13 |
6 files changed, 56 insertions, 49 deletions
diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index c7b2d08..ac56495 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -32,10 +32,6 @@ public: //! offset postion from the game object transform component vec2 offset; - //! pos the postion of the camera in world space this will be filled with - //pos = transform + offset - vec2 pos = {0, 0}; - //! screen the display size in pixels ( output resolution ) const ivec2 screen = {1080, 720}; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 0d42a4c..7317a77 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -109,7 +109,7 @@ 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 double & img_scale) const { + const vec2 & cam_pos, const double & img_scale) const { int width = sprite.height * sprite.aspect_ratio; int height = sprite.height; @@ -118,38 +118,25 @@ SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const height *= img_scale * cam.zoom; return SDL_Rect{ - .x = static_cast<int>((pos.x - cam.pos.x + (cam.viewport.x / 2) - width / 2)), - .y = static_cast<int>((pos.y - cam.pos.y + (cam.viewport.y / 2) - height / 2)), + .x = static_cast<int>((pos.x - cam_pos.x + (cam.viewport.x / 2) - width / 2)), + .y = static_cast<int>((pos.y - cam_pos.y + (cam.viewport.y / 2) - height / 2)), .w = width, .h = height, }; } -void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle, - const double & img_scale, const Camera & cam) { +void SDLContext::draw(const RenderCtx & ctx) { SDL_RendererFlip render_flip - = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) - | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); + = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * ctx.sprite.flip.flip_x) + | (SDL_FLIP_VERTICAL * ctx.sprite.flip.flip_y)); - SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam, img_scale); + SDL_Rect srcrect = this->get_src_rect(ctx.sprite); + SDL_Rect dstrect + = this->get_dst_rect(ctx.sprite, ctx.pos, ctx.cam, ctx.cam_pos, ctx.scale); - SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image.texture.get(), &srcrect, - &dstrect, angle, NULL, render_flip); -} - -void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam) { - - SDL_RendererFlip render_flip - = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) - | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); - - SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, cam, transform.scale); - - SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image.texture.get(), &srcrect, - &dstrect, transform.rotation, NULL, render_flip); + SDL_RenderCopyEx(this->game_renderer.get(), ctx.sprite.sprite_image.texture.get(), + &srcrect, &dstrect, ctx.angle, NULL, render_flip); } void SDLContext::set_camera(const Camera & cam) { diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 35d667d..7907a0f 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -11,7 +11,6 @@ #include "../api/Camera.h" #include "../api/Sprite.h" -#include "../api/Transform.h" #include "types.h" @@ -115,23 +114,9 @@ private: /** * \brief Draws a sprite to the screen using the specified transform and camera. - * \param sprite Reference to the Sprite to draw. - * \param transform Reference to the Transform for positioning. - * \param cam camera of the current scene + * \param RenderCtx Reference to rendering data to draw */ - void draw(const Sprite & sprite, const Transform & transform, const Camera & cam); - - /** - * \brief Draws a particle to the screen using the specified parameters - * - * \param sprite Referenceto the sprite to draw - * \param pos particle position in world units - * \param angle particle angle in degrees - * \param img_scale scalar multiplier to increase image size - * \param cam camera of the current scene - */ - void draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle, - const double & img_scale, const Camera & cam); + void draw(const RenderCtx & ctx); //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -160,10 +145,11 @@ private: * \param sprite Reference to the sprite to calculate rectangle * \param pos the pos in world units * \param cam the camera of the current scene + * \param cam_pos the current postion of the camera * \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_Rect 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/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index a1443cd..bfee658 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -11,6 +11,7 @@ #include "../api/Transform.h" #include "../facade/SDLContext.h" #include "api/Camera.h" +#include "types.h" #include "RenderSystem.h" @@ -33,7 +34,7 @@ const Camera & RenderSystem::update_camera() { const Transform & transform = mgr.get_components_by_id<Transform>(cam.game_object_id).front().get(); this->context.set_camera(cam); - cam.pos = transform.position + cam.offset; + this->cam_pos = transform.position + cam.offset; return cam; } throw std::runtime_error("No active cameras in current scene"); @@ -76,14 +77,32 @@ bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, for (const Particle & p : em.data.particles) { if (!p.active) continue; - this->context.draw_particle(sprite, p.position, p.angle, scale, cam); + + RenderCtx ctx{ + .sprite = sprite, + .cam = cam, + .cam_pos = this->cam_pos, + .pos = p.position, + .angle = p.angle, + .scale = scale, + }; + this->context.draw(ctx); } } return rendering_particles; } void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam, const Transform & tm) { - this->context.draw(sprite, tm, cam); + + RenderCtx ctx{ + .sprite = sprite, + .cam = cam, + .cam_pos = this->cam_pos, + .pos = tm.position, + .angle = tm.rotation, + .scale = tm.scale, + }; + this->context.draw(ctx); } void RenderSystem::render() { diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 46ebb92..4667424 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -11,6 +11,8 @@ namespace crepe { class Camera; class Sprite; +class Transform; + /** * \class RenderSystem @@ -75,6 +77,10 @@ private: private: SDLContext & context = SDLContext::get_instance(); + + //! camera postion in the current scene + vec2 cam_pos = {0,0}; + }; } // namespace crepe diff --git a/src/crepe/types.h b/src/crepe/types.h index 17f1619..aa03f53 100644 --- a/src/crepe/types.h +++ b/src/crepe/types.h @@ -27,4 +27,17 @@ typedef Vector2<float> vec2; //! Default Vector2<double> type typedef Vector2<double> dvec2; +class Sprite; +class Camera; + +struct RenderCtx{ + const Sprite & sprite; + const Camera & cam; + const vec2 & cam_pos; + const vec2 & pos; + const double & angle; + const double & scale; + +} ; + } // namespace crepe |