diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-02 10:18:08 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-02 10:18:08 +0100 |
commit | 54ab44e3508d526be6275378e5979290ec188d6f (patch) | |
tree | c8768edc0066d5149f1e7088e6fc82e3d51671b3 | |
parent | 3afcae9dd472ead2d5f2b667fc6479f8ee6db10c (diff) |
comments i animator and sprite. added sprite indepentdent scale and angle
-rw-r--r-- | src/crepe/api/Animator.cpp | 17 | ||||
-rw-r--r-- | src/crepe/api/Animator.h | 74 | ||||
-rw-r--r-- | src/crepe/api/Sprite.h | 16 | ||||
-rw-r--r-- | src/crepe/api/Transform.h | 2 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 8 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 2 |
6 files changed, 94 insertions, 25 deletions
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<double>(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 @@ -19,6 +19,59 @@ 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. * * \param id The unique identifier for the component, typically assigned automatically. @@ -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 <cmath> #include <cstddef> #include <functional> -#include <iostream> #include <memory> #include <stdexcept> @@ -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; /** |