From 9288e4964526f1ce6b7d0aca0f075a04f56ede32 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 18 Nov 2024 17:50:17 +0100 Subject: first version world postions with camera --- src/crepe/api/Camera.h | 30 ++++++++++++++++++++------- src/crepe/api/Texture.cpp | 2 +- src/crepe/api/Vector2.cpp | 12 +++++++++++ src/crepe/api/Vector2.h | 7 +++++++ src/crepe/facade/SDLContext.cpp | 45 +++++++++++++++++++++++++++++++---------- 5 files changed, 77 insertions(+), 19 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index e0cda34..73d4ef4 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -2,6 +2,7 @@ #include "Color.h" #include "Component.h" +#include "api/Vector2.h" namespace crepe { @@ -27,20 +28,35 @@ public: //! Background color of the camera view. Color bg_color; - //! Aspect ratio height for the camera. - double aspect_height = 480; + Vector2 pos = {0,0}; - //! Aspect ratio width for the camera. - double aspect_width = 640; + Vector2 screen = {640,480}; - //! X-coordinate of the camera position. + Vector2 viewport = {500,500}; + + /* + //! screen width in pixel coordinates + double screen_w = 480; + + //! screen height in pixel coordinates + double screen_h = 640; + + //! screen widht in world units + double viewport_w = 500.0f; + + //! screen height in world units + double viewport_h = 500.0f; + + //! X-coordinate of the camera position. in world space double x = 0.0; - //! Y-coordinate of the camera position. + //! Y-coordinate of the camera position. in world space double y = 0.0; + */ + //! Zoom level of the camera view. - double zoom = 1.0; + double zoom = 1.0f; public: /** diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index de0d0ea..734a5bb 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -35,5 +35,5 @@ int Texture::get_width() const { } int Texture::get_height() const { if (this->texture == nullptr) return 0; - return SDLContext::get_instance().get_width(*this); + return SDLContext::get_instance().get_height(*this); } diff --git a/src/crepe/api/Vector2.cpp b/src/crepe/api/Vector2.cpp index 30b968e..c3a49b7 100644 --- a/src/crepe/api/Vector2.cpp +++ b/src/crepe/api/Vector2.cpp @@ -8,11 +8,19 @@ Vector2 Vector2::operator+(const Vector2 & other) const { return {x + other.x, y Vector2 Vector2::operator*(double scalar) const { return {x * scalar, y * scalar}; } +Vector2 Vector2::operator*(const Vector2 & other) const{ + return {this->x * other.x, this->y * other.y}; +} Vector2 & Vector2::operator*=(const Vector2 & other) { x *= other.x; y *= other.y; return *this; } +Vector2 & Vector2::operator*=(const double & other) { + x *= other; + y *= other; + return *this; +} Vector2 & Vector2::operator+=(const Vector2 & other) { x += other.x; @@ -26,6 +34,10 @@ Vector2 & Vector2::operator+=(double other) { return *this; } +Vector2 Vector2::operator/(const Vector2 & other) const { + return {this->x / other.x, this->y / other.y}; +} + Vector2 Vector2::operator-() const { return {-x, -y}; } bool Vector2::operator==(const Vector2 & other) const { return x == other.x && y == other.y; } diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h index 2fb6136..2a5db1d 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -18,9 +18,16 @@ struct Vector2 { //! Multiplies this vector by a scalar and returns the result. Vector2 operator*(double scalar) const; + Vector2 operator*(const Vector2 & other) const; + //! Multiplies this vector by another vector element-wise and updates this vector. Vector2 & operator*=(const Vector2 & other); + //! Multiplies a scalar value to both components of this vector and updates this vector. + Vector2 & operator*=(const double & other); + + Vector2 operator/(const Vector2 & other) const; + //! Adds another vector to this vector and updates this vector. Vector2 & operator+=(const Vector2 & other); diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 83e91f8..5185adc 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -15,6 +15,7 @@ #include "../api/Texture.h" #include "../api/Transform.h" #include "../util/Log.h" +#include "api/Vector2.h" #include "SDLContext.h" @@ -37,7 +38,7 @@ SDLContext::SDLContext() { } SDL_Window * tmp_window = SDL_CreateWindow("Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - this->viewport.w, this->viewport.h, 0); + this->viewport.w, this->viewport.h, SDL_WINDOW_RESIZABLE); if (!tmp_window) { // FIXME: throw exception std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl; @@ -103,17 +104,30 @@ void SDLContext::handle_events(bool & running) { void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); } void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } + void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); + + double screen_aspect = cam.screen.x / cam.screen.y; + double viewport_aspect = cam.viewport.x / cam.viewport.y; + Vector2 scale; + + if (screen_aspect > viewport_aspect) { + scale.x = scale.y = cam.screen.x / cam.viewport.x; + } else { + scale.y = scale.x = cam.screen.y / cam.viewport.y; + } + + Vector2 zoomed_viewport = cam.viewport * cam.zoom; + Vector2 pixel_coord = (transform.position - cam.pos) * scale; - double adjusted_x = (transform.position.x - cam.x) * cam.zoom; - double adjusted_y = (transform.position.y - cam.y) * cam.zoom; - double adjusted_w = sprite.sprite_rect.w * transform.scale * cam.zoom; - double adjusted_h = sprite.sprite_rect.h * transform.scale * cam.zoom; + double pixel_w = sprite.sprite_rect.w * transform.scale * scale.x; + double pixel_h = sprite.sprite_rect.h * transform.scale * scale.y; + // decides which part of the sprite will be drawn SDL_Rect srcrect = { .x = sprite.sprite_rect.x, .y = sprite.sprite_rect.y, @@ -121,11 +135,12 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const .h = sprite.sprite_rect.h, }; + // decides where the clipped image is drawn SDL_Rect dstrect = { - .x = static_cast(adjusted_x), - .y = static_cast(adjusted_y), - .w = static_cast(adjusted_w), - .h = static_cast(adjusted_h), + .x = static_cast(pixel_coord.x), + .y = static_cast(pixel_coord.y), + .w = static_cast(pixel_w), + .h = static_cast(pixel_h), }; SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, @@ -133,11 +148,19 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const } void SDLContext::camera(const Camera & cam) { - this->viewport.w = static_cast(cam.aspect_width); - this->viewport.h = static_cast(cam.aspect_height); + + if (this->viewport.w != cam.screen.x && this->viewport.h != cam.screen.y) { + this->viewport.w = cam.screen.x; + this->viewport.h = cam.screen.y; + SDL_SetWindowSize(this->game_window.get(), cam.screen.x , cam.screen.y); + } + + /* this->viewport.x = static_cast(cam.x) - (SCREEN_WIDTH / 2); this->viewport.y = static_cast(cam.y) - (SCREEN_HEIGHT / 2); + */ + SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g, cam.bg_color.b, cam.bg_color.a); } -- cgit v1.2.3 From 88f613cbb2e4aaf3ed55ac0c6490706dd6f6f19d Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 18 Nov 2024 20:12:31 +0100 Subject: rendering based on world unites instead of pixels --- src/crepe/api/Animator.cpp | 2 +- src/crepe/api/Camera.h | 39 +++++++++++++++---------------------- src/crepe/facade/SDLContext.cpp | 36 ++++++++++++++-------------------- src/crepe/facade/SDLContext.h | 2 +- src/crepe/system/AnimatorSystem.cpp | 3 ++- src/crepe/system/AnimatorSystem.h | 3 +-- src/crepe/system/RenderSystem.cpp | 1 - src/example/rendering.cpp | 9 +++++++-- 8 files changed, 43 insertions(+), 52 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 464b0fd..f3d809c 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -18,7 +18,7 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a animator_rect.h /= col; animator_rect.w /= row; animator_rect.x = 0; - animator_rect.y = col_animator * animator_rect.h; + animator_rect.y = (col_animator - 1)* animator_rect.h; this->active = false; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index 73d4ef4..dfccd24 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -28,32 +28,25 @@ public: //! Background color of the camera view. Color bg_color; - Vector2 pos = {0,0}; - - Vector2 screen = {640,480}; - - Vector2 viewport = {500,500}; - - /* - //! screen width in pixel coordinates - double screen_w = 480; - - //! screen height in pixel coordinates - double screen_h = 640; - - //! screen widht in world units - double viewport_w = 500.0f; - - //! screen height in world units - double viewport_h = 500.0f; + /** + * \pos The position of the camera in world units + */ + Vector2 pos = {0, 0}; - //! X-coordinate of the camera position. in world space - double x = 0.0; + /** + * \screen the display size in pixels ( output resolution ) + */ + Vector2 screen = {640, 480}; - //! Y-coordinate of the camera position. in world space - double y = 0.0; + /** + * \viewport is the area of the world visible through the camera (in world units) + */ + Vector2 viewport = {500, 500}; - */ + /** + * \scale scaling factor from world units to pixel coordinates + */ + Vector2 scale = {0,0}; //! Zoom level of the camera view. double zoom = 1.0f; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 5185adc..785b285 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -111,21 +111,10 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); - double screen_aspect = cam.screen.x / cam.screen.y; - double viewport_aspect = cam.viewport.x / cam.viewport.y; - Vector2 scale; - if (screen_aspect > viewport_aspect) { - scale.x = scale.y = cam.screen.x / cam.viewport.x; - } else { - scale.y = scale.x = cam.screen.y / cam.viewport.y; - } - - Vector2 zoomed_viewport = cam.viewport * cam.zoom; - Vector2 pixel_coord = (transform.position - cam.pos) * scale; - - double pixel_w = sprite.sprite_rect.w * transform.scale * scale.x; - double pixel_h = sprite.sprite_rect.h * transform.scale * scale.y; + Vector2 pixel_coord = (transform.position - cam.pos) * cam.scale; + double pixel_w = sprite.sprite_rect.w * transform.scale * cam.scale.x; + double pixel_h = sprite.sprite_rect.h * transform.scale * cam.scale.y; // decides which part of the sprite will be drawn SDL_Rect srcrect = { @@ -147,7 +136,18 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const &dstrect, transform.rotation, NULL, render_flip); } -void SDLContext::camera(const Camera & cam) { +void SDLContext::camera(Camera & cam) { + + double screen_aspect = cam.screen.x / cam.screen.y; + double viewport_aspect = cam.viewport.x / cam.viewport.y; + Vector2 zoomed_viewport = cam.viewport * cam.zoom; + + if (screen_aspect > viewport_aspect) { + cam.scale.x = cam.scale.y = cam.screen.x / zoomed_viewport.x; + } else { + cam.scale.y = cam.scale.x = cam.screen.y / zoomed_viewport.y; + } + if (this->viewport.w != cam.screen.x && this->viewport.h != cam.screen.y) { this->viewport.w = cam.screen.x; @@ -155,12 +155,6 @@ void SDLContext::camera(const Camera & cam) { SDL_SetWindowSize(this->game_window.get(), cam.screen.x , cam.screen.y); } - /* - this->viewport.x = static_cast(cam.x) - (SCREEN_WIDTH / 2); - this->viewport.y = static_cast(cam.y) - (SCREEN_HEIGHT / 2); - - */ - SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g, cam.bg_color.b, cam.bg_color.a); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 007092b..b69e509 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -137,7 +137,7 @@ private: * \brief Sets the current camera for rendering. * \param camera Reference to the Camera object. */ - void camera(const Camera & camera); + void camera(Camera & camera); private: //! sdl Window diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 9d18873..ceb5bfd 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -3,8 +3,8 @@ #include #include "api/Animator.h" -#include "facade/SDLContext.h" +#include "facade/SDLContext.h" #include "AnimatorSystem.h" #include "ComponentManager.h" @@ -19,6 +19,7 @@ void AnimatorSystem::update() { uint64_t tick = SDLContext::get_instance().get_ticks(); for (Animator & a : animations) { if (a.active) { + // (10 frames per second) a.curr_row = (tick / 100) % a.row; a.animator_rect.x = (a.curr_row * a.animator_rect.w) + a.curr_col; a.spritesheet.sprite_rect = a.animator_rect; diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h index 56cc7b3..f8179a9 100644 --- a/src/crepe/system/AnimatorSystem.h +++ b/src/crepe/system/AnimatorSystem.h @@ -21,12 +21,11 @@ public: /** * \brief Updates the Animator components. * - * This method is called periodically (likely every frame) to update the state of all + * This method is called to update the state of all * Animator components, moving the animations forward and managing their behavior (e.g., * looping). */ void update() override; - // FIXME: never say "likely" in the documentation lmao }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index fa3d0de..989a82f 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -5,7 +5,6 @@ #include "../api/Sprite.h" #include "../api/Transform.h" #include "../facade/SDLContext.h" -#include "../util/Log.h" #include "RenderSystem.h" diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index 3631c76..1db57e5 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -1,4 +1,6 @@ +#include "api/Animator.h" #include "api/Camera.h" +#include "system/AnimatorSystem.h" #include #include #include @@ -22,6 +24,7 @@ int main() { ComponentManager mgr{}; RenderSystem sys{mgr}; + AnimatorSystem anim_sys {mgr}; GameObject obj = mgr.new_object("name", "tag", Vector2{250, 0}, 0, 1); GameObject obj1 = mgr.new_object("name", "tag", Vector2{500, 0}, 1, 0.1); @@ -30,10 +33,10 @@ int main() { // Normal adding components { Color color(0, 0, 0, 0); - obj.add_component(make_shared("../asset/texture/img.png"), color, + Sprite & sprite = obj.add_component(make_shared("../asset/spritesheet/spritesheet_test.png"), color, FlipSettings{false, false}); Camera & cam = obj.add_component(Color::get_red()); - + obj.add_component(sprite, 4,1,1).active = true; } /* { @@ -51,8 +54,10 @@ int main() { } */ + auto start = std::chrono::steady_clock::now(); while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { + anim_sys.update(); sys.update(); } } -- cgit v1.2.3 From bdc81e355e5bee5d2a3e29346ba08f7bc55196ca Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 20 Nov 2024 12:07:07 +0100 Subject: adjusted branch based on feedback --- mwe/events/include/event.h | 2 +- src/crepe/api/Animator.cpp | 2 +- src/crepe/api/Camera.h | 18 +++++------------- src/crepe/api/Vector2.cpp | 4 +++- src/crepe/api/Vector2.h | 2 ++ src/crepe/facade/SDLContext.cpp | 7 ++----- src/crepe/system/AnimatorSystem.cpp | 2 +- src/crepe/system/RenderSystem.cpp | 1 + src/example/rendering.cpp | 10 +++++----- 9 files changed, 21 insertions(+), 27 deletions(-) (limited to 'src/crepe') diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index ee1bf52..e1b220b 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -148,7 +148,7 @@ private: }; class ShutDownEvent : public Event { public: - ShutDownEvent() : Event("ShutDownEvent") {}; + ShutDownEvent() : Event("ShutDownEvent"){}; REGISTER_EVENT_TYPE(ShutDownEvent) diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index f3d809c..d206428 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -18,7 +18,7 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a animator_rect.h /= col; animator_rect.w /= row; animator_rect.x = 0; - animator_rect.y = (col_animator - 1)* animator_rect.h; + animator_rect.y = (col_animator - 1) * animator_rect.h; this->active = false; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index dfccd24..d7292ef 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -28,25 +28,17 @@ public: //! Background color of the camera view. Color bg_color; - /** - * \pos The position of the camera in world units - */ + //! pos The position of the camera in world units Vector2 pos = {0, 0}; - /** - * \screen the display size in pixels ( output resolution ) - */ + //! screen the display size in pixels ( output resolution ) Vector2 screen = {640, 480}; - /** - * \viewport is the area of the world visible through the camera (in world units) - */ + //! viewport is the area of the world visible through the camera (in world units) Vector2 viewport = {500, 500}; - /** - * \scale scaling factor from world units to pixel coordinates - */ - Vector2 scale = {0,0}; + //! scale scaling factor from world units to pixel coordinates + Vector2 scale = {0, 0}; //! Zoom level of the camera view. double zoom = 1.0f; diff --git a/src/crepe/api/Vector2.cpp b/src/crepe/api/Vector2.cpp index c3a49b7..8658c00 100644 --- a/src/crepe/api/Vector2.cpp +++ b/src/crepe/api/Vector2.cpp @@ -8,14 +8,16 @@ Vector2 Vector2::operator+(const Vector2 & other) const { return {x + other.x, y Vector2 Vector2::operator*(double scalar) const { return {x * scalar, y * scalar}; } -Vector2 Vector2::operator*(const Vector2 & other) const{ +Vector2 Vector2::operator*(const Vector2 & other) const { return {this->x * other.x, this->y * other.y}; } + Vector2 & Vector2::operator*=(const Vector2 & other) { x *= other.x; y *= other.y; return *this; } + Vector2 & Vector2::operator*=(const double & other) { x *= other; y *= other; diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h index 2a5db1d..790160d 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -18,6 +18,7 @@ struct Vector2 { //! Multiplies this vector by a scalar and returns the result. Vector2 operator*(double scalar) const; + //! Multiplies this vector by another vector element-wise and updates this vector. Vector2 operator*(const Vector2 & other) const; //! Multiplies this vector by another vector element-wise and updates this vector. @@ -26,6 +27,7 @@ struct Vector2 { //! Multiplies a scalar value to both components of this vector and updates this vector. Vector2 & operator*=(const double & other); + //! Divides this vector by another vector element-wise and updates this vector. Vector2 operator/(const Vector2 & other) const; //! Adds another vector to this vector and updates this vector. diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 785b285..0cb7be9 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -38,7 +38,7 @@ SDLContext::SDLContext() { } SDL_Window * tmp_window = SDL_CreateWindow("Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - this->viewport.w, this->viewport.h, SDL_WINDOW_RESIZABLE); + this->viewport.w, this->viewport.h, 0); if (!tmp_window) { // FIXME: throw exception std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl; @@ -104,13 +104,11 @@ void SDLContext::handle_events(bool & running) { void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); } void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } - void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); - Vector2 pixel_coord = (transform.position - cam.pos) * cam.scale; double pixel_w = sprite.sprite_rect.w * transform.scale * cam.scale.x; @@ -148,11 +146,10 @@ void SDLContext::camera(Camera & cam) { cam.scale.y = cam.scale.x = cam.screen.y / zoomed_viewport.y; } - if (this->viewport.w != cam.screen.x && this->viewport.h != cam.screen.y) { this->viewport.w = cam.screen.x; this->viewport.h = cam.screen.y; - SDL_SetWindowSize(this->game_window.get(), cam.screen.x , cam.screen.y); + SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y); } SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g, diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index ceb5bfd..efe0e48 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -3,8 +3,8 @@ #include #include "api/Animator.h" - #include "facade/SDLContext.h" + #include "AnimatorSystem.h" #include "ComponentManager.h" diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 989a82f..7538e20 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -13,6 +13,7 @@ using namespace crepe; void RenderSystem::clear_screen() const { SDLContext::get_instance().clear_screen(); } void RenderSystem::present_screen() const { SDLContext::get_instance().present_screen(); } + void RenderSystem::update_camera() { ComponentManager & mgr = this->component_manager; diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index 1db57e5..418e03d 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -24,7 +24,7 @@ int main() { ComponentManager mgr{}; RenderSystem sys{mgr}; - AnimatorSystem anim_sys {mgr}; + AnimatorSystem anim_sys{mgr}; GameObject obj = mgr.new_object("name", "tag", Vector2{250, 0}, 0, 1); GameObject obj1 = mgr.new_object("name", "tag", Vector2{500, 0}, 1, 0.1); @@ -33,10 +33,11 @@ int main() { // Normal adding components { Color color(0, 0, 0, 0); - Sprite & sprite = obj.add_component(make_shared("../asset/spritesheet/spritesheet_test.png"), color, - FlipSettings{false, false}); + Sprite & sprite = obj.add_component( + make_shared("../asset/spritesheet/spritesheet_test.png"), color, + FlipSettings{false, false}); Camera & cam = obj.add_component(Color::get_red()); - obj.add_component(sprite, 4,1,1).active = true; + obj.add_component(sprite, 4, 1, 1).active = true; } /* { @@ -54,7 +55,6 @@ int main() { } */ - auto start = std::chrono::steady_clock::now(); while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { anim_sys.update(); -- cgit v1.2.3 From 08fee6eff251337dd4dca2b514a9c06d57ede0af Mon Sep 17 00:00:00 2001 From: max-001 Date: Fri, 22 Nov 2024 11:14:13 +0100 Subject: Implemented persistent objects --- src/crepe/ComponentManager.cpp | 26 +++++++++++++++- src/crepe/ComponentManager.h | 12 ++++++++ src/crepe/ComponentManager.hpp | 13 +++++++- src/crepe/api/GameObject.cpp | 6 ++++ src/crepe/api/GameObject.h | 7 +++++ src/test/ECSTest.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 130 insertions(+), 2 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index e310577..97df1f3 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -10,6 +10,11 @@ ComponentManager::ComponentManager() { dbg_trace(); } ComponentManager::~ComponentManager() { dbg_trace(); } void ComponentManager::delete_all_components_of_id(game_object_id_t id) { + // Do not delete persistent objects + if (this->persistent[id]) { + return; + } + // Loop through all the types (in the unordered_map<>) for (auto & [type, componentArray] : this->components) { // Make sure that the id (that we are looking for) is within the boundaries of the vector<> @@ -21,14 +26,33 @@ void ComponentManager::delete_all_components_of_id(game_object_id_t id) { } void ComponentManager::delete_all_components() { - this->components.clear(); + // Loop through all the ids and delete all components of each id + for (game_object_id_t id = 0; id < next_id; id++) { + delete_all_components_of_id(id); + } this->next_id = 0; } GameObject ComponentManager::new_object(const string & name, const string & tag, const Vector2 & position, double rotation, double scale) { + // Find the first available id (taking persistent objects into account) + while (this->persistent[this->next_id]) { + this->next_id++; + // Make sure that the persistent vector is large enough + if (persistent.size() <= next_id) { + this->persistent.resize(next_id + 1, false); + } + } + GameObject object{*this, this->next_id, name, tag, position, rotation, scale}; this->next_id++; + + // Make sure that the persistent vector is large enough + if (persistent.size() <= next_id) { + this->persistent.resize(next_id + 1, false); + } return object; } + +void ComponentManager::set_persistent(game_object_id_t id) { this->persistent[id] = true; } diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 0956d1e..8fc7d6c 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -101,6 +101,15 @@ protected: * This method deletes all components. */ void delete_all_components(); + /** + * \brief Set a GameObject as persistent + * + * This method sets a GameObject as persistent. If a GameObject is persistent, its + * components will not be deleted. + * + * \param id The id of the GameObject to set as persistent + */ + void set_persistent(game_object_id_t id); public: /** @@ -139,6 +148,9 @@ private: std::unordered_map>>> components; + //! Persistent flag for each GameObject + std::vector persistent = {false}; + //! ID of next GameObject allocated by \c ComponentManager::new_object game_object_id_t next_id = 0; }; diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 4d5eaf4..ffb38ec 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -54,6 +54,11 @@ template void ComponentManager::delete_components_by_id(game_object_id_t id) { using namespace std; + // Do not delete persistent objects + if (this->persistent[id]) { + return; + } + // Determine the type of T (this is used as the key of the unordered_map<>) type_index type = typeid(T); @@ -77,7 +82,13 @@ void ComponentManager::delete_components() { if (this->components.find(type) == this->components.end()) return; - this->components[type].clear(); + // Loop through the whole vector<> of this specific type + for (game_object_id_t i = 0; i < this->components[type].size(); ++i) { + // Do not delete persistent objects + if (!this->persistent[i]) { + this->components[type][i].clear(); + } + } } template diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index 4874426..7c873ec 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -30,3 +30,9 @@ void GameObject::set_parent(const GameObject & parent) { RefVector parent_metadata = mgr.get_components_by_id(parent.id); parent_metadata.at(0).get().children.push_back(this->id); } + +void GameObject::set_persistent() { + ComponentManager & mgr = this->component_manager; + + mgr.set_persistent(this->id); +} diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index 34ef8bb..07d5ad2 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -59,6 +59,13 @@ public: */ template T & add_component(Args &&... args); + /** + * \brief Components will not be deleted if this method is called + * + * This method sets the persistent flag of the GameObject to true. If the persistent + * flag is set to true, the GameObject will not be deleted when the scene is changed. + */ + void set_persistent(); public: //! The id of the GameObject diff --git a/src/test/ECSTest.cpp b/src/test/ECSTest.cpp index d5a5826..e1585bb 100644 --- a/src/test/ECSTest.cpp +++ b/src/test/ECSTest.cpp @@ -234,3 +234,71 @@ TEST_F(ECSTest, partentChild) { EXPECT_EQ(metadata[1].get().children[0], 3); EXPECT_EQ(metadata[2].get().children[0], 4); } + +TEST_F(ECSTest, persistent) { + GameObject obj0 = mgr.new_object("obj0", "obj0", Vector2{0, 0}, 0, 1); + GameObject obj1 = mgr.new_object("obj1", "obj1", Vector2{0, 0}, 0, 1); + obj1.set_persistent(); + GameObject obj2 = mgr.new_object("obj2", "obj2", Vector2{0, 0}, 0, 1); + + vector> metadata = mgr.get_components_by_type(); + vector> transform = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 3); + EXPECT_EQ(transform.size(), 3); + + mgr.delete_components_by_id(1); + mgr.delete_components(); + mgr.delete_all_components_of_id(1); + + metadata = mgr.get_components_by_type(); + transform = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 1); + EXPECT_EQ(transform.size(), 3); + + mgr.delete_all_components(); + + metadata = mgr.get_components_by_type(); + transform = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 1); + EXPECT_EQ(transform.size(), 1); + + EXPECT_EQ(metadata[0].get().game_object_id, 1); + EXPECT_EQ(metadata[0].get().name, "obj1"); + EXPECT_EQ(metadata[0].get().tag, "obj1"); + EXPECT_EQ(metadata[0].get().parent, -1); + EXPECT_EQ(metadata[0].get().children.size(), 0); + + EXPECT_EQ(transform[0].get().game_object_id, 1); + EXPECT_EQ(transform[0].get().position.x, 0); + EXPECT_EQ(transform[0].get().position.y, 0); + + GameObject obj3 = mgr.new_object("obj3", "obj3", Vector2{0, 0}, 0, 5); + GameObject obj4 = mgr.new_object("obj4", "obj4", Vector2{0, 0}, 0, 5); + + metadata = mgr.get_components_by_type(); + transform = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 3); + EXPECT_EQ(transform.size(), 3); + + EXPECT_EQ(metadata[0].get().game_object_id, 0); + EXPECT_EQ(metadata[0].get().name, "obj3"); + + EXPECT_EQ(metadata[1].get().game_object_id, 1); + EXPECT_EQ(metadata[1].get().name, "obj1"); + + EXPECT_EQ(metadata[2].get().game_object_id, 2); + EXPECT_EQ(metadata[2].get().name, "obj4"); + + EXPECT_EQ(transform[0].get().game_object_id, 0); + EXPECT_EQ(transform[0].get().scale, 5); + + EXPECT_EQ(transform[1].get().game_object_id, 1); + EXPECT_EQ(transform[1].get().scale, 1); + + EXPECT_EQ(transform[2].get().game_object_id, 2); + EXPECT_EQ(transform[2].get().scale, 5); +} -- cgit v1.2.3 From 385f19a8c896ec126c569f1e5337d6d370d20517 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 22 Nov 2024 13:54:13 +0100 Subject: working aspect ratio in world units. and add pillarboxing/letterboxing depending on ratio --- src/crepe/api/Camera.h | 4 +- src/crepe/api/Color.cpp | 2 +- src/crepe/facade/SDLContext.cpp | 98 ++++++++++++++++++++++++++++---------- src/crepe/facade/SDLContext.h | 4 +- src/example/rendering_particle.cpp | 12 +++-- 5 files changed, 87 insertions(+), 33 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index d7292ef..c42ed0d 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -32,10 +32,10 @@ public: Vector2 pos = {0, 0}; //! screen the display size in pixels ( output resolution ) - Vector2 screen = {640, 480}; + Vector2 screen = {1080, 720}; //! viewport is the area of the world visible through the camera (in world units) - Vector2 viewport = {500, 500}; + Vector2 viewport = {2000, 1000}; //! scale scaling factor from world units to pixel coordinates Vector2 scale = {0, 0}; diff --git a/src/crepe/api/Color.cpp b/src/crepe/api/Color.cpp index 29bd77a..dc7c15f 100644 --- a/src/crepe/api/Color.cpp +++ b/src/crepe/api/Color.cpp @@ -2,7 +2,7 @@ using namespace crepe; -const Color Color::WHITE{0xff, 0xff, 0xff}; +const Color Color::WHITE{0xff, 0xff, 0xff, 0xff}; const Color Color::RED{0xff, 0x00, 0x00}; const Color Color::GREEN{0x00, 0xff, 0x00}; const Color Color::BLUE{0x00, 0x00, 0xff}; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 74af25f..e187d67 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -5,12 +5,12 @@ #include #include #include +#include #include #include #include #include #include -#include #include "../api/Camera.h" #include "../api/Sprite.h" @@ -94,7 +94,10 @@ void SDLContext::handle_events(bool & running) { */ } -void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); } +void SDLContext::clear_screen() { + SDL_SetRenderDrawColor(this->game_renderer.get(), 0, 0, 0, 255); + SDL_RenderClear(this->game_renderer.get()); +} void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { @@ -108,20 +111,11 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, const double & scale, const Camera & cam) const { - Vector2 pixel_coord = (transform.position - cam.pos) * cam.scale; - double pixel_w = sprite.sprite_rect.w * transform.scale * cam.scale.x; - double pixel_h = sprite.sprite_rect.h * transform.scale * cam.scale.y; - - double adjusted_x = (pos.x - cam.x) * cam.zoom; - double adjusted_y = (pos.y - cam.y) * cam.zoom; - double adjusted_w = sprite.sprite_rect.w * scale * cam.zoom; - double adjusted_h = sprite.sprite_rect.h * scale * cam.zoom; - return SDL_Rect{ - .x = static_cast(adjusted_x), - .y = static_cast(adjusted_y), - .w = static_cast(adjusted_w), - .h = static_cast(adjusted_h), + .x = static_cast(sprite.sprite_rect.x - 400 / 2), + .y = static_cast(sprite.sprite_rect.y - 300 / 2), + .w = static_cast(400), + .h = static_cast(300), }; } @@ -153,27 +147,81 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const &dstrect, transform.rotation, NULL, render_flip); } -void SDLContext::set_camera(const Camera & cam) { +void SDLContext::set_camera(Camera & cam) { + if (this->viewport.w != (int)cam.screen.x && this->viewport.h != (int)cam.screen.y) { + SDL_SetWindowSize(this->game_window.get(), (int)cam.screen.x, (int)cam.screen.y); + this->viewport.h = cam.screen.y; + this->viewport.w = cam.screen.x; + } double screen_aspect = cam.screen.x / cam.screen.y; double viewport_aspect = cam.viewport.x / cam.viewport.y; - Vector2 zoomed_viewport = cam.viewport * cam.zoom; + + SDL_Rect view; if (screen_aspect > viewport_aspect) { - cam.scale.x = cam.scale.y = cam.screen.x / zoomed_viewport.x; + view.h = static_cast(cam.screen.y); + view.w = static_cast(cam.screen.y * viewport_aspect); + view.x = static_cast(cam.screen.x - view.w) / 2; + view.y = 0; } else { - cam.scale.y = cam.scale.x = cam.screen.y / zoomed_viewport.y; + view.w = static_cast(cam.screen.x); + view.h = static_cast(cam.screen.x / viewport_aspect); + view.x = 0; + view.y = static_cast(cam.screen.y - view.h) / 2; } + SDL_RenderSetViewport(this->game_renderer.get(), &view); - if (this->viewport.w != cam.screen.x && this->viewport.h != cam.screen.y) { - this->viewport.w = cam.screen.x; - this->viewport.h = cam.screen.y; - SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y); + SDL_RenderSetLogicalSize(this->game_renderer.get(), cam.viewport.x, cam.viewport.y); + SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g, + cam.bg_color.b, cam.bg_color.a); + SDL_Rect bg = { + .x = 0, + .y = 0, + .w = static_cast(cam.viewport.x), + .h = static_cast(cam.viewport.y), + }; + SDL_RenderFillRect(this->game_renderer.get(), &bg); + + /* + float offset_x = 0, offset_y = 0; + + + double scale_factor = min(cam.screen.x / cam.viewport.x, cam.screen.y / cam.viewport.y); + cam.scale.x = scale_factor * cam.viewport.x; + cam.scale.y = scale_factor * cam.viewport.y; + + offset_x = (cam.screen.x - cam.scale.x) / 2; + offset_y = (cam.screen.y - cam.scale.y) / 2; + + float bar_w = cam.screen.x - cam.scale.x; + float bar_h = cam.screen.y - cam.scale.y; + + SDL_SetRenderDrawColor(this->game_renderer.get(), 0, 0, 0, 255); + if (bar_w > 0) { + SDL_Rect left_bar = {0, 0, static_cast(offset_x), static_cast(cam.screen.y)}; + SDL_RenderDrawRect(this->game_renderer.get(), &left_bar); + + SDL_Rect right_bar = {static_cast(offset_x + cam.scale.x), 0, + static_cast(offset_x), static_cast(cam.screen.y)}; + SDL_RenderDrawRect(this->game_renderer.get(), &right_bar); } - SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g, - cam.bg_color.b, cam.bg_color.a); + if (screen_aspect > viewport_aspect) { + // pillarboxing + cam.scale.x = cam.scale.y = cam.screen.x / cam.viewport.x; + offset_y = (cam.screen.y - (cam.viewport.y * cam.scale.y)) / 2; + } else if (screen_aspect < viewport_aspect) { + // lettor boxing + cam.scale.y = cam.scale.x = cam.screen.y / zoomed_viewport.y; + offset_x = (cam.screen.x - (cam.viewport.x * cam.scale.x)) / 2; + } else { + // screen ration is even + offset_y = (cam.screen.y - (cam.viewport.y * cam.scale.y)) / 2; + offset_x = (cam.screen.x - (cam.viewport.x * cam.scale.x)) / 2; + } + */ } uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 841ffc9..45bbda6 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -133,7 +133,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); + void set_camera(Camera & camera); private: /** @@ -164,7 +164,7 @@ private: std::unique_ptr> game_renderer; //! viewport for the camera window - SDL_Rect viewport = {0, 0, 640, 480}; + SDL_Rect viewport = {0, 0, 1280, 720}; }; } // namespace crepe diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 4571afb..96ef3df 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -23,16 +23,18 @@ using namespace std; int main(int argc, char * argv[]) { ComponentManager mgr; - GameObject game_object = mgr.new_object("", "", Vector2{100, 100}, 0, 0.1); + GameObject game_object = mgr.new_object("", "", Vector2{400, 300}, 0, 0.1); RenderSystem sys{mgr}; ParticleSystem psys{mgr}; Color color(255, 255, 255, 255); Sprite & test_sprite = game_object.add_component( - make_shared("../asset/texture/img.png"), color, FlipSettings{false, false}); + make_shared("asset/texture/test_ap43.png"), color, FlipSettings{false, false}); test_sprite.order_in_layer = 5; + + /* auto & test = game_object.add_component(ParticleEmitter::Data{ .position = {0, 0}, .max_particles = 10, @@ -52,13 +54,17 @@ int main(int argc, char * argv[]) { }, .sprite = test_sprite, }); + */ + game_object.add_component(Color::WHITE); + /* game_object - .add_component(make_shared("../asset/texture/img.png"), color, + .add_component(make_shared("asset/texture/img.png"), color, FlipSettings{false, false}) .order_in_layer = 6; + */ auto start = std::chrono::steady_clock::now(); while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { -- cgit v1.2.3 From 4ce924b1b1322ee4da3ba50d6da856ad13a2190b Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 22 Nov 2024 16:16:27 +0100 Subject: working scaling image with scaling world to screen --- src/crepe/api/Camera.h | 4 +- src/crepe/api/Sprite.cpp | 4 +- src/crepe/api/Sprite.h | 8 ++++ src/crepe/api/Vector2.cpp | 4 ++ src/crepe/api/Vector2.h | 3 ++ src/crepe/facade/SDLContext.cpp | 86 ++++++++++++++------------------------ src/crepe/facade/SDLContext.h | 10 ++--- src/crepe/system/RenderSystem.cpp | 7 ++-- src/crepe/system/RenderSystem.h | 5 +++ src/example/rendering_particle.cpp | 4 +- 10 files changed, 65 insertions(+), 70 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index c42ed0d..083dc19 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -38,10 +38,8 @@ public: Vector2 viewport = {2000, 1000}; //! scale scaling factor from world units to pixel coordinates - Vector2 scale = {0, 0}; - //! Zoom level of the camera view. - double zoom = 1.0f; + double zoom = 1.5f; public: /** diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index bd2d5cf..3853aab 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -15,7 +15,9 @@ Sprite::Sprite(game_object_id_t id, const shared_ptr image, const Color : Component(id), color(color), flip(flip), - sprite_image(image) { + sprite_image(image), + aspect_ratio(sprite_image->get_width() / sprite_image->get_height()) +{ dbg_trace(); this->sprite_rect.w = sprite_image->get_width(); diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 74a55d4..66599c9 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "../Component.h" @@ -62,6 +63,13 @@ public: //! Order within the sorting layer uint8_t order_in_layer = 0; + //! width in world units + int width = 0; + //! height in world units + int height = 0; + + const double aspect_ratio; + public: /** * \brief Gets the maximum number of instances allowed for this sprite. diff --git a/src/crepe/api/Vector2.cpp b/src/crepe/api/Vector2.cpp index 8658c00..c8253d7 100644 --- a/src/crepe/api/Vector2.cpp +++ b/src/crepe/api/Vector2.cpp @@ -40,6 +40,10 @@ Vector2 Vector2::operator/(const Vector2 & other) const { return {this->x / other.x, this->y / other.y}; } +Vector2 Vector2::operator/(const double & other) const { + return {this->x / other, this->y / other}; +} + Vector2 Vector2::operator-() const { return {-x, -y}; } bool Vector2::operator==(const Vector2 & other) const { return x == other.x && y == other.y; } diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h index 790160d..5a23699 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -30,6 +30,9 @@ struct Vector2 { //! Divides this vector by another vector element-wise and updates this vector. Vector2 operator/(const Vector2 & other) const; + //! Divides a scalar value to both components of this vector and updates this vector. + Vector2 operator/(const double & other) const; + //! Adds another vector to this vector and updates this vector. Vector2 & operator+=(const Vector2 & other); diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index e187d67..f49539c 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -109,48 +110,60 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { }; } SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, - const double & scale, const Camera & cam) const { + const Vector2 & scale) const { + + int pixel_width, pixel_height; + + if (sprite.sprite_rect.w > sprite.sprite_rect.h) { + pixel_width = static_cast(sprite.width * scale.x); + pixel_height = static_cast(pixel_width / sprite.aspect_ratio); + } else { + pixel_height = static_cast(sprite.height * scale.y); + pixel_width = static_cast(pixel_height * sprite.aspect_ratio); + } + + int pixel_x = static_cast((pos.x - pixel_width / 2)); + int pixel_y = static_cast((pos.y - pixel_height / 2)); return SDL_Rect{ - .x = static_cast(sprite.sprite_rect.x - 400 / 2), - .y = static_cast(sprite.sprite_rect.y - 300 / 2), - .w = static_cast(400), - .h = static_cast(300), + .x = pixel_x, + .y = pixel_y, + .w = pixel_width, + .h = pixel_height, }; } void SDLContext::draw_particle(const Sprite & sprite, const Vector2 & pos, - const double & angle, const double & scale, - const Camera & camera) { + const double & angle, const Vector2 & scale) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, pos, scale, camera); + SDL_Rect dstrect = this->get_dst_rect(sprite, pos, scale); SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, &dstrect, angle, NULL, render_flip); } -void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam) { +void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Vector2 & scale) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, transform.scale, cam); + SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, scale); SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, &dstrect, transform.rotation, NULL, render_flip); } -void SDLContext::set_camera(Camera & cam) { +void SDLContext::set_camera(const Camera & cam, Vector2 & scale) { - if (this->viewport.w != (int)cam.screen.x && this->viewport.h != (int)cam.screen.y) { - SDL_SetWindowSize(this->game_window.get(), (int)cam.screen.x, (int)cam.screen.y); + if (this->viewport.w != (int) cam.screen.x && this->viewport.h != (int) cam.screen.y) { + SDL_SetWindowSize(this->game_window.get(), (int) cam.screen.x, (int) cam.screen.y); this->viewport.h = cam.screen.y; this->viewport.w = cam.screen.x; } @@ -158,16 +171,18 @@ void SDLContext::set_camera(Camera & cam) { double screen_aspect = cam.screen.x / cam.screen.y; double viewport_aspect = cam.viewport.x / cam.viewport.y; + scale = cam.screen / cam.viewport * cam.zoom; + SDL_Rect view; if (screen_aspect > viewport_aspect) { - view.h = static_cast(cam.screen.y); + view.h = static_cast(cam.screen.y / cam.zoom); view.w = static_cast(cam.screen.y * viewport_aspect); view.x = static_cast(cam.screen.x - view.w) / 2; view.y = 0; } else { - view.w = static_cast(cam.screen.x); view.h = static_cast(cam.screen.x / viewport_aspect); + view.w = static_cast(cam.screen.x / cam.zoom); view.x = 0; view.y = static_cast(cam.screen.y - view.h) / 2; } @@ -175,7 +190,7 @@ void SDLContext::set_camera(Camera & cam) { SDL_RenderSetLogicalSize(this->game_renderer.get(), cam.viewport.x, cam.viewport.y); SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g, - cam.bg_color.b, cam.bg_color.a); + cam.bg_color.b, cam.bg_color.a); SDL_Rect bg = { .x = 0, .y = 0, @@ -183,45 +198,6 @@ void SDLContext::set_camera(Camera & cam) { .h = static_cast(cam.viewport.y), }; SDL_RenderFillRect(this->game_renderer.get(), &bg); - - /* - float offset_x = 0, offset_y = 0; - - - double scale_factor = min(cam.screen.x / cam.viewport.x, cam.screen.y / cam.viewport.y); - cam.scale.x = scale_factor * cam.viewport.x; - cam.scale.y = scale_factor * cam.viewport.y; - - offset_x = (cam.screen.x - cam.scale.x) / 2; - offset_y = (cam.screen.y - cam.scale.y) / 2; - - float bar_w = cam.screen.x - cam.scale.x; - float bar_h = cam.screen.y - cam.scale.y; - - SDL_SetRenderDrawColor(this->game_renderer.get(), 0, 0, 0, 255); - if (bar_w > 0) { - SDL_Rect left_bar = {0, 0, static_cast(offset_x), static_cast(cam.screen.y)}; - SDL_RenderDrawRect(this->game_renderer.get(), &left_bar); - - SDL_Rect right_bar = {static_cast(offset_x + cam.scale.x), 0, - static_cast(offset_x), static_cast(cam.screen.y)}; - SDL_RenderDrawRect(this->game_renderer.get(), &right_bar); - } - - if (screen_aspect > viewport_aspect) { - // pillarboxing - cam.scale.x = cam.scale.y = cam.screen.x / cam.viewport.x; - offset_y = (cam.screen.y - (cam.viewport.y * cam.scale.y)) / 2; - } else if (screen_aspect < viewport_aspect) { - // lettor boxing - cam.scale.y = cam.scale.x = cam.screen.y / zoomed_viewport.y; - offset_x = (cam.screen.x - (cam.viewport.x * cam.scale.x)) / 2; - } else { - // screen ration is even - offset_y = (cam.screen.y - (cam.viewport.y * cam.scale.y)) / 2; - offset_x = (cam.screen.x - (cam.viewport.x * cam.scale.x)) / 2; - } - */ } uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 45bbda6..68d1630 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -118,10 +118,9 @@ private: * \param transform Reference to the Transform for positioning. * \param camera Reference to the Camera for view adjustments. */ - void draw(const Sprite & sprite, const Transform & transform, const Camera & camera); + void draw(const Sprite & sprite, const Transform & transform, const Vector2 & scale); - void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, - const double & scale, const Camera & camera); + void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, const Vector2 & scale); //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -133,7 +132,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(Camera & camera); + void set_camera(const Camera & camera, Vector2 & scale); private: /** @@ -153,8 +152,7 @@ private: * on the camera * \return sdl rectangle to draw a dst image to draw on the screen */ - SDL_Rect get_dst_rect(const Sprite & sprite, const Vector2 & pos, const double & scale, - const Camera & cam) const; + SDL_Rect get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & scale) const; private: //! sdl Window diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index ad510f5..a16fbb5 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -30,7 +30,7 @@ void RenderSystem::update_camera() { for (Camera & cam : cameras) { if (!cam.active) continue; - this->context.set_camera(cam); + this->context.set_camera(cam, this->scale); this->curr_cam_ref = &cam; } } @@ -72,14 +72,13 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) for (const Particle & p : em.data.particles) { if (!p.active) continue; - this->context.draw_particle(sprite, p.position, p.angle, scale, - *this->curr_cam_ref); + this->context.draw_particle(sprite, p.position, p.angle, this->scale * scale); } } return rendering_particles; } void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { - this->context.draw(sprite, tm, *this->curr_cam_ref); + this->context.draw(sprite, tm, this->scale * tm.scale); } void RenderSystem::render() { diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 30b41cf..19edc02 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -3,6 +3,7 @@ #include #include +#include "api/Vector2.h" #include "facade/SDLContext.h" #include "System.h" @@ -80,8 +81,12 @@ private: //! Pointer to the current active camera for rendering Camera * curr_cam_ref = nullptr; // TODO: needs a better solution + + Vector2 scale; SDLContext & context = SDLContext::get_instance(); + + }; } // namespace crepe diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 96ef3df..741c985 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -23,7 +23,7 @@ using namespace std; int main(int argc, char * argv[]) { ComponentManager mgr; - GameObject game_object = mgr.new_object("", "", Vector2{400, 300}, 0, 0.1); + GameObject game_object = mgr.new_object("", "", Vector2{1000, 500}, 0, 2); RenderSystem sys{mgr}; ParticleSystem psys{mgr}; @@ -32,6 +32,8 @@ int main(int argc, char * argv[]) { Sprite & test_sprite = game_object.add_component( make_shared("asset/texture/test_ap43.png"), color, FlipSettings{false, false}); test_sprite.order_in_layer = 5; + test_sprite.width = 1000; + test_sprite.height = 500; /* -- cgit v1.2.3 From 8513b2dad0ec43678f17cca0510db4d1a938279a Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 22 Nov 2024 17:08:55 +0100 Subject: working moving camera, flip animations, resizing with animations, and black bars --- src/crepe/api/Animator.cpp | 5 ++++- src/crepe/api/Sprite.h | 10 ++++++++-- src/crepe/facade/SDLContext.cpp | 25 +++++++++++++------------ src/crepe/facade/SDLContext.h | 6 +++--- src/crepe/system/RenderSystem.cpp | 4 ++-- src/example/rendering_particle.cpp | 11 ++++++++--- 6 files changed, 38 insertions(+), 23 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index d206428..178b165 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -18,7 +18,10 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a animator_rect.h /= col; animator_rect.w /= row; animator_rect.x = 0; - animator_rect.y = (col_animator - 1) * animator_rect.h; + animator_rect.y = col_animator * animator_rect.h; this->active = false; + + // need to do this for to get the aspect ratio for a single clipping in the spritesheet + this->spritesheet.aspect_ratio = (double)animator_rect.w / (double)animator_rect.h; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 66599c9..89f9121 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -68,7 +68,13 @@ public: //! height in world units int height = 0; - const 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 + */ + double aspect_ratio; public: /** @@ -90,7 +96,7 @@ private: friend class AnimatorSystem; //! Render area of the sprite this will also be adjusted by the AnimatorSystem if an Animator - // object is present in GameObject + // object is present in GameObject. this is in sprite pixels Rect sprite_rect; }; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index f49539c..29a8195 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -5,11 +5,9 @@ #include #include #include -#include #include #include #include -#include #include #include @@ -109,21 +107,24 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { .h = sprite.sprite_rect.h, }; } -SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, - const Vector2 & scale) const { +SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & cam_pos, + const double & img_scale, const Vector2 & cam_scale) const { int pixel_width, pixel_height; if (sprite.sprite_rect.w > sprite.sprite_rect.h) { - pixel_width = static_cast(sprite.width * scale.x); + pixel_width = static_cast(sprite.width * cam_scale.x); pixel_height = static_cast(pixel_width / sprite.aspect_ratio); } else { - pixel_height = static_cast(sprite.height * scale.y); + pixel_height = static_cast(sprite.height * cam_scale.y); pixel_width = static_cast(pixel_height * sprite.aspect_ratio); } - int pixel_x = static_cast((pos.x - pixel_width / 2)); - int pixel_y = static_cast((pos.y - pixel_height / 2)); + pixel_width *= img_scale; + pixel_height *= img_scale; + + int pixel_x = static_cast((pos.x - cam_pos.x - pixel_width / 2)); + int pixel_y = static_cast((pos.y - cam_pos.y - pixel_height / 2)); return SDL_Rect{ .x = pixel_x, @@ -134,27 +135,27 @@ SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, } void SDLContext::draw_particle(const Sprite & sprite, const Vector2 & pos, - const double & angle, const Vector2 & scale) { + const double & angle, const Vector2 & cam_pos, const double & img_scale, const Vector2 & cam_scale) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, pos, scale); + SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam_pos, img_scale, cam_scale); SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, &dstrect, angle, NULL, render_flip); } -void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Vector2 & scale) { +void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Vector2 & cam_pos, const Vector2 & cam_scale) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, scale); + SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, cam_pos, transform.scale, cam_scale); SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, &dstrect, transform.rotation, NULL, render_flip); diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 68d1630..04c60cc 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -118,9 +118,9 @@ private: * \param transform Reference to the Transform for positioning. * \param camera Reference to the Camera for view adjustments. */ - void draw(const Sprite & sprite, const Transform & transform, const Vector2 & scale); + void draw(const Sprite & sprite, const Transform & transform, const Vector2 & cam_pos, const Vector2 & cam_scale); - void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, const Vector2 & scale); + void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, const Vector2 & cam_pos, const double & img_scale, const Vector2 & cam_scale); //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -152,7 +152,7 @@ private: * on the camera * \return sdl rectangle to draw a dst image to draw on the screen */ - SDL_Rect get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & scale) const; + SDL_Rect get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & cam_pos, const double & img_scale , const Vector2 & scale) const; private: //! sdl Window diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index a16fbb5..676ded6 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -72,13 +72,13 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) for (const Particle & p : em.data.particles) { if (!p.active) continue; - this->context.draw_particle(sprite, p.position, p.angle, this->scale * scale); + this->context.draw_particle(sprite, p.position ,p.angle, this->curr_cam_ref->pos ,scale, this->scale); } } return rendering_particles; } void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { - this->context.draw(sprite, tm, this->scale * tm.scale); + this->context.draw(sprite, tm , this->curr_cam_ref->pos,this->scale); } void RenderSystem::render() { diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 741c985..6a91c19 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -1,4 +1,6 @@ +#include "api/Animator.h" #include "api/Camera.h" +#include "system/AnimatorSystem.h" #include "system/ParticleSystem.h" #include #include @@ -15,7 +17,6 @@ #include #include -#include #include using namespace crepe; @@ -23,18 +24,21 @@ using namespace std; int main(int argc, char * argv[]) { ComponentManager mgr; - GameObject game_object = mgr.new_object("", "", Vector2{1000, 500}, 0, 2); + GameObject game_object = mgr.new_object("", "", Vector2{1000, 500}, 0, 1); RenderSystem sys{mgr}; ParticleSystem psys{mgr}; + AnimatorSystem asys{mgr}; Color color(255, 255, 255, 255); Sprite & test_sprite = game_object.add_component( - make_shared("asset/texture/test_ap43.png"), color, FlipSettings{false, false}); + make_shared("asset/spritesheet/spritesheet_test.png"), color, FlipSettings{true, true}); test_sprite.order_in_layer = 5; test_sprite.width = 1000; test_sprite.height = 500; + game_object.add_component(test_sprite, 4,1,0).active = true; + /* auto & test = game_object.add_component(ParticleEmitter::Data{ @@ -71,6 +75,7 @@ int main(int argc, char * argv[]) { auto start = std::chrono::steady_clock::now(); while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { psys.update(); + asys.update(); sys.update(); SDL_Delay(10); } -- cgit v1.2.3 From aed904bdc2ddaf669940d922611229a171a1f220 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 22 Nov 2024 17:09:40 +0100 Subject: make format --- asset/texture/test_ap43.png | Bin 0 -> 2394 bytes src/crepe/api/Animator.cpp | 4 ++-- src/crepe/api/Sprite.cpp | 3 +-- src/crepe/facade/SDLContext.cpp | 14 +++++++++----- src/crepe/facade/SDLContext.h | 10 +++++++--- src/crepe/system/RenderSystem.cpp | 5 +++-- src/crepe/system/RenderSystem.h | 6 ++---- src/example/rendering_particle.cpp | 6 +++--- 8 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 asset/texture/test_ap43.png (limited to 'src/crepe') diff --git a/asset/texture/test_ap43.png b/asset/texture/test_ap43.png new file mode 100644 index 0000000..e758ed7 Binary files /dev/null and b/asset/texture/test_ap43.png differ diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 178b165..0043896 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -18,10 +18,10 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a animator_rect.h /= col; animator_rect.w /= row; animator_rect.x = 0; - animator_rect.y = col_animator * animator_rect.h; + animator_rect.y = col_animator * animator_rect.h; this->active = false; // need to do this for to get the aspect ratio for a single clipping in the spritesheet - this->spritesheet.aspect_ratio = (double)animator_rect.w / (double)animator_rect.h; + this->spritesheet.aspect_ratio = (double) animator_rect.w / (double) animator_rect.h; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 3853aab..c219dd0 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -16,8 +16,7 @@ Sprite::Sprite(game_object_id_t id, const shared_ptr image, const Color color(color), flip(flip), sprite_image(image), - aspect_ratio(sprite_image->get_width() / sprite_image->get_height()) -{ + aspect_ratio(sprite_image->get_width() / sprite_image->get_height()) { dbg_trace(); this->sprite_rect.w = sprite_image->get_width(); diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 29a8195..4619c46 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -107,8 +107,9 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { .h = sprite.sprite_rect.h, }; } -SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & cam_pos, - const double & img_scale, const Vector2 & cam_scale) const { +SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, + const Vector2 & cam_pos, const double & img_scale, + const Vector2 & cam_scale) const { int pixel_width, pixel_height; @@ -135,7 +136,8 @@ SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, co } void SDLContext::draw_particle(const Sprite & sprite, const Vector2 & pos, - const double & angle, const Vector2 & cam_pos, const double & img_scale, const Vector2 & cam_scale) { + const double & angle, const Vector2 & cam_pos, + const double & img_scale, const Vector2 & cam_scale) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) @@ -148,14 +150,16 @@ void SDLContext::draw_particle(const Sprite & sprite, const Vector2 & pos, &dstrect, angle, NULL, render_flip); } -void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Vector2 & cam_pos, const Vector2 & cam_scale) { +void SDLContext::draw(const Sprite & sprite, const Transform & transform, + const Vector2 & cam_pos, const Vector2 & cam_scale) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, cam_pos, transform.scale, cam_scale); + SDL_Rect dstrect + = this->get_dst_rect(sprite, transform.position, cam_pos, transform.scale, cam_scale); SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, &dstrect, transform.rotation, NULL, render_flip); diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 04c60cc..4d97699 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -118,9 +118,12 @@ private: * \param transform Reference to the Transform for positioning. * \param camera Reference to the Camera for view adjustments. */ - void draw(const Sprite & sprite, const Transform & transform, const Vector2 & cam_pos, const Vector2 & cam_scale); + void draw(const Sprite & sprite, const Transform & transform, const Vector2 & cam_pos, + const Vector2 & cam_scale); - void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, const Vector2 & cam_pos, const double & img_scale, const Vector2 & cam_scale); + void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, + const Vector2 & cam_pos, const double & img_scale, + const Vector2 & cam_scale); //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -152,7 +155,8 @@ private: * on the camera * \return sdl rectangle to draw a dst image to draw on the screen */ - SDL_Rect get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & cam_pos, const double & img_scale , const Vector2 & scale) const; + SDL_Rect get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & cam_pos, + const double & img_scale, const Vector2 & scale) const; private: //! sdl Window diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 676ded6..1dd1699 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -72,13 +72,14 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) for (const Particle & p : em.data.particles) { if (!p.active) continue; - this->context.draw_particle(sprite, p.position ,p.angle, this->curr_cam_ref->pos ,scale, this->scale); + this->context.draw_particle(sprite, p.position, p.angle, this->curr_cam_ref->pos, + scale, this->scale); } } return rendering_particles; } void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { - this->context.draw(sprite, tm , this->curr_cam_ref->pos,this->scale); + this->context.draw(sprite, tm, this->curr_cam_ref->pos, this->scale); } void RenderSystem::render() { diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 19edc02..f010a83 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -81,12 +81,10 @@ private: //! Pointer to the current active camera for rendering Camera * curr_cam_ref = nullptr; // TODO: needs a better solution - - Vector2 scale; - - SDLContext & context = SDLContext::get_instance(); + Vector2 scale; + SDLContext & context = SDLContext::get_instance(); }; } // namespace crepe diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 6a91c19..fad174e 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -32,13 +32,13 @@ int main(int argc, char * argv[]) { Color color(255, 255, 255, 255); Sprite & test_sprite = game_object.add_component( - make_shared("asset/spritesheet/spritesheet_test.png"), color, FlipSettings{true, true}); + make_shared("asset/spritesheet/spritesheet_test.png"), color, + FlipSettings{true, true}); test_sprite.order_in_layer = 5; test_sprite.width = 1000; test_sprite.height = 500; - game_object.add_component(test_sprite, 4,1,0).active = true; - + game_object.add_component(test_sprite, 4, 1, 0).active = true; /* auto & test = game_object.add_component(ParticleEmitter::Data{ -- cgit v1.2.3 From b570225f5166b18fdd881fc53cd7013669067ca1 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 22 Nov 2024 17:23:33 +0100 Subject: added doxygen comments --- src/crepe/facade/SDLContext.cpp | 15 +++++++++++++-- src/crepe/facade/SDLContext.h | 27 ++++++++++++++++++++------- 2 files changed, 33 insertions(+), 9 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 4619c46..778e746 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -167,6 +167,7 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, void SDLContext::set_camera(const Camera & cam, Vector2 & scale) { + // resize window if (this->viewport.w != (int) cam.screen.x && this->viewport.h != (int) cam.screen.y) { SDL_SetWindowSize(this->game_window.get(), (int) cam.screen.x, (int) cam.screen.y); this->viewport.h = cam.screen.y; @@ -175,33 +176,43 @@ void SDLContext::set_camera(const Camera & cam, Vector2 & scale) { double screen_aspect = cam.screen.x / cam.screen.y; double viewport_aspect = cam.viewport.x / cam.viewport.y; - + + // decide scaling factor for world to screen scale = cam.screen / cam.viewport * cam.zoom; SDL_Rect view; - + + // calculate black bars if (screen_aspect > viewport_aspect) { + // lettorboxing view.h = static_cast(cam.screen.y / cam.zoom); view.w = static_cast(cam.screen.y * viewport_aspect); view.x = static_cast(cam.screen.x - view.w) / 2; view.y = 0; } else { + // pillarboxing view.h = static_cast(cam.screen.x / viewport_aspect); view.w = static_cast(cam.screen.x / cam.zoom); view.x = 0; view.y = static_cast(cam.screen.y - view.h) / 2; } + + // set drawing area SDL_RenderSetViewport(this->game_renderer.get(), &view); SDL_RenderSetLogicalSize(this->game_renderer.get(), cam.viewport.x, cam.viewport.y); + + // set bg color SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g, cam.bg_color.b, cam.bg_color.a); + SDL_Rect bg = { .x = 0, .y = 0, .w = static_cast(cam.viewport.x), .h = static_cast(cam.viewport.y), }; + // fill bg color SDL_RenderFillRect(this->game_renderer.get(), &bg); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 4d97699..03f9ec9 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -116,11 +116,22 @@ private: * \brief Draws a sprite to the screen using the specified transform and camera. * \param sprite Reference to the Sprite to draw. * \param transform Reference to the Transform for positioning. - * \param camera Reference to the Camera for view adjustments. + * \param cam_pos position of the current camera in the scene + * \param cam_scale multiplier for the world to screen */ void draw(const Sprite & sprite, const Transform & transform, const Vector2 & cam_pos, const Vector2 & cam_scale); + /** + * \brief Draws a particle to the screen using the specified parameters + * + * \param sprite Referenceto the sprite to draw + * \param pos particle position in world units + * \param angle particle angle in degrees + * \param cam_pos camera position in world units + * \param img_scale scalar multiplier to increase image size + * \param cam_scale camera scalar for world to screen + */ void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, const Vector2 & cam_pos, const double & img_scale, const Vector2 & cam_scale); @@ -145,14 +156,15 @@ private: * \return sdl rectangle to draw a src image */ SDL_Rect get_src_rect(const Sprite & sprite) const; + /** - * \brief calculates the sqaure size of the image for an destination + * \brief calculates the sqaure size of the image for destination * - * \param sprite Reference to the sprite to calculate the rectangle - * \param pos the pos in pixel positions - * \param scale the multiplier to increase of decrease for the specified sprite - * \param cam Reference to the current camera in the scene to calculate the position based - * on the camera + * \param sprite Reference to the sprite to calculate rectangle + * \param pos the pos in world units + * \param cam_pos the camera position in world units + * \param img_scale the image multiplier for increasing img size + * \param scale the multiplier for world to screen * \return sdl rectangle to draw a dst image to draw on the screen */ SDL_Rect get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & cam_pos, @@ -166,6 +178,7 @@ private: std::unique_ptr> game_renderer; //! viewport for the camera window + //todo change this so that it becomes a vec2 for only width and height SDL_Rect viewport = {0, 0, 1280, 720}; }; -- cgit v1.2.3 From b5fc0b66f7db21c2b134dbec8ce16398fd4e19fe Mon Sep 17 00:00:00 2001 From: max-001 Date: Sun, 24 Nov 2024 12:00:57 +0100 Subject: Corrected include --- src/crepe/ComponentManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/crepe') diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 2d12034..5c0ef3d 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -1,8 +1,8 @@ #include "api/GameObject.h" -#include "types.h" #include "util/Log.h" #include "ComponentManager.h" +#include "types.h" using namespace crepe; using namespace std; -- cgit v1.2.3 From 41df3ad8836ce9c686a12fa7da5b4daebe94ba05 Mon Sep 17 00:00:00 2001 From: max-001 Date: Sun, 24 Nov 2024 12:01:41 +0100 Subject: Make format --- src/crepe/ComponentManager.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 5c0ef3d..a90502d 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -35,8 +35,7 @@ void ComponentManager::delete_all_components() { } GameObject ComponentManager::new_object(const string & name, const string & tag, - const vec2 & position, double rotation, - double scale) { + const vec2 & position, double rotation, double scale) { // Find the first available id (taking persistent objects into account) while (this->persistent[this->next_id]) { this->next_id++; -- cgit v1.2.3 From 9aca3626e31a7549f8c436672d4f479bd64c0057 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 25 Nov 2024 19:39:11 +0100 Subject: fixed a rezing and camera origin is now middlepoint --- src/crepe/api/Camera.h | 4 +++- src/crepe/api/Config.h | 2 +- src/crepe/facade/SDLContext.cpp | 13 ++++++------- src/crepe/facade/SDLContext.h | 2 +- src/example/rendering_particle.cpp | 5 +++-- 5 files changed, 14 insertions(+), 12 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index 083dc19..137c8ed 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -32,14 +32,16 @@ public: Vector2 pos = {0, 0}; //! screen the display size in pixels ( output resolution ) + //Vector2 screen = {720, 480}; Vector2 screen = {1080, 720}; //! viewport is the area of the world visible through the camera (in world units) + //Vector2 viewport = {720, 480}; Vector2 viewport = {2000, 1000}; //! scale scaling factor from world units to pixel coordinates //! Zoom level of the camera view. - double zoom = 1.5f; + double zoom = 1.0f; public: /** diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 13eabd1..b6c2ccf 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -33,7 +33,7 @@ public: * * Only messages with equal or higher priority than this value will be logged. */ - Log::Level level = Log::Level::INFO; + Log::Level level = Log::Level::DEBUG; /** * \brief Colored log output * diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 778e746..de7d08f 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -124,8 +125,8 @@ SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, pixel_width *= img_scale; pixel_height *= img_scale; - int pixel_x = static_cast((pos.x - cam_pos.x - pixel_width / 2)); - int pixel_y = static_cast((pos.y - cam_pos.y - pixel_height / 2)); + int pixel_x = static_cast((pos.x - cam_pos.x + this->viewport.w / 2 - pixel_width / 2)); + int pixel_y = static_cast((pos.y - cam_pos.y + this->viewport.h / 2 - pixel_height / 2)); return SDL_Rect{ .x = pixel_x, @@ -168,10 +169,10 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, void SDLContext::set_camera(const Camera & cam, Vector2 & scale) { // resize window - if (this->viewport.w != (int) cam.screen.x && this->viewport.h != (int) cam.screen.y) { + if (this->viewport.w != (int) cam.screen.x || this->viewport.h != (int) cam.screen.y) { SDL_SetWindowSize(this->game_window.get(), (int) cam.screen.x, (int) cam.screen.y); - this->viewport.h = cam.screen.y; - this->viewport.w = cam.screen.x; + this->viewport.h = (int)cam.screen.y; + this->viewport.w = (int)cam.screen.x; } double screen_aspect = cam.screen.x / cam.screen.y; @@ -185,7 +186,6 @@ void SDLContext::set_camera(const Camera & cam, Vector2 & scale) { // calculate black bars if (screen_aspect > viewport_aspect) { // lettorboxing - view.h = static_cast(cam.screen.y / cam.zoom); view.w = static_cast(cam.screen.y * viewport_aspect); view.x = static_cast(cam.screen.x - view.w) / 2; view.y = 0; @@ -196,7 +196,6 @@ void SDLContext::set_camera(const Camera & cam, Vector2 & scale) { view.x = 0; view.y = static_cast(cam.screen.y - view.h) / 2; } - // set drawing area SDL_RenderSetViewport(this->game_renderer.get(), &view); diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 03f9ec9..3e9b8db 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -179,7 +179,7 @@ private: //! viewport for the camera window //todo change this so that it becomes a vec2 for only width and height - SDL_Rect viewport = {0, 0, 1280, 720}; + SDL_Rect viewport = {0, 0, 1200, 1200}; }; } // namespace crepe diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index fad174e..36997be 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -24,7 +24,7 @@ using namespace std; int main(int argc, char * argv[]) { ComponentManager mgr; - GameObject game_object = mgr.new_object("", "", Vector2{1000, 500}, 0, 1); + GameObject game_object = mgr.new_object("", "", Vector2{0, 0}, 0, 1); RenderSystem sys{mgr}; ParticleSystem psys{mgr}; AnimatorSystem asys{mgr}; @@ -62,7 +62,8 @@ int main(int argc, char * argv[]) { }); */ - game_object.add_component(Color::WHITE); + auto & cam = game_object.add_component(Color::WHITE); + cam.pos = {500, 200}; /* game_object -- cgit v1.2.3 From f52e7e1450d47604983dba5f3cbab364ffd77cdc Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 26 Nov 2024 09:20:55 +0100 Subject: Replaced vector by unordered_map --- src/crepe/ComponentManager.cpp | 8 -------- src/crepe/ComponentManager.h | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index a90502d..e99419e 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -39,19 +39,11 @@ GameObject ComponentManager::new_object(const string & name, const string & tag, // Find the first available id (taking persistent objects into account) while (this->persistent[this->next_id]) { this->next_id++; - // Make sure that the persistent vector is large enough - if (persistent.size() <= next_id) { - this->persistent.resize(next_id + 1, false); - } } GameObject object{*this, this->next_id, name, tag, position, rotation, scale}; this->next_id++; - // Make sure that the persistent vector is large enough - if (persistent.size() <= next_id) { - this->persistent.resize(next_id + 1, false); - } return object; } diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 810bc38..8462698 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -147,7 +147,7 @@ private: components; //! Persistent flag for each GameObject - std::vector persistent = {false}; + std::unordered_map persistent; //! ID of next GameObject allocated by \c ComponentManager::new_object game_object_id_t next_id = 0; -- cgit v1.2.3 From 07ac07ce4cafe8ec454a1e53b1541dcaab10cf40 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 26 Nov 2024 09:28:12 +0100 Subject: It is now possible to reset the persistent flag --- src/crepe/ComponentManager.cpp | 4 +++- src/crepe/ComponentManager.h | 3 ++- src/crepe/api/GameObject.cpp | 4 ++-- src/crepe/api/GameObject.h | 4 +++- 4 files changed, 10 insertions(+), 5 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index e99419e..af4b9f4 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -47,4 +47,6 @@ GameObject ComponentManager::new_object(const string & name, const string & tag, return object; } -void ComponentManager::set_persistent(game_object_id_t id) { this->persistent[id] = true; } +void ComponentManager::set_persistent(game_object_id_t id, bool persistent) { + this->persistent[id] = persistent; +} diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 8462698..480124f 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -106,8 +106,9 @@ protected: * components will not be deleted. * * \param id The id of the GameObject to set as persistent + * \param persistent The persistent flag */ - void set_persistent(game_object_id_t id); + void set_persistent(game_object_id_t id, bool persistent); public: /** diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index 6c1de12..9ef4682 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -31,8 +31,8 @@ void GameObject::set_parent(const GameObject & parent) { parent_metadata.at(0).get().children.push_back(this->id); } -void GameObject::set_persistent() { +void GameObject::set_persistent(bool persistent) { ComponentManager & mgr = this->component_manager; - mgr.set_persistent(this->id); + mgr.set_persistent(this->id, persistent); } diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index a30dce3..4cd2bc0 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -63,8 +63,10 @@ public: * * This method sets the persistent flag of the GameObject to true. If the persistent * flag is set to true, the GameObject will not be deleted when the scene is changed. + * + * \param persistent The persistent flag */ - void set_persistent(); + void set_persistent(bool persistent = true); public: //! The id of the GameObject -- cgit v1.2.3 From 0927b0b21189df130292cc59bda92cf51348b7c6 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 26 Nov 2024 09:38:44 +0100 Subject: Improved delete_all_components() --- src/crepe/ComponentManager.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index af4b9f4..68b5edb 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -27,10 +27,18 @@ void ComponentManager::delete_all_components_of_id(game_object_id_t id) { } void ComponentManager::delete_all_components() { - // Loop through all the ids and delete all components of each id - for (game_object_id_t id = 0; id < next_id; id++) { - delete_all_components_of_id(id); + // Loop through all the types (in the unordered_map<>) + for (auto & [type, component_array] : this->components) { + // Loop through all the ids (in the vector<>) + for (game_object_id_t id = 0; id < component_array.size(); id++) { + // Do not delete persistent objects + if (!this->persistent[id]) { + // Clear the components at this specific id + component_array[id].clear(); + } + } } + this->next_id = 0; } -- cgit v1.2.3 From 85ae4b874262012af277492beb0c45cb4d86feef Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 26 Nov 2024 09:47:46 +0100 Subject: Renamed componentArray to component_array --- src/crepe/ComponentManager.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 68b5edb..5b73009 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -17,11 +17,11 @@ void ComponentManager::delete_all_components_of_id(game_object_id_t id) { } // Loop through all the types (in the unordered_map<>) - for (auto & [type, componentArray] : this->components) { + for (auto & [type, component_array] : this->components) { // Make sure that the id (that we are looking for) is within the boundaries of the vector<> - if (id < componentArray.size()) { + if (id < component_array.size()) { // Clear the components at this specific id - componentArray[id].clear(); + component_array[id].clear(); } } } -- cgit v1.2.3 From 8cfb59093ce7b18c2b81cc8429a7568a3ba21a73 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 26 Nov 2024 10:03:06 +0100 Subject: adjusted vector2 to vec2 --- src/crepe/api/Camera.h | 13 +++++++------ src/crepe/api/Vector2.h | 11 ++--------- src/crepe/facade/SDLContext.cpp | 20 +++++++++----------- src/crepe/facade/SDLContext.h | 16 ++++++++-------- src/crepe/system/RenderSystem.h | 8 +++----- src/example/rendering_particle.cpp | 5 ++--- 6 files changed, 31 insertions(+), 42 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index 137c8ed..eea1b28 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -2,7 +2,8 @@ #include "Color.h" #include "Component.h" -#include "api/Vector2.h" + +#include "types.h" namespace crepe { @@ -29,15 +30,15 @@ public: Color bg_color; //! pos The position of the camera in world units - Vector2 pos = {0, 0}; + vec2 pos = {0, 0}; //! screen the display size in pixels ( output resolution ) - //Vector2 screen = {720, 480}; - Vector2 screen = {1080, 720}; + //vec2 screen = {720, 480}; + vec2 screen = {1080, 720}; //! viewport is the area of the world visible through the camera (in world units) - //Vector2 viewport = {720, 480}; - Vector2 viewport = {2000, 1000}; + //vec2 viewport = {720, 480}; + vec2 viewport = {2000, 1000}; //! scale scaling factor from world units to pixel coordinates //! Zoom level of the camera view. diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h index 019d849..0688fac 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -34,15 +34,11 @@ struct Vector2 { //! Multiplies this vector by another vector element-wise and updates this vector. Vector2 & operator*=(const Vector2 & other); - //! Multiplies a scalar value to both components of this vector and updates this vector. - Vector2 & operator*=(const Vector2 & other); - - //! Divides this vector by another vector element-wise and updates this vector. - Vector2 operator/(const Vector2 & other) const; - //! Divides a scalar value to both components of this vector and updates this vector. Vector2 operator/(const T & other) const; + Vector2 operator/(T other) const; + //! Adds another vector to this vector and updates this vector. Vector2 & operator+=(const Vector2 & other); @@ -55,9 +51,6 @@ struct Vector2 { //! Subtracts a scalar value from both components of this vector and updates this vector. Vector2 & operator-=(T other); - //! Multiplies this vector by another vector element-wise and updates this vector. - Vector2 & operator*=(const Vector2 & other); - //! Multiplies this vector by a scalar and updates this vector. Vector2 & operator*=(T other); diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index de7d08f..fc59d84 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include @@ -16,10 +15,9 @@ #include "../api/Sprite.h" #include "../api/Texture.h" #include "../api/Transform.h" -#include "../api/Vector2.h" #include "../util/Log.h" -#include "api/Vector2.h" +#include "types.h" #include "SDLContext.h" using namespace crepe; @@ -108,9 +106,9 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { .h = sprite.sprite_rect.h, }; } -SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, - const Vector2 & cam_pos, const double & img_scale, - const Vector2 & cam_scale) const { +SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, + const vec2 & cam_pos, const double & img_scale, + const vec2 & cam_scale) const { int pixel_width, pixel_height; @@ -136,9 +134,9 @@ SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, }; } -void SDLContext::draw_particle(const Sprite & sprite, const Vector2 & pos, - const double & angle, const Vector2 & cam_pos, - const double & img_scale, const Vector2 & cam_scale) { +void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, + const double & angle, const vec2 & cam_pos, + const double & img_scale, const vec2 & cam_scale) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) @@ -152,7 +150,7 @@ void SDLContext::draw_particle(const Sprite & sprite, const Vector2 & pos, } void SDLContext::draw(const Sprite & sprite, const Transform & transform, - const Vector2 & cam_pos, const Vector2 & cam_scale) { + const vec2 & cam_pos, const vec2 & cam_scale) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) @@ -166,7 +164,7 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, &dstrect, transform.rotation, NULL, render_flip); } -void SDLContext::set_camera(const Camera & cam, Vector2 & scale) { +void SDLContext::set_camera(const Camera & cam, vec2 & scale) { // resize window if (this->viewport.w != (int) cam.screen.x || this->viewport.h != (int) cam.screen.y) { diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index aed5797..c9f3299 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -120,8 +120,8 @@ private: * \param cam_pos position of the current camera in the scene * \param cam_scale multiplier for the world to screen */ - void draw(const Sprite & sprite, const Transform & transform, const Vector2 & cam_pos, - const Vector2 & cam_scale); + void draw(const Sprite & sprite, const Transform & transform, const vec2 & cam_pos, + const vec2 & cam_scale); /** * \brief Draws a particle to the screen using the specified parameters @@ -133,9 +133,9 @@ private: * \param img_scale scalar multiplier to increase image size * \param cam_scale camera scalar for world to screen */ - void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, - const Vector2 & cam_pos, const double & img_scale, - const Vector2 & cam_scale); + void draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle, + const vec2 & cam_pos, const double & img_scale, + const vec2 & cam_scale); //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -147,7 +147,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, Vector2 & scale); + void set_camera(const Camera & camera, vec2 & scale); private: /** @@ -168,8 +168,8 @@ private: * \param scale the multiplier for world to screen * \return sdl rectangle to draw a dst image to draw on the screen */ - SDL_Rect get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & cam_pos, - const double & img_scale, const Vector2 & scale) const; + SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const vec2 & cam_pos, + const double & img_scale, const vec2 & scale) const; private: //! sdl Window diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index f010a83..97222f3 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -1,13 +1,11 @@ #pragma once -#include -#include +#include -#include "api/Vector2.h" #include "facade/SDLContext.h" #include "System.h" -#include +#include "types.h" namespace crepe { @@ -82,7 +80,7 @@ private: Camera * curr_cam_ref = nullptr; // TODO: needs a better solution - Vector2 scale; + vec2 scale; SDLContext & context = SDLContext::get_instance(); }; diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 49e8e9d..80726bc 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -2,7 +2,6 @@ #include "api/Camera.h" #include "system/AnimatorSystem.h" #include "system/ParticleSystem.h" -#include "types.h" #include #include @@ -14,7 +13,7 @@ #include #include #include -#include +#include #include #include @@ -25,7 +24,7 @@ using namespace std; int main(int argc, char * argv[]) { ComponentManager mgr; - GameObject game_object = mgr.new_object("", "", Vector2{0, 0}, 0, 1); + GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 1); RenderSystem sys{mgr}; ParticleSystem psys{mgr}; AnimatorSystem asys{mgr}; -- cgit v1.2.3 From 60a9879f268977d57b460eec830a3f8b5b24e1a2 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 26 Nov 2024 10:26:42 +0100 Subject: added window default config settings --- src/crepe/api/Camera.h | 11 +++++------ src/crepe/api/Config.h | 9 +++++++++ src/crepe/facade/SDLContext.cpp | 12 +++++------- src/crepe/facade/SDLContext.h | 4 ++-- src/example/rendering_particle.cpp | 1 + 5 files changed, 22 insertions(+), 15 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index eea1b28..ec94c44 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -3,6 +3,7 @@ #include "Color.h" #include "Component.h" +#include "api/Config.h" #include "types.h" namespace crepe { @@ -30,19 +31,17 @@ public: Color bg_color; //! pos The position of the camera in world units - vec2 pos = {0, 0}; + vec2 pos = Config::get_instance().win_set.pos; //! screen the display size in pixels ( output resolution ) - //vec2 screen = {720, 480}; - vec2 screen = {1080, 720}; + vec2 screen = Config::get_instance().win_set.def_size; //! viewport is the area of the world visible through the camera (in world units) - //vec2 viewport = {720, 480}; - vec2 viewport = {2000, 1000}; + vec2 viewport = Config::get_instance().win_set.def_size; //! scale scaling factor from world units to pixel coordinates //! Zoom level of the camera view. - double zoom = 1.0f; + double zoom = Config::get_instance().win_set.zoom; public: /** diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 671fd02..e1aef7d 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -1,6 +1,7 @@ #pragma once #include "../util/Log.h" +#include "types.h" namespace crepe { @@ -62,6 +63,14 @@ public: double gravity = 1; } physics; + //! default window settings + struct { + //TODO make this constexpr because this will never change + vec2 def_size = {1080,720}; + vec2 pos = {0,0}; + float zoom = 1.0f; + } win_set; + //! Asset loading options struct { /** diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index fc59d84..8b29f8a 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -30,14 +30,13 @@ SDLContext & SDLContext::get_instance() { SDLContext::SDLContext() { dbg_trace(); - // FIXME: read window defaults from config manager if (SDL_Init(SDL_INIT_VIDEO) != 0) { throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError())); } SDL_Window * tmp_window = SDL_CreateWindow("Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - this->viewport.w, this->viewport.h, 0); + this->window.x, this->window.y, 0); if (!tmp_window) { throw runtime_error(format("SDLContext: SDL_Window error: {}", SDL_GetError())); } @@ -123,8 +122,8 @@ SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, pixel_width *= img_scale; pixel_height *= img_scale; - int pixel_x = static_cast((pos.x - cam_pos.x + this->viewport.w / 2 - pixel_width / 2)); - int pixel_y = static_cast((pos.y - cam_pos.y + this->viewport.h / 2 - pixel_height / 2)); + int pixel_x = static_cast((pos.x - cam_pos.x + this->window.x / 2 - pixel_width / 2)); + int pixel_y = static_cast((pos.y - cam_pos.y + this->window.y / 2 - pixel_height / 2)); return SDL_Rect{ .x = pixel_x, @@ -167,10 +166,9 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, void SDLContext::set_camera(const Camera & cam, vec2 & scale) { // resize window - if (this->viewport.w != (int) cam.screen.x || this->viewport.h != (int) cam.screen.y) { + if ((int)this->window.x != (int) cam.screen.x || (int)this->window.y != (int) cam.screen.y) { SDL_SetWindowSize(this->game_window.get(), (int) cam.screen.x, (int) cam.screen.y); - this->viewport.h = (int)cam.screen.y; - this->viewport.w = (int)cam.screen.x; + this->window = cam.screen; } double screen_aspect = cam.screen.x / cam.screen.y; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index c9f3299..f72eecb 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -13,6 +13,7 @@ #include "../api/Transform.h" #include "api/Camera.h" +#include "api/Config.h" #include "types.h" namespace crepe { @@ -179,8 +180,7 @@ private: std::unique_ptr> game_renderer; //! viewport for the camera window - //todo change this so that it becomes a vec2 for only width and height - SDL_Rect viewport = {0, 0, 1200, 1200}; + vec2 window = Config::get_instance().win_set.def_size; }; } // namespace crepe diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 80726bc..54a90b5 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -64,6 +64,7 @@ int main(int argc, char * argv[]) { auto & cam = game_object.add_component(Color::WHITE); cam.pos = {500, 200}; + cam.viewport = {2000,1000}; /* game_object -- cgit v1.2.3 From b4c07db977ab18302a0d953ef22a32a171e688f8 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 26 Nov 2024 10:27:14 +0100 Subject: make format --- src/crepe/api/Config.h | 6 +++--- src/crepe/facade/SDLContext.cpp | 24 +++++++++++++----------- src/crepe/facade/SDLContext.h | 3 +-- src/example/rendering_particle.cpp | 4 ++-- 4 files changed, 19 insertions(+), 18 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index e1aef7d..6de93f0 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -65,9 +65,9 @@ public: //! default window settings struct { - //TODO make this constexpr because this will never change - vec2 def_size = {1080,720}; - vec2 pos = {0,0}; + //TODO make this constexpr because this will never change + vec2 def_size = {1080, 720}; + vec2 pos = {0, 0}; float zoom = 1.0f; } win_set; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 8b29f8a..ac6b089 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -17,8 +17,8 @@ #include "../api/Transform.h" #include "../util/Log.h" -#include "types.h" #include "SDLContext.h" +#include "types.h" using namespace crepe; using namespace std; @@ -123,7 +123,8 @@ SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, pixel_height *= img_scale; int pixel_x = static_cast((pos.x - cam_pos.x + this->window.x / 2 - pixel_width / 2)); - int pixel_y = static_cast((pos.y - cam_pos.y + this->window.y / 2 - pixel_height / 2)); + int pixel_y + = static_cast((pos.y - cam_pos.y + this->window.y / 2 - pixel_height / 2)); return SDL_Rect{ .x = pixel_x, @@ -133,9 +134,9 @@ SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, }; } -void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, - const double & angle, const vec2 & cam_pos, - const double & img_scale, const vec2 & cam_scale) { +void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle, + const vec2 & cam_pos, const double & img_scale, + const vec2 & cam_scale) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) @@ -148,8 +149,8 @@ void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, &dstrect, angle, NULL, render_flip); } -void SDLContext::draw(const Sprite & sprite, const Transform & transform, - const vec2 & cam_pos, const vec2 & cam_scale) { +void SDLContext::draw(const Sprite & sprite, const Transform & transform, const vec2 & cam_pos, + const vec2 & cam_scale) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) @@ -166,19 +167,20 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, void SDLContext::set_camera(const Camera & cam, vec2 & scale) { // resize window - if ((int)this->window.x != (int) cam.screen.x || (int)this->window.y != (int) cam.screen.y) { + if ((int) this->window.x != (int) cam.screen.x + || (int) this->window.y != (int) cam.screen.y) { SDL_SetWindowSize(this->game_window.get(), (int) cam.screen.x, (int) cam.screen.y); this->window = cam.screen; } double screen_aspect = cam.screen.x / cam.screen.y; double viewport_aspect = cam.viewport.x / cam.viewport.y; - + // decide scaling factor for world to screen scale = cam.screen / cam.viewport * cam.zoom; SDL_Rect view; - + // calculate black bars if (screen_aspect > viewport_aspect) { // lettorboxing @@ -192,7 +194,7 @@ void SDLContext::set_camera(const Camera & cam, vec2 & scale) { view.x = 0; view.y = static_cast(cam.screen.y - view.h) / 2; } - // set drawing area + // set drawing area SDL_RenderSetViewport(this->game_renderer.get(), &view); SDL_RenderSetLogicalSize(this->game_renderer.get(), cam.viewport.x, cam.viewport.y); diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index f72eecb..542a8bf 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -135,8 +135,7 @@ private: * \param cam_scale camera scalar for world to screen */ void draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle, - const vec2 & cam_pos, const double & img_scale, - const vec2 & cam_scale); + const vec2 & cam_pos, const double & img_scale, const vec2 & cam_scale); //! Clears the screen, preparing for a new frame. void clear_screen(); diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 54a90b5..c70e1af 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -13,8 +13,8 @@ #include #include #include -#include #include +#include #include #include @@ -64,7 +64,7 @@ int main(int argc, char * argv[]) { auto & cam = game_object.add_component(Color::WHITE); cam.pos = {500, 200}; - cam.viewport = {2000,1000}; + cam.viewport = {2000, 1000}; /* game_object -- cgit v1.2.3 From c5d1b46ba804eaabdb4fc2f4f4295292032def65 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 26 Nov 2024 19:19:21 +0100 Subject: implemented all the feedback --- src/crepe/api/Texture.cpp | 2 -- src/crepe/facade/SDLContext.cpp | 39 +++++++++++++++++--------------------- src/crepe/facade/SDLContext.h | 8 ++++---- src/crepe/system/RenderSystem.cpp | 6 +++--- src/example/rendering_particle.cpp | 3 +-- 5 files changed, 25 insertions(+), 33 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index 264d7b1..eeb86e9 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -1,5 +1,3 @@ -#include - #include "../facade/SDLContext.h" #include "../util/Log.h" diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index ac6b089..55b0082 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -105,37 +105,32 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { .h = sprite.sprite_rect.h, }; } -SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, - const vec2 & cam_pos, const double & img_scale, - const vec2 & cam_scale) const { +SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, + const double & img_scale, const vec2 & cam_scale) const { - int pixel_width, pixel_height; + int width, height; if (sprite.sprite_rect.w > sprite.sprite_rect.h) { - pixel_width = static_cast(sprite.width * cam_scale.x); - pixel_height = static_cast(pixel_width / sprite.aspect_ratio); + width = static_cast(sprite.width * cam_scale.x); + height = static_cast(width / sprite.aspect_ratio); } else { - pixel_height = static_cast(sprite.height * cam_scale.y); - pixel_width = static_cast(pixel_height * sprite.aspect_ratio); + height = static_cast(sprite.height * cam_scale.y); + width = static_cast(height * sprite.aspect_ratio); } - pixel_width *= img_scale; - pixel_height *= img_scale; - - int pixel_x = static_cast((pos.x - cam_pos.x + this->window.x / 2 - pixel_width / 2)); - int pixel_y - = static_cast((pos.y - cam_pos.y + this->window.y / 2 - pixel_height / 2)); + width *= img_scale; + height *= img_scale; return SDL_Rect{ - .x = pixel_x, - .y = pixel_y, - .w = pixel_width, - .h = pixel_height, + .x = static_cast((pos.x - cam.pos.x + (cam.viewport.x / 2) - width / 2)), + .y = static_cast((pos.y - cam.pos.y + (cam.viewport.y / 2) - height / 2)), + .w = width, + .h = height, }; } void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle, - const vec2 & cam_pos, const double & img_scale, + const double & img_scale, const Camera & cam, const vec2 & cam_scale) { SDL_RendererFlip render_flip @@ -143,13 +138,13 @@ void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, const do | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam_pos, img_scale, cam_scale); + SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam , img_scale, cam_scale); SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, &dstrect, angle, NULL, render_flip); } -void SDLContext::draw(const Sprite & sprite, const Transform & transform, const vec2 & cam_pos, +void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam, const vec2 & cam_scale) { SDL_RendererFlip render_flip @@ -158,7 +153,7 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const SDL_Rect srcrect = this->get_src_rect(sprite); SDL_Rect dstrect - = this->get_dst_rect(sprite, transform.position, cam_pos, transform.scale, cam_scale); + = this->get_dst_rect(sprite, transform.position, cam, transform.scale, cam_scale); SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, &dstrect, transform.rotation, NULL, render_flip); diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 542a8bf..1a9316e 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -121,7 +121,7 @@ private: * \param cam_pos position of the current camera in the scene * \param cam_scale multiplier for the world to screen */ - void draw(const Sprite & sprite, const Transform & transform, const vec2 & cam_pos, + void draw(const Sprite & sprite, const Transform & transform, const Camera & cam, const vec2 & cam_scale); /** @@ -135,7 +135,7 @@ private: * \param cam_scale camera scalar for world to screen */ void draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle, - const vec2 & cam_pos, const double & img_scale, const vec2 & cam_scale); + const double & img_scale, const Camera & cam, const vec2 & cam_scale); //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -168,8 +168,8 @@ private: * \param scale the multiplier for world to screen * \return sdl rectangle to draw a dst image to draw on the screen */ - SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const vec2 & cam_pos, - const double & img_scale, const vec2 & scale) const; + SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, + const double & img_scale, const vec2 & cam_scale) const; private: //! sdl Window diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 1dd1699..564fc79 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -72,14 +72,14 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) for (const Particle & p : em.data.particles) { if (!p.active) continue; - this->context.draw_particle(sprite, p.position, p.angle, this->curr_cam_ref->pos, - scale, this->scale); + this->context.draw_particle(sprite, p.position, p.angle, scale, + *this->curr_cam_ref, this->scale); } } return rendering_particles; } void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { - this->context.draw(sprite, tm, this->curr_cam_ref->pos, this->scale); + this->context.draw(sprite, tm, *this->curr_cam_ref, this->scale); } void RenderSystem::render() { diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index c70e1af..eec2769 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -63,8 +63,7 @@ int main(int argc, char * argv[]) { */ auto & cam = game_object.add_component(Color::WHITE); - cam.pos = {500, 200}; - cam.viewport = {2000, 1000}; + cam.pos = {0, 0}; /* game_object -- cgit v1.2.3 From 368aeba5bcfafe445b3af5748f3798aa75003bf2 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 27 Nov 2024 10:35:06 +0100 Subject: implemented feedback on PR40 and made camera sizes ivec2 --- src/crepe/api/Camera.h | 11 ++++----- src/crepe/api/Config.h | 2 +- src/crepe/api/Sprite.cpp | 3 ++- src/crepe/facade/SDLContext.cpp | 49 +++++++++++++++++++------------------- src/crepe/facade/SDLContext.h | 27 ++++++++------------- src/crepe/system/RenderSystem.cpp | 8 +++---- src/crepe/system/RenderSystem.h | 4 +--- src/example/rendering_particle.cpp | 8 +++---- 8 files changed, 51 insertions(+), 61 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index ec94c44..151e5d9 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -2,8 +2,6 @@ #include "Color.h" #include "Component.h" - -#include "api/Config.h" #include "types.h" namespace crepe { @@ -31,17 +29,18 @@ public: Color bg_color; //! pos The position of the camera in world units - vec2 pos = Config::get_instance().win_set.pos; + vec2 pos = {0,0}; //! screen the display size in pixels ( output resolution ) - vec2 screen = Config::get_instance().win_set.def_size; + ivec2 screen = {1080,720}; //! viewport is the area of the world visible through the camera (in world units) - vec2 viewport = Config::get_instance().win_set.def_size; + //vec2 viewport = {1000, 2000}; + ivec2 viewport = {500, 1000}; //! scale scaling factor from world units to pixel coordinates //! Zoom level of the camera view. - double zoom = Config::get_instance().win_set.zoom; + double zoom = 1.0f; public: /** diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 6de93f0..2723461 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 - vec2 def_size = {1080, 720}; + ivec2 def_size = {1080, 720}; vec2 pos = {0, 0}; float zoom = 1.0f; } win_set; diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index c219dd0..27f219c 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -1,3 +1,4 @@ +#include #include #include "../util/Log.h" @@ -16,7 +17,7 @@ Sprite::Sprite(game_object_id_t id, const shared_ptr image, const Color color(color), flip(flip), sprite_image(image), - aspect_ratio(sprite_image->get_width() / sprite_image->get_height()) { + aspect_ratio(static_cast(sprite_image->get_width()) / sprite_image->get_height()) { dbg_trace(); this->sprite_rect.w = sprite_image->get_width(); diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 55b0082..fca3de4 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -18,6 +19,7 @@ #include "../util/Log.h" #include "SDLContext.h" +#include "api/Config.h" #include "types.h" using namespace crepe; @@ -34,9 +36,11 @@ SDLContext::SDLContext() { if (SDL_Init(SDL_INIT_VIDEO) != 0) { throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError())); } + + auto & cfg = Config::get_instance().win_set; SDL_Window * tmp_window = SDL_CreateWindow("Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - this->window.x, this->window.y, 0); + cfg.def_size.x,cfg.def_size.y, 0); if (!tmp_window) { throw runtime_error(format("SDLContext: SDL_Window error: {}", SDL_GetError())); } @@ -106,20 +110,22 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { }; } SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, - const double & img_scale, const vec2 & cam_scale) const { + const double & img_scale) const { int width, height; - if (sprite.sprite_rect.w > sprite.sprite_rect.h) { - width = static_cast(sprite.width * cam_scale.x); - height = static_cast(width / sprite.aspect_ratio); + if (sprite.width > sprite.height) { + width = sprite.width; + height = static_cast(sprite.width / sprite.aspect_ratio); } else { - height = static_cast(sprite.height * cam_scale.y); - width = static_cast(height * sprite.aspect_ratio); + height = sprite.height; + width = static_cast(sprite.height * sprite.aspect_ratio); } - width *= img_scale; - height *= img_scale; + cout << width << " " << height << " " << " " << sprite.aspect_ratio << endl; + + width *= img_scale * cam.zoom; + height *= img_scale * cam.zoom; return SDL_Rect{ .x = static_cast((pos.x - cam.pos.x + (cam.viewport.x / 2) - width / 2)), @@ -130,22 +136,20 @@ SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const } void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle, - const double & img_scale, const Camera & cam, - const vec2 & cam_scale) { + const double & img_scale, const Camera & cam) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam , img_scale, cam_scale); + SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam , img_scale); SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, &dstrect, angle, NULL, render_flip); } -void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam, - const vec2 & cam_scale) { +void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) @@ -153,32 +157,29 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const SDL_Rect srcrect = this->get_src_rect(sprite); SDL_Rect dstrect - = this->get_dst_rect(sprite, transform.position, cam, transform.scale, cam_scale); + = this->get_dst_rect(sprite, transform.position, cam, transform.scale); SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, &dstrect, transform.rotation, NULL, render_flip); } -void SDLContext::set_camera(const Camera & cam, vec2 & scale) { +void SDLContext::set_camera(const Camera & cam) { // resize window - if ((int) this->window.x != (int) cam.screen.x - || (int) this->window.y != (int) cam.screen.y) { - SDL_SetWindowSize(this->game_window.get(), (int) cam.screen.x, (int) cam.screen.y); - this->window = cam.screen; + int w,h; + SDL_GetWindowSize(this->game_window.get(), &w, &h); + if ( w != cam.screen.x || h != cam.screen.y) { + SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y); } double screen_aspect = cam.screen.x / cam.screen.y; double viewport_aspect = cam.viewport.x / cam.viewport.y; - // decide scaling factor for world to screen - scale = cam.screen / cam.viewport * cam.zoom; - SDL_Rect view; - // calculate black bars if (screen_aspect > viewport_aspect) { // lettorboxing + view.h = static_cast(cam.screen.y / cam.zoom); view.w = static_cast(cam.screen.y * viewport_aspect); view.x = static_cast(cam.screen.x - view.w) / 2; view.y = 0; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 1a9316e..2e40b6f 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -11,9 +11,8 @@ #include "../api/Sprite.h" #include "../api/Transform.h" -#include "api/Camera.h" +#include "../api/Camera.h" -#include "api/Config.h" #include "types.h" namespace crepe { @@ -101,14 +100,14 @@ private: * \param texture Reference to the Texture object. * \return Width of the texture as an integer. */ - int get_width(const Texture &) const; + int get_width(const Texture & texture) const; /** * \brief Gets the height of a texture. * \param texture Reference to the Texture object. * \return Height of the texture as an integer. */ - int get_height(const Texture &) const; + int get_height(const Texture & texture) const; private: //! Will use draw,clear_screen, present_screen, camera. @@ -118,11 +117,9 @@ private: * \brief Draws a sprite to the screen using the specified transform and camera. * \param sprite Reference to the Sprite to draw. * \param transform Reference to the Transform for positioning. - * \param cam_pos position of the current camera in the scene - * \param cam_scale multiplier for the world to screen + * \param cam camera of the current scene */ - void draw(const Sprite & sprite, const Transform & transform, const Camera & cam, - const vec2 & cam_scale); + void draw(const Sprite & sprite, const Transform & transform, const Camera & cam); /** * \brief Draws a particle to the screen using the specified parameters @@ -130,12 +127,11 @@ private: * \param sprite Referenceto the sprite to draw * \param pos particle position in world units * \param angle particle angle in degrees - * \param cam_pos camera position in world units * \param img_scale scalar multiplier to increase image size - * \param cam_scale camera scalar for world to screen + * \param cam camera of the current scene */ void draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle, - const double & img_scale, const Camera & cam, const vec2 & cam_scale); + const double & img_scale, const Camera & cam); //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -147,7 +143,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, vec2 & scale); + void set_camera(const Camera & camera); private: /** @@ -163,13 +159,12 @@ private: * * \param sprite Reference to the sprite to calculate rectangle * \param pos the pos in world units - * \param cam_pos the camera position in world units + * \param cam the camera of the current scene * \param img_scale the image multiplier for increasing img size - * \param scale the multiplier for world to screen * \return sdl rectangle to draw a dst image to draw on the screen */ SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, - const double & img_scale, const vec2 & cam_scale) const; + const double & img_scale) const; private: //! sdl Window @@ -178,8 +173,6 @@ private: //! renderer for the crepe engine std::unique_ptr> game_renderer; - //! viewport for the camera window - vec2 window = Config::get_instance().win_set.def_size; }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 564fc79..beed9ce 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include #include @@ -10,7 +9,6 @@ #include "../api/ParticleEmitter.h" #include "../api/Sprite.h" #include "../api/Transform.h" -#include "../api/Vector2.h" #include "../facade/SDLContext.h" #include "RenderSystem.h" @@ -30,7 +28,7 @@ void RenderSystem::update_camera() { for (Camera & cam : cameras) { if (!cam.active) continue; - this->context.set_camera(cam, this->scale); + this->context.set_camera(cam); this->curr_cam_ref = &cam; } } @@ -73,13 +71,13 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) for (const Particle & p : em.data.particles) { if (!p.active) continue; this->context.draw_particle(sprite, p.position, p.angle, scale, - *this->curr_cam_ref, this->scale); + *this->curr_cam_ref); } } return rendering_particles; } void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { - this->context.draw(sprite, tm, *this->curr_cam_ref, this->scale); + this->context.draw(sprite, tm, *this->curr_cam_ref); } void RenderSystem::render() { diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 97222f3..08930b0 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -77,10 +77,8 @@ private: private: //! Pointer to the current active camera for rendering - Camera * curr_cam_ref = nullptr; // TODO: needs a better solution - - vec2 scale; + Camera * curr_cam_ref = nullptr; SDLContext & context = SDLContext::get_instance(); }; diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index eec2769..680c0ac 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -32,13 +32,13 @@ int main(int argc, char * argv[]) { Color color(255, 255, 255, 255); Sprite & test_sprite = game_object.add_component( - make_shared("asset/spritesheet/spritesheet_test.png"), color, + make_shared("asset/texture/test_ap43.png"), color, FlipSettings{true, true}); test_sprite.order_in_layer = 5; - test_sprite.width = 1000; - test_sprite.height = 500; + test_sprite.width = 259; + test_sprite.height = 195; - game_object.add_component(test_sprite, 4, 1, 0).active = true; + //game_object.add_component(test_sprite, 4, 1, 0).active = true; /* auto & test = game_object.add_component(ParticleEmitter::Data{ -- cgit v1.2.3 From 3b433353a83c2436b69f73ca1048842d906b1147 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 27 Nov 2024 10:38:47 +0100 Subject: removed most static cast because of camera sizes vec2 -> ivec2 --- src/crepe/facade/SDLContext.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index fca3de4..6e22316 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -116,10 +116,10 @@ SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const if (sprite.width > sprite.height) { width = sprite.width; - height = static_cast(sprite.width / sprite.aspect_ratio); + height = sprite.width / sprite.aspect_ratio; } else { height = sprite.height; - width = static_cast(sprite.height * sprite.aspect_ratio); + width = sprite.height * sprite.aspect_ratio; } cout << width << " " << height << " " << " " << sprite.aspect_ratio << endl; @@ -179,16 +179,16 @@ void SDLContext::set_camera(const Camera & cam) { // calculate black bars if (screen_aspect > viewport_aspect) { // lettorboxing - view.h = static_cast(cam.screen.y / cam.zoom); - view.w = static_cast(cam.screen.y * viewport_aspect); - view.x = static_cast(cam.screen.x - view.w) / 2; + view.h = cam.screen.y / cam.zoom; + view.w = cam.screen.y * viewport_aspect; + view.x = (cam.screen.x - view.w) / 2; view.y = 0; } else { // pillarboxing - view.h = static_cast(cam.screen.x / viewport_aspect); - view.w = static_cast(cam.screen.x / cam.zoom); + view.h = cam.screen.x / viewport_aspect; + view.w = cam.screen.x / cam.zoom; view.x = 0; - view.y = static_cast(cam.screen.y - view.h) / 2; + view.y = (cam.screen.y - view.h) / 2; } // set drawing area SDL_RenderSetViewport(this->game_renderer.get(), &view); @@ -202,8 +202,8 @@ void SDLContext::set_camera(const Camera & cam) { SDL_Rect bg = { .x = 0, .y = 0, - .w = static_cast(cam.viewport.x), - .h = static_cast(cam.viewport.y), + .w = cam.viewport.x, + .h = cam.viewport.y, }; // fill bg color SDL_RenderFillRect(this->game_renderer.get(), &bg); -- cgit v1.2.3 From 72e605528ebe8347e1457358fe9025c215afe163 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 27 Nov 2024 11:08:50 +0100 Subject: implemented feedback, removed width so that user only can set height. --- src/crepe/api/Animator.cpp | 2 +- src/crepe/api/Sprite.cpp | 1 - src/crepe/api/Sprite.h | 2 -- src/crepe/facade/SDLContext.cpp | 13 ++----------- src/example/rendering_particle.cpp | 2 +- 5 files changed, 4 insertions(+), 16 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 0043896..b6540cf 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -22,6 +22,6 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a this->active = false; // need to do this for to get the aspect ratio for a single clipping in the spritesheet - this->spritesheet.aspect_ratio = (double) animator_rect.w / (double) animator_rect.h; + this->spritesheet.aspect_ratio = static_cast(animator_rect.w) / animator_rect.h; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 27f219c..58e0884 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -2,7 +2,6 @@ #include #include "../util/Log.h" -#include "facade/SDLContext.h" #include "Component.h" #include "Sprite.h" diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 89f9121..7d5f4c3 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -63,8 +63,6 @@ public: //! Order within the sorting layer uint8_t order_in_layer = 0; - //! width in world units - int width = 0; //! height in world units int height = 0; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 6e22316..abb3cb7 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -112,17 +112,8 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, const double & img_scale) const { - int width, height; - - if (sprite.width > sprite.height) { - width = sprite.width; - height = sprite.width / sprite.aspect_ratio; - } else { - height = sprite.height; - width = sprite.height * sprite.aspect_ratio; - } - - cout << width << " " << height << " " << " " << sprite.aspect_ratio << endl; + int width = sprite.height * sprite.aspect_ratio; + int height = sprite.height; width *= img_scale * cam.zoom; height *= img_scale * cam.zoom; diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 680c0ac..afa064d 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -35,10 +35,10 @@ int main(int argc, char * argv[]) { make_shared("asset/texture/test_ap43.png"), color, FlipSettings{true, true}); test_sprite.order_in_layer = 5; - test_sprite.width = 259; test_sprite.height = 195; //game_object.add_component(test_sprite, 4, 1, 0).active = true; + game_object.add_component(test_sprite, 1, 1, 0).active = true; /* auto & test = game_object.add_component(ParticleEmitter::Data{ -- cgit v1.2.3 From 7f66cd4a9b609f6bf36d005f5e38ef2a57d1c0d3 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 27 Nov 2024 11:15:02 +0100 Subject: removed shared_ptr from sprite --- src/crepe/api/Sprite.cpp | 9 ++++----- src/crepe/api/Sprite.h | 4 ++-- src/crepe/facade/SDLContext.cpp | 4 ++-- src/example/rendering_particle.cpp | 4 ++-- 4 files changed, 10 insertions(+), 11 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 58e0884..2c2ca65 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -1,5 +1,4 @@ #include -#include #include "../util/Log.h" @@ -10,17 +9,17 @@ using namespace std; using namespace crepe; -Sprite::Sprite(game_object_id_t id, const shared_ptr image, const Color & color, +Sprite::Sprite(game_object_id_t id, const Texture & image, const Color & color, const FlipSettings & flip) : Component(id), color(color), flip(flip), sprite_image(image), - aspect_ratio(static_cast(sprite_image->get_width()) / sprite_image->get_height()) { + aspect_ratio(static_cast(sprite_image.get_width()) / sprite_image.get_height()) { dbg_trace(); - this->sprite_rect.w = sprite_image->get_width(); - this->sprite_rect.h = sprite_image->get_height(); + this->sprite_rect.w = sprite_image.get_width(); + this->sprite_rect.h = sprite_image.get_height(); } Sprite::~Sprite() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 7d5f4c3..82dc4a7 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -44,7 +44,7 @@ public: * \param color Color tint applied to the sprite. * \param flip Flip settings for horizontal and vertical orientation. */ - Sprite(game_object_id_t id, const std::shared_ptr image, const Color & color, + Sprite(game_object_id_t id, const Texture & image, const Color & color, const FlipSettings & flip); /** @@ -53,7 +53,7 @@ public: ~Sprite(); //! Texture used for the sprite - const std::shared_ptr sprite_image; + const Texture & sprite_image; //! Color tint of the sprite Color color; //! Flip settings for the sprite diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index abb3cb7..72542e8 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -136,7 +136,7 @@ void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, const do SDL_Rect srcrect = this->get_src_rect(sprite); SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam , img_scale); - SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, + SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image.texture.get(), &srcrect, &dstrect, angle, NULL, render_flip); } @@ -150,7 +150,7 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, cam, transform.scale); - SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, + SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image.texture.get(), &srcrect, &dstrect, transform.rotation, NULL, render_flip); } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index afa064d..3589cad 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -31,8 +31,8 @@ int main(int argc, char * argv[]) { Color color(255, 255, 255, 255); - Sprite & test_sprite = game_object.add_component( - make_shared("asset/texture/test_ap43.png"), color, + auto img = Texture("asset/texture/test_ap43.png"); + Sprite & test_sprite = game_object.add_component(img, color, FlipSettings{true, true}); test_sprite.order_in_layer = 5; test_sprite.height = 195; -- cgit v1.2.3 From 2b35e8f51a3536b62ea21dc82deec1e3b65568f6 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 27 Nov 2024 11:56:48 +0100 Subject: make format and offset for the cameara relative from the transform gameobject --- src/crepe/api/Camera.h | 12 +++++++----- src/crepe/facade/SDLContext.cpp | 11 +++++------ src/crepe/facade/SDLContext.h | 3 +-- src/crepe/system/RenderSystem.cpp | 3 +++ src/example/rendering_particle.cpp | 4 ++-- 5 files changed, 18 insertions(+), 15 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index 151e5d9..1505107 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -28,17 +28,19 @@ public: //! Background color of the camera view. Color bg_color; - //! pos The position of the camera in world units - vec2 pos = {0,0}; + //! offset postion from the game object transform component + vec2 offset = {0, 0}; + + //! pos the postion of the camera in world space this will be filled with + //pos = transform + offset + vec2 pos = {0, 0}; //! screen the display size in pixels ( output resolution ) - ivec2 screen = {1080,720}; + ivec2 screen = {1080, 720}; //! viewport is the area of the world visible through the camera (in world units) - //vec2 viewport = {1000, 2000}; ivec2 viewport = {500, 1000}; - //! scale scaling factor from world units to pixel coordinates //! Zoom level of the camera view. double zoom = 1.0f; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 72542e8..4887d35 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -40,7 +40,7 @@ SDLContext::SDLContext() { auto & cfg = Config::get_instance().win_set; SDL_Window * tmp_window = SDL_CreateWindow("Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - cfg.def_size.x,cfg.def_size.y, 0); + cfg.def_size.x, cfg.def_size.y, 0); if (!tmp_window) { throw runtime_error(format("SDLContext: SDL_Window error: {}", SDL_GetError())); } @@ -134,7 +134,7 @@ void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, const do | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam , img_scale); + SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam, img_scale); SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image.texture.get(), &srcrect, &dstrect, angle, NULL, render_flip); @@ -147,8 +147,7 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect - = this->get_dst_rect(sprite, transform.position, cam, transform.scale); + SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, cam, transform.scale); SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image.texture.get(), &srcrect, &dstrect, transform.rotation, NULL, render_flip); @@ -157,9 +156,9 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const void SDLContext::set_camera(const Camera & cam) { // resize window - int w,h; + int w, h; SDL_GetWindowSize(this->game_window.get(), &w, &h); - if ( w != cam.screen.x || h != cam.screen.y) { + if (w != cam.screen.x || h != cam.screen.y) { SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 2e40b6f..35d667d 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -9,9 +9,9 @@ #include #include +#include "../api/Camera.h" #include "../api/Sprite.h" #include "../api/Transform.h" -#include "../api/Camera.h" #include "types.h" @@ -172,7 +172,6 @@ private: //! renderer for the crepe engine std::unique_ptr> game_renderer; - }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index beed9ce..9a8c1ba 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -28,8 +28,11 @@ void RenderSystem::update_camera() { for (Camera & cam : cameras) { if (!cam.active) continue; + const Transform & transform + = mgr.get_components_by_id(cam.game_object_id).front().get(); this->context.set_camera(cam); this->curr_cam_ref = &cam; + this->curr_cam_ref->pos = transform.position + this->curr_cam_ref->offset; } } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 3589cad..ec71260 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -32,8 +32,8 @@ int main(int argc, char * argv[]) { Color color(255, 255, 255, 255); auto img = Texture("asset/texture/test_ap43.png"); - Sprite & test_sprite = game_object.add_component(img, color, - FlipSettings{true, true}); + Sprite & test_sprite + = game_object.add_component(img, color, FlipSettings{true, true}); test_sprite.order_in_layer = 5; test_sprite.height = 195; -- cgit v1.2.3 From ddb5bde6e5dd4d89faf419630086ece66690d6b5 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 27 Nov 2024 19:57:16 +0100 Subject: implemented feedback. biggest changes are teh camera_ref removed --- src/crepe/api/Animator.h | 5 ----- src/crepe/api/Camera.cpp | 8 ++++++-- src/crepe/api/Camera.h | 12 ++++++------ src/crepe/api/Config.h | 4 +--- src/crepe/api/Sprite.cpp | 8 ++++++-- src/crepe/api/Sprite.h | 18 +++++++++++------- src/crepe/api/Vector2.h | 1 + src/crepe/facade/SDLContext.cpp | 3 +-- src/crepe/system/RenderSystem.cpp | 28 ++++++++++++++++------------ src/crepe/system/RenderSystem.h | 13 ++++--------- src/example/rendering_particle.cpp | 8 +++----- 11 files changed, 55 insertions(+), 53 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 53f4b91..7a5ef03 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -40,11 +40,6 @@ public: Animator(uint32_t id, Sprite & spritesheet, int row, int col, int col_animate); ~Animator(); // dbg_trace - Animator(const Animator &) = delete; - Animator(Animator &&) = delete; - Animator & operator=(const Animator &) = delete; - Animator & operator=(Animator &&) = delete; - private: //! A reference to the Sprite sheet containing the animation frames. Sprite & spritesheet; diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index 5835bdd..4397ac7 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -6,9 +6,13 @@ using namespace crepe; -Camera::Camera(game_object_id_t id, const Color & bg_color) +Camera::Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen, + const ivec2 & viewport, const double & zoom) : Component(id), - bg_color(bg_color) { + bg_color(bg_color), + screen(screen), + viewport(viewport), + zoom(zoom) { dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index 1505107..2ba37fc 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -21,12 +21,12 @@ public: * \param id Unique identifier for the camera component. * \param bg_color Background color for the camera view. */ - Camera(game_object_id_t id, const Color & bg_color); + Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen, const ivec2 & viewport, const double & zoom); ~Camera(); // dbg_trace only public: //! Background color of the camera view. - Color bg_color; + const Color bg_color; //! offset postion from the game object transform component vec2 offset = {0, 0}; @@ -36,19 +36,19 @@ public: vec2 pos = {0, 0}; //! screen the display size in pixels ( output resolution ) - ivec2 screen = {1080, 720}; + const ivec2 screen = {1080, 720}; //! viewport is the area of the world visible through the camera (in world units) - ivec2 viewport = {500, 1000}; + const ivec2 viewport = {500, 1000}; //! Zoom level of the camera view. - double zoom = 1.0f; + const double zoom = 1.0f; public: /** * \brief Gets the maximum number of camera instances allowed. * \return Maximum instance count as an integer. */ - virtual int get_instances_max() const { return 10; } + virtual int get_instances_max() const { return 1; } }; } // namespace crepe diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 2723461..9b43cdf 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -66,9 +66,7 @@ public: //! default window settings struct { //TODO make this constexpr because this will never change - ivec2 def_size = {1080, 720}; - vec2 pos = {0, 0}; - float zoom = 1.0f; + const ivec2 def_size = {1080, 720}; } win_set; //! Asset loading options diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 2c2ca65..21c8377 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -10,16 +10,20 @@ using namespace std; using namespace crepe; Sprite::Sprite(game_object_id_t id, const Texture & image, const Color & color, - const FlipSettings & flip) + const FlipSettings & flip, uint8_t sort_layer, uint8_t order_layer, int height) : Component(id), color(color), flip(flip), sprite_image(image), - aspect_ratio(static_cast(sprite_image.get_width()) / sprite_image.get_height()) { + sorting_in_layer(sort_layer), + order_in_layer(order_layer), + height(height) { + dbg_trace(); this->sprite_rect.w = sprite_image.get_width(); this->sprite_rect.h = sprite_image.get_height(); + this->aspect_ratio = static_cast(this->sprite_rect.w) / this->sprite_rect.h; } Sprite::~Sprite() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 82dc4a7..1c40501 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -1,12 +1,10 @@ #pragma once -#include -#include - #include "../Component.h" #include "Color.h" #include "Texture.h" +#include namespace crepe { @@ -43,9 +41,12 @@ public: * \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, const Texture & image, const Color & color, - const FlipSettings & flip); + const FlipSettings & flip, uint8_t sort_layer, uint8_t order_layer, int height); /** * \brief Destroys the Sprite instance. @@ -54,17 +55,20 @@ public: //! 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 - uint8_t sorting_in_layer = 0; + const uint8_t sorting_in_layer; //! Order within the sorting layer - uint8_t order_in_layer = 0; + const uint8_t order_in_layer; //! height in world units - int height = 0; + const int height; /** * \aspect_ratio ratio of the img so that scaling will not become weird diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h index 0688fac..2b31d90 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -37,6 +37,7 @@ struct Vector2 { //! Divides a scalar value to both components of this vector and updates this vector. Vector2 operator/(const T & other) const; + //! Divides a scalar value to both components of this vector and updates this vector. Vector2 operator/(T other) const; //! Adds another vector to this vector and updates this vector. diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 4887d35..6c6af55 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include @@ -16,10 +15,10 @@ #include "../api/Sprite.h" #include "../api/Texture.h" #include "../api/Transform.h" +#include "../api/Config.h" #include "../util/Log.h" #include "SDLContext.h" -#include "api/Config.h" #include "types.h" using namespace crepe; diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 9a8c1ba..a1443cd 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -10,6 +10,7 @@ #include "../api/Sprite.h" #include "../api/Transform.h" #include "../facade/SDLContext.h" +#include "api/Camera.h" #include "RenderSystem.h" @@ -19,7 +20,8 @@ using namespace std; void RenderSystem::clear_screen() { this->context.clear_screen(); } void RenderSystem::present_screen() { this->context.present_screen(); } -void RenderSystem::update_camera() { + +const Camera & RenderSystem::update_camera() { ComponentManager & mgr = this->component_manager; RefVector cameras = mgr.get_components_by_type(); @@ -31,9 +33,10 @@ void RenderSystem::update_camera() { const Transform & transform = mgr.get_components_by_id(cam.game_object_id).front().get(); this->context.set_camera(cam); - this->curr_cam_ref = &cam; - this->curr_cam_ref->pos = transform.position + this->curr_cam_ref->offset; + cam.pos = transform.position + cam.offset; + return cam; } + throw std::runtime_error("No active cameras in current scene"); } bool sorting_comparison(const Sprite & a, const Sprite & b) { @@ -52,12 +55,12 @@ RefVector RenderSystem::sort(RefVector & objs) const { void RenderSystem::update() { this->clear_screen(); - this->update_camera(); this->render(); this->present_screen(); } -bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) { +bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, + const double & scale) { ComponentManager & mgr = this->component_manager; @@ -73,19 +76,20 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) for (const Particle & p : em.data.particles) { if (!p.active) continue; - this->context.draw_particle(sprite, p.position, p.angle, scale, - *this->curr_cam_ref); + this->context.draw_particle(sprite, p.position, p.angle, scale, cam); } } return rendering_particles; } -void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { - this->context.draw(sprite, tm, *this->curr_cam_ref); +void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam, + const Transform & tm) { + this->context.draw(sprite, tm, cam); } void RenderSystem::render() { - ComponentManager & mgr = this->component_manager; + const Camera & cam = this->update_camera(); + RefVector sprites = mgr.get_components_by_type(); RefVector sorted_sprites = this->sort(sprites); @@ -94,10 +98,10 @@ void RenderSystem::render() { const Transform & transform = mgr.get_components_by_id(sprite.game_object_id).front().get(); - bool rendered_particles = this->render_particle(sprite, transform.scale); + bool rendered_particles = this->render_particle(sprite, cam, transform.scale); if (rendered_particles) continue; - this->render_normal(sprite, transform); + this->render_normal(sprite, cam, transform); } } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 08930b0..264dc4c 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -36,7 +36,7 @@ private: void present_screen(); //! Updates the active camera used for rendering. - void update_camera(); + const Camera & update_camera(); //! Renders the whole screen void render(); @@ -48,7 +48,7 @@ private: * \param tm the Transform component for scale * \return true if particles have been rendered */ - bool render_particle(const Sprite & sprite, const double & scale); + bool render_particle(const Sprite & sprite, const Camera & cam, const double & scale); /** * \brief renders a sprite with a Transform component on the screen @@ -56,7 +56,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 Transform & tm); + void render_normal(const Sprite & sprite, const Camera & cam, const Transform & tm); /** * \brief sort a vector sprite objects with @@ -70,17 +70,12 @@ private: * \todo Include color handling for sprites. * \todo Add text rendering using SDL_ttf for text components. * \todo Implement a text component and a button component. - * \todo Ensure each sprite is checked for active status before rendering. - * \todo Sort all layers by order before rendering. * \todo Consider adding text input functionality. */ private: - //! Pointer to the current active camera for rendering - // TODO: needs a better solution - Camera * curr_cam_ref = nullptr; - SDLContext & context = SDLContext::get_instance(); + }; } // namespace crepe diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index ec71260..6e426cf 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -33,9 +33,7 @@ int main(int argc, char * argv[]) { auto img = Texture("asset/texture/test_ap43.png"); Sprite & test_sprite - = game_object.add_component(img, color, FlipSettings{true, true}); - test_sprite.order_in_layer = 5; - test_sprite.height = 195; + = game_object.add_component(img, color, FlipSettings{true, true}, 1, 1, 500); //game_object.add_component(test_sprite, 4, 1, 0).active = true; game_object.add_component(test_sprite, 1, 1, 0).active = true; @@ -62,8 +60,8 @@ int main(int argc, char * argv[]) { }); */ - auto & cam = game_object.add_component(Color::WHITE); - cam.pos = {0, 0}; + auto & cam = game_object.add_component(Color::WHITE, ivec2{1080, 720}, + ivec2{2000, 2000}, 1.0f); /* game_object -- cgit v1.2.3 From 14cbc9061ecd7ab1c9e58e5f9ba75a5216e1de2b Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Thu, 28 Nov 2024 07:53:24 +0100 Subject: feedback implemented --- src/crepe/api/Camera.cpp | 4 +++- src/crepe/api/Camera.h | 4 ++-- src/crepe/api/Sprite.h | 10 ---------- 3 files changed, 5 insertions(+), 13 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index 4397ac7..99a0831 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -1,3 +1,4 @@ +#include "types.h" #include "util/Log.h" #include "Camera.h" @@ -7,9 +8,10 @@ using namespace crepe; Camera::Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen, - const ivec2 & viewport, const double & zoom) + const ivec2 & viewport, const double & zoom, const vec2& offset) : Component(id), bg_color(bg_color), + offset(offset), screen(screen), viewport(viewport), zoom(zoom) { diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index 2ba37fc..8908caa 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -21,7 +21,7 @@ public: * \param id Unique identifier for the camera component. * \param bg_color Background color for the camera view. */ - Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen, const ivec2 & viewport, const double & zoom); + Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen, const ivec2 & viewport, const double & zoom, const vec2 & offset = {0,0}); ~Camera(); // dbg_trace only public: @@ -29,7 +29,7 @@ public: const Color bg_color; //! offset postion from the game object transform component - vec2 offset = {0, 0}; + vec2 offset; //! pos the postion of the camera in world space this will be filled with //pos = transform + offset diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 1c40501..9644e72 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -77,16 +77,6 @@ public: * does it need to be calculated again in the Animator */ double aspect_ratio; - -public: - /** - * \brief Gets the maximum number of instances allowed for this sprite. - * \return Maximum instance count as an integer. - * - * For now is this number randomly picked. I think it will eventually be 1. - */ - virtual int get_instances_max() const { return 10; } - private: //! Reads the sprite_rect of sprite friend class SDLContext; -- cgit v1.2.3 From 43695ecee65d63ba89bdab3eb49b5ada0eef399f Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Thu, 28 Nov 2024 07:54:04 +0100 Subject: make format --- src/crepe/api/Animator.h | 1 + src/crepe/api/Camera.cpp | 2 +- src/crepe/api/Camera.h | 3 ++- src/crepe/api/Sprite.h | 1 + src/crepe/facade/SDLContext.cpp | 2 +- src/crepe/system/RenderSystem.h | 1 - 6 files changed, 6 insertions(+), 4 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 7a5ef03..19c9ebd 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -40,6 +40,7 @@ public: Animator(uint32_t id, Sprite & spritesheet, int row, int col, int col_animate); ~Animator(); // dbg_trace + private: //! A reference to the Sprite sheet containing the animation frames. Sprite & spritesheet; diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index 99a0831..0831f45 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -8,7 +8,7 @@ using namespace crepe; Camera::Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen, - const ivec2 & viewport, const double & zoom, const vec2& offset) + const ivec2 & viewport, const double & zoom, const vec2 & offset) : Component(id), bg_color(bg_color), offset(offset), diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index 8908caa..c7b2d08 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -21,7 +21,8 @@ public: * \param id Unique identifier for the camera component. * \param bg_color Background color for the camera view. */ - Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen, const ivec2 & viewport, const double & zoom, const vec2 & offset = {0,0}); + Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen, + const ivec2 & viewport, const double & zoom, const vec2 & offset = {0, 0}); ~Camera(); // dbg_trace only public: diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 9644e72..c406b91 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -77,6 +77,7 @@ public: * does it need to be calculated again in the Animator */ double aspect_ratio; + private: //! Reads the sprite_rect of sprite friend class SDLContext; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 6c6af55..0d42a4c 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -12,10 +12,10 @@ #include #include "../api/Camera.h" +#include "../api/Config.h" #include "../api/Sprite.h" #include "../api/Texture.h" #include "../api/Transform.h" -#include "../api/Config.h" #include "../util/Log.h" #include "SDLContext.h" diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 264dc4c..46ebb92 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -75,7 +75,6 @@ private: private: SDLContext & context = SDLContext::get_instance(); - }; } // namespace crepe -- cgit v1.2.3 From 80836bce1294898f3d115ed363edd6d921aa15d5 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Thu, 28 Nov 2024 08:21:24 +0100 Subject: adjusted texture and sprite to hold a texture, instead of reference --- src/crepe/api/Config.h | 2 +- src/crepe/api/Sprite.cpp | 5 +++-- src/crepe/api/Sprite.h | 4 ++-- src/crepe/api/Texture.cpp | 11 +++++++++++ src/crepe/api/Texture.h | 5 +++++ 5 files changed, 22 insertions(+), 5 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 9b43cdf..2525120 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -33,7 +33,7 @@ public: * * Only messages with equal or higher priority than this value will be logged. */ - Log::Level level = Log::Level::DEBUG; + Log::Level level = Log::Level::INFO; /** * \brief Colored log output * diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 21c8377..65c6cc3 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -1,4 +1,5 @@ #include +#include #include "../util/Log.h" @@ -9,12 +10,12 @@ using namespace std; using namespace crepe; -Sprite::Sprite(game_object_id_t id, const Texture & image, const Color & color, +Sprite::Sprite(game_object_id_t id, Texture & image, const Color & color, const FlipSettings & flip, uint8_t sort_layer, uint8_t order_layer, int height) : Component(id), color(color), flip(flip), - sprite_image(image), + sprite_image(std::move(image)), sorting_in_layer(sort_layer), order_in_layer(order_layer), height(height) { diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index c406b91..9d75ab6 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -45,7 +45,7 @@ public: * \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, const Texture & image, const Color & color, + Sprite(game_object_id_t id, Texture & image, const Color & color, const FlipSettings & flip, uint8_t sort_layer, uint8_t order_layer, int height); /** @@ -54,7 +54,7 @@ public: ~Sprite(); //! Texture used for the sprite - const Texture & sprite_image; + const Texture sprite_image; //! Color tint of the sprite Color color; diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index eeb86e9..576bdc3 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -17,6 +17,17 @@ Texture::~Texture() { this->texture.reset(); } +Texture::Texture(Texture&& other) noexcept + : texture(std::move(other.texture)){ +} + +Texture& Texture::operator=(Texture&& other) noexcept { + if (this != &other) { + texture = std::move(other.texture); + } + return *this; +} + void Texture::load(const Asset & res) { SDLContext & ctx = SDLContext::get_instance(); this->texture = ctx.texture_from_path(res.get_path()); diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h index b4f7d07..dc4a6d7 100644 --- a/src/crepe/api/Texture.h +++ b/src/crepe/api/Texture.h @@ -35,6 +35,11 @@ public: */ ~Texture(); // FIXME: this constructor shouldn't be necessary because this class doesn't manage memory + + Texture(Texture&& other) noexcept; + Texture& operator=(Texture&& other) noexcept; + Texture(const Texture&) = delete; + Texture& operator=(const Texture&) = delete; /** * \brief Gets the width of the texture. -- cgit v1.2.3 From 5fa57ab2d8f809b8cb6bd72f54567a01f3e63b95 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Thu, 28 Nov 2024 08:51:20 +0100 Subject: making test work again --- src/crepe/api/Config.h | 2 +- src/crepe/api/Vector2.h | 13 ++++------ src/test/ParticleTest.cpp | 7 +++--- src/test/RenderSystemTest.cpp | 58 +++++++++++++++++++------------------------ 4 files changed, 35 insertions(+), 45 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 2525120..d73e488 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 - const ivec2 def_size = {1080, 720}; + ivec2 def_size = {1080, 720}; } win_set; //! Asset loading options diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h index 2b31d90..c278c87 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -31,14 +31,8 @@ struct Vector2 { //! Divides this vector by another vector element-wise and returns the result. Vector2 operator/(const Vector2 & other) const; - //! Multiplies this vector by another vector element-wise and updates this vector. - Vector2 & operator*=(const Vector2 & other); - - //! Divides a scalar value to both components of this vector and updates this vector. - Vector2 operator/(const T & other) const; - - //! Divides a scalar value to both components of this vector and updates this vector. - Vector2 operator/(T other) const; + //! Divides this vector by a scalar and returns the result. + Vector2 operator/(T scalar) const; //! Adds another vector to this vector and updates this vector. Vector2 & operator+=(const Vector2 & other); @@ -52,6 +46,9 @@ struct Vector2 { //! Subtracts a scalar value from both components of this vector and updates this vector. Vector2 & operator-=(T other); + //! Multiplies this vector by another vector element-wise and updates this vector. + Vector2 & operator*=(const Vector2 & other); + //! Multiplies this vector by a scalar and updates this vector. Vector2 & operator*=(T other); diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 8b81e74..ea3652a 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -1,4 +1,4 @@ -#include "api/Vector2.h" +#include "api/Texture.h" #include #include #include @@ -28,9 +28,10 @@ public: GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 0); Color color(0, 0, 0, 0); + auto s1 = Texture("asset/texture/img.png"); Sprite & test_sprite = game_object.add_component( - make_shared("asset/texture/img.png"), color, - FlipSettings{true, true}); + s1, color, + FlipSettings{true, true}, 1,1,100); game_object.add_component(ParticleEmitter::Data{ .position = {0, 0}, diff --git a/src/test/RenderSystemTest.cpp b/src/test/RenderSystemTest.cpp index f37fb56..2c4f21c 100644 --- a/src/test/RenderSystemTest.cpp +++ b/src/test/RenderSystemTest.cpp @@ -1,4 +1,4 @@ -#include "api/Camera.h" +#include "types.h" #include #include #include @@ -12,6 +12,7 @@ #include #include #include +#include "crepe/api/Camera.h" #include @@ -29,40 +30,30 @@ public: GameObject entity4 = this->mgr.new_object("name"); void SetUp() override { - auto & sprite1 - = entity1.add_component(make_shared("asset/texture/img.png"), - Color(0, 0, 0, 0), FlipSettings{false, false}); - ASSERT_NE(sprite1.sprite_image.get(), nullptr); - sprite1.order_in_layer = 5; - sprite1.sorting_in_layer = 5; + auto s1 = Texture("asset/texture/img.png"); + auto s2 = Texture("asset/texture/img.png"); + auto s3 = Texture("asset/texture/img.png"); + auto s4 = Texture("asset/texture/img.png"); + auto & sprite1 = entity1.add_component(s1, Color(0, 0, 0, 0), + FlipSettings{false, false}, 5, 5, 100); + ASSERT_NE(sprite1.sprite_image.texture.get(), nullptr); EXPECT_EQ(sprite1.order_in_layer, 5); EXPECT_EQ(sprite1.sorting_in_layer, 5); - auto & sprite2 - = entity2.add_component(make_shared("asset/texture/img.png"), - Color(0, 0, 0, 0), FlipSettings{false, false}); - ASSERT_NE(sprite2.sprite_image.get(), nullptr); - sprite2.sorting_in_layer = 2; - sprite2.order_in_layer = 1; - + auto & sprite2 = entity2.add_component(s2, Color(0, 0, 0, 0), + FlipSettings{false, false}, 2, 1, 100); + ASSERT_NE(sprite2.sprite_image.texture.get(), nullptr); EXPECT_EQ(sprite2.sorting_in_layer, 2); EXPECT_EQ(sprite2.order_in_layer, 1); - auto & sprite3 - = entity3.add_component(make_shared("asset/texture/img.png"), - Color(0, 0, 0, 0), FlipSettings{false, false}); - ASSERT_NE(sprite3.sprite_image.get(), nullptr); - sprite3.sorting_in_layer = 1; - sprite3.order_in_layer = 2; - + auto & sprite3 = entity3.add_component(s3, Color(0, 0, 0, 0), + FlipSettings{false, false}, 1, 2, 100); + ASSERT_NE(sprite3.sprite_image.texture.get(), nullptr); EXPECT_EQ(sprite3.sorting_in_layer, 1); EXPECT_EQ(sprite3.order_in_layer, 2); - auto & sprite4 - = entity4.add_component(make_shared("asset/texture/img.png"), - Color(0, 0, 0, 0), FlipSettings{false, false}); - ASSERT_NE(sprite4.sprite_image.get(), nullptr); - sprite4.sorting_in_layer = 1; - sprite4.order_in_layer = 1; + auto & sprite4 = entity4.add_component(s4, Color(0, 0, 0, 0), + FlipSettings{false, false}, 1, 1, 100); + ASSERT_NE(sprite4.sprite_image.texture.get(), nullptr); EXPECT_EQ(sprite4.sorting_in_layer, 1); EXPECT_EQ(sprite4.order_in_layer, 1); } @@ -73,8 +64,9 @@ TEST_F(RenderSystemTest, expected_throws) { // no texture img EXPECT_ANY_THROW({ - entity1.add_component(make_shared("NO_IMAGE"), Color(0, 0, 0, 0), - FlipSettings{false, false}); + auto test = Texture(""); + entity1.add_component(test, Color(0, 0, 0, 0), + FlipSettings{false, false},1,1,100); }); // No camera @@ -121,7 +113,7 @@ TEST_F(RenderSystemTest, sorting_sprites) { } TEST_F(RenderSystemTest, Update) { - entity1.add_component(Color::WHITE); + entity1.add_component(Color::WHITE, ivec2{1080, 720}, ivec2{2000, 2000}, 1.0f); { vector> sprites = this->mgr.get_components_by_type(); ASSERT_EQ(sprites.size(), 4); @@ -149,7 +141,7 @@ TEST_F(RenderSystemTest, Camera) { EXPECT_NE(cameras.size(), 1); } { - entity1.add_component(Color::WHITE); + entity1.add_component(Color::WHITE, ivec2{1080, 720}, ivec2{2000, 2000}, 1.0f); auto cameras = this->mgr.get_components_by_type(); EXPECT_EQ(cameras.size(), 1); } @@ -157,9 +149,9 @@ TEST_F(RenderSystemTest, Camera) { //TODO improve with newer version } TEST_F(RenderSystemTest, Color) { - entity1.add_component(Color::WHITE); + entity1.add_component(Color::WHITE, ivec2{1080, 720}, ivec2{2000, 2000}, 1.0f); auto & sprite = this->mgr.get_components_by_id(entity1.id).front().get(); - ASSERT_NE(sprite.sprite_image.get(), nullptr); + ASSERT_NE(sprite.sprite_image.texture.get(), nullptr); sprite.color = Color::GREEN; EXPECT_EQ(sprite.color.r, Color::GREEN.r); -- cgit v1.2.3 From 71be3e36dbb402c3e84d87ea0255c08cb2a1b7ca Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Thu, 28 Nov 2024 09:18:13 +0100 Subject: implemented feedback to have draw struct --- src/crepe/api/Camera.h | 4 ---- src/crepe/facade/SDLContext.cpp | 35 +++++++++++------------------------ src/crepe/facade/SDLContext.h | 22 ++++------------------ src/crepe/system/RenderSystem.cpp | 25 ++++++++++++++++++++++--- src/crepe/system/RenderSystem.h | 6 ++++++ src/crepe/types.h | 13 +++++++++++++ 6 files changed, 56 insertions(+), 49 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index c7b2d08..ac56495 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -32,10 +32,6 @@ public: //! offset postion from the game object transform component vec2 offset; - //! pos the postion of the camera in world space this will be filled with - //pos = transform + offset - vec2 pos = {0, 0}; - //! screen the display size in pixels ( output resolution ) const ivec2 screen = {1080, 720}; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 0d42a4c..7317a77 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -109,7 +109,7 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { }; } SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, - const double & img_scale) const { + const vec2 & cam_pos, const double & img_scale) const { int width = sprite.height * sprite.aspect_ratio; int height = sprite.height; @@ -118,38 +118,25 @@ SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const height *= img_scale * cam.zoom; return SDL_Rect{ - .x = static_cast((pos.x - cam.pos.x + (cam.viewport.x / 2) - width / 2)), - .y = static_cast((pos.y - cam.pos.y + (cam.viewport.y / 2) - height / 2)), + .x = static_cast((pos.x - cam_pos.x + (cam.viewport.x / 2) - width / 2)), + .y = static_cast((pos.y - cam_pos.y + (cam.viewport.y / 2) - height / 2)), .w = width, .h = height, }; } -void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle, - const double & img_scale, const Camera & cam) { +void SDLContext::draw(const RenderCtx & ctx) { SDL_RendererFlip render_flip - = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) - | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); + = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * ctx.sprite.flip.flip_x) + | (SDL_FLIP_VERTICAL * ctx.sprite.flip.flip_y)); - SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam, img_scale); + SDL_Rect srcrect = this->get_src_rect(ctx.sprite); + SDL_Rect dstrect + = this->get_dst_rect(ctx.sprite, ctx.pos, ctx.cam, ctx.cam_pos, ctx.scale); - SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image.texture.get(), &srcrect, - &dstrect, angle, NULL, render_flip); -} - -void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam) { - - SDL_RendererFlip render_flip - = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) - | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); - - SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, cam, transform.scale); - - SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image.texture.get(), &srcrect, - &dstrect, transform.rotation, NULL, render_flip); + SDL_RenderCopyEx(this->game_renderer.get(), ctx.sprite.sprite_image.texture.get(), + &srcrect, &dstrect, ctx.angle, NULL, render_flip); } void SDLContext::set_camera(const Camera & cam) { diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 35d667d..7907a0f 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -11,7 +11,6 @@ #include "../api/Camera.h" #include "../api/Sprite.h" -#include "../api/Transform.h" #include "types.h" @@ -115,23 +114,9 @@ private: /** * \brief Draws a sprite to the screen using the specified transform and camera. - * \param sprite Reference to the Sprite to draw. - * \param transform Reference to the Transform for positioning. - * \param cam camera of the current scene + * \param RenderCtx Reference to rendering data to draw */ - void draw(const Sprite & sprite, const Transform & transform, const Camera & cam); - - /** - * \brief Draws a particle to the screen using the specified parameters - * - * \param sprite Referenceto the sprite to draw - * \param pos particle position in world units - * \param angle particle angle in degrees - * \param img_scale scalar multiplier to increase image size - * \param cam camera of the current scene - */ - void draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle, - const double & img_scale, const Camera & cam); + void draw(const RenderCtx & ctx); //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -160,10 +145,11 @@ private: * \param sprite Reference to the sprite to calculate rectangle * \param pos the pos in world units * \param cam the camera of the current scene + * \param cam_pos the current postion of the camera * \param img_scale the image multiplier for increasing img size * \return sdl rectangle to draw a dst image to draw on the screen */ - SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, + SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, const vec2 & cam_pos, const double & img_scale) const; private: diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index a1443cd..bfee658 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -11,6 +11,7 @@ #include "../api/Transform.h" #include "../facade/SDLContext.h" #include "api/Camera.h" +#include "types.h" #include "RenderSystem.h" @@ -33,7 +34,7 @@ const Camera & RenderSystem::update_camera() { const Transform & transform = mgr.get_components_by_id(cam.game_object_id).front().get(); this->context.set_camera(cam); - cam.pos = transform.position + cam.offset; + this->cam_pos = transform.position + cam.offset; return cam; } throw std::runtime_error("No active cameras in current scene"); @@ -76,14 +77,32 @@ bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, for (const Particle & p : em.data.particles) { if (!p.active) continue; - this->context.draw_particle(sprite, p.position, p.angle, scale, cam); + + RenderCtx ctx{ + .sprite = sprite, + .cam = cam, + .cam_pos = this->cam_pos, + .pos = p.position, + .angle = p.angle, + .scale = scale, + }; + this->context.draw(ctx); } } return rendering_particles; } void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam, const Transform & tm) { - this->context.draw(sprite, tm, cam); + + RenderCtx ctx{ + .sprite = sprite, + .cam = cam, + .cam_pos = this->cam_pos, + .pos = tm.position, + .angle = tm.rotation, + .scale = tm.scale, + }; + this->context.draw(ctx); } void RenderSystem::render() { diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 46ebb92..4667424 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -11,6 +11,8 @@ namespace crepe { class Camera; class Sprite; +class Transform; + /** * \class RenderSystem @@ -75,6 +77,10 @@ private: private: SDLContext & context = SDLContext::get_instance(); + + //! camera postion in the current scene + vec2 cam_pos = {0,0}; + }; } // namespace crepe diff --git a/src/crepe/types.h b/src/crepe/types.h index 17f1619..aa03f53 100644 --- a/src/crepe/types.h +++ b/src/crepe/types.h @@ -27,4 +27,17 @@ typedef Vector2 vec2; //! Default Vector2 type typedef Vector2 dvec2; +class Sprite; +class Camera; + +struct RenderCtx{ + const Sprite & sprite; + const Camera & cam; + const vec2 & cam_pos; + const vec2 & pos; + const double & angle; + const double & scale; + +} ; + } // namespace crepe -- cgit v1.2.3 From 2d623522db0677ca5e88a53e3705a10ce59ba8b6 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Thu, 28 Nov 2024 10:04:14 +0100 Subject: removed animator rectangle --- src/crepe/api/Animator.cpp | 11 +++++------ src/crepe/api/Animator.h | 2 -- src/crepe/api/Camera.h | 6 +++--- src/crepe/system/AnimatorSystem.cpp | 11 +++++------ src/crepe/system/RenderSystem.h | 2 +- 5 files changed, 14 insertions(+), 18 deletions(-) (limited to 'src/crepe') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index b6540cf..31b9632 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -14,14 +14,13 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a col(col) { dbg_trace(); - animator_rect = spritesheet.sprite_rect; - animator_rect.h /= col; - animator_rect.w /= row; - animator_rect.x = 0; - animator_rect.y = col_animator * animator_rect.h; + this->spritesheet.sprite_rect.h /= col; + this->spritesheet.sprite_rect.w /= row; + this->spritesheet.sprite_rect.x = 0; + this->spritesheet.sprite_rect.y = col_animator * this->spritesheet.sprite_rect.h; this->active = false; // need to do this for to get the aspect ratio for a single clipping in the spritesheet - this->spritesheet.aspect_ratio = static_cast(animator_rect.w) / animator_rect.h; + this->spritesheet.aspect_ratio = static_cast(this->spritesheet.sprite_rect.w) / this->spritesheet.sprite_rect.h; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 19c9ebd..6c506aa 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -57,8 +57,6 @@ private: //! The current row being animated. int curr_row = 0; - Rect animator_rect; - //TODO: Is this necessary? //int fps; diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index ac56495..3682222 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -33,13 +33,13 @@ public: vec2 offset; //! screen the display size in pixels ( output resolution ) - const ivec2 screen = {1080, 720}; + const ivec2 screen; //! viewport is the area of the world visible through the camera (in world units) - const ivec2 viewport = {500, 1000}; + const ivec2 viewport; //! Zoom level of the camera view. - const double zoom = 1.0f; + const double zoom; public: /** diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index bc94253..e5b277f 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -15,11 +15,10 @@ void AnimatorSystem::update() { uint64_t tick = SDLContext::get_instance().get_ticks(); for (Animator & a : animations) { - if (a.active) { - // (10 frames per second) - a.curr_row = (tick / 100) % a.row; - a.animator_rect.x = (a.curr_row * a.animator_rect.w) + a.curr_col; - a.spritesheet.sprite_rect = a.animator_rect; - } + if (!a.active) continue; + // (10 frames per second) + a.curr_row = (tick / 100) % a.row; + a.spritesheet.sprite_rect.x = (a.curr_row * a.spritesheet.sprite_rect.w) + a.curr_col; + a.spritesheet.sprite_rect = a.spritesheet.sprite_rect; } } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 4667424..7279b5c 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -79,7 +79,7 @@ private: SDLContext & context = SDLContext::get_instance(); //! camera postion in the current scene - vec2 cam_pos = {0,0}; + vec2 cam_pos; }; -- cgit v1.2.3 From 6fd7cec7d4bbf5aeb361b3f1337671bb0f9af61b Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 28 Nov 2024 10:13:11 +0100 Subject: manager mediator refactor --- src/crepe/CMakeLists.txt | 4 +- src/crepe/ComponentManager.cpp | 60 ------------ src/crepe/ComponentManager.h | 159 ------------------------------ src/crepe/ComponentManager.hpp | 161 ------------------------------ src/crepe/api/BehaviorScript.cpp | 6 +- src/crepe/api/BehaviorScript.h | 9 +- src/crepe/api/BehaviorScript.hpp | 4 +- src/crepe/api/CMakeLists.txt | 8 -- src/crepe/api/EventManager.cpp | 46 --------- src/crepe/api/EventManager.h | 161 ------------------------------ src/crepe/api/EventManager.hpp | 36 ------- src/crepe/api/GameObject.hpp | 2 +- src/crepe/api/IKeyListener.h | 3 +- src/crepe/api/IMouseListener.h | 3 +- src/crepe/api/LoopManager.cpp | 25 ++--- src/crepe/api/LoopManager.h | 19 +++- src/crepe/api/LoopManager.hpp | 7 +- src/crepe/api/SaveManager.cpp | 173 --------------------------------- src/crepe/api/SaveManager.h | 114 ---------------------- src/crepe/api/Scene.h | 7 +- src/crepe/api/SceneManager.cpp | 34 ------- src/crepe/api/SceneManager.h | 53 ---------- src/crepe/api/SceneManager.hpp | 25 ----- src/crepe/api/Script.cpp | 17 +++- src/crepe/api/Script.h | 16 +-- src/crepe/api/Script.hpp | 8 +- src/crepe/manager/CMakeLists.txt | 20 ++++ src/crepe/manager/ComponentManager.cpp | 63 ++++++++++++ src/crepe/manager/ComponentManager.h | 161 ++++++++++++++++++++++++++++++ src/crepe/manager/ComponentManager.hpp | 161 ++++++++++++++++++++++++++++++ src/crepe/manager/EventManager.cpp | 46 +++++++++ src/crepe/manager/EventManager.h | 161 ++++++++++++++++++++++++++++++ src/crepe/manager/EventManager.hpp | 36 +++++++ src/crepe/manager/Manager.cpp | 6 ++ src/crepe/manager/Manager.h | 17 ++++ src/crepe/manager/Mediator.h | 29 ++++++ src/crepe/manager/SaveManager.cpp | 173 +++++++++++++++++++++++++++++++++ src/crepe/manager/SaveManager.h | 114 ++++++++++++++++++++++ src/crepe/manager/SceneManager.cpp | 35 +++++++ src/crepe/manager/SceneManager.h | 52 ++++++++++ src/crepe/manager/SceneManager.hpp | 25 +++++ src/crepe/system/AnimatorSystem.cpp | 6 +- src/crepe/system/ParticleSystem.cpp | 6 +- src/crepe/system/PhysicsSystem.cpp | 2 +- src/crepe/system/RenderSystem.cpp | 3 +- src/crepe/system/ScriptSystem.cpp | 2 +- src/crepe/system/System.cpp | 2 +- src/crepe/system/System.h | 4 +- src/test/DBTest.cpp | 3 +- src/test/ECSTest.cpp | 5 +- src/test/EventTest.cpp | 11 ++- src/test/ParticleTest.cpp | 8 +- src/test/PhysicsTest.cpp | 7 +- src/test/RenderSystemTest.cpp | 9 +- src/test/SceneManagerTest.cpp | 23 +++-- src/test/ScriptTest.cpp | 11 ++- 56 files changed, 1240 insertions(+), 1121 deletions(-) delete mode 100644 src/crepe/ComponentManager.cpp delete mode 100644 src/crepe/ComponentManager.h delete mode 100644 src/crepe/ComponentManager.hpp delete mode 100644 src/crepe/api/EventManager.cpp delete mode 100644 src/crepe/api/EventManager.h delete mode 100644 src/crepe/api/EventManager.hpp delete mode 100644 src/crepe/api/SaveManager.cpp delete mode 100644 src/crepe/api/SaveManager.h delete mode 100644 src/crepe/api/SceneManager.cpp delete mode 100644 src/crepe/api/SceneManager.h delete mode 100644 src/crepe/api/SceneManager.hpp create mode 100644 src/crepe/manager/CMakeLists.txt create mode 100644 src/crepe/manager/ComponentManager.cpp create mode 100644 src/crepe/manager/ComponentManager.h create mode 100644 src/crepe/manager/ComponentManager.hpp create mode 100644 src/crepe/manager/EventManager.cpp create mode 100644 src/crepe/manager/EventManager.h create mode 100644 src/crepe/manager/EventManager.hpp create mode 100644 src/crepe/manager/Manager.cpp create mode 100644 src/crepe/manager/Manager.h create mode 100644 src/crepe/manager/Mediator.h create mode 100644 src/crepe/manager/SaveManager.cpp create mode 100644 src/crepe/manager/SaveManager.h create mode 100644 src/crepe/manager/SceneManager.cpp create mode 100644 src/crepe/manager/SceneManager.h create mode 100644 src/crepe/manager/SceneManager.hpp (limited to 'src/crepe') diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 7e176e7..da9d492 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -1,13 +1,10 @@ target_sources(crepe PUBLIC Particle.cpp - ComponentManager.cpp Component.cpp Collider.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES - ComponentManager.h - ComponentManager.hpp Component.h Collider.h ValueBroker.h @@ -16,6 +13,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES add_subdirectory(api) add_subdirectory(facade) +add_subdirectory(manager) add_subdirectory(system) add_subdirectory(util) diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp deleted file mode 100644 index 5b73009..0000000 --- a/src/crepe/ComponentManager.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "api/GameObject.h" -#include "util/Log.h" - -#include "ComponentManager.h" -#include "types.h" - -using namespace crepe; -using namespace std; - -ComponentManager::ComponentManager() { dbg_trace(); } -ComponentManager::~ComponentManager() { dbg_trace(); } - -void ComponentManager::delete_all_components_of_id(game_object_id_t id) { - // Do not delete persistent objects - if (this->persistent[id]) { - return; - } - - // Loop through all the types (in the unordered_map<>) - for (auto & [type, component_array] : this->components) { - // Make sure that the id (that we are looking for) is within the boundaries of the vector<> - if (id < component_array.size()) { - // Clear the components at this specific id - component_array[id].clear(); - } - } -} - -void ComponentManager::delete_all_components() { - // Loop through all the types (in the unordered_map<>) - for (auto & [type, component_array] : this->components) { - // Loop through all the ids (in the vector<>) - for (game_object_id_t id = 0; id < component_array.size(); id++) { - // Do not delete persistent objects - if (!this->persistent[id]) { - // Clear the components at this specific id - component_array[id].clear(); - } - } - } - - this->next_id = 0; -} - -GameObject ComponentManager::new_object(const string & name, const string & tag, - const vec2 & position, double rotation, double scale) { - // Find the first available id (taking persistent objects into account) - while (this->persistent[this->next_id]) { - this->next_id++; - } - - GameObject object{*this, this->next_id, name, tag, position, rotation, scale}; - this->next_id++; - - return object; -} - -void ComponentManager::set_persistent(game_object_id_t id, bool persistent) { - this->persistent[id] = persistent; -} diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h deleted file mode 100644 index 480124f..0000000 --- a/src/crepe/ComponentManager.h +++ /dev/null @@ -1,159 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "Component.h" -#include "types.h" - -namespace crepe { - -class GameObject; - -/** - * \brief Manages all components - * - * This class manages all components. It provides methods to add, delete and get components. - */ -class ComponentManager { - // TODO: This relation should be removed! I (loek) believe that the scene manager should - // create/destroy components because the GameObject's are stored in concrete Scene classes, - // which will in turn call GameObject's destructor, which will in turn call - // ComponentManager::delete_components_by_id or something. This is a pretty major change, so - // here is a comment and temporary fix instead :tada: - friend class SceneManager; - -public: - ComponentManager(); // dbg_trace - ~ComponentManager(); // dbg_trace - - /** - * \brief Create a new game object using the component manager - * - * \param name Metadata::name (required) - * \param tag Metadata::tag (optional, empty by default) - * \param position Transform::position (optional, origin by default) - * \param rotation Transform::rotation (optional, 0 by default) - * \param scale Transform::scale (optional, 1 by default) - * - * \returns GameObject interface - * - * \note This method automatically assigns a new entity ID - */ - GameObject new_object(const std::string & name, const std::string & tag = "", - const vec2 & position = {0, 0}, double rotation = 0, - double scale = 1); - -protected: - /** - * GameObject is used as an interface to add/remove components, and the game programmer is - * supposed to use it instead of interfacing with the component manager directly. - */ - friend class GameObject; - /** - * \brief Add a component to the ComponentManager - * - * This method adds a component to the ComponentManager. The component is created with the - * given arguments and added to the ComponentManager. - * - * \tparam T The type of the component - * \tparam Args The types of the arguments - * \param id The id of the GameObject this component belongs to - * \param args The arguments to create the component - * \return The created component - */ - template - T & add_component(game_object_id_t id, Args &&... args); - /** - * \brief Delete all components of a specific type and id - * - * This method deletes all components of a specific type and id. - * - * \tparam T The type of the component - * \param id The id of the GameObject this component belongs to - */ - template - void delete_components_by_id(game_object_id_t id); - /** - * \brief Delete all components of a specific type - * - * This method deletes all components of a specific type. - * - * \tparam T The type of the component - */ - template - void delete_components(); - /** - * \brief Delete all components of a specific id - * - * This method deletes all components of a specific id. - * - * \param id The id of the GameObject this component belongs to - */ - void delete_all_components_of_id(game_object_id_t id); - /** - * \brief Delete all components - * - * This method deletes all components. - */ - void delete_all_components(); - /** - * \brief Set a GameObject as persistent - * - * This method sets a GameObject as persistent. If a GameObject is persistent, its - * components will not be deleted. - * - * \param id The id of the GameObject to set as persistent - * \param persistent The persistent flag - */ - void set_persistent(game_object_id_t id, bool persistent); - -public: - /** - * \brief Get all components of a specific type and id - * - * This method gets all components of a specific type and id. - * - * \tparam T The type of the component - * \param id The id of the GameObject this component belongs to - * \return A vector of all components of the specific type and id - */ - template - RefVector get_components_by_id(game_object_id_t id) const; - /** - * \brief Get all components of a specific type - * - * This method gets all components of a specific type. - * - * \tparam T The type of the component - * \return A vector of all components of the specific type - */ - template - RefVector get_components_by_type() const; - -private: - /** - * \brief The components - * - * This unordered_map stores all components. The key is the type of the component and the - * value is a vector of vectors of unique pointers to the components. - * - * Every component type has its own vector of vectors of unique pointers to the components. - * The first vector is for the ids of the GameObjects and the second vector is for the - * components (because a GameObject might have multiple components). - */ - std::unordered_map>>> - components; - - //! Persistent flag for each GameObject - std::unordered_map persistent; - - //! ID of next GameObject allocated by \c ComponentManager::new_object - game_object_id_t next_id = 0; -}; - -} // namespace crepe - -#include "ComponentManager.hpp" diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp deleted file mode 100644 index ffb38ec..0000000 --- a/src/crepe/ComponentManager.hpp +++ /dev/null @@ -1,161 +0,0 @@ -#pragma once - -#include - -#include "ComponentManager.h" -#include "types.h" - -namespace crepe { - -template -T & ComponentManager::add_component(game_object_id_t id, Args &&... args) { - using namespace std; - - static_assert(is_base_of::value, - "add_component must recieve a derivative class of Component"); - - // Determine the type of T (this is used as the key of the unordered_map<>) - type_index type = typeid(T); - - // Check if this component type is already in the unordered_map<> - if (this->components.find(type) == this->components.end()) { - //If not, create a new (empty) vector<> of vector> - this->components[type] = vector>>(); - } - - // Resize the vector<> if the id is greater than the current size - if (id >= this->components[type].size()) { - // Initialize new slots to nullptr (resize does automatically init to nullptr) - this->components[type].resize(id + 1); - } - - // Create a new component of type T (arguments directly forwarded). The - // constructor must be called by ComponentManager. - T * instance_ptr = new T(id, forward(args)...); - if (instance_ptr == nullptr) throw std::bad_alloc(); - - T & instance_ref = *instance_ptr; - unique_ptr instance = unique_ptr(instance_ptr); - - // Check if the vector size is not greater than get_instances_max - int max_instances = instance->get_instances_max(); - if (max_instances != -1 && components[type][id].size() >= max_instances) { - throw std::runtime_error( - "Exceeded maximum number of instances for this component type"); - } - - // store its unique_ptr in the vector<> - this->components[type][id].push_back(std::move(instance)); - - return instance_ref; -} - -template -void ComponentManager::delete_components_by_id(game_object_id_t id) { - using namespace std; - - // Do not delete persistent objects - if (this->persistent[id]) { - return; - } - - // Determine the type of T (this is used as the key of the unordered_map<>) - type_index type = typeid(T); - - // Find the type (in the unordered_map<>) - if (this->components.find(type) != this->components.end()) { - // Get the correct vector<> - vector>> & component_array = this->components[type]; - - // Make sure that the id (that we are looking for) is within the boundaries of the vector<> - if (id < component_array.size()) { - // Clear the whole vector<> of this specific type and id - component_array[id].clear(); - } - } -} - -template -void ComponentManager::delete_components() { - // Determine the type of T (this is used as the key of the unordered_map<>) - std::type_index type = typeid(T); - - if (this->components.find(type) == this->components.end()) return; - - // Loop through the whole vector<> of this specific type - for (game_object_id_t i = 0; i < this->components[type].size(); ++i) { - // Do not delete persistent objects - if (!this->persistent[i]) { - this->components[type][i].clear(); - } - } -} - -template -RefVector ComponentManager::get_components_by_id(game_object_id_t id) const { - using namespace std; - - // Determine the type of T (this is used as the key of the unordered_map<>) - type_index type = typeid(T); - - // Create an empty vector<> - RefVector component_vector; - - if (this->components.find(type) == this->components.end()) return component_vector; - - // Get the correct vector<> - const vector>> & component_array = this->components.at(type); - - // Make sure that the id (that we are looking for) is within the boundaries of the vector<> - if (id >= component_array.size()) return component_vector; - - // Loop trough the whole vector<> - for (const unique_ptr & component_ptr : component_array[id]) { - // Cast the unique_ptr to a raw pointer - T * casted_component = static_cast(component_ptr.get()); - - if (casted_component == nullptr) continue; - - // Add the dereferenced raw pointer to the vector<> - component_vector.push_back(*casted_component); - } - - return component_vector; -} - -template -RefVector ComponentManager::get_components_by_type() const { - using namespace std; - - // Determine the type of T (this is used as the key of the unordered_map<>) - type_index type = typeid(T); - - // Create an empty vector<> - RefVector component_vector; - - // Find the type (in the unordered_map<>) - if (this->components.find(type) == this->components.end()) return component_vector; - - // Get the correct vector<> - const vector>> & component_array = this->components.at(type); - - // Loop through the whole vector<> - for (const vector> & component : component_array) { - // Loop trough the whole vector<> - for (const unique_ptr & component_ptr : component) { - // Cast the unique_ptr to a raw pointer - T * casted_component = static_cast(component_ptr.get()); - - // Ensure that the cast was successful - if (casted_component == nullptr) continue; - - // Add the dereferenced raw pointer to the vector<> - component_vector.emplace_back(ref(*casted_component)); - } - } - - // Return the vector<> - return component_vector; -} - -} // namespace crepe diff --git a/src/crepe/api/BehaviorScript.cpp b/src/crepe/api/BehaviorScript.cpp index 7bbace0..d22afdf 100644 --- a/src/crepe/api/BehaviorScript.cpp +++ b/src/crepe/api/BehaviorScript.cpp @@ -4,12 +4,12 @@ using namespace crepe; -BehaviorScript::BehaviorScript(game_object_id_t id, ComponentManager & mgr) +BehaviorScript::BehaviorScript(game_object_id_t id, Mediator & mediator) : Component(id), - component_manager(mgr) {} + mediator(mediator) {} template <> BehaviorScript & GameObject::add_component() { ComponentManager & mgr = this->component_manager; - return mgr.add_component(this->id, mgr); + return mgr.add_component(this->id, mgr.mediator); } diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index d556fe5..3909b96 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -23,14 +23,13 @@ class BehaviorScript : public Component { protected: /** * \param id Parent \c GameObject id - * \param component_manager Reference to component manager (passed through to \c Script - * instance) + * \param mediator Mediator reference * * \note Calls to this constructor (should) always pass through \c GameObject::add_component, * which has an exception for this specific component type. This was done so the user does * not have to pass references used within \c Script to each \c BehaviorScript instance. */ - BehaviorScript(game_object_id_t id, ComponentManager & component_manager); + BehaviorScript(game_object_id_t id, Mediator & mediator); //! Only ComponentManager is allowed to instantiate BehaviorScript friend class ComponentManager; @@ -55,8 +54,8 @@ protected: friend class ScriptSystem; protected: - //! Reference to component manager (passed to Script) - ComponentManager & component_manager; + //! Reference mediator + Mediator & mediator; }; /** diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp index bd59337..6de0157 100644 --- a/src/crepe/api/BehaviorScript.hpp +++ b/src/crepe/api/BehaviorScript.hpp @@ -14,11 +14,11 @@ BehaviorScript & BehaviorScript::set_script(Args &&... args) { dbg_trace(); static_assert(std::is_base_of::value); Script * s = new T(std::forward(args)...); + Mediator & mediator = this->mediator; s->game_object_id = this->game_object_id; s->active = this->active; - s->component_manager = this->component_manager; - s->event_manager = EventManager::get_instance(); + s->mediator = mediator; this->script = std::unique_ptr