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 | 118 | ||||
-rw-r--r-- | src/crepe/api/Camera.cpp | 11 | ||||
-rw-r--r-- | src/crepe/api/Camera.h | 38 | ||||
-rw-r--r-- | src/crepe/api/Config.h | 4 | ||||
-rw-r--r-- | src/crepe/api/LoopManager.cpp | 2 | ||||
-rw-r--r-- | src/crepe/api/Sprite.cpp | 16 | ||||
-rw-r--r-- | src/crepe/api/Sprite.h | 96 | ||||
-rw-r--r-- | src/crepe/api/Transform.h | 6 |
9 files changed, 233 insertions, 106 deletions
diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 45f67f6..154135f 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, unsigned int max_row, unsigned int max_col, + const Animator::Data & data) : Component(id), spritesheet(ss), - row(row), - col(col) { + max_rows(max_row), + max_columns(max_col), + data(data) { 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->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 = 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.col = 0; + this->data.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.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.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 6c506aa..23d29f6 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -1,7 +1,10 @@ #pragma once +#include <sys/types.h> + #include "Component.h" #include "Sprite.h" +#include "types.h" namespace crepe { @@ -16,56 +19,117 @@ 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 + unsigned int fps = 1; + + //! The current col being animated. + unsigned int col = 0; + + //! The current row being animated. + unsigned int row = 0; + + //! should the animation loop + bool looping = false; + + //! starting frame for cycling + unsigned int cycle_start = 0; + + //! end frame for cycling (-1 --> use last frame) + int cycle_end = -1; + + //! offset in pixels. + // TODO implement + unsigned int white_space = 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 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 data 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 & spritesheet, int row, int col, int col_animate); - + Animator(game_object_id_t id, Sprite & ss, unsigned int max_row, unsigned int max_col, + const Animator::Data & data); ~Animator(); // dbg_trace -private: - //! A reference to the Sprite sheet containing the animation frames. - Sprite & spritesheet; - +public: //! The maximum number of columns in the sprite sheet. - const int col; + const unsigned int max_columns; //! The maximum number of rows in the sprite sheet. - const int row; - - //! The current col being animated. - int curr_col = 0; + const unsigned int max_rows; - //! 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; + //! A reference to the Sprite sheet containing. + Sprite & spritesheet; - //! SDLContext reads the Animator member var's - friend class SDLContext; + // uses the spritesheet + friend AnimatorSystem; }; } // namespace crepe // diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index 39d8ab0..179dc18 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -1,20 +1,17 @@ -#include "types.h" #include "util/Log.h" #include "Camera.h" -#include "Color.h" #include "Component.h" +#include "types.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 & data) : Component(id), - bg_color(bg_color), - offset(offset), screen(screen), viewport_size(viewport_size), - zoom(zoom) { + data(data) { dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index 2d8fa48..54d9a73 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -14,23 +14,42 @@ 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 { + /** + * \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 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 + vec2 postion_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 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 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 Camera::Data & data); ~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 +57,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..f1bca62 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -66,8 +66,8 @@ public: //! default window settings struct { - //TODO make this constexpr because this will never change - ivec2 default_size = {1080, 720}; + //! default screen size in pixels + 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 8ccc0a4..044f096 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -65,11 +65,13 @@ void LoopManager::setup() { this->scene_manager.load_next_scene(); timer.start(); timer.set_fps(200); + this->scene_manager.load_next_scene(); } void LoopManager::render() { if (!this->game_running) return; + this->get_system<AnimatorSystem>().update(); this->get_system<RenderSystem>().update(); } diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 0a2ad4c..cc0e20a 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 & data) : 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(data) { 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..ea8104c 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,81 @@ class AnimatorSystem; * flip settings, and is managed in layers with defined sorting orders. */ class Sprite : public Component { - public: + //! settings to flip the image struct FlipSettings { + //! horizantal flip bool flip_x = false; + //! vertical flip bool flip_y = false; }; + //! Sprite data that does not have to be set in the constructor + struct Data { + /** + * \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 + 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 + float angle_offset = 0; + + //! independent sprite scale multiplier + float scale_offset = 1; + + //! independent sprite offset position + vec2 position_offset; + }; + 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 & data); ~Sprite(); //! Texture used for the sprite - const Texture sprite_image; + const Texture texture; - //! 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; - - //! 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 + * + * - 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; -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..3ef0fb5 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -15,10 +15,10 @@ class Transform : public Component { public: //! Translation (shift) vec2 position = {0, 0}; - //! Rotation, in degrees - double rotation = 0; + //! Rotation, in degrees clockwise + float rotation = 0; //! Multiplication factor - double scale = 0; + float scale = 0; protected: /** |