From 004ee3aafb6beb4e984877186bced560010f4ddb Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 22 Nov 2024 21:57:08 +0100 Subject: fix RenderSystem unit test path resolution + reset Config before each test --- src/crepe/api/Texture.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'src/crepe/api/Texture.cpp') diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index 9be9421..264d7b1 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -9,14 +9,9 @@ using namespace crepe; using namespace std; -Texture::Texture(unique_ptr res) { +Texture::Texture(const Asset & src) { dbg_trace(); - this->load(std::move(res)); -} - -Texture::Texture(const char * src) { - dbg_trace(); - this->load(make_unique(src)); + this->load(src); } Texture::~Texture() { @@ -24,9 +19,9 @@ Texture::~Texture() { this->texture.reset(); } -void Texture::load(unique_ptr res) { +void Texture::load(const Asset & res) { SDLContext & ctx = SDLContext::get_instance(); - this->texture = std::move(ctx.texture_from_path(res->get_path())); + this->texture = ctx.texture_from_path(res.get_path()); } int Texture::get_width() const { -- 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/api/Texture.cpp') 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 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/api/Texture.cpp') 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 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/api/Texture.cpp') 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