diff options
Diffstat (limited to 'src')
-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 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 25 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 17 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 5 | ||||
-rw-r--r-- | src/example/rendering_particle.cpp | 19 |
10 files changed, 166 insertions, 163 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 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<int>(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<Sprite>( - 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<Animator>(test_sprite, 4, 4, 0); + auto & anim = game_object.add_component<Animator>(Animator::Data{ + .spritesheet = test_sprite, + .col = 4, + .row = 4, + .fps = 10, + }); - auto & cam = game_object.add_component<Camera>(Color::, ivec2{720, 1280}, + auto & cam = game_object.add_component<Camera>(Color::WHITE, ivec2{720, 1280}, vec2{400, 400}, 1.0); } |