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/system/RenderSystem.h | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/crepe/system/RenderSystem.h') 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 -- 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/system/RenderSystem.h') 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 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/system/RenderSystem.h') 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 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/system/RenderSystem.h') 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 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/system/RenderSystem.h') 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 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/system/RenderSystem.h') 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 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/system/RenderSystem.h') 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/system/RenderSystem.h') 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 ca81cf4cdb99cfe42359526ff3840f58f4cf214a Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Thu, 28 Nov 2024 10:21:22 +0100 Subject: make format --- src/crepe/api/Animator.cpp | 3 ++- src/crepe/api/Texture.cpp | 16 +++++++--------- src/crepe/api/Texture.h | 10 +++++----- src/crepe/facade/SDLContext.h | 9 +++------ src/crepe/system/RenderSystem.cpp | 2 +- src/crepe/system/RenderSystem.h | 2 -- src/example/rendering_particle.cpp | 4 ++-- src/test/ParticleTest.cpp | 3 +-- src/test/RenderSystemTest.cpp | 20 ++++++++++---------- 9 files changed, 31 insertions(+), 38 deletions(-) (limited to 'src/crepe/system/RenderSystem.h') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 31b9632..2b21c6c 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -21,6 +21,7 @@ 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 = static_cast(this->spritesheet.sprite_rect.w) / this->spritesheet.sprite_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/Texture.cpp b/src/crepe/api/Texture.cpp index 576bdc3..e43bdaa 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -17,15 +17,13 @@ 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; +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) { diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h index dc4a6d7..7206a66 100644 --- a/src/crepe/api/Texture.h +++ b/src/crepe/api/Texture.h @@ -35,11 +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; + + 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. diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 25f2818..5f141be 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -16,14 +16,13 @@ namespace crepe { -struct RenderCtx{ +struct RenderCtx { const Sprite & sprite; const Camera & cam; const vec2 & cam_pos; const vec2 & pos; const double & angle; const double & scale; - }; // TODO: SDL_Keycode is defined in a header not distributed with crepe, which means this @@ -39,8 +38,6 @@ typedef SDL_Keycode CREPE_KEYCODES; */ class SDLContext { - - public: /** * \brief Gets the singleton instance of SDLContext. @@ -161,8 +158,8 @@ private: * \param img_scale the image multiplier for increasing img size * \return sdl rectangle to draw a dst image to draw on the screen */ - SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, const vec2 & cam_pos, - const double & img_scale) const; + SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, + const vec2 & cam_pos, const double & img_scale) const; private: //! sdl Window diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 0bef69b..8895f02 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -6,10 +6,10 @@ #include #include "../ComponentManager.h" +#include "../api/Camera.h" #include "../api/ParticleEmitter.h" #include "../api/Sprite.h" #include "../api/Transform.h" -#include "../api/Camera.h" #include "../facade/SDLContext.h" #include "RenderSystem.h" diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 7279b5c..e70831e 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -13,7 +13,6 @@ class Camera; class Sprite; class Transform; - /** * \class RenderSystem * \brief Manages rendering operations for all game objects. @@ -80,7 +79,6 @@ private: //! camera postion in the current scene vec2 cam_pos; - }; } // namespace crepe diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 68dadd7..8fdbb58 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); auto img = Texture("asset/texture/test_ap43.png"); - Sprite & test_sprite - = game_object.add_component(img, color, Sprite::FlipSettings{true, true}, 1, 1, 500); + Sprite & test_sprite = game_object.add_component( + img, color, Sprite::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; diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 1ac058f..976f9a1 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -30,8 +30,7 @@ public: Color color(0, 0, 0, 0); auto s1 = Texture("asset/texture/img.png"); Sprite & test_sprite = game_object.add_component( - s1, color, - Sprite::FlipSettings{true, true}, 1,1,100); + s1, color, Sprite::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 138aa36..e0bd953 100644 --- a/src/test/RenderSystemTest.cpp +++ b/src/test/RenderSystemTest.cpp @@ -7,12 +7,12 @@ #define private public #define protected public +#include "crepe/api/Camera.h" #include #include #include #include #include -#include "crepe/api/Camera.h" #include @@ -34,25 +34,25 @@ public: 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), - Sprite::FlipSettings{false, false}, 5, 5, 100); + auto & sprite1 = entity1.add_component( + s1, Color(0, 0, 0, 0), Sprite::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(s2, Color(0, 0, 0, 0), - Sprite::FlipSettings{false, false}, 2, 1, 100); + auto & sprite2 = entity2.add_component( + s2, Color(0, 0, 0, 0), Sprite::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(s3, Color(0, 0, 0, 0), - Sprite::FlipSettings{false, false}, 1, 2, 100); + auto & sprite3 = entity3.add_component( + s3, Color(0, 0, 0, 0), Sprite::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(s4, Color(0, 0, 0, 0), - Sprite::FlipSettings{false, false}, 1, 1, 100); + auto & sprite4 = entity4.add_component( + s4, Color(0, 0, 0, 0), Sprite::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); @@ -66,7 +66,7 @@ TEST_F(RenderSystemTest, expected_throws) { EXPECT_ANY_THROW({ auto test = Texture(""); entity1.add_component(test, Color(0, 0, 0, 0), - Sprite::FlipSettings{false, false},1,1,100); + Sprite::FlipSettings{false, false}, 1, 1, 100); }); // No camera -- cgit v1.2.3