From 05e099c03a2242bf5194d06191a0ff667d404817 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 6 Dec 2024 19:21:56 +0100 Subject: implemented feedback --- src/crepe/api/Animator.cpp | 30 +++++++++++++++--------------- src/crepe/api/Animator.h | 25 +++++++++++++++---------- src/crepe/api/Camera.cpp | 6 +++--- src/crepe/api/Camera.h | 22 +++++++++++++++++----- src/crepe/api/Config.h | 2 +- src/crepe/api/Sprite.cpp | 4 ++-- src/crepe/api/Sprite.h | 30 +++++++++++++++++++----------- src/crepe/api/Transform.h | 4 ++-- src/crepe/facade/SDLContext.cpp | 21 ++++++++++++--------- src/crepe/facade/SDLContext.h | 32 ++++++++++++++++++++++++++------ src/crepe/manager/Mediator.h | 2 ++ src/crepe/system/AnimatorSystem.cpp | 18 +++++++++--------- src/crepe/system/AnimatorSystem.h | 3 --- src/crepe/system/RenderSystem.cpp | 20 ++++++++++++++------ src/crepe/system/RenderSystem.h | 3 --- 15 files changed, 137 insertions(+), 85 deletions(-) (limited to 'src') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 8b91859..154135f 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -8,18 +8,18 @@ using namespace crepe; Animator::Animator(uint32_t id, Sprite & ss, unsigned int max_row, unsigned int max_col, - const Animator::Data & ctx) + const Animator::Data & data) : Component(id), spritesheet(ss), - row(max_row), - col(max_col), - data(ctx) { + max_rows(max_row), + max_columns(max_col), + data(data) { dbg_trace(); - 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; + 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 @@ -36,8 +36,8 @@ void Animator::pause() { this->active = false; } void Animator::stop() { this->active = false; - this->data.curr_col = 0; - this->data.curr_row = 0; + this->data.col = 0; + this->data.row = 0; } void Animator::set_fps(int fps) { this->data.fps = fps; } @@ -47,13 +47,13 @@ void Animator::set_cycle_range(int start, int 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; + 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.curr_row = ctx.curr_row++ % this->row; - this->spritesheet.mask.x = ctx.curr_row * this->spritesheet.mask.w; + 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 2a0a889..23d29f6 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -1,9 +1,10 @@ #pragma once +#include + #include "Component.h" #include "Sprite.h" #include "types.h" -#include namespace crepe { @@ -25,10 +26,10 @@ public: unsigned int fps = 1; //! The current col being animated. - unsigned int curr_col = 0; + unsigned int col = 0; //! The current row being animated. - unsigned int curr_row = 0; + unsigned int row = 0; //! should the animation loop bool looping = false; @@ -105,26 +106,30 @@ public: * \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 ctx extra animation data for more control + * \param data extra animation data for more control * * This constructor sets up the Animator with the given parameters, and initializes the * animation system. */ Animator(game_object_id_t id, Sprite & ss, unsigned int max_row, unsigned int max_col, - const Animator::Data & ctx); + const Animator::Data & data); ~Animator(); // dbg_trace public: - //! A reference to the Sprite sheet containing. - Sprite & spritesheet; - //! The maximum number of columns in the sprite sheet. - const unsigned int col; + const unsigned int max_columns; //! The maximum number of rows in the sprite sheet. - const unsigned int row; + const unsigned int max_rows; Animator::Data data; + +private: + //! A reference to the Sprite sheet containing. + Sprite & spritesheet; + + // uses the spritesheet + friend AnimatorSystem; }; } // namespace crepe // diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index b042c35..179dc18 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -1,17 +1,17 @@ -#include "types.h" #include "util/Log.h" #include "Camera.h" #include "Component.h" +#include "types.h" using namespace crepe; Camera::Camera(game_object_id_t id, const ivec2 & screen, const vec2 & viewport_size, - const Data & ctx) + const Data & data) : Component(id), screen(screen), viewport_size(viewport_size), - data(ctx) { + data(data) { dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index 84ca9e1..54d9a73 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -16,10 +16,20 @@ namespace crepe { class Camera : public Component { public: struct Data { - //! Background color of the camera view. - const Color bg_color = Color::WHITE; + /** + * \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 level of the camera view. + /** + * \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 @@ -30,10 +40,12 @@ public: /** * \brief Constructs a Camera with the specified ID and background color. * \param id Unique identifier for the camera component. - * \param ctx the camera component data + * \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 ivec2 & screen, const vec2 & viewport_size, - const Data & ctx); + const Camera::Data & data); ~Camera(); // dbg_trace only public: diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 200a3b0..f1bca62 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -66,7 +66,7 @@ public: //! default window settings struct { - //TODO make this constexpr because this will never change + //! default screen size in pixels ivec2 default_size = {1280, 720}; std::string window_title = "Jetpack joyride clone"; diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index b52a344..cc0e20a 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -11,10 +11,10 @@ using namespace std; using namespace crepe; -Sprite::Sprite(game_object_id_t id, Texture & texture, const Sprite::Data & ctx) +Sprite::Sprite(game_object_id_t id, Texture & texture, const Sprite::Data & data) : Component(id), texture(std::move(texture)), - data(ctx) { + data(data) { dbg_trace(); diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 78ed7ad..ea8104c 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -28,8 +28,15 @@ public: bool flip_y = false; }; + //! Sprite data that does not have to be set in the constructor struct Data { - //! Color tint of the sprite + /** + * \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 @@ -44,10 +51,10 @@ public: /** * \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 + * - 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; @@ -69,7 +76,7 @@ public: * \param texture asset of the image * \param ctx all the sprite data */ - Sprite(game_object_id_t id, Texture & texture, const Data & ctx); + Sprite(game_object_id_t id, Texture & texture, const Data & data); ~Sprite(); //! Texture used for the sprite @@ -79,11 +86,12 @@ public: 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 - */ + * \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; //! Reads the mask of sprite diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index 34ac70a..3ef0fb5 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -16,9 +16,9 @@ public: //! Translation (shift) vec2 position = {0, 0}; //! Rotation, in degrees clockwise - double rotation = 0; + float rotation = 0; //! Multiplication factor - double scale = 0; + float scale = 0; protected: /** diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index cf9f7d5..4cc2206 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -15,13 +15,13 @@ #include #include "../api/Camera.h" +#include "../api/Color.h" #include "../api/Config.h" #include "../api/Sprite.h" #include "../api/Texture.h" #include "../util/Log.h" #include "SDLContext.h" -#include "api/Color.h" #include "types.h" using namespace crepe; @@ -230,14 +230,17 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { }; } -SDL_FRect SDLContext::get_dst_rect(const DstRect & ctx) const { +SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { const Sprite::Data & data = ctx.sprite.data; - vec2 size = {data.size.x == 0 && data.size.y != 0 ? data.size.y * ctx.sprite.aspect_ratio - : data.size.x, - data.size.y == 0 && data.size.x != 0 ? data.size.x / ctx.sprite.aspect_ratio - : data.size.y}; + vec2 size; + if (data.size.x == 0 && data.size.y != 0) { + size.x = data.size.y * ctx.sprite.aspect_ratio; + } + if (data.size.y == 0 && data.size.x != 0) { + size.y = data.size.x / ctx.sprite.aspect_ratio; + } const CameraValues & cam = ctx.cam; @@ -264,7 +267,7 @@ void SDLContext::draw(const RenderContext & ctx) { | (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{ + SDL_FRect dstrect = this->get_dst_rect(SDLContext::DestinationRectangleData{ .sprite = ctx.sprite, .cam = ctx.cam, .pos = ctx.pos, @@ -294,8 +297,8 @@ SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { vec2 & render_scale = ret_cam.render_scale; zoomed_viewport = cam.viewport_size * cam_data.zoom; - double screen_aspect = static_cast(cam.screen.x) / cam.screen.y; - double viewport_aspect = zoomed_viewport.x / zoomed_viewport.y; + float screen_aspect = static_cast(cam.screen.x) / cam.screen.y; + float viewport_aspect = zoomed_viewport.x / zoomed_viewport.y; // calculate black bars if (screen_aspect > viewport_aspect) { diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index dfdaa56..bbe87c3 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -9,11 +9,9 @@ #include #include #include -#include #include "api/Camera.h" #include "api/Color.h" -#include "api/Event.h" #include "api/KeyCodes.h" #include "api/Sprite.h" #include "api/Texture.h" @@ -35,12 +33,33 @@ class SDLContext { public: //! data that the camera component cannot hold struct CameraValues { + + //! zoomed in viewport in game_units vec2 zoomed_viewport; + + /** + * \render_scale scaling factor + * + * depending on the black bars type will the scaling be different. + * - lettorboxing --> scaling on the y-as + * - pillarboxing --> scaling on the x-as + */ vec2 render_scale; + + /** + * \bar_size size of calculated black bars + * + * depending on the black bars type will the size be different + * - lettorboxing --> {0, bar_height} + * - pillarboxing --> {bar_width , 0} + */ vec2 bar_size; + + //! Calculated camera position vec2 cam_pos; }; - + + //! rendering data needed to render on screen struct RenderContext { const Sprite & sprite; const CameraValues & cam; @@ -192,7 +211,8 @@ private: CameraValues set_camera(const Camera & camera); private: - struct DstRect { + //! the data needed to construct a sdl dst rectangle + struct DestinationRectangleData { const Sprite & sprite; const CameraValues & cam; const vec2 & pos; @@ -216,7 +236,7 @@ private: * \param img_scale the image multiplier for increasing img size * \return sdl rectangle to draw a dst image to draw on the screen */ - SDL_FRect get_dst_rect(const DstRect & ctx) const; + SDL_FRect get_dst_rect(const DestinationRectangleData & data) const; /** * \brief Set an additional color value multiplied into render copy operations. * @@ -233,7 +253,7 @@ private: std::unique_ptr> game_renderer; //! black bars rectangle to draw - SDL_FRect black_bars[2]; + SDL_FRect black_bars[2] = {}; }; } // namespace crepe diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index f8517a3..8094d80 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -6,6 +6,7 @@ #include "../facade/SDLContext.h" #include "EventManager.h" #include "SaveManager.h" +#include "api/LoopTimer.h" namespace crepe { @@ -30,6 +31,7 @@ struct Mediator { OptionalRef save_manager = SaveManager::get_instance(); OptionalRef event_manager = EventManager::get_instance(); OptionalRef sdl_context = SDLContext::get_instance(); + OptionalRef timer = LoopTimer::get_instance(); }; } // namespace crepe diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 9aede7f..549c35d 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -1,8 +1,8 @@ #include "../api/Animator.h" -#include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" +#include "api/LoopTimer.h" #include "AnimatorSystem.h" @@ -10,27 +10,27 @@ using namespace crepe; void AnimatorSystem::update() { ComponentManager & mgr = this->mediator.component_manager; - + LoopTimer & timer = this->mediator.timer; RefVector animations = mgr.get_components_by_type(); - double elapsed_time = this->timer.get_current_time(); + double elapsed_time = timer.get_current_time(); for (Animator & a : animations) { if (!a.active) continue; Animator::Data & ctx = a.data; - double frame_duration = 1.0f / ctx.fps; + float frame_duration = 1.0f / ctx.fps; - int last_frame = ctx.curr_row; + int last_frame = ctx.row; - int cycle_end = (ctx.cycle_end == -1) ? a.row : ctx.cycle_end; + int cycle_end = (ctx.cycle_end == -1) ? a.max_rows : ctx.cycle_end; int total_frames = cycle_end - ctx.cycle_start; int curr_frame = static_cast(elapsed_time / frame_duration) % total_frames; - ctx.curr_row = ctx.cycle_start + curr_frame; - a.spritesheet.mask.x = ctx.curr_row * a.spritesheet.mask.w; - a.spritesheet.mask.y = (ctx.curr_col * a.spritesheet.mask.h); + ctx.row = ctx.cycle_start + curr_frame; + a.spritesheet.mask.x = ctx.row * a.spritesheet.mask.w; + a.spritesheet.mask.y = (ctx.col * a.spritesheet.mask.h); if (!ctx.looping && curr_frame == ctx.cycle_start && last_frame == total_frames - 1) { a.active = false; diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h index e8a5158..1e8cd7d 100644 --- a/src/crepe/system/AnimatorSystem.h +++ b/src/crepe/system/AnimatorSystem.h @@ -1,7 +1,6 @@ #pragma once #include "System.h" -#include "api/LoopTimer.h" namespace crepe { @@ -25,8 +24,6 @@ public: */ void update() override; -private: - LoopTimer & timer = LoopTimer::get_instance(); }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 0ba71ec..241e833 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -17,13 +17,19 @@ using namespace crepe; using namespace std; -void RenderSystem::clear_screen() { this->context.clear_screen(); } +void RenderSystem::clear_screen() { + SDLContext & ctx = this->mediator.sdl_context; + ctx.clear_screen(); +} -void RenderSystem::present_screen() { this->context.present_screen(); } +void RenderSystem::present_screen() { + SDLContext & ctx = this->mediator.sdl_context; + ctx.present_screen(); +} SDLContext::CameraValues RenderSystem::update_camera() { ComponentManager & mgr = this->mediator.component_manager; - + SDLContext & ctx = this->mediator.sdl_context; RefVector cameras = mgr.get_components_by_type(); if (cameras.size() == 0) throw std::runtime_error("No cameras in current scene"); @@ -32,7 +38,7 @@ SDLContext::CameraValues RenderSystem::update_camera() { if (!cam.active) continue; const Transform & transform = mgr.get_components_by_id(cam.game_object_id).front().get(); - SDLContext::CameraValues cam_val = this->context.set_camera(cam); + SDLContext::CameraValues cam_val = ctx.set_camera(cam); cam_val.cam_pos = transform.position + cam.data.postion_offset; return cam_val; } @@ -64,6 +70,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::Came const double & scale) { ComponentManager & mgr = this->mediator.component_manager; + SDLContext & ctx = this->mediator.sdl_context; vector> emitters = mgr.get_components_by_id(sprite.game_object_id); @@ -78,7 +85,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::Came for (const Particle & p : em.data.particles) { if (!p.active) continue; - this->context.draw(SDLContext::RenderContext{ + ctx.draw(SDLContext::RenderContext{ .sprite = sprite, .cam = cam, .pos = p.position, @@ -91,7 +98,8 @@ bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::Came } void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam, const Transform & tm) { - this->context.draw(SDLContext::RenderContext{ + SDLContext & ctx = this->mediator.sdl_context; + ctx.draw(SDLContext::RenderContext{ .sprite = sprite, .cam = cam, .pos = tm.position, diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 91b386f..de76229 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -77,9 +77,6 @@ private: * \todo Implement a text component and a button component. * \todo Consider adding text input functionality. */ - -private: - SDLContext & context = this->mediator.sdl_context; }; } // namespace crepe -- cgit v1.2.3