diff options
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/Animator.cpp | 34 | ||||
-rw-r--r-- | src/crepe/api/Animator.h | 66 | ||||
-rw-r--r-- | src/crepe/api/Camera.cpp | 11 | ||||
-rw-r--r-- | src/crepe/api/Camera.h | 37 | ||||
-rw-r--r-- | src/crepe/api/Sprite.cpp | 17 | ||||
-rw-r--r-- | src/crepe/api/Sprite.h | 98 |
6 files changed, 126 insertions, 137 deletions
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<double>(this->spritesheet.mask.w) / this->spritesheet.mask.h; + Sprite & ss = this->data.spritesheet; + ss.data.aspect_ratio + = static_cast<double>(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<double>(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<double>(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 |