diff options
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/Animator.cpp | 48 | ||||
-rw-r--r-- | src/crepe/api/Animator.h | 101 | ||||
-rw-r--r-- | src/crepe/api/Camera.cpp | 9 | ||||
-rw-r--r-- | src/crepe/api/Camera.h | 26 | ||||
-rw-r--r-- | src/crepe/api/Config.h | 2 | ||||
-rw-r--r-- | src/crepe/api/LoopManager.cpp | 1 | ||||
-rw-r--r-- | src/crepe/api/Sprite.cpp | 16 | ||||
-rw-r--r-- | src/crepe/api/Sprite.h | 82 | ||||
-rw-r--r-- | src/crepe/api/Transform.h | 2 |
9 files changed, 190 insertions, 97 deletions
diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 45f67f6..dc99fd4 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -7,21 +7,53 @@ using namespace crepe; -Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_animator) +Animator::Animator(uint32_t id, Sprite & ss, int max_row, int max_col, + const Animator::Data & ctx) : Component(id), spritesheet(ss), - row(row), - col(col) { + row(max_row), + col(max_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->active = false; + 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 this->spritesheet.aspect_ratio = static_cast<double>(this->spritesheet.mask.w) / this->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) { + 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++ % 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 6c506aa..dab6697 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -16,33 +16,100 @@ class SDLContext; * sheet. It can be used to play animations, loop them, or stop them. */ class Animator : public Component { +public: + struct Data { + + //! frames per second for animation + int fps = 1; + + //! 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; + + //! offset in pixels. + // TODO implement + int offset_x = 0; + }; public: - //TODO: need to implement this + /** + * \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. - * \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. */ - Animator(uint32_t id, Sprite & spritesheet, int row, int col, int col_animate); - + Animator(uint32_t id, Sprite & ss, int max_row, int max_col, const Animator::Data & ctx); ~Animator(); // dbg_trace -private: - //! A reference to the Sprite sheet containing the animation frames. +public: + //! A reference to the Sprite sheet containing. Sprite & spritesheet; //! The maximum number of columns in the sprite sheet. @@ -51,21 +118,11 @@ private: //! 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; - - //TODO: Is this necessary? - //int fps; + Animator::Data data; private: //! AnimatorSystem adjust the private member parameters of Animator; friend class AnimatorSystem; - - //! SDLContext reads the Animator member var's - friend class SDLContext; }; } // namespace crepe // diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index 39d8ab0..b042c35 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -2,19 +2,16 @@ #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) +Camera::Camera(game_object_id_t id, const ivec2 & screen, const vec2 & viewport_size, + const Data & ctx) : Component(id), - bg_color(bg_color), - offset(offset), screen(screen), viewport_size(viewport_size), - zoom(zoom) { + data(ctx) { dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index 2d8fa48..f626379 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -14,23 +14,30 @@ 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 = Color::WHITE; + + //! Zoom level of the camera view. + double zoom = 1; + + //! 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 ivec2 & screen, const vec2 & viewport_size, + 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; + Camera::Data data; //! screen the display size in pixels ( output resolution ) const ivec2 screen; @@ -38,9 +45,6 @@ public: //! 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; - public: /** * \brief Gets the maximum number of camera instances allowed. diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 225e9b9..200a3b0 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -67,7 +67,7 @@ public: //! default window settings struct { //TODO make this constexpr because this will never change - ivec2 default_size = {1080, 720}; + ivec2 default_size = {1280, 720}; std::string window_title = "Jetpack joyride clone"; } window_settings; diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index dc2d9e0..3e9a88e 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -60,6 +60,7 @@ void LoopManager::setup() { this->game_running = true; timer.start(); timer.set_fps(200); + this->scene_manager.load_next_scene(); } void LoopManager::render() { diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 0a2ad4c..b52a344 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -6,24 +6,20 @@ #include "Component.h" #include "Sprite.h" #include "Texture.h" +#include "types.h" 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, int height) +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), - height(height) { + texture(std::move(texture)), + data(ctx) { dbg_trace(); - this->mask.w = sprite_image.get_size().x; - this->mask.h = sprite_image.get_size().y; + this->mask.w = this->texture.get_size().x; + this->mask.h = this->texture.get_size().y; this->aspect_ratio = static_cast<double>(this->mask.w) / this->mask.h; } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index a0e90a0..a1230db 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -1,11 +1,10 @@ #pragma once -#include <cstdint> - #include "../Component.h" #include "Color.h" #include "Texture.h" +#include "types.h" namespace crepe { @@ -20,60 +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 = Color::WHITE; + + //! Flip settings for the sprite + FlipSettings flip; + + //! Layer sorting level of the sprite + const int sorting_in_layer = 0; + + //! Order within the sorting layer + const int order_in_layer = 0; + + /** + * \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_offset = 1; + }; + 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, int height); - - /** - * \brief Destroys the Sprite instance. + * \param texture asset of the image + * \param ctx all the sprite data */ + 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; + const Texture texture; - //! Layer sorting level of the sprite - const int sorting_in_layer; - //! Order within the sorting layer - const int order_in_layer; - - //! height in world units - const int height; + 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 - */ - double aspect_ratio; + * \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; -private: //! Reads the mask of sprite friend class SDLContext; 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; |