diff options
-rw-r--r-- | src/crepe/api/Animator.cpp | 29 | ||||
-rw-r--r-- | src/crepe/api/Animator.h | 22 | ||||
-rw-r--r-- | src/crepe/api/Camera.cpp | 7 | ||||
-rw-r--r-- | src/crepe/api/Camera.h | 19 | ||||
-rw-r--r-- | src/crepe/api/Sprite.cpp | 2 | ||||
-rw-r--r-- | src/crepe/api/Sprite.h | 25 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 48 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 2 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 6 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 19 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.h | 8 | ||||
-rw-r--r-- | src/example/rendering_particle.cpp | 14 |
12 files changed, 103 insertions, 98 deletions
diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 1234967..dc99fd4 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -7,20 +7,23 @@ using namespace crepe; -Animator::Animator(game_object_id_t id, const Animator::Data & ctx) +Animator::Animator(uint32_t id, Sprite & ss, int max_row, int max_col, + const Animator::Data & ctx) : Component(id), + spritesheet(ss), + row(max_row), + col(max_col), data(ctx) { dbg_trace(); - 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; + 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 - Sprite & ss = this->data.spritesheet; - ss.data.aspect_ratio - = static_cast<double>(this->data.spritesheet.mask.w) / this->data.spritesheet.mask.h; + this->spritesheet.aspect_ratio + = static_cast<double>(this->spritesheet.mask.w) / this->spritesheet.mask.h; } Animator::~Animator() { dbg_trace(); } @@ -43,12 +46,14 @@ void Animator::set_cycle_range(int start, int end) { } void Animator::set_anim(int col) { - this->data.curr_row = 0; - this->data.curr_col = 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++ % ctx.row; - ctx.spritesheet.mask.x = ctx.curr_row * ctx.spritesheet.mask.w; + 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 74abd5e..1fe2b6f 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -18,20 +18,13 @@ class SDLContext; 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; + int fps = 1; //! The current col being animated. - int curr_col; + int curr_col = 0; //! The current row being animated. int curr_row = 0; @@ -113,10 +106,19 @@ public: * This constructor sets up the Animator with the given parameters, and initializes the * animation system. */ - Animator(uint32_t id, const Animator::Data & ctx); + Animator(uint32_t id, Sprite & ss, int max_row, int max_col, 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; + Animator::Data data; private: diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index ff41710..b042c35 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -6,7 +6,12 @@ using namespace crepe; -Camera::Camera(game_object_id_t id, const Data & ctx) : Component(id), data(ctx) { +Camera::Camera(game_object_id_t id, const ivec2 & screen, const vec2 & viewport_size, + const Data & ctx) + : Component(id), + screen(screen), + viewport_size(viewport_size), + data(ctx) { dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index e466d36..f626379 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -17,16 +17,10 @@ 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; + const Color bg_color = Color::WHITE; //! Zoom level of the camera view. - double zoom; + double zoom = 1; //! offset postion from the game object transform component vec2 offset; @@ -38,12 +32,19 @@ public: * \param id Unique identifier for the camera component. * \param ctx the camera component data */ - Camera(game_object_id_t id, const Data & ctx); + Camera(game_object_id_t id, const ivec2 & screen, const vec2 & viewport_size, + const Data & ctx); ~Camera(); // dbg_trace only public: Camera::Data data; + //! 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; + public: /** * \brief Gets the maximum number of camera instances allowed. diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index fe495a1..3a1acac 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -20,7 +20,7 @@ Sprite::Sprite(game_object_id_t id, Texture & texture, const Sprite::Data & ctx) 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; + this->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 aef6a8d..d82ae8d 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -27,16 +27,16 @@ public: struct Data { //! Color tint of the sprite - Color color; + Color color = Color::WHITE; //! Flip settings for the sprite FlipSettings flip; //! Layer sorting level of the sprite - const int sorting_in_layer; + const int sorting_in_layer = 0; //! Order within the sorting layer - const int order_in_layer; + const int order_in_layer = 0; /** * \size width and height of the sprite in game units @@ -53,15 +53,8 @@ public: double angle_offset = 0; //! independent sprite scale multiplier - double scale = 1; + double scale_offset = 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: @@ -71,8 +64,6 @@ public: * \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(); @@ -82,6 +73,14 @@ public: 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 + */ + float aspect_ratio; + //! Reads the mask of sprite friend class SDLContext; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 391aa78..d3a15d9 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -115,12 +115,12 @@ SDL_FRect SDLContext::get_dst_rect(const DstRect & ctx) const { 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}; + 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}; const CameraValues & cam = ctx.cam; - size *= cam.render_scale * ctx.img_scale * data.scale; + size *= cam.render_scale * ctx.img_scale * data.scale_offset; vec2 screen_pos = (ctx.pos - cam.cam_pos + (cam.zoomed_viewport) / 2) * cam.render_scale - size / 2 + cam.bar_size; @@ -154,44 +154,45 @@ void SDLContext::draw(const RenderContext & ctx) { &dstrect, angle, NULL, render_flip); } -void SDLContext::set_camera(const Camera & cam, CameraValues & ctx) { +SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { const Camera::Data & cam_data = cam.data; + CameraValues ret_cam; // resize window int w, h; SDL_GetWindowSize(this->game_window.get(), &w, &h); - if (w != cam_data.screen.x || h != cam_data.screen.y) { - SDL_SetWindowSize(this->game_window.get(), cam_data.screen.x, cam_data.screen.y); + if (w != cam.screen.x || h != cam.screen.y) { + SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y); } - vec2 & zoomed_viewport = ctx.zoomed_viewport; - vec2 & bar_size = ctx.bar_size; - vec2 & render_scale = ctx.render_scale; + vec2 & zoomed_viewport = ret_cam.zoomed_viewport; + vec2 & bar_size = ret_cam.bar_size; + vec2 & render_scale = ret_cam.render_scale; - zoomed_viewport = cam_data.viewport_size * cam_data.zoom; - double screen_aspect = static_cast<double>(cam_data.screen.x) / cam_data.screen.y; + zoomed_viewport = cam.viewport_size * cam_data.zoom; + double screen_aspect = static_cast<double>(cam.screen.x) / cam.screen.y; double viewport_aspect = zoomed_viewport.x / zoomed_viewport.y; // calculate black bars if (screen_aspect > viewport_aspect) { // pillarboxing - float scale = cam_data.screen.y / zoomed_viewport.y; + float scale = cam.screen.y / zoomed_viewport.y; float adj_width = zoomed_viewport.x * scale; - float bar_width = (cam_data.screen.x - adj_width) / 2; - this->black_bars[0] = {0, 0, bar_width, (float) cam_data.screen.y}; + float bar_width = (cam.screen.x - adj_width) / 2; + this->black_bars[0] = {0, 0, bar_width, (float) cam.screen.y}; this->black_bars[1] - = {(cam_data.screen.x - bar_width), 0, bar_width, (float) cam_data.screen.y}; + = {(cam.screen.x - bar_width), 0, bar_width, (float) cam.screen.y}; bar_size = {bar_width, 0}; render_scale.x = render_scale.y = scale; } else { // letterboxing - float scale = cam_data.screen.x / (cam_data.viewport_size.x * cam_data.zoom); - float adj_height = cam_data.viewport_size.y * scale; - float bar_height = (cam_data.screen.y - adj_height) / 2; - this->black_bars[0] = {0, 0, (float) cam_data.screen.x, bar_height}; + float scale = cam.screen.x / (cam.viewport_size.x * cam_data.zoom); + float adj_height = cam.viewport_size.y * scale; + float bar_height = (cam.screen.y - adj_height) / 2; + this->black_bars[0] = {0, 0, (float) cam.screen.x, bar_height}; this->black_bars[1] - = {0, (cam_data.screen.y - bar_height), (float) cam_data.screen.x, bar_height}; + = {0, (cam.screen.y - bar_height), (float) cam.screen.x, bar_height}; bar_size = {0, bar_height}; render_scale.x = render_scale.y = scale; @@ -203,12 +204,15 @@ void SDLContext::set_camera(const Camera & cam, CameraValues & ctx) { SDL_Rect bg = { .x = 0, .y = 0, - .w = cam_data.screen.x, - .h = cam_data.screen.y, + .w = cam.screen.x, + .h = cam.screen.y, }; // fill bg color SDL_RenderFillRect(this->game_renderer.get(), &bg); + + + return ret_cam; } uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index a0d1da8..d662bee 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -142,7 +142,7 @@ private: * \brief sets the background of the camera (will be adjusted in future PR) * \param camera Reference to the Camera object. */ - void set_camera(const Camera & camera, CameraValues & ctx); + CameraValues set_camera(const Camera & camera); private: struct DstRect { diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index e9cdd4c..a2ae529 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -22,14 +22,14 @@ void AnimatorSystem::update() { int last_frame = ctx.curr_row; - int cycle_end = (ctx.cycle_end == -1) ? ctx.row : ctx.cycle_end; + int cycle_end = (ctx.cycle_end == -1) ? a.row : ctx.cycle_end; int total_frames = cycle_end - ctx.cycle_start; int curr_frame = static_cast<int>(elapsed_time / frame_duration) % total_frames; 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); + a.spritesheet.mask.x = ctx.curr_row * a.spritesheet.mask.w; + a.spritesheet.mask.y = (ctx.curr_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/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 1bf5f65..b5db45a 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -21,7 +21,7 @@ void RenderSystem::clear_screen() { this->context.clear_screen(); } void RenderSystem::present_screen() { this->context.present_screen(); } -const Camera & RenderSystem::update_camera() { +SDLContext::CameraValues RenderSystem::update_camera() { ComponentManager & mgr = this->component_manager; RefVector<Camera> cameras = mgr.get_components_by_type<Camera>(); @@ -32,9 +32,9 @@ const Camera & RenderSystem::update_camera() { if (!cam.active) continue; const Transform & transform = mgr.get_components_by_id<Transform>(cam.game_object_id).front().get(); - this->context.set_camera(cam, this->cam_ctx); - this->cam_ctx.cam_pos = transform.position + cam.data.offset; - return cam; + SDLContext::CameraValues cam_val = this->context.set_camera(cam); + cam_val.cam_pos = transform.position + cam.data.offset; + return cam_val; } throw std::runtime_error("No active cameras in current scene"); } @@ -60,7 +60,7 @@ void RenderSystem::update() { this->present_screen(); } -bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, +bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::CameraValues & cam, const double & scale) { ComponentManager & mgr = this->component_manager; @@ -80,7 +80,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, this->context.draw(SDLContext::RenderContext{ .sprite = sprite, - .cam = this->cam_ctx, + .cam = cam, .pos = p.position, .angle = p.angle, .scale = scale, @@ -89,11 +89,10 @@ bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, } return rendering_particles; } -void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam, - const Transform & tm) { +void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam, const Transform & tm) { this->context.draw(SDLContext::RenderContext{ .sprite = sprite, - .cam = this->cam_ctx, + .cam = cam, .pos = tm.position, .angle = tm.rotation, .scale = tm.scale, @@ -102,7 +101,7 @@ void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam, void RenderSystem::render() { ComponentManager & mgr = this->component_manager; - const Camera & cam = this->update_camera(); + const SDLContext::CameraValues & cam = this->update_camera(); RefVector<Sprite> sprites = mgr.get_components_by_type<Sprite>(); RefVector<Sprite> sorted_sprites = this->sort(sprites); diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 249f3b8..e779047 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -37,7 +37,7 @@ private: void present_screen(); //! Updates the active camera used for rendering. - const Camera & update_camera(); + SDLContext::CameraValues update_camera(); //! Renders the whole screen void render(); @@ -49,7 +49,7 @@ private: * \param tm the Transform component for scale * \return true if particles have been rendered */ - bool render_particle(const Sprite & sprite, const Camera & cam, const double & scale); + bool render_particle(const Sprite & sprite, const SDLContext::CameraValues & cam, const double & scale); /** * \brief renders a sprite with a Transform component on the screen @@ -57,7 +57,7 @@ private: * \param sprite the sprite component that holds all the data * \param tm the Transform component that holds the position,rotation and scale */ - void render_normal(const Sprite & sprite, const Camera & cam, const Transform & tm); + void render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam, const Transform & tm); /** * \brief sort a vector sprite objects with @@ -76,8 +76,6 @@ private: private: SDLContext & context = SDLContext::get_instance(); - - SDLContext::CameraValues cam_ctx; }; } // namespace crepe diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index e18c7b7..2e2552d 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -45,8 +45,6 @@ using namespace std; class TestScene : public Scene { public: - using Scene::Scene; - void load_scene() { ComponentManager & mgr = this->component_manager; GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 1); @@ -63,23 +61,17 @@ public: .order_in_layer = 2, .size = {0, 100}, .angle_offset = 0, - .scale = 1, }); - auto & anim = game_object.add_component<Animator>(Animator::Data{ - .spritesheet = test_sprite, - .col = 4, - .row = 4, + auto & anim = game_object.add_component<Animator>(test_sprite, 4, 4, Animator::Data{ .fps = 1, .looping = false, }); anim.set_anim(2); + anim.active = false; - auto & cam = game_object.add_component<Camera>(Camera::Data{ + auto & cam = game_object.add_component<Camera>(ivec2{1280,720}, vec2{400,400},Camera::Data{ .bg_color = Color::WHITE, - .screen = ivec2{1280, 720}, - .viewport_size = vec2{400, 400}, - .zoom = 1.0, }); } |