From 6ae0b9038e432869b506cbdfe2779e97d3732d87 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Sat, 30 Nov 2024 21:02:27 +0100 Subject: improved animator --- src/crepe/api/Animator.h | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'src/crepe/api/Animator.h') diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 6c506aa..ede450a 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -17,11 +17,6 @@ class SDLContext; */ class Animator : public Component { -public: - //TODO: need to implement this - void loop(); - void stop(); - public: /** * \brief Constructs an Animator object that will control animations for a sprite sheet. @@ -57,15 +52,32 @@ private: //! The current row being animated. int curr_row = 0; - //TODO: Is this necessary? - //int fps; + //! should the animation loop + bool looping = false; + + //! starting frame for cycling + int cycle_start = 0; + + //! end frame for cycling (-1 --> use last frame) + int cycle_end = -1; + + //! frames per second for animation + int fps = 1; + + int offset_x = 0; + +public: + void loop() { this->looping = true; } + void play() {this->active = true;} + void pause() {this->active = false;} + void stop() {this->active = false; this->curr_col = 0; this->curr_row = 0;} + void set_fps(int fps) {this->fps = fps;} + void set_cycle_range(int start, int end) {this->cycle_start = start, this->cycle_end = end;} + void set_anim(int col) {this->curr_row = 0; this->curr_col = col; } private: //! AnimatorSystem adjust the private member parameters of Animator; friend class AnimatorSystem; - - //! SDLContext reads the Animator member var's - friend class SDLContext; }; } // namespace crepe // -- cgit v1.2.3 From 3afcae9dd472ead2d5f2b667fc6479f8ee6db10c Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Sat, 30 Nov 2024 21:03:11 +0100 Subject: make format --- src/crepe/api/Animator.h | 21 +++++++++++++++------ src/crepe/api/LoopManager.cpp | 4 ++-- src/crepe/facade/SDLContext.cpp | 13 +++++++------ src/crepe/facade/SDLContext.h | 1 - src/crepe/system/AnimatorSystem.cpp | 4 ++-- src/crepe/system/RenderSystem.h | 2 +- 6 files changed, 27 insertions(+), 18 deletions(-) (limited to 'src/crepe/api/Animator.h') diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index ede450a..0da0469 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -68,12 +68,21 @@ private: public: void loop() { this->looping = true; } - void play() {this->active = true;} - void pause() {this->active = false;} - void stop() {this->active = false; this->curr_col = 0; this->curr_row = 0;} - void set_fps(int fps) {this->fps = fps;} - void set_cycle_range(int start, int end) {this->cycle_start = start, this->cycle_end = end;} - void set_anim(int col) {this->curr_row = 0; this->curr_col = col; } + void play() { this->active = true; } + void pause() { this->active = false; } + void stop() { + this->active = false; + this->curr_col = 0; + this->curr_row = 0; + } + void set_fps(int fps) { this->fps = fps; } + void set_cycle_range(int start, int end) { + this->cycle_start = start, this->cycle_end = end; + } + void set_anim(int col) { + this->curr_row = 0; + this->curr_col = col; + } private: //! AnimatorSystem adjust the private member parameters of Animator; diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 88ca704..51d9122 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -67,7 +67,7 @@ void LoopManager::render() { } } -void LoopManager::update() { - LoopTimer & timer = LoopTimer::get_instance(); +void LoopManager::update() { + LoopTimer & timer = LoopTimer::get_instance(); this->get_system().update(); } diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index f0a21d5..9c2ead4 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -113,11 +113,12 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { SDL_FRect SDLContext::get_dst_rect(const DstRect & ctx) const { - - vec2 size = { - ctx.sprite.size.x == 0 && ctx.sprite.size.y != 0 ? ctx.sprite.size.y * ctx.sprite.aspect_ratio : ctx.sprite.size.x, - ctx.sprite.size.y == 0 && ctx.sprite.size.x != 0 ? ctx.sprite.size.x / ctx.sprite.aspect_ratio : ctx.sprite.size.y - }; + vec2 size = {ctx.sprite.size.x == 0 && ctx.sprite.size.y != 0 + ? ctx.sprite.size.y * ctx.sprite.aspect_ratio + : ctx.sprite.size.x, + ctx.sprite.size.y == 0 && ctx.sprite.size.x != 0 + ? ctx.sprite.size.x / ctx.sprite.aspect_ratio + : ctx.sprite.size.y}; const CameraValues & cam = ctx.cam; @@ -169,7 +170,7 @@ void SDLContext::set_camera(const Camera & cam, CameraValues & ctx) { double screen_aspect = static_cast(cam.screen.x) / cam.screen.y; double viewport_aspect = zoomed_viewport.x / zoomed_viewport.y; - // calculate black bars + // calculate black bars if (screen_aspect > viewport_aspect) { // pillarboxing float scale = cam.screen.y / zoomed_viewport.y; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 285eee2..cd52be6 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -45,7 +45,6 @@ public: const double & scale; }; - public: /** * \brief Gets the singleton instance of SDLContext. diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 6a84150..ee00728 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -22,11 +22,11 @@ void AnimatorSystem::update() { int cycle_end = (a.cycle_end == -1) ? a.row : cycle_end; int total_frames = cycle_end - a.cycle_start; - int curr_frame = static_cast(elapsed_time / frame_duration) % total_frames; a.curr_row = a.cycle_start + curr_frame; - a.spritesheet.mask.x = std::clamp((a.curr_row * a.spritesheet.mask.w - a.offset_x), 0, a.spritesheet.mask.w); + a.spritesheet.mask.x = std::clamp((a.curr_row * a.spritesheet.mask.w - a.offset_x), 0, + a.spritesheet.mask.w); a.spritesheet.mask.y = (a.curr_col * a.spritesheet.mask.h); if (!a.looping && curr_frame == total_frames) { diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 9ff015e..249f3b8 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -76,7 +76,7 @@ private: private: SDLContext & context = SDLContext::get_instance(); - + SDLContext::CameraValues cam_ctx; }; -- cgit v1.2.3 From 54ab44e3508d526be6275378e5979290ec188d6f Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 2 Dec 2024 10:18:08 +0100 Subject: comments i animator and sprite. added sprite indepentdent scale and angle --- src/crepe/api/Animator.cpp | 17 ++++++++++ src/crepe/api/Animator.h | 74 ++++++++++++++++++++++++++++++----------- src/crepe/api/Sprite.h | 16 +++++++++ src/crepe/api/Transform.h | 2 +- src/crepe/facade/SDLContext.cpp | 8 +++-- src/crepe/facade/SDLContext.h | 2 -- 6 files changed, 94 insertions(+), 25 deletions(-) (limited to 'src/crepe/api/Animator.h') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 7fe49ee..0b7e86d 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -24,3 +24,20 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a = static_cast(this->spritesheet.mask.w) / this->spritesheet.mask.h; } Animator::~Animator() { dbg_trace(); } + +void Animator::loop() { this->looping = true; } +void Animator::play() { this->active = true; } +void Animator::pause() { this->active = false; } +void Animator::stop() { + this->active = false; + this->curr_col = 0; + this->curr_row = 0; +} +void Animator::set_fps(int fps) { this->fps = fps; } +void Animator::set_cycle_range(int start, int end) { + this->cycle_start = start, this->cycle_end = end; +} +void Animator::set_anim(int col) { + this->curr_row = 0; + this->curr_col = col; +} diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 0da0469..188f193 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -17,6 +17,59 @@ class SDLContext; */ class Animator : public Component { +public: + /** + * \brief Animator will repeat the animation + * + */ + void loop(); + + /** + * \brief starts the animation + * + */ + void play(); + /** + * \brief pauses the animation + * + * sets the active false + * + */ + void pause(); + + /** + * \brief stops the animation + * + * sets the active on false and resets all the current rows and columns + * + */ + void stop(); + /** + * \brief set frames per second + * + * \param fps frames per second + */ + void set_fps(int fps); + /** + * \brief set the range in the row + * + * \param start of row animation + * \param end of row animation + */ + void set_cycle_range(int start, int end); + /** + * \brief select which column to animate from + * + * \param col animation column + */ + void set_anim(int col); + + /** + * \brief will go to the next animaiton of current row + * + */ + void next_anim(); + public: /** * \brief Constructs an Animator object that will control animations for a sprite sheet. @@ -37,7 +90,7 @@ public: ~Animator(); // dbg_trace private: - //! A reference to the Sprite sheet containing the animation frames. + //! A reference to the Sprite sheet containing. Sprite & spritesheet; //! The maximum number of columns in the sprite sheet. @@ -64,26 +117,9 @@ private: //! frames per second for animation int fps = 1; + //! offset in pixels. int offset_x = 0; -public: - void loop() { this->looping = true; } - void play() { this->active = true; } - void pause() { this->active = false; } - void stop() { - this->active = false; - this->curr_col = 0; - this->curr_row = 0; - } - void set_fps(int fps) { this->fps = fps; } - void set_cycle_range(int start, int end) { - this->cycle_start = start, this->cycle_end = end; - } - void set_anim(int col) { - this->curr_row = 0; - this->curr_col = col; - } - private: //! AnimatorSystem adjust the private member parameters of Animator; friend class AnimatorSystem; diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index f04f70c..96b57e1 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -58,11 +58,27 @@ public: //! Layer sorting level of the sprite const int sorting_in_layer; + //! Order within the sorting layer const int order_in_layer; + /** + * \size width and height of the sprite in game units + * + * if height is filled in and not width it will multiply width by aspect_ratio. + * if width is filled in and not height it will multiply height by aspect_ratio. + * if neither is filled it will not show sprite because size will be zero + * if both are filled will it use the width and height without making sure the aspect_ratio + * is correct + */ vec2 size; + //! independent sprite angle. rotating clockwise direction in degrees + double angle_offset; + + //! independent sprite scale multiplier + double scale; + /** * \aspect_ratio ratio of the img so that scaling will not become weird * diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index 6243a93..34ac70a 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -15,7 +15,7 @@ class Transform : public Component { public: //! Translation (shift) vec2 position = {0, 0}; - //! Rotation, in degrees + //! Rotation, in degrees clockwise double rotation = 0; //! Multiplication factor double scale = 0; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 9c2ead4..bba26a3 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include @@ -113,6 +112,7 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { SDL_FRect SDLContext::get_dst_rect(const DstRect & ctx) const { + // this might not work all the time because of float checking zero -_- vec2 size = {ctx.sprite.size.x == 0 && ctx.sprite.size.y != 0 ? ctx.sprite.size.y * ctx.sprite.aspect_ratio : ctx.sprite.size.x, @@ -122,7 +122,7 @@ SDL_FRect SDLContext::get_dst_rect(const DstRect & ctx) const { const CameraValues & cam = ctx.cam; - size *= cam.render_scale * ctx.img_scale; + size *= cam.render_scale * ctx.img_scale * ctx.sprite.scale; vec2 screen_pos = (ctx.pos - cam.cam_pos + (cam.zoomed_viewport) / 2) * cam.render_scale - size / 2 + cam.bar_size; @@ -149,8 +149,10 @@ void SDLContext::draw(const RenderContext & ctx) { .img_scale = ctx.scale, }); + double angle = ctx.angle + ctx.sprite.angle_offset; + SDL_RenderCopyExF(this->game_renderer.get(), ctx.sprite.sprite_image.texture.get(), - &srcrect, &dstrect, ctx.angle, NULL, render_flip); + &srcrect, &dstrect, angle, NULL, render_flip); } void SDLContext::set_camera(const Camera & cam, CameraValues & ctx) { diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index cd52be6..0a6fce6 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -67,8 +67,6 @@ private: void handle_events(bool & running); private: - //! Will only use get_ticks - friend class AnimatorSystem; //! Will only use delay friend class LoopTimer; /** -- cgit v1.2.3 From 135af8b0e0eaf8a5a5b7e1a42bfb20bb14ec97e5 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 2 Dec 2024 18:51:47 +0100 Subject: tmp commit --- src/crepe/api/Animator.h | 2 +- src/crepe/api/Sprite.h | 4 ++-- src/crepe/system/AnimatorSystem.cpp | 9 +++++---- src/example/rendering_particle.cpp | 11 +++++------ 4 files changed, 13 insertions(+), 13 deletions(-) (limited to 'src/crepe/api/Animator.h') diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 188f193..511d6ef 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -89,7 +89,7 @@ public: ~Animator(); // dbg_trace -private: +public: //! A reference to the Sprite sheet containing. Sprite & spritesheet; diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 96b57e1..354f663 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -74,10 +74,10 @@ public: vec2 size; //! independent sprite angle. rotating clockwise direction in degrees - double angle_offset; + double angle_offset = 0; //! independent sprite scale multiplier - double scale; + double scale = 1; /** * \aspect_ratio ratio of the img so that scaling will not become weird diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index ee00728..1650b3d 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -4,6 +4,7 @@ #include "AnimatorSystem.h" #include "ComponentManager.h" +#include using namespace crepe; @@ -25,11 +26,11 @@ void AnimatorSystem::update() { int curr_frame = static_cast(elapsed_time / frame_duration) % total_frames; a.curr_row = a.cycle_start + curr_frame; - a.spritesheet.mask.x = std::clamp((a.curr_row * a.spritesheet.mask.w - a.offset_x), 0, - a.spritesheet.mask.w); + a.spritesheet.mask.x = a.curr_row * a.spritesheet.mask.w; a.spritesheet.mask.y = (a.curr_col * a.spritesheet.mask.h); - - if (!a.looping && curr_frame == total_frames) { + + std::cout << curr_frame << " " << total_frames << std::endl; + if (!a.looping && curr_frame == 0) { a.active = false; } } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 026c9ce..5a50d27 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -53,16 +53,15 @@ public: Color color(255, 255, 255, 255); - auto img = Texture("asset/spritesheet/spritesheet_test.png"); + auto img = Texture("asset/spritesheet/pokemon_spritesheet.png"); Sprite & test_sprite = game_object.add_component( - img, color, Sprite::FlipSettings{true, true}, 1, 1, vec2{1, 1}); + img, color, Sprite::FlipSettings{false, false}, 1, 1, vec2{100, 100}); - //game_object.add_component(test_sprite, 4, 1, 0).active = true; - game_object.add_component(test_sprite, 4, 1, 0).active = true; + auto & anim = game_object.add_component(test_sprite, 4, 4, 0); - auto & cam = game_object.add_component(Color::RED, ivec2{1280, 720}, - vec2{2.59, 1.95}, 2.0); + auto & cam = game_object.add_component(Color::, ivec2{720, 1280}, + vec2{400, 400}, 1.0); } string get_name() const { return "TestScene"; }; -- cgit v1.2.3 From a84ca09e97d466643f022acfffcf4c6a77f42052 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 2 Dec 2024 20:02:14 +0100 Subject: making struct constructors --- src/crepe/api/Animator.cpp | 34 ++++++------- src/crepe/api/Animator.h | 66 +++++++++++++------------ src/crepe/api/Camera.cpp | 11 +---- src/crepe/api/Camera.h | 37 +++++++------- src/crepe/api/Sprite.cpp | 17 +++---- src/crepe/api/Sprite.h | 98 +++++++++++++++++-------------------- src/crepe/facade/SDLContext.cpp | 25 +++++----- src/crepe/system/AnimatorSystem.cpp | 17 ++++--- src/crepe/system/RenderSystem.cpp | 5 +- src/example/rendering_particle.cpp | 19 +++++-- 10 files changed, 166 insertions(+), 163 deletions(-) (limited to 'src/crepe/api/Animator.h') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 0b7e86d..ce824e6 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -7,37 +7,37 @@ using namespace crepe; -Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_animator) +Animator::Animator(game_object_id_t id, const Animator::Data & ctx) : Component(id), - spritesheet(ss), - row(row), - col(col) { + data(ctx) +{ dbg_trace(); - this->spritesheet.mask.h /= col; - this->spritesheet.mask.w /= row; - this->spritesheet.mask.x = 0; - this->spritesheet.mask.y = col_animator * this->spritesheet.mask.h; + this->data.spritesheet.mask.h /= this->data.col; + this->data.spritesheet.mask.w /= this->data.row; + this->data.spritesheet.mask.x = 0; + this->data.spritesheet.mask.y = this->data.col * this->data.spritesheet.mask.h; // need to do this for to get the aspect ratio for a single clipping in the spritesheet - this->spritesheet.aspect_ratio - = static_cast(this->spritesheet.mask.w) / this->spritesheet.mask.h; + Sprite & ss = this->data.spritesheet; + ss.data.aspect_ratio + = static_cast(this->data.spritesheet.mask.w) / this->data.spritesheet.mask.h; } Animator::~Animator() { dbg_trace(); } -void Animator::loop() { this->looping = true; } +void Animator::loop() { this->data.looping = true; } void Animator::play() { this->active = true; } void Animator::pause() { this->active = false; } void Animator::stop() { this->active = false; - this->curr_col = 0; - this->curr_row = 0; + this->data.curr_col = 0; + this->data.curr_row = 0; } -void Animator::set_fps(int fps) { this->fps = fps; } +void Animator::set_fps(int fps) { this->data.fps = fps; } void Animator::set_cycle_range(int start, int end) { - this->cycle_start = start, this->cycle_end = end; + this->data.cycle_start = start, this->data.cycle_end = end; } void Animator::set_anim(int col) { - this->curr_row = 0; - this->curr_col = col; + this->data.curr_row = 0; + this->data.curr_col = col; } diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 511d6ef..194a9cf 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -16,6 +16,39 @@ class SDLContext; * sheet. It can be used to play animations, loop them, or stop them. */ class Animator : public Component { +public: + struct Data { + //! A reference to the Sprite sheet containing. + Sprite & spritesheet; + + //! The maximum number of columns in the sprite sheet. + const int col; + + //! The maximum number of rows in the sprite sheet. + const int row; + + //! frames per second for animation + int fps; + + //! The current col being animated. + int curr_col; + + //! The current row being animated. + int curr_row = 0; + + //! should the animation loop + bool looping = false; + + //! starting frame for cycling + int cycle_start = 0; + + //! end frame for cycling (-1 --> use last frame) + int cycle_end = -1; + + + //! offset in pixels. + int offset_x = 0; + }; public: /** @@ -85,40 +118,11 @@ public: * This constructor sets up the Animator with the given parameters, and initializes the * animation system. */ - Animator(uint32_t id, Sprite & spritesheet, int row, int col, int col_animate); - + Animator(uint32_t id, const Animator::Data & ctx); ~Animator(); // dbg_trace public: - //! A reference to the Sprite sheet containing. - Sprite & spritesheet; - - //! The maximum number of columns in the sprite sheet. - const int col; - - //! The maximum number of rows in the sprite sheet. - const int row; - - //! The current col being animated. - int curr_col = 0; - - //! The current row being animated. - int curr_row = 0; - - //! should the animation loop - bool looping = false; - - //! starting frame for cycling - int cycle_start = 0; - - //! end frame for cycling (-1 --> use last frame) - int cycle_end = -1; - - //! frames per second for animation - int fps = 1; - - //! offset in pixels. - int offset_x = 0; + Animator::Data data; private: //! AnimatorSystem adjust the private member parameters of Animator; diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index 39d8ab0..27bcb35 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -2,19 +2,12 @@ #include "util/Log.h" #include "Camera.h" -#include "Color.h" #include "Component.h" using namespace crepe; -Camera::Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen, - const vec2 & viewport_size, const double & zoom, const vec2 & offset) - : Component(id), - bg_color(bg_color), - offset(offset), - screen(screen), - viewport_size(viewport_size), - zoom(zoom) { +Camera::Camera(game_object_id_t id, const Data & ctx) : Component(id), data(ctx) { + dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index 2d8fa48..e466d36 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -14,32 +14,35 @@ namespace crepe { * position, and zoom level. It controls what part of the game world is visible on the screen. */ class Camera : public Component { +public: + struct Data { + //! Background color of the camera view. + const Color bg_color; + + //! screen the display size in pixels ( output resolution ) + const ivec2 screen; + + //! viewport is the area of the world visible through the camera (in world units) + const vec2 viewport_size; + + //! Zoom level of the camera view. + double zoom; + + //! offset postion from the game object transform component + vec2 offset; + }; public: /** * \brief Constructs a Camera with the specified ID and background color. * \param id Unique identifier for the camera component. - * \param bg_color Background color for the camera view. + * \param ctx the camera component data */ - Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen, - const vec2 & viewport_size, const double & zoom, const vec2 & offset = {0, 0}); + Camera(game_object_id_t id, const Data & ctx); ~Camera(); // dbg_trace only public: - //! Background color of the camera view. - const Color bg_color; - - //! offset postion from the game object transform component - vec2 offset; - - //! screen the display size in pixels ( output resolution ) - const ivec2 screen; - - //! viewport is the area of the world visible through the camera (in world units) - const vec2 viewport_size; - - //! Zoom level of the camera view. - const double zoom; + Camera::Data data; public: /** diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 29e415f..fe495a1 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -11,21 +11,16 @@ 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, const vec2 & size) +Sprite::Sprite(game_object_id_t id, Texture & texture, const Sprite::Data & ctx) : Component(id), - color(color), - flip(flip), - sprite_image(std::move(image)), - sorting_in_layer(sort_layer), - order_in_layer(order_layer), - size(size) { + texture(std::move(texture)), + data(ctx) { dbg_trace(); - this->mask.w = sprite_image.get_width(); - this->mask.h = sprite_image.get_height(); - this->aspect_ratio = static_cast(this->mask.w) / this->mask.h; + this->mask.w = this->texture.get_width(); + this->mask.h = this->texture.get_height(); + this->data.aspect_ratio = static_cast(this->mask.w) / this->mask.h; } Sprite::~Sprite() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 354f663..aef6a8d 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -19,73 +19,67 @@ class AnimatorSystem; * flip settings, and is managed in layers with defined sorting orders. */ class Sprite : public Component { - public: struct FlipSettings { bool flip_x = false; bool flip_y = false; }; + struct Data { + //! Color tint of the sprite + Color color; + + //! Flip settings for the sprite + FlipSettings flip; + + //! Layer sorting level of the sprite + const int sorting_in_layer; + + //! Order within the sorting layer + const int order_in_layer; + + /** + * \size width and height of the sprite in game units + * + * if height is filled in and not width it will multiply width by aspect_ratio. + * if width is filled in and not height it will multiply height by aspect_ratio. + * if neither is filled it will not show sprite because size will be zero + * if both are filled will it use the width and height without making sure the aspect_ratio + * is correct + */ + vec2 size; + + //! independent sprite angle. rotating clockwise direction in degrees + double angle_offset = 0; + + //! independent sprite scale multiplier + double scale = 1; + + /** + * \aspect_ratio ratio of the img so that scaling will not become weird + * + * cannot be const because if Animator component is addded then ratio becomes scuffed and + * does it need to be calculated again in the Animator + */ + float aspect_ratio; + }; + public: - // TODO: Loek comment in github #27 will be looked another time - // about shared_ptr Texture /** * \brief Constructs a Sprite with specified parameters. * \param game_id Unique identifier for the game object this sprite belongs to. - * \param image Shared pointer to the texture for this sprite. - * \param color Color tint applied to the sprite. - * \param flip Flip settings for horizontal and vertical orientation. - * \param order_layer decides the sorting in layer of the sprite. - * \param sort_layer decides the order in layer of the sprite. - * \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, const vec2 & size); - - /** - * \brief Destroys the Sprite instance. + * \param texture asset of the image + * \param ctx all the sprite data */ + //TODO: texture is outside the Sprite::Data because of the deleted copy constructer. eventually + // texture will go into data when it becomes asset + Sprite(game_object_id_t id, Texture & texture, const Data & ctx); ~Sprite(); //! Texture used for the sprite - const Texture sprite_image; - - //! Color tint of the sprite - Color color; - - //! Flip settings for the sprite - FlipSettings flip; - - //! Layer sorting level of the sprite - const int sorting_in_layer; + const Texture texture; - //! Order within the sorting layer - const int order_in_layer; - - /** - * \size width and height of the sprite in game units - * - * if height is filled in and not width it will multiply width by aspect_ratio. - * if width is filled in and not height it will multiply height by aspect_ratio. - * if neither is filled it will not show sprite because size will be zero - * if both are filled will it use the width and height without making sure the aspect_ratio - * is correct - */ - vec2 size; - - //! independent sprite angle. rotating clockwise direction in degrees - double angle_offset = 0; - - //! independent sprite scale multiplier - double scale = 1; - - /** - * \aspect_ratio ratio of the img so that scaling will not become weird - * - * cannot be const because if Animator component is addded then ratio becomes scuffed and - * does it need to be calculated again in the Animator - */ - float aspect_ratio; + Data data; private: //! Reads the mask of sprite diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index bba26a3..ab3fa45 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -112,17 +112,15 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { SDL_FRect SDLContext::get_dst_rect(const DstRect & ctx) const { - // this might not work all the time because of float checking zero -_- - vec2 size = {ctx.sprite.size.x == 0 && ctx.sprite.size.y != 0 - ? ctx.sprite.size.y * ctx.sprite.aspect_ratio - : ctx.sprite.size.x, - ctx.sprite.size.y == 0 && ctx.sprite.size.x != 0 - ? ctx.sprite.size.x / ctx.sprite.aspect_ratio - : ctx.sprite.size.y}; + const Sprite::Data & data = ctx.sprite.data; + + vec2 size = { + data.size.x == 0 && data.size.y != 0 ? data.size.y * data.aspect_ratio : data.size.x, + data.size.y == 0 && data.size.x != 0 ? data.size.x / data.aspect_ratio : data.size.y}; const CameraValues & cam = ctx.cam; - size *= cam.render_scale * ctx.img_scale * ctx.sprite.scale; + size *= cam.render_scale * ctx.img_scale * data.scale; vec2 screen_pos = (ctx.pos - cam.cam_pos + (cam.zoomed_viewport) / 2) * cam.render_scale - size / 2 + cam.bar_size; @@ -137,9 +135,10 @@ SDL_FRect SDLContext::get_dst_rect(const DstRect & ctx) const { void SDLContext::draw(const RenderContext & ctx) { + const Sprite::Data & data = ctx.sprite.data; SDL_RendererFlip render_flip - = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * ctx.sprite.flip.flip_x) - | (SDL_FLIP_VERTICAL * ctx.sprite.flip.flip_y)); + = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * data.flip.flip_x) + | (SDL_FLIP_VERTICAL * data.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(ctx.sprite); SDL_FRect dstrect = this->get_dst_rect(SDLContext::DstRect{ @@ -149,10 +148,10 @@ void SDLContext::draw(const RenderContext & ctx) { .img_scale = ctx.scale, }); - double angle = ctx.angle + ctx.sprite.angle_offset; + double angle = ctx.angle + data.angle_offset; - SDL_RenderCopyExF(this->game_renderer.get(), ctx.sprite.sprite_image.texture.get(), - &srcrect, &dstrect, angle, NULL, render_flip); + SDL_RenderCopyExF(this->game_renderer.get(), ctx.sprite.texture.texture.get(), &srcrect, + &dstrect, angle, NULL, render_flip); } void SDLContext::set_camera(const Camera & cam, CameraValues & ctx) { diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 1650b3d..b1f23d1 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -17,20 +17,21 @@ void AnimatorSystem::update() { for (Animator & a : animations) { if (!a.active) continue; + + Animator::Data & ctx = a.data; + double frame_duration = 1.0f / ctx.fps; - double frame_duration = 1.0f / a.fps; - - int cycle_end = (a.cycle_end == -1) ? a.row : cycle_end; - int total_frames = cycle_end - a.cycle_start; + int cycle_end = (ctx.cycle_end == -1) ? ctx.row : cycle_end; + int total_frames = cycle_end - ctx.cycle_start; int curr_frame = static_cast(elapsed_time / frame_duration) % total_frames; - a.curr_row = a.cycle_start + curr_frame; - a.spritesheet.mask.x = a.curr_row * a.spritesheet.mask.w; - a.spritesheet.mask.y = (a.curr_col * a.spritesheet.mask.h); + ctx.curr_row = ctx.cycle_start + curr_frame; + ctx.spritesheet.mask.x = ctx.curr_row * ctx.spritesheet.mask.w; + ctx.spritesheet.mask.y = (ctx.curr_col * ctx.spritesheet.mask.h); std::cout << curr_frame << " " << total_frames << std::endl; - if (!a.looping && curr_frame == 0) { + if (!ctx.looping && curr_frame == 0) { a.active = false; } } diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 944ac86..08f254f 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -40,8 +40,9 @@ const Camera & RenderSystem::update_camera() { } bool sorting_comparison(const Sprite & a, const Sprite & b) { - if (a.sorting_in_layer < b.sorting_in_layer) return true; - if (a.sorting_in_layer == b.sorting_in_layer) return a.order_in_layer < b.order_in_layer; + if (a.data.sorting_in_layer < b.data.sorting_in_layer) return true; + if (a.data.sorting_in_layer == b.data.sorting_in_layer) + return a.data.order_in_layer < b.data.order_in_layer; return false; } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 5a50d27..4fd4071 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -56,11 +56,24 @@ public: auto img = Texture("asset/spritesheet/pokemon_spritesheet.png"); Sprite & test_sprite = game_object.add_component( - img, color, Sprite::FlipSettings{false, false}, 1, 1, vec2{100, 100}); + img, Sprite::Data{ + .color = color, + .flip = Sprite::FlipSettings{false, false}, + .sorting_in_layer = 2, + .order_in_layer = 2, + .size = {0, 100}, + .angle_offset = 0, + .scale = 1, + }); - auto & anim = game_object.add_component(test_sprite, 4, 4, 0); + auto & anim = game_object.add_component(Animator::Data{ + .spritesheet = test_sprite, + .col = 4, + .row = 4, + .fps = 10, + }); - auto & cam = game_object.add_component(Color::, ivec2{720, 1280}, + auto & cam = game_object.add_component(Color::WHITE, ivec2{720, 1280}, vec2{400, 400}, 1.0); } -- cgit v1.2.3 From 0a1739de21e5ab270cb45efb6fc0aed092d81227 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 2 Dec 2024 21:00:54 +0100 Subject: added all the contructor strutcts for animator,sprite and camera --- src/crepe/api/Animator.cpp | 15 +++++++++++++-- src/crepe/api/Animator.h | 8 ++------ src/crepe/api/Camera.cpp | 1 - src/crepe/facade/SDLContext.cpp | 35 ++++++++++++++++++----------------- src/crepe/facade/SDLContext.h | 2 +- src/crepe/system/AnimatorSystem.cpp | 6 ++---- src/crepe/system/RenderSystem.cpp | 2 +- src/example/rendering_particle.cpp | 14 +++++++++++--- 8 files changed, 48 insertions(+), 35 deletions(-) (limited to 'src/crepe/api/Animator.h') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index ce824e6..1234967 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -9,8 +9,7 @@ using namespace crepe; Animator::Animator(game_object_id_t id, const Animator::Data & ctx) : Component(id), - data(ctx) -{ + data(ctx) { dbg_trace(); this->data.spritesheet.mask.h /= this->data.col; @@ -23,21 +22,33 @@ Animator::Animator(game_object_id_t id, const Animator::Data & ctx) ss.data.aspect_ratio = static_cast(this->data.spritesheet.mask.w) / this->data.spritesheet.mask.h; } + Animator::~Animator() { dbg_trace(); } void Animator::loop() { this->data.looping = true; } + void Animator::play() { this->active = true; } + void Animator::pause() { this->active = false; } + void Animator::stop() { this->active = false; this->data.curr_col = 0; this->data.curr_row = 0; } void Animator::set_fps(int fps) { this->data.fps = fps; } + void Animator::set_cycle_range(int start, int end) { this->data.cycle_start = start, this->data.cycle_end = end; } + void Animator::set_anim(int col) { this->data.curr_row = 0; this->data.curr_col = col; } + +void Animator::next_anim() { + Animator::Data & ctx = this->data; + ctx.curr_row = ctx.curr_row++ % ctx.row; + ctx.spritesheet.mask.x = ctx.curr_row * ctx.spritesheet.mask.w; +} diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 194a9cf..36bc9f4 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -47,6 +47,7 @@ public: //! offset in pixels. + // TODO implement int offset_x = 0; }; @@ -108,12 +109,7 @@ public: * \brief Constructs an Animator object that will control animations for a sprite sheet. * * \param id The unique identifier for the component, typically assigned automatically. - * \param spritesheet A reference to the Sprite object which holds the sprite sheet for - * animation. - * \param row The maximum number of rows in the sprite sheet. - * \param col The maximum number of columns in the sprite sheet. - * \param col_animate The specific col index of the sprite sheet to animate. This allows - * selecting which col to animate from multiple col in the sheet. + * \param ctx animator data * * This constructor sets up the Animator with the given parameters, and initializes the * animation system. diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index 27bcb35..ff41710 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -7,7 +7,6 @@ using namespace crepe; Camera::Camera(game_object_id_t id, const Data & ctx) : Component(id), data(ctx) { - dbg_trace(); } diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index ab3fa45..32461f2 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -156,53 +156,54 @@ void SDLContext::draw(const RenderContext & ctx) { void SDLContext::set_camera(const Camera & cam, CameraValues & ctx) { + const Camera::Data & cam_data = cam.data; // resize window int w, h; SDL_GetWindowSize(this->game_window.get(), &w, &h); - if (w != cam.screen.x || h != cam.screen.y) { - SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y); + if (w != cam_data.screen.x || h != cam_data.screen.y) { + SDL_SetWindowSize(this->game_window.get(), cam_data.screen.x, cam_data.screen.y); } 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(cam.screen.x) / cam.screen.y; + zoomed_viewport = cam_data.viewport_size * cam_data.zoom; + double screen_aspect = static_cast(cam_data.screen.x) / cam_data.screen.y; double viewport_aspect = zoomed_viewport.x / zoomed_viewport.y; // calculate black bars if (screen_aspect > viewport_aspect) { // pillarboxing - float scale = cam.screen.y / zoomed_viewport.y; + float scale = cam_data.screen.y / zoomed_viewport.y; float adj_width = zoomed_viewport.x * scale; - float bar_width = (cam.screen.x - adj_width) / 2; - 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}; + float bar_width = (cam_data.screen.x - adj_width) / 2; + this->black_bars[0] = {0, 0, bar_width, (float) cam_data.screen.y}; + this->black_bars[1] = {(cam_data.screen.x - bar_width), 0, bar_width, (float) cam_data.screen.y}; bar_size = {bar_width, 0}; render_scale.x = render_scale.y = scale; } else { // letterboxing - 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; - this->black_bars[0] = {0, 0, (float) cam.screen.x, bar_height}; + float scale = cam_data.screen.x / (cam_data.viewport_size.x * cam_data.zoom); + float adj_height = cam_data.viewport_size.y * scale; + float bar_height = (cam_data.screen.y - adj_height) / 2; + this->black_bars[0] = {0, 0, (float) cam_data.screen.x, bar_height}; this->black_bars[1] - = {0, (cam.screen.y - bar_height), (float) cam.screen.x, bar_height}; + = {0, (cam_data.screen.y - bar_height), (float) cam_data.screen.x, bar_height}; bar_size = {0, bar_height}; render_scale.x = render_scale.y = scale; } - SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g, - cam.bg_color.b, cam.bg_color.a); + SDL_SetRenderDrawColor(this->game_renderer.get(), cam_data.bg_color.r, cam_data.bg_color.g, + cam_data.bg_color.b, cam_data.bg_color.a); SDL_Rect bg = { .x = 0, .y = 0, - .w = cam.screen.x, - .h = cam.screen.y, + .w = cam_data.screen.x, + .h = cam_data.screen.y, }; // fill bg color diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 0a6fce6..a0d1da8 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -128,7 +128,7 @@ private: /** * \brief Draws a sprite to the screen using the specified transform and camera. - * \param RenderCtx Reference to rendering data to draw + * \param RenderContext Reference to rendering data to draw */ void draw(const RenderContext & ctx); diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index b1f23d1..ff8d2ce 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -4,7 +4,6 @@ #include "AnimatorSystem.h" #include "ComponentManager.h" -#include using namespace crepe; @@ -21,7 +20,7 @@ void AnimatorSystem::update() { Animator::Data & ctx = a.data; double frame_duration = 1.0f / ctx.fps; - int cycle_end = (ctx.cycle_end == -1) ? ctx.row : cycle_end; + int cycle_end = (ctx.cycle_end == -1) ? ctx.row : ctx.cycle_end; int total_frames = cycle_end - ctx.cycle_start; int curr_frame = static_cast(elapsed_time / frame_duration) % total_frames; @@ -30,8 +29,7 @@ void AnimatorSystem::update() { ctx.spritesheet.mask.x = ctx.curr_row * ctx.spritesheet.mask.w; ctx.spritesheet.mask.y = (ctx.curr_col * ctx.spritesheet.mask.h); - std::cout << curr_frame << " " << total_frames << std::endl; - if (!ctx.looping && curr_frame == 0) { + if (!ctx.looping && curr_frame == total_frames - 1) { a.active = false; } } diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 08f254f..1bf5f65 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -33,7 +33,7 @@ const Camera & RenderSystem::update_camera() { const Transform & transform = mgr.get_components_by_id(cam.game_object_id).front().get(); this->context.set_camera(cam, this->cam_ctx); - this->cam_ctx.cam_pos = transform.position + cam.offset; + this->cam_ctx.cam_pos = transform.position + cam.data.offset; return cam; } throw std::runtime_error("No active cameras in current scene"); diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 4fd4071..f84eb94 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -70,11 +70,19 @@ public: .spritesheet = test_sprite, .col = 4, .row = 4, - .fps = 10, + .fps = 1, + .looping = true, + .cycle_start = 1, + .cycle_end = 3, }); + anim.set_anim(2); - auto & cam = game_object.add_component(Color::WHITE, ivec2{720, 1280}, - vec2{400, 400}, 1.0); + auto & cam = game_object.add_component(Camera::Data{ + .bg_color = Color::WHITE, + .screen = ivec2{720, 1280}, + .viewport_size = vec2{400, 400}, + .zoom = 1.0, + }); } string get_name() const { return "TestScene"; }; -- cgit v1.2.3 From 900cfa9f31de40308b9ea3d0c73372d44f11094c Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 2 Dec 2024 21:01:40 +0100 Subject: make format --- src/crepe/api/Animator.h | 1 - src/crepe/facade/SDLContext.cpp | 3 ++- src/crepe/system/AnimatorSystem.cpp | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/crepe/api/Animator.h') diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 36bc9f4..74abd5e 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -45,7 +45,6 @@ public: //! end frame for cycling (-1 --> use last frame) int cycle_end = -1; - //! offset in pixels. // TODO implement int offset_x = 0; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 32461f2..391aa78 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -179,7 +179,8 @@ void SDLContext::set_camera(const Camera & cam, CameraValues & ctx) { float adj_width = zoomed_viewport.x * scale; float bar_width = (cam_data.screen.x - adj_width) / 2; this->black_bars[0] = {0, 0, bar_width, (float) cam_data.screen.y}; - this->black_bars[1] = {(cam_data.screen.x - bar_width), 0, bar_width, (float) cam_data.screen.y}; + this->black_bars[1] + = {(cam_data.screen.x - bar_width), 0, bar_width, (float) cam_data.screen.y}; bar_size = {bar_width, 0}; render_scale.x = render_scale.y = scale; diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index ff8d2ce..9cf8ba5 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -16,7 +16,7 @@ void AnimatorSystem::update() { for (Animator & a : animations) { if (!a.active) continue; - + Animator::Data & ctx = a.data; double frame_duration = 1.0f / ctx.fps; @@ -28,7 +28,7 @@ void AnimatorSystem::update() { ctx.curr_row = ctx.cycle_start + curr_frame; ctx.spritesheet.mask.x = ctx.curr_row * ctx.spritesheet.mask.w; ctx.spritesheet.mask.y = (ctx.curr_col * ctx.spritesheet.mask.h); - + if (!ctx.looping && curr_frame == total_frames - 1) { a.active = false; } -- cgit v1.2.3 From a11824956b478e356fa684c9d88b980aa22cb19a Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 4 Dec 2024 10:56:53 +0100 Subject: implemented feedback, cameraValues need better name? --- src/crepe/api/Animator.cpp | 29 ++++++++++++---------- src/crepe/api/Animator.h | 22 +++++++++-------- src/crepe/api/Camera.cpp | 7 +++++- src/crepe/api/Camera.h | 19 ++++++++------- src/crepe/api/Sprite.cpp | 2 +- src/crepe/api/Sprite.h | 25 ++++++++++--------- src/crepe/facade/SDLContext.cpp | 48 ++++++++++++++++++++----------------- src/crepe/facade/SDLContext.h | 2 +- src/crepe/system/AnimatorSystem.cpp | 6 ++--- src/crepe/system/RenderSystem.cpp | 19 +++++++-------- src/crepe/system/RenderSystem.h | 8 +++---- src/example/rendering_particle.cpp | 14 +++-------- 12 files changed, 103 insertions(+), 98 deletions(-) (limited to 'src/crepe/api/Animator.h') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 1234967..dc99fd4 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -7,20 +7,23 @@ using namespace crepe; -Animator::Animator(game_object_id_t id, const Animator::Data & ctx) +Animator::Animator(uint32_t id, Sprite & ss, int max_row, int max_col, + const Animator::Data & ctx) : Component(id), + spritesheet(ss), + row(max_row), + col(max_col), data(ctx) { dbg_trace(); - this->data.spritesheet.mask.h /= this->data.col; - this->data.spritesheet.mask.w /= this->data.row; - this->data.spritesheet.mask.x = 0; - this->data.spritesheet.mask.y = this->data.col * this->data.spritesheet.mask.h; + this->spritesheet.mask.h /= this->col; + this->spritesheet.mask.w /= this->row; + this->spritesheet.mask.x = this->data.curr_row * this->spritesheet.mask.w; + this->spritesheet.mask.y = this->data.curr_col * this->spritesheet.mask.h; // need to do this for to get the aspect ratio for a single clipping in the spritesheet - Sprite & ss = this->data.spritesheet; - ss.data.aspect_ratio - = static_cast(this->data.spritesheet.mask.w) / this->data.spritesheet.mask.h; + this->spritesheet.aspect_ratio + = static_cast(this->spritesheet.mask.w) / this->spritesheet.mask.h; } Animator::~Animator() { dbg_trace(); } @@ -43,12 +46,14 @@ void Animator::set_cycle_range(int start, int end) { } void Animator::set_anim(int col) { - this->data.curr_row = 0; - this->data.curr_col = col; + Animator::Data & ctx = this->data; + this->spritesheet.mask.x = ctx.curr_row = 0; + ctx.curr_col = col; + this->spritesheet.mask.y = ctx.curr_col * this->spritesheet.mask.h; } void Animator::next_anim() { Animator::Data & ctx = this->data; - ctx.curr_row = ctx.curr_row++ % ctx.row; - ctx.spritesheet.mask.x = ctx.curr_row * ctx.spritesheet.mask.w; + ctx.curr_row = ctx.curr_row++ % this->row; + this->spritesheet.mask.x = ctx.curr_row * this->spritesheet.mask.w; } diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 74abd5e..1fe2b6f 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -18,20 +18,13 @@ class SDLContext; class Animator : public Component { public: struct Data { - //! A reference to the Sprite sheet containing. - Sprite & spritesheet; - //! The maximum number of columns in the sprite sheet. - const int col; - - //! The maximum number of rows in the sprite sheet. - const int row; //! frames per second for animation - int fps; + int fps = 1; //! The current col being animated. - int curr_col; + int curr_col = 0; //! The current row being animated. int curr_row = 0; @@ -113,10 +106,19 @@ public: * This constructor sets up the Animator with the given parameters, and initializes the * animation system. */ - Animator(uint32_t id, const Animator::Data & ctx); + Animator(uint32_t id, Sprite & ss, int max_row, int max_col, const Animator::Data & ctx); ~Animator(); // dbg_trace public: + //! A reference to the Sprite sheet containing. + Sprite & spritesheet; + + //! The maximum number of columns in the sprite sheet. + const int col; + + //! The maximum number of rows in the sprite sheet. + const int row; + Animator::Data data; private: diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index ff41710..b042c35 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -6,7 +6,12 @@ using namespace crepe; -Camera::Camera(game_object_id_t id, const Data & ctx) : Component(id), data(ctx) { +Camera::Camera(game_object_id_t id, const ivec2 & screen, const vec2 & viewport_size, + const Data & ctx) + : Component(id), + screen(screen), + viewport_size(viewport_size), + data(ctx) { dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index e466d36..f626379 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -17,16 +17,10 @@ class Camera : public Component { public: struct Data { //! Background color of the camera view. - const Color bg_color; - - //! screen the display size in pixels ( output resolution ) - const ivec2 screen; - - //! viewport is the area of the world visible through the camera (in world units) - const vec2 viewport_size; + const Color bg_color = Color::WHITE; //! Zoom level of the camera view. - double zoom; + double zoom = 1; //! offset postion from the game object transform component vec2 offset; @@ -38,12 +32,19 @@ public: * \param id Unique identifier for the camera component. * \param ctx the camera component data */ - Camera(game_object_id_t id, const Data & ctx); + Camera(game_object_id_t id, const ivec2 & screen, const vec2 & viewport_size, + const Data & ctx); ~Camera(); // dbg_trace only public: Camera::Data data; + //! screen the display size in pixels ( output resolution ) + const ivec2 screen; + + //! viewport is the area of the world visible through the camera (in world units) + const vec2 viewport_size; + public: /** * \brief Gets the maximum number of camera instances allowed. diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index fe495a1..3a1acac 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -20,7 +20,7 @@ Sprite::Sprite(game_object_id_t id, Texture & texture, const Sprite::Data & ctx) this->mask.w = this->texture.get_width(); this->mask.h = this->texture.get_height(); - this->data.aspect_ratio = static_cast(this->mask.w) / this->mask.h; + this->aspect_ratio = static_cast(this->mask.w) / this->mask.h; } Sprite::~Sprite() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index aef6a8d..d82ae8d 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -27,16 +27,16 @@ public: struct Data { //! Color tint of the sprite - Color color; + Color color = Color::WHITE; //! Flip settings for the sprite FlipSettings flip; //! Layer sorting level of the sprite - const int sorting_in_layer; + const int sorting_in_layer = 0; //! Order within the sorting layer - const int order_in_layer; + const int order_in_layer = 0; /** * \size width and height of the sprite in game units @@ -53,15 +53,8 @@ public: double angle_offset = 0; //! independent sprite scale multiplier - double scale = 1; + double scale_offset = 1; - /** - * \aspect_ratio ratio of the img so that scaling will not become weird - * - * cannot be const because if Animator component is addded then ratio becomes scuffed and - * does it need to be calculated again in the Animator - */ - float aspect_ratio; }; public: @@ -71,8 +64,6 @@ public: * \param texture asset of the image * \param ctx all the sprite data */ - //TODO: texture is outside the Sprite::Data because of the deleted copy constructer. eventually - // texture will go into data when it becomes asset Sprite(game_object_id_t id, Texture & texture, const Data & ctx); ~Sprite(); @@ -82,6 +73,14 @@ public: Data data; private: + /** + * \aspect_ratio ratio of the img so that scaling will not become weird + * + * cannot be const because if Animator component is addded then ratio becomes scuffed and + * does it need to be calculated again in the Animator + */ + float aspect_ratio; + //! Reads the mask of sprite friend class SDLContext; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 391aa78..d3a15d9 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -115,12 +115,12 @@ SDL_FRect SDLContext::get_dst_rect(const DstRect & ctx) const { const Sprite::Data & data = ctx.sprite.data; vec2 size = { - data.size.x == 0 && data.size.y != 0 ? data.size.y * data.aspect_ratio : data.size.x, - data.size.y == 0 && data.size.x != 0 ? data.size.x / data.aspect_ratio : data.size.y}; + data.size.x == 0 && data.size.y != 0 ? data.size.y * ctx.sprite.aspect_ratio : data.size.x, + data.size.y == 0 && data.size.x != 0 ? data.size.x / ctx.sprite.aspect_ratio : data.size.y}; const CameraValues & cam = ctx.cam; - size *= cam.render_scale * ctx.img_scale * data.scale; + size *= cam.render_scale * ctx.img_scale * data.scale_offset; vec2 screen_pos = (ctx.pos - cam.cam_pos + (cam.zoomed_viewport) / 2) * cam.render_scale - size / 2 + cam.bar_size; @@ -154,44 +154,45 @@ void SDLContext::draw(const RenderContext & ctx) { &dstrect, angle, NULL, render_flip); } -void SDLContext::set_camera(const Camera & cam, CameraValues & ctx) { +SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { const Camera::Data & cam_data = cam.data; + CameraValues ret_cam; // resize window int w, h; SDL_GetWindowSize(this->game_window.get(), &w, &h); - if (w != cam_data.screen.x || h != cam_data.screen.y) { - SDL_SetWindowSize(this->game_window.get(), cam_data.screen.x, cam_data.screen.y); + if (w != cam.screen.x || h != cam.screen.y) { + SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y); } - vec2 & zoomed_viewport = ctx.zoomed_viewport; - vec2 & bar_size = ctx.bar_size; - vec2 & render_scale = ctx.render_scale; + vec2 & zoomed_viewport = ret_cam.zoomed_viewport; + vec2 & bar_size = ret_cam.bar_size; + vec2 & render_scale = ret_cam.render_scale; - zoomed_viewport = cam_data.viewport_size * cam_data.zoom; - double screen_aspect = static_cast(cam_data.screen.x) / cam_data.screen.y; + zoomed_viewport = cam.viewport_size * cam_data.zoom; + double screen_aspect = static_cast(cam.screen.x) / cam.screen.y; double viewport_aspect = zoomed_viewport.x / zoomed_viewport.y; // calculate black bars if (screen_aspect > viewport_aspect) { // pillarboxing - float scale = cam_data.screen.y / zoomed_viewport.y; + float scale = cam.screen.y / zoomed_viewport.y; float adj_width = zoomed_viewport.x * scale; - float bar_width = (cam_data.screen.x - adj_width) / 2; - this->black_bars[0] = {0, 0, bar_width, (float) cam_data.screen.y}; + float bar_width = (cam.screen.x - adj_width) / 2; + this->black_bars[0] = {0, 0, bar_width, (float) cam.screen.y}; this->black_bars[1] - = {(cam_data.screen.x - bar_width), 0, bar_width, (float) cam_data.screen.y}; + = {(cam.screen.x - bar_width), 0, bar_width, (float) cam.screen.y}; bar_size = {bar_width, 0}; render_scale.x = render_scale.y = scale; } else { // letterboxing - float scale = cam_data.screen.x / (cam_data.viewport_size.x * cam_data.zoom); - float adj_height = cam_data.viewport_size.y * scale; - float bar_height = (cam_data.screen.y - adj_height) / 2; - this->black_bars[0] = {0, 0, (float) cam_data.screen.x, bar_height}; + float scale = cam.screen.x / (cam.viewport_size.x * cam_data.zoom); + float adj_height = cam.viewport_size.y * scale; + float bar_height = (cam.screen.y - adj_height) / 2; + this->black_bars[0] = {0, 0, (float) cam.screen.x, bar_height}; this->black_bars[1] - = {0, (cam_data.screen.y - bar_height), (float) cam_data.screen.x, bar_height}; + = {0, (cam.screen.y - bar_height), (float) cam.screen.x, bar_height}; bar_size = {0, bar_height}; render_scale.x = render_scale.y = scale; @@ -203,12 +204,15 @@ void SDLContext::set_camera(const Camera & cam, CameraValues & ctx) { SDL_Rect bg = { .x = 0, .y = 0, - .w = cam_data.screen.x, - .h = cam_data.screen.y, + .w = cam.screen.x, + .h = cam.screen.y, }; // fill bg color SDL_RenderFillRect(this->game_renderer.get(), &bg); + + + return ret_cam; } uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index a0d1da8..d662bee 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -142,7 +142,7 @@ 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, CameraValues & ctx); + CameraValues set_camera(const Camera & camera); private: struct DstRect { diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index e9cdd4c..a2ae529 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -22,14 +22,14 @@ void AnimatorSystem::update() { int last_frame = ctx.curr_row; - int cycle_end = (ctx.cycle_end == -1) ? ctx.row : ctx.cycle_end; + int cycle_end = (ctx.cycle_end == -1) ? a.row : ctx.cycle_end; int total_frames = cycle_end - ctx.cycle_start; int curr_frame = static_cast(elapsed_time / frame_duration) % total_frames; ctx.curr_row = ctx.cycle_start + curr_frame; - ctx.spritesheet.mask.x = ctx.curr_row * ctx.spritesheet.mask.w; - ctx.spritesheet.mask.y = (ctx.curr_col * ctx.spritesheet.mask.h); + a.spritesheet.mask.x = ctx.curr_row * a.spritesheet.mask.w; + a.spritesheet.mask.y = (ctx.curr_col * a.spritesheet.mask.h); if (!ctx.looping && curr_frame == ctx.cycle_start && last_frame == total_frames - 1) { a.active = false; diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 1bf5f65..b5db45a 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -21,7 +21,7 @@ void RenderSystem::clear_screen() { this->context.clear_screen(); } void RenderSystem::present_screen() { this->context.present_screen(); } -const Camera & RenderSystem::update_camera() { +SDLContext::CameraValues RenderSystem::update_camera() { ComponentManager & mgr = this->component_manager; RefVector cameras = mgr.get_components_by_type(); @@ -32,9 +32,9 @@ const Camera & RenderSystem::update_camera() { if (!cam.active) continue; const Transform & transform = mgr.get_components_by_id(cam.game_object_id).front().get(); - this->context.set_camera(cam, this->cam_ctx); - this->cam_ctx.cam_pos = transform.position + cam.data.offset; - return cam; + SDLContext::CameraValues cam_val = this->context.set_camera(cam); + cam_val.cam_pos = transform.position + cam.data.offset; + return cam_val; } throw std::runtime_error("No active cameras in current scene"); } @@ -60,7 +60,7 @@ void RenderSystem::update() { this->present_screen(); } -bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, +bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::CameraValues & cam, const double & scale) { ComponentManager & mgr = this->component_manager; @@ -80,7 +80,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, this->context.draw(SDLContext::RenderContext{ .sprite = sprite, - .cam = this->cam_ctx, + .cam = cam, .pos = p.position, .angle = p.angle, .scale = scale, @@ -89,11 +89,10 @@ bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, } return rendering_particles; } -void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam, - const Transform & tm) { +void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam, const Transform & tm) { this->context.draw(SDLContext::RenderContext{ .sprite = sprite, - .cam = this->cam_ctx, + .cam = cam, .pos = tm.position, .angle = tm.rotation, .scale = tm.scale, @@ -102,7 +101,7 @@ void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam, void RenderSystem::render() { ComponentManager & mgr = this->component_manager; - const Camera & cam = this->update_camera(); + const SDLContext::CameraValues & cam = this->update_camera(); RefVector sprites = mgr.get_components_by_type(); RefVector sorted_sprites = this->sort(sprites); diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 249f3b8..e779047 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -37,7 +37,7 @@ private: void present_screen(); //! Updates the active camera used for rendering. - const Camera & update_camera(); + SDLContext::CameraValues update_camera(); //! Renders the whole screen void render(); @@ -49,7 +49,7 @@ private: * \param tm the Transform component for scale * \return true if particles have been rendered */ - bool render_particle(const Sprite & sprite, const Camera & cam, const double & scale); + bool render_particle(const Sprite & sprite, const SDLContext::CameraValues & cam, const double & scale); /** * \brief renders a sprite with a Transform component on the screen @@ -57,7 +57,7 @@ private: * \param sprite the sprite component that holds all the data * \param tm the Transform component that holds the position,rotation and scale */ - void render_normal(const Sprite & sprite, const Camera & cam, const Transform & tm); + void render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam, const Transform & tm); /** * \brief sort a vector sprite objects with @@ -76,8 +76,6 @@ private: private: SDLContext & context = SDLContext::get_instance(); - - SDLContext::CameraValues cam_ctx; }; } // namespace crepe diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index e18c7b7..2e2552d 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -45,8 +45,6 @@ using namespace std; class TestScene : public Scene { public: - using Scene::Scene; - void load_scene() { ComponentManager & mgr = this->component_manager; GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 1); @@ -63,23 +61,17 @@ public: .order_in_layer = 2, .size = {0, 100}, .angle_offset = 0, - .scale = 1, }); - auto & anim = game_object.add_component(Animator::Data{ - .spritesheet = test_sprite, - .col = 4, - .row = 4, + auto & anim = game_object.add_component(test_sprite, 4, 4, Animator::Data{ .fps = 1, .looping = false, }); anim.set_anim(2); + anim.active = false; - auto & cam = game_object.add_component(Camera::Data{ + auto & cam = game_object.add_component(ivec2{1280,720}, vec2{400,400},Camera::Data{ .bg_color = Color::WHITE, - .screen = ivec2{1280, 720}, - .viewport_size = vec2{400, 400}, - .zoom = 1.0, }); } -- cgit v1.2.3 From 9cde6875186b335c75eafa6402f0957cd4252c76 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 4 Dec 2024 10:57:20 +0100 Subject: make format --- src/crepe/api/Animator.h | 1 - src/crepe/api/Sprite.h | 1 - src/crepe/facade/SDLContext.cpp | 11 +++++------ src/crepe/system/RenderSystem.cpp | 3 ++- src/crepe/system/RenderSystem.h | 6 ++++-- src/example/rendering_particle.cpp | 16 +++++++++------- 6 files changed, 20 insertions(+), 18 deletions(-) (limited to 'src/crepe/api/Animator.h') diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 1fe2b6f..dab6697 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -19,7 +19,6 @@ class Animator : public Component { public: struct Data { - //! frames per second for animation int fps = 1; diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index d82ae8d..a1230db 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -54,7 +54,6 @@ public: //! independent sprite scale multiplier double scale_offset = 1; - }; public: diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index d3a15d9..86969ed 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -114,9 +114,10 @@ SDL_FRect SDLContext::get_dst_rect(const DstRect & ctx) const { const Sprite::Data & data = ctx.sprite.data; - vec2 size = { - data.size.x == 0 && data.size.y != 0 ? data.size.y * ctx.sprite.aspect_ratio : data.size.x, - data.size.y == 0 && data.size.x != 0 ? data.size.x / ctx.sprite.aspect_ratio : data.size.y}; + vec2 size = {data.size.x == 0 && data.size.y != 0 ? data.size.y * ctx.sprite.aspect_ratio + : data.size.x, + data.size.y == 0 && data.size.x != 0 ? data.size.x / ctx.sprite.aspect_ratio + : data.size.y}; const CameraValues & cam = ctx.cam; @@ -180,8 +181,7 @@ SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { float adj_width = zoomed_viewport.x * scale; float bar_width = (cam.screen.x - adj_width) / 2; 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}; + this->black_bars[1] = {(cam.screen.x - bar_width), 0, bar_width, (float) cam.screen.y}; bar_size = {bar_width, 0}; render_scale.x = render_scale.y = scale; @@ -211,7 +211,6 @@ SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { // fill bg color SDL_RenderFillRect(this->game_renderer.get(), &bg); - return ret_cam; } diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index b5db45a..4c618d8 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -89,7 +89,8 @@ bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::Came } return rendering_particles; } -void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam, const Transform & tm) { +void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam, + const Transform & tm) { this->context.draw(SDLContext::RenderContext{ .sprite = sprite, .cam = cam, diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index e779047..9c306c5 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -49,7 +49,8 @@ private: * \param tm the Transform component for scale * \return true if particles have been rendered */ - bool render_particle(const Sprite & sprite, const SDLContext::CameraValues & cam, const double & scale); + bool render_particle(const Sprite & sprite, const SDLContext::CameraValues & cam, + const double & scale); /** * \brief renders a sprite with a Transform component on the screen @@ -57,7 +58,8 @@ private: * \param sprite the sprite component that holds all the data * \param tm the Transform component that holds the position,rotation and scale */ - void render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam, const Transform & tm); + void render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam, + const Transform & tm); /** * \brief sort a vector sprite objects with diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 2e2552d..07e43a1 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -63,16 +63,18 @@ public: .angle_offset = 0, }); - auto & anim = game_object.add_component(test_sprite, 4, 4, Animator::Data{ - .fps = 1, - .looping = false, - }); + auto & anim = game_object.add_component(test_sprite, 4, 4, + Animator::Data{ + .fps = 1, + .looping = false, + }); anim.set_anim(2); anim.active = false; - auto & cam = game_object.add_component(ivec2{1280,720}, vec2{400,400},Camera::Data{ - .bg_color = Color::WHITE, - }); + auto & cam = game_object.add_component(ivec2{1280, 720}, vec2{400, 400}, + Camera::Data{ + .bg_color = Color::WHITE, + }); } string get_name() const { return "TestScene"; }; -- cgit v1.2.3 From 7ec1fcfcff0c01d204ccbf1bac9919ba610b8606 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 6 Dec 2024 15:42:25 +0100 Subject: implemented final max feedback --- src/crepe/api/Animator.cpp | 2 +- src/crepe/api/Animator.h | 26 ++++++++++++++------------ src/crepe/api/Camera.h | 2 +- src/crepe/api/Sprite.h | 12 +++++++++--- src/crepe/facade/SDLContext.cpp | 2 +- src/crepe/manager/Mediator.h | 2 ++ src/crepe/system/InputSystem.cpp | 4 ++-- src/crepe/system/RenderSystem.cpp | 2 +- src/crepe/system/RenderSystem.h | 3 +-- src/example/rendering_particle.cpp | 1 + 10 files changed, 33 insertions(+), 23 deletions(-) (limited to 'src/crepe/api/Animator.h') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index dc99fd4..8b91859 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -7,7 +7,7 @@ using namespace crepe; -Animator::Animator(uint32_t id, Sprite & ss, int max_row, int max_col, +Animator::Animator(uint32_t id, Sprite & ss, unsigned int max_row, unsigned int max_col, const Animator::Data & ctx) : Component(id), spritesheet(ss), diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index dab6697..e0399a8 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -2,6 +2,8 @@ #include "Component.h" #include "Sprite.h" +#include "types.h" +#include namespace crepe { @@ -20,26 +22,26 @@ public: struct Data { //! frames per second for animation - int fps = 1; + unsigned int fps = 1; //! The current col being animated. - int curr_col = 0; + unsigned int curr_col = 0; //! The current row being animated. - int curr_row = 0; + unsigned int curr_row = 0; //! should the animation loop bool looping = false; //! starting frame for cycling - int cycle_start = 0; + unsigned int cycle_start = 0; //! end frame for cycling (-1 --> use last frame) int cycle_end = -1; //! offset in pixels. // TODO implement - int offset_x = 0; + unsigned int white_space = 0; }; public: @@ -100,12 +102,15 @@ public: * \brief Constructs an Animator object that will control animations for a sprite sheet. * * \param id The unique identifier for the component, typically assigned automatically. - * \param ctx animator data + * \param ss the reference to the spritesheet + * \param max_row maximum of rows inside the given spritesheet + * \param max_col maximum of columns inside the given spritesheet + * \param ctx extra animation data for more control * * This constructor sets up the Animator with the given parameters, and initializes the * animation system. */ - Animator(uint32_t id, Sprite & ss, int max_row, int max_col, const Animator::Data & ctx); + Animator(game_object_id_t id, Sprite & ss, unsigned int max_row, unsigned int max_col, const Animator::Data & ctx); ~Animator(); // dbg_trace public: @@ -113,16 +118,13 @@ public: Sprite & spritesheet; //! The maximum number of columns in the sprite sheet. - const int col; + const unsigned int col; //! The maximum number of rows in the sprite sheet. - const int row; + const unsigned int row; Animator::Data data; -private: - //! AnimatorSystem adjust the private member parameters of Animator; - friend class AnimatorSystem; }; } // namespace crepe // diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index f626379..84ca9e1 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -23,7 +23,7 @@ public: double zoom = 1; //! offset postion from the game object transform component - vec2 offset; + vec2 postion_offset; }; public: diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index a1230db..0ccc296 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -20,11 +20,14 @@ class AnimatorSystem; */ class Sprite : public Component { public: + //! settings to flip the image struct FlipSettings { + //! horizantal flip bool flip_x = false; + //! vertical flip bool flip_y = false; }; - + struct Data { //! Color tint of the sprite Color color = Color::WHITE; @@ -50,10 +53,13 @@ public: vec2 size; //! independent sprite angle. rotating clockwise direction in degrees - double angle_offset = 0; + float angle_offset = 0; //! independent sprite scale multiplier - double scale_offset = 1; + float scale_offset = 1; + + //! independent sprite offset position + vec2 position_offset; }; public: diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 0097070..9533b8a 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -243,7 +243,7 @@ SDL_FRect SDLContext::get_dst_rect(const DstRect & ctx) const { size *= cam.render_scale * ctx.img_scale * data.scale_offset; - vec2 screen_pos = (ctx.pos - cam.cam_pos + (cam.zoomed_viewport) / 2) * cam.render_scale + vec2 screen_pos = (ctx.pos + data.position_offset - cam.cam_pos + (cam.zoomed_viewport) / 2) * cam.render_scale - size / 2 + cam.bar_size; return SDL_FRect{ diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index 71bd1c9..5b53bcc 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -5,6 +5,7 @@ // TODO: remove these singletons: #include "EventManager.h" #include "SaveManager.h" +#include "../facade/SDLContext.h" namespace crepe { @@ -28,6 +29,7 @@ struct Mediator { OptionalRef scene_manager; OptionalRef save_manager = SaveManager::get_instance(); OptionalRef event_manager = EventManager::get_instance(); + OptionalRef sdl_context = SDLContext::get_instance(); }; } // namespace crepe diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index 20da644..aaa8bdf 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -24,9 +24,9 @@ void InputSystem::update() { RefVector transform_vec = mgr.get_components_by_id(current_cam.game_object_id); Transform & cam_transform = transform_vec.front().get(); - int camera_origin_x = cam_transform.position.x + current_cam.data.offset.x + int camera_origin_x = cam_transform.position.x + current_cam.data.postion_offset.x - (current_cam.viewport_size.x / 2); - int camera_origin_y = cam_transform.position.y + current_cam.data.offset.y + int camera_origin_y = cam_transform.position.y + current_cam.data.postion_offset.y - (current_cam.viewport_size.y / 2); for (const SDLContext::EventData & event : event_list) { diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 111ad7d..0ba71ec 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -33,7 +33,7 @@ SDLContext::CameraValues RenderSystem::update_camera() { const Transform & transform = mgr.get_components_by_id(cam.game_object_id).front().get(); SDLContext::CameraValues cam_val = this->context.set_camera(cam); - cam_val.cam_pos = transform.position + cam.data.offset; + cam_val.cam_pos = transform.position + cam.data.postion_offset; return cam_val; } throw std::runtime_error("No active cameras in current scene"); diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index eaf1213..91b386f 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -79,8 +79,7 @@ private: */ private: - // FIXME: retrieve sdlcontext via mediator after #PR57 - SDLContext & context = SDLContext::get_instance(); + SDLContext & context = this->mediator.sdl_context; }; } // namespace crepe diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 9d6b537..145bc5e 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -57,6 +57,7 @@ public: .order_in_layer = 2, .size = {0, 100}, .angle_offset = 0, + .position_offset = {100,0}, }); auto & anim = game_object.add_component(test_sprite, 4, 4, -- cgit v1.2.3 From 83ee80ea439fc6a9042307a25b214b3efcf28d91 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 6 Dec 2024 15:42:52 +0100 Subject: make format --- src/crepe/api/Animator.h | 4 ++-- src/crepe/api/Sprite.h | 4 ++-- src/crepe/facade/SDLContext.cpp | 6 ++++-- src/crepe/manager/Mediator.h | 2 +- src/example/rendering_particle.cpp | 2 +- 5 files changed, 10 insertions(+), 8 deletions(-) (limited to 'src/crepe/api/Animator.h') diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index e0399a8..2a0a889 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -110,7 +110,8 @@ public: * This constructor sets up the Animator with the given parameters, and initializes the * animation system. */ - Animator(game_object_id_t id, Sprite & ss, unsigned int max_row, unsigned int max_col, const Animator::Data & ctx); + Animator(game_object_id_t id, Sprite & ss, unsigned int max_row, unsigned int max_col, + const Animator::Data & ctx); ~Animator(); // dbg_trace public: @@ -124,7 +125,6 @@ public: const unsigned int row; Animator::Data data; - }; } // namespace crepe // diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 0ccc296..78ed7ad 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -27,7 +27,7 @@ public: //! vertical flip bool flip_y = false; }; - + struct Data { //! Color tint of the sprite Color color = Color::WHITE; @@ -58,7 +58,7 @@ public: //! independent sprite scale multiplier float scale_offset = 1; - //! independent sprite offset position + //! independent sprite offset position vec2 position_offset; }; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 9533b8a..cf9f7d5 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -243,8 +243,10 @@ SDL_FRect SDLContext::get_dst_rect(const DstRect & ctx) const { size *= cam.render_scale * ctx.img_scale * data.scale_offset; - vec2 screen_pos = (ctx.pos + data.position_offset - cam.cam_pos + (cam.zoomed_viewport) / 2) * cam.render_scale - - size / 2 + cam.bar_size; + vec2 screen_pos + = (ctx.pos + data.position_offset - cam.cam_pos + (cam.zoomed_viewport) / 2) + * cam.render_scale + - size / 2 + cam.bar_size; return SDL_FRect{ .x = screen_pos.x, diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index 5b53bcc..f8517a3 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -3,9 +3,9 @@ #include "../util/OptionalRef.h" // TODO: remove these singletons: +#include "../facade/SDLContext.h" #include "EventManager.h" #include "SaveManager.h" -#include "../facade/SDLContext.h" namespace crepe { diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 145bc5e..29d475d 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -57,7 +57,7 @@ public: .order_in_layer = 2, .size = {0, 100}, .angle_offset = 0, - .position_offset = {100,0}, + .position_offset = {100, 0}, }); auto & anim = game_object.add_component(test_sprite, 4, 4, -- cgit v1.2.3 From 05e099c03a2242bf5194d06191a0ff667d404817 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 6 Dec 2024 19:21:56 +0100 Subject: implemented feedback --- src/crepe/api/Animator.cpp | 30 +++++++++++++++--------------- src/crepe/api/Animator.h | 25 +++++++++++++++---------- src/crepe/api/Camera.cpp | 6 +++--- src/crepe/api/Camera.h | 22 +++++++++++++++++----- src/crepe/api/Config.h | 2 +- src/crepe/api/Sprite.cpp | 4 ++-- src/crepe/api/Sprite.h | 30 +++++++++++++++++++----------- src/crepe/api/Transform.h | 4 ++-- src/crepe/facade/SDLContext.cpp | 21 ++++++++++++--------- src/crepe/facade/SDLContext.h | 32 ++++++++++++++++++++++++++------ src/crepe/manager/Mediator.h | 2 ++ src/crepe/system/AnimatorSystem.cpp | 18 +++++++++--------- src/crepe/system/AnimatorSystem.h | 3 --- src/crepe/system/RenderSystem.cpp | 20 ++++++++++++++------ src/crepe/system/RenderSystem.h | 3 --- 15 files changed, 137 insertions(+), 85 deletions(-) (limited to 'src/crepe/api/Animator.h') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 8b91859..154135f 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -8,18 +8,18 @@ using namespace crepe; Animator::Animator(uint32_t id, Sprite & ss, unsigned int max_row, unsigned int max_col, - const Animator::Data & ctx) + const Animator::Data & data) : Component(id), spritesheet(ss), - row(max_row), - col(max_col), - data(ctx) { + max_rows(max_row), + max_columns(max_col), + data(data) { dbg_trace(); - this->spritesheet.mask.h /= this->col; - this->spritesheet.mask.w /= this->row; - this->spritesheet.mask.x = this->data.curr_row * this->spritesheet.mask.w; - this->spritesheet.mask.y = this->data.curr_col * this->spritesheet.mask.h; + this->spritesheet.mask.h /= this->max_columns; + this->spritesheet.mask.w /= this->max_rows; + this->spritesheet.mask.x = this->data.row * this->spritesheet.mask.w; + this->spritesheet.mask.y = this->data.col * this->spritesheet.mask.h; // need to do this for to get the aspect ratio for a single clipping in the spritesheet this->spritesheet.aspect_ratio @@ -36,8 +36,8 @@ void Animator::pause() { this->active = false; } void Animator::stop() { this->active = false; - this->data.curr_col = 0; - this->data.curr_row = 0; + this->data.col = 0; + this->data.row = 0; } void Animator::set_fps(int fps) { this->data.fps = fps; } @@ -47,13 +47,13 @@ void Animator::set_cycle_range(int start, int end) { void Animator::set_anim(int col) { Animator::Data & ctx = this->data; - this->spritesheet.mask.x = ctx.curr_row = 0; - ctx.curr_col = col; - this->spritesheet.mask.y = ctx.curr_col * this->spritesheet.mask.h; + this->spritesheet.mask.x = ctx.row = 0; + ctx.col = col; + this->spritesheet.mask.y = ctx.col * this->spritesheet.mask.h; } void Animator::next_anim() { Animator::Data & ctx = this->data; - ctx.curr_row = ctx.curr_row++ % this->row; - this->spritesheet.mask.x = ctx.curr_row * this->spritesheet.mask.w; + ctx.row = ctx.row++ % this->max_rows; + this->spritesheet.mask.x = ctx.row * this->spritesheet.mask.w; } diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 2a0a889..23d29f6 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -1,9 +1,10 @@ #pragma once +#include + #include "Component.h" #include "Sprite.h" #include "types.h" -#include namespace crepe { @@ -25,10 +26,10 @@ public: unsigned int fps = 1; //! The current col being animated. - unsigned int curr_col = 0; + unsigned int col = 0; //! The current row being animated. - unsigned int curr_row = 0; + unsigned int row = 0; //! should the animation loop bool looping = false; @@ -105,26 +106,30 @@ public: * \param ss the reference to the spritesheet * \param max_row maximum of rows inside the given spritesheet * \param max_col maximum of columns inside the given spritesheet - * \param ctx extra animation data for more control + * \param data extra animation data for more control * * This constructor sets up the Animator with the given parameters, and initializes the * animation system. */ Animator(game_object_id_t id, Sprite & ss, unsigned int max_row, unsigned int max_col, - const Animator::Data & ctx); + const Animator::Data & data); ~Animator(); // dbg_trace public: - //! A reference to the Sprite sheet containing. - Sprite & spritesheet; - //! The maximum number of columns in the sprite sheet. - const unsigned int col; + const unsigned int max_columns; //! The maximum number of rows in the sprite sheet. - const unsigned int row; + const unsigned int max_rows; Animator::Data data; + +private: + //! A reference to the Sprite sheet containing. + Sprite & spritesheet; + + // uses the spritesheet + friend AnimatorSystem; }; } // namespace crepe // diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index b042c35..179dc18 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -1,17 +1,17 @@ -#include "types.h" #include "util/Log.h" #include "Camera.h" #include "Component.h" +#include "types.h" using namespace crepe; Camera::Camera(game_object_id_t id, const ivec2 & screen, const vec2 & viewport_size, - const Data & ctx) + const Data & data) : Component(id), screen(screen), viewport_size(viewport_size), - data(ctx) { + data(data) { dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index 84ca9e1..54d9a73 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -16,10 +16,20 @@ namespace crepe { class Camera : public Component { public: struct Data { - //! Background color of the camera view. - const Color bg_color = Color::WHITE; + /** + * \bg_color background color of the game + * + * This will make the background the same color as the given value. + */ + const Color bg_color = Color::BLACK; - //! Zoom level of the camera view. + /** + * \zoom Zooming level of the game + * + * zoom = 1 --> no zoom. + * zoom < 1 --> zoom out + * zoom > 1 --> zoom in + */ double zoom = 1; //! offset postion from the game object transform component @@ -30,10 +40,12 @@ public: /** * \brief Constructs a Camera with the specified ID and background color. * \param id Unique identifier for the camera component. - * \param ctx the camera component data + * \param screen is the actual screen size in pixels + * \param viewport_size is the view of the world in game units + * \param data the camera component data */ Camera(game_object_id_t id, const ivec2 & screen, const vec2 & viewport_size, - const Data & ctx); + const Camera::Data & data); ~Camera(); // dbg_trace only public: diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 200a3b0..f1bca62 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -66,7 +66,7 @@ public: //! default window settings struct { - //TODO make this constexpr because this will never change + //! default screen size in pixels ivec2 default_size = {1280, 720}; std::string window_title = "Jetpack joyride clone"; diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index b52a344..cc0e20a 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -11,10 +11,10 @@ using namespace std; using namespace crepe; -Sprite::Sprite(game_object_id_t id, Texture & texture, const Sprite::Data & ctx) +Sprite::Sprite(game_object_id_t id, Texture & texture, const Sprite::Data & data) : Component(id), texture(std::move(texture)), - data(ctx) { + data(data) { dbg_trace(); diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 78ed7ad..ea8104c 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -28,8 +28,15 @@ public: bool flip_y = false; }; + //! Sprite data that does not have to be set in the constructor struct Data { - //! Color tint of the sprite + /** + * \color tint of the sprite + * + * the default value is white because of the color multiplier. + * this means that the orginal image will be shown. if color is BLACK for example + * then it turns the image black because of the Color channels being 0. + */ Color color = Color::WHITE; //! Flip settings for the sprite @@ -44,10 +51,10 @@ public: /** * \size width and height of the sprite in game units * - * if height is filled in and not width it will multiply width by aspect_ratio. - * if width is filled in and not height it will multiply height by aspect_ratio. - * if neither is filled it will not show sprite because size will be zero - * if both are filled will it use the width and height without making sure the aspect_ratio + * - if height is filled in and not width it will multiply width by aspect_ratio. + * - if width is filled in and not height it will multiply height by aspect_ratio. + * - if neither is filled it will not show sprite because size will be zero + * - if both are filled will it use the width and height without making sure the aspect_ratio * is correct */ vec2 size; @@ -69,7 +76,7 @@ public: * \param texture asset of the image * \param ctx all the sprite data */ - Sprite(game_object_id_t id, Texture & texture, const Data & ctx); + Sprite(game_object_id_t id, Texture & texture, const Data & data); ~Sprite(); //! Texture used for the sprite @@ -79,11 +86,12 @@ public: private: /** - * \aspect_ratio ratio of the img so that scaling will not become weird - * - * cannot be const because if Animator component is addded then ratio becomes scuffed and - * does it need to be calculated again in the Animator - */ + * \aspect_ratio ratio of the img + * + * - This will multiply one of \c size variable if it is 0. + * - Will be adjusted if \c Animator component is added to an GameObject + * that is why this value cannot be const. + */ float aspect_ratio; //! Reads the mask of sprite diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index 34ac70a..3ef0fb5 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -16,9 +16,9 @@ public: //! Translation (shift) vec2 position = {0, 0}; //! Rotation, in degrees clockwise - double rotation = 0; + float rotation = 0; //! Multiplication factor - double scale = 0; + float scale = 0; protected: /** diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index cf9f7d5..4cc2206 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -15,13 +15,13 @@ #include #include "../api/Camera.h" +#include "../api/Color.h" #include "../api/Config.h" #include "../api/Sprite.h" #include "../api/Texture.h" #include "../util/Log.h" #include "SDLContext.h" -#include "api/Color.h" #include "types.h" using namespace crepe; @@ -230,14 +230,17 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { }; } -SDL_FRect SDLContext::get_dst_rect(const DstRect & ctx) const { +SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { const Sprite::Data & data = ctx.sprite.data; - vec2 size = {data.size.x == 0 && data.size.y != 0 ? data.size.y * ctx.sprite.aspect_ratio - : data.size.x, - data.size.y == 0 && data.size.x != 0 ? data.size.x / ctx.sprite.aspect_ratio - : data.size.y}; + vec2 size; + if (data.size.x == 0 && data.size.y != 0) { + size.x = data.size.y * ctx.sprite.aspect_ratio; + } + if (data.size.y == 0 && data.size.x != 0) { + size.y = data.size.x / ctx.sprite.aspect_ratio; + } const CameraValues & cam = ctx.cam; @@ -264,7 +267,7 @@ void SDLContext::draw(const RenderContext & ctx) { | (SDL_FLIP_VERTICAL * data.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(ctx.sprite); - SDL_FRect dstrect = this->get_dst_rect(SDLContext::DstRect{ + SDL_FRect dstrect = this->get_dst_rect(SDLContext::DestinationRectangleData{ .sprite = ctx.sprite, .cam = ctx.cam, .pos = ctx.pos, @@ -294,8 +297,8 @@ SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { vec2 & render_scale = ret_cam.render_scale; zoomed_viewport = cam.viewport_size * cam_data.zoom; - double screen_aspect = static_cast(cam.screen.x) / cam.screen.y; - double viewport_aspect = zoomed_viewport.x / zoomed_viewport.y; + float screen_aspect = static_cast(cam.screen.x) / cam.screen.y; + float viewport_aspect = zoomed_viewport.x / zoomed_viewport.y; // calculate black bars if (screen_aspect > viewport_aspect) { diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index dfdaa56..bbe87c3 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -9,11 +9,9 @@ #include #include #include -#include #include "api/Camera.h" #include "api/Color.h" -#include "api/Event.h" #include "api/KeyCodes.h" #include "api/Sprite.h" #include "api/Texture.h" @@ -35,12 +33,33 @@ class SDLContext { public: //! data that the camera component cannot hold struct CameraValues { + + //! zoomed in viewport in game_units vec2 zoomed_viewport; + + /** + * \render_scale scaling factor + * + * depending on the black bars type will the scaling be different. + * - lettorboxing --> scaling on the y-as + * - pillarboxing --> scaling on the x-as + */ vec2 render_scale; + + /** + * \bar_size size of calculated black bars + * + * depending on the black bars type will the size be different + * - lettorboxing --> {0, bar_height} + * - pillarboxing --> {bar_width , 0} + */ vec2 bar_size; + + //! Calculated camera position vec2 cam_pos; }; - + + //! rendering data needed to render on screen struct RenderContext { const Sprite & sprite; const CameraValues & cam; @@ -192,7 +211,8 @@ private: CameraValues set_camera(const Camera & camera); private: - struct DstRect { + //! the data needed to construct a sdl dst rectangle + struct DestinationRectangleData { const Sprite & sprite; const CameraValues & cam; const vec2 & pos; @@ -216,7 +236,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 DstRect & ctx) const; + SDL_FRect get_dst_rect(const DestinationRectangleData & data) const; /** * \brief Set an additional color value multiplied into render copy operations. * @@ -233,7 +253,7 @@ private: std::unique_ptr> game_renderer; //! black bars rectangle to draw - SDL_FRect black_bars[2]; + SDL_FRect black_bars[2] = {}; }; } // namespace crepe diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index f8517a3..8094d80 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -6,6 +6,7 @@ #include "../facade/SDLContext.h" #include "EventManager.h" #include "SaveManager.h" +#include "api/LoopTimer.h" namespace crepe { @@ -30,6 +31,7 @@ struct Mediator { OptionalRef save_manager = SaveManager::get_instance(); OptionalRef event_manager = EventManager::get_instance(); OptionalRef sdl_context = SDLContext::get_instance(); + OptionalRef timer = LoopTimer::get_instance(); }; } // namespace crepe diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 9aede7f..549c35d 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -1,8 +1,8 @@ #include "../api/Animator.h" -#include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" +#include "api/LoopTimer.h" #include "AnimatorSystem.h" @@ -10,27 +10,27 @@ using namespace crepe; void AnimatorSystem::update() { ComponentManager & mgr = this->mediator.component_manager; - + LoopTimer & timer = this->mediator.timer; RefVector animations = mgr.get_components_by_type(); - double elapsed_time = this->timer.get_current_time(); + double elapsed_time = timer.get_current_time(); for (Animator & a : animations) { if (!a.active) continue; Animator::Data & ctx = a.data; - double frame_duration = 1.0f / ctx.fps; + float frame_duration = 1.0f / ctx.fps; - int last_frame = ctx.curr_row; + int last_frame = ctx.row; - int cycle_end = (ctx.cycle_end == -1) ? a.row : ctx.cycle_end; + int cycle_end = (ctx.cycle_end == -1) ? a.max_rows : ctx.cycle_end; int total_frames = cycle_end - ctx.cycle_start; int curr_frame = static_cast(elapsed_time / frame_duration) % total_frames; - ctx.curr_row = ctx.cycle_start + curr_frame; - a.spritesheet.mask.x = ctx.curr_row * a.spritesheet.mask.w; - a.spritesheet.mask.y = (ctx.curr_col * a.spritesheet.mask.h); + ctx.row = ctx.cycle_start + curr_frame; + a.spritesheet.mask.x = ctx.row * a.spritesheet.mask.w; + a.spritesheet.mask.y = (ctx.col * a.spritesheet.mask.h); if (!ctx.looping && curr_frame == ctx.cycle_start && last_frame == total_frames - 1) { a.active = false; diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h index e8a5158..1e8cd7d 100644 --- a/src/crepe/system/AnimatorSystem.h +++ b/src/crepe/system/AnimatorSystem.h @@ -1,7 +1,6 @@ #pragma once #include "System.h" -#include "api/LoopTimer.h" namespace crepe { @@ -25,8 +24,6 @@ public: */ void update() override; -private: - LoopTimer & timer = LoopTimer::get_instance(); }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 0ba71ec..241e833 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -17,13 +17,19 @@ using namespace crepe; using namespace std; -void RenderSystem::clear_screen() { this->context.clear_screen(); } +void RenderSystem::clear_screen() { + SDLContext & ctx = this->mediator.sdl_context; + ctx.clear_screen(); +} -void RenderSystem::present_screen() { this->context.present_screen(); } +void RenderSystem::present_screen() { + SDLContext & ctx = this->mediator.sdl_context; + ctx.present_screen(); +} SDLContext::CameraValues RenderSystem::update_camera() { ComponentManager & mgr = this->mediator.component_manager; - + SDLContext & ctx = this->mediator.sdl_context; RefVector cameras = mgr.get_components_by_type(); if (cameras.size() == 0) throw std::runtime_error("No cameras in current scene"); @@ -32,7 +38,7 @@ SDLContext::CameraValues RenderSystem::update_camera() { if (!cam.active) continue; const Transform & transform = mgr.get_components_by_id(cam.game_object_id).front().get(); - SDLContext::CameraValues cam_val = this->context.set_camera(cam); + SDLContext::CameraValues cam_val = ctx.set_camera(cam); cam_val.cam_pos = transform.position + cam.data.postion_offset; return cam_val; } @@ -64,6 +70,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::Came const double & scale) { ComponentManager & mgr = this->mediator.component_manager; + SDLContext & ctx = this->mediator.sdl_context; vector> emitters = mgr.get_components_by_id(sprite.game_object_id); @@ -78,7 +85,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::Came for (const Particle & p : em.data.particles) { if (!p.active) continue; - this->context.draw(SDLContext::RenderContext{ + ctx.draw(SDLContext::RenderContext{ .sprite = sprite, .cam = cam, .pos = p.position, @@ -91,7 +98,8 @@ bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::Came } void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam, const Transform & tm) { - this->context.draw(SDLContext::RenderContext{ + SDLContext & ctx = this->mediator.sdl_context; + ctx.draw(SDLContext::RenderContext{ .sprite = sprite, .cam = cam, .pos = tm.position, diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 91b386f..de76229 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -77,9 +77,6 @@ private: * \todo Implement a text component and a button component. * \todo Consider adding text input functionality. */ - -private: - SDLContext & context = this->mediator.sdl_context; }; } // namespace crepe -- cgit v1.2.3