From ca878336da5cdd120a929e6ce84f565ce5365248 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 11 Nov 2024 19:43:03 +0100 Subject: adjusted render a little and added a error img of the chosen img did not load. this prevents exceptions --- src/crepe/system/RenderSystem.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 10211a3..3a1def5 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -6,6 +6,7 @@ #include "../api/Transform.h" #include "../facade/SDLContext.h" #include "../util/log.h" +#include "api/ParticleEmitter.h" #include "RenderSystem.h" @@ -42,8 +43,7 @@ void RenderSystem::render_sprites() const { ComponentManager & mgr = ComponentManager::get_instance(); - std::vector> sprites - = mgr.get_components_by_type(); + auto sprites = mgr.get_components_by_type(); SDLContext & render = SDLContext::get_instance(); for (const Sprite & sprite : sprites) { -- cgit v1.2.3 From 3d97f7c60536bf14f314cc703883f74345aacd21 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 11 Nov 2024 20:49:49 +0100 Subject: rendering particle --- src/crepe/api/Texture.cpp | 2 +- src/crepe/facade/SDLContext.cpp | 4 +-- src/crepe/facade/SDLContext.h | 7 +++-- src/crepe/system/RenderSystem.cpp | 28 +++++++++++++---- src/crepe/system/RenderSystem.h | 6 +++- src/example/CMakeLists.txt | 1 + src/example/rendering_particle.cpp | 61 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 98 insertions(+), 11 deletions(-) create mode 100644 src/example/rendering_particle.cpp (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index 5ebd23d..8ce65c2 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/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index e87a2c5..cc2e2f8 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -181,12 +181,12 @@ SDLContext::texture_from_path(const std::string & path) { return img_texture; } -int SDLContext::get_width(const Texture & ctx) const { +int SDLContext::get_width(const Texture & ctx) { int w; SDL_QueryTexture(ctx.texture.get(), NULL, NULL, &w, NULL); return w; } -int SDLContext::get_height(const Texture & ctx) const { +int SDLContext::get_height(const Texture & ctx) { int h; SDL_QueryTexture(ctx.texture.get(), NULL, NULL, NULL, &h); return h; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index e358c21..7329b74 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -10,6 +10,7 @@ #include "../api/Sprite.h" #include "../api/Transform.h" #include "api/Camera.h" +#include "api/Vector2.h" // FIXME: this needs to be removed const int SCREEN_WIDTH = 640; @@ -97,14 +98,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 &) ; /** * \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 &) ; private: //! Will use draw,clear_screen, present_screen, camera. @@ -119,6 +120,8 @@ private: void draw(const Sprite & sprite, const Transform & transform, const Camera & camera); + void draw_particle(const Vector2 & pos, const Camera & camera); + //! Clears the screen, preparing for a new frame. void clear_screen(); diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 3a1def5..3de8330 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -6,7 +7,9 @@ #include "../api/Transform.h" #include "../facade/SDLContext.h" #include "../util/log.h" +#include "Particle.h" #include "api/ParticleEmitter.h" +#include "api/Vector2.h" #include "RenderSystem.h" @@ -39,17 +42,32 @@ void RenderSystem::update_camera() { this->curr_cam = &cam; } } -void RenderSystem::render_sprites() const { + + +void RenderSystem::render_particle(const ParticleEmitter& em, Transform & tm){ + if (!em.active) return; + + SDLContext & render = SDLContext::get_instance(); + for (const Particle& p : em.data.particles) { + tm.position = p.position; + render.draw(em.data.sprite, tm , *curr_cam); + } +} +void RenderSystem::render_sprites() { ComponentManager & mgr = ComponentManager::get_instance(); - auto sprites = mgr.get_components_by_type(); + auto emitter = mgr.get_components_by_type(); SDLContext & render = SDLContext::get_instance(); - for (const Sprite & sprite : sprites) { + Transform test(1, Vector2{0,0},0,0); + + for (const ParticleEmitter & em : emitter) { auto transforms - = mgr.get_components_by_id(sprite.game_object_id); - render.draw(sprite, transforms[0], *curr_cam); + = mgr.get_components_by_id(em.game_object_id); + test.scale = transforms[0].get().scale; + test.rotation = transforms[0].get().rotation; + this->render_particle(em, test); } } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 70db21a..8dd8b7e 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -3,6 +3,8 @@ #include "api/Camera.h" #include "System.h" +#include "api/ParticleEmitter.h" +#include "api/Transform.h" namespace crepe { @@ -44,7 +46,9 @@ private: void update_camera(); //! Renders all active sprites to the screen. - void render_sprites() const; + void render_sprites() ; + + void render_particle(const ParticleEmitter& em, Transform & tm); /** * \todo Include color handling for sprites. diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 722ffea..72dc172 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -29,4 +29,5 @@ add_example(db) add_example(ecs) add_example(scene_manager) add_example(particles) +add_example(rendering_particle) diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp new file mode 100644 index 0000000..54a9c23 --- /dev/null +++ b/src/example/rendering_particle.cpp @@ -0,0 +1,61 @@ +#include "api/Camera.h" +#include "system/ParticleSystem.h" +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +using namespace crepe; +using namespace std; + +int main(int argc, char * argv[]) { + GameObject game_object(0, "", "", Vector2{0, 0}, 0, 0.1); + Color color(0, 0, 0, 0); + Sprite test_sprite = game_object.add_component( + make_shared("../asset/texture/img.png"), color, + FlipSettings{false, false}); + game_object.add_component(ParticleEmitter::Data{ + .position = {0, 0}, + .max_particles = 100, + .emission_rate = 0.5, + .min_speed = 1, + .max_speed = 1, + .min_angle = 0, + .max_angle = 0, + .begin_lifespan = 0, + .end_lifespan = 60, + .force_over_time = Vector2{0, 0}, + .boundary{ + .width = 1000, + .height = 1000, + .offset = Vector2{0, 0}, + .reset_on_exit = false, + }, + .sprite = test_sprite, + }); + game_object.add_component(Color::get_white()); + + auto & sys = crepe::RenderSystem::get_instance(); + auto sys_part = crepe::ParticleSystem(); + auto start = std::chrono::steady_clock::now(); + while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { + sys_part.update(); + sys.update(); + SDL_Delay(10 ); + } + + return 0; +} + -- cgit v1.2.3 From cf96d9639dbd18df6451a07fd7b623616e842c4a Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 11 Nov 2024 21:27:54 +0100 Subject: adjusted rendering and example --- src/crepe/system/RenderSystem.cpp | 1 + src/example/rendering_particle.cpp | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 3de8330..3fbd175 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -49,6 +49,7 @@ void RenderSystem::render_particle(const ParticleEmitter& em, Transform & tm){ SDLContext & render = SDLContext::get_instance(); for (const Particle& p : em.data.particles) { + if (!p.active) continue; tm.position = p.position; render.draw(em.data.sprite, tm , *curr_cam); } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 54a9c23..bd4ceb4 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -21,17 +21,17 @@ using namespace crepe; using namespace std; int main(int argc, char * argv[]) { - GameObject game_object(0, "", "", Vector2{0, 0}, 0, 0.1); + GameObject game_object(0, "", "", Vector2{100, 0}, 0, 0.1); Color color(0, 0, 0, 0); Sprite test_sprite = game_object.add_component( make_shared("../asset/texture/img.png"), color, FlipSettings{false, false}); game_object.add_component(ParticleEmitter::Data{ .position = {0, 0}, - .max_particles = 100, - .emission_rate = 0.5, - .min_speed = 1, - .max_speed = 1, + .max_particles = 10, + .emission_rate = 0.1, + .min_speed = 10, + .max_speed = 10, .min_angle = 0, .max_angle = 0, .begin_lifespan = 0, -- cgit v1.2.3 From 0d88ba90c55c0df97e418ce36c435955481b2850 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 11 Nov 2024 21:37:08 +0100 Subject: adjusted renderingsystem --- src/crepe/system/RenderSystem.cpp | 2 +- src/example/rendering_particle.cpp | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 3fbd175..8bbf441 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -51,6 +51,7 @@ void RenderSystem::render_particle(const ParticleEmitter& em, Transform & tm){ for (const Particle& p : em.data.particles) { if (!p.active) continue; tm.position = p.position; + tm.rotation = p.angle; render.draw(em.data.sprite, tm , *curr_cam); } } @@ -67,7 +68,6 @@ void RenderSystem::render_sprites() { auto transforms = mgr.get_components_by_id(em.game_object_id); test.scale = transforms[0].get().scale; - test.rotation = transforms[0].get().rotation; this->render_particle(em, test); } } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index bd4ceb4..dd08354 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -21,7 +21,7 @@ using namespace crepe; using namespace std; int main(int argc, char * argv[]) { - GameObject game_object(0, "", "", Vector2{100, 0}, 0, 0.1); + GameObject game_object(0, "", "", Vector2{100, 100}, 0, 0.1); Color color(0, 0, 0, 0); Sprite test_sprite = game_object.add_component( make_shared("../asset/texture/img.png"), color, @@ -29,11 +29,11 @@ int main(int argc, char * argv[]) { game_object.add_component(ParticleEmitter::Data{ .position = {0, 0}, .max_particles = 10, - .emission_rate = 0.1, - .min_speed = 10, - .max_speed = 10, - .min_angle = 0, - .max_angle = 0, + .emission_rate = 0.5, + .min_speed = 6, + .max_speed = 20, + .min_angle = -20, + .max_angle = 20, .begin_lifespan = 0, .end_lifespan = 60, .force_over_time = Vector2{0, 0}, -- cgit v1.2.3 From fabdc6e27bdb41042d7a530c5f2440ca5a330118 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 12 Nov 2024 19:52:11 +0100 Subject: Adjusted rendering so that now a gameobject can have multiple sprites with particles and another texture --- src/crepe/system/RenderSystem.cpp | 65 ++++++++++++++++++++++++++------------- src/crepe/system/RenderSystem.h | 22 ++++++++----- src/example/rendering.cpp | 1 + 3 files changed, 60 insertions(+), 28 deletions(-) (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 8bbf441..17a2337 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -7,10 +7,10 @@ #include "../api/Transform.h" #include "../facade/SDLContext.h" #include "../util/log.h" -#include "Particle.h" -#include "api/ParticleEmitter.h" -#include "api/Vector2.h" +#include "../api/ParticleEmitter.h" +#include "../api/Vector2.h" +#include "Particle.h" #include "RenderSystem.h" using namespace crepe; @@ -43,38 +43,61 @@ void RenderSystem::update_camera() { } } +bool RenderSystem::render_particle(const Sprite & sprite, + const Transform & tm) { -void RenderSystem::render_particle(const ParticleEmitter& em, Transform & tm){ - if (!em.active) return; - + ComponentManager & mgr = ComponentManager::get_instance(); SDLContext & render = SDLContext::get_instance(); - for (const Particle& p : em.data.particles) { - if (!p.active) continue; - tm.position = p.position; - tm.rotation = p.angle; - render.draw(em.data.sprite, tm , *curr_cam); + + auto emitters = mgr.get_components_by_id(sprite.game_object_id); + + bool rendering_particles = false; + + Transform tmp(0, Vector2{0, 0}, 0, 0); + tmp.scale = tm.scale; + for (const ParticleEmitter & em : emitters) { + if (!em.active) continue; + if (!(em.data.sprite.game_object_id == sprite.game_object_id)) continue; + + rendering_particles = true; + + for (const Particle & p : em.data.particles) { + if (!p.active) continue; + tmp.position = p.position; + tmp.rotation = p.angle; + render.draw(em.data.sprite, tmp, *curr_cam); + } } + return rendering_particles; } -void RenderSystem::render_sprites() { +void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { ComponentManager & mgr = ComponentManager::get_instance(); + SDLContext & render = SDLContext::get_instance(); + + render.draw(sprite, tm, *curr_cam); +} - auto emitter = mgr.get_components_by_type(); +void RenderSystem::render() { - SDLContext & render = SDLContext::get_instance(); - Transform test(1, Vector2{0,0},0,0); + ComponentManager & mgr = ComponentManager::get_instance(); + + auto sprites = mgr.get_components_by_type(); + for (const Sprite & sprite : sprites) { + if (!sprite.active) continue; + auto transform = mgr.get_components_by_id(sprite.game_object_id); + + bool rendered_particles = this->render_particle(sprite, transform[0].get()); + + if (rendered_particles) continue; - for (const ParticleEmitter & em : emitter) { - auto transforms - = mgr.get_components_by_id(em.game_object_id); - test.scale = transforms[0].get().scale; - this->render_particle(em, test); + this->render_normal(sprite, transform[0].get()); } } void RenderSystem::update() { this->clear_screen(); this->update_camera(); - this->render_sprites(); + this->render(); this->present_screen(); } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 8dd8b7e..468f79b 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -1,10 +1,10 @@ #pragma once #include "api/Camera.h" +#include "api/Sprite.h" +#include "api/Transform.h" #include "System.h" -#include "api/ParticleEmitter.h" -#include "api/Transform.h" namespace crepe { @@ -45,14 +45,22 @@ private: //! Updates the active camera used for rendering. void update_camera(); - //! Renders all active sprites to the screen. - void render_sprites() ; - - void render_particle(const ParticleEmitter& em, Transform & tm); + //! Renders the whole screen + void render(); + + /** + * \brief Renders all the particles on the screen from a given sprite. + * + * \param sprite renders the particles with given texture + * \param tm the Transform component for scale + * \return true if particles have been rendered + */ + bool render_particle(const Sprite &, const Transform & tm); + + void render_normal(const Sprite &, const Transform & tm); /** * \todo Include color handling for sprites. - * \todo Implement particle emitter rendering with 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. diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index e02f6a3..a7cb5d6 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -35,6 +35,7 @@ int main() { { Color color(0, 0, 0, 0); obj1.add_component(make_shared("../asset/texture/second.png"), color, FlipSettings{true, true}); + obj1.add_component(make_shared("../asset/texture/second.png"), color, FlipSettings{true, true}); } /* -- cgit v1.2.3 From 21f5b66d0bcc13d903800adf3bb614e380ff8591 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Thu, 14 Nov 2024 15:01:26 +0100 Subject: first part sorted still needing testing --- src/crepe/system/RenderSystem.cpp | 35 ++++++++++++++++++++++++++++++----- src/crepe/system/RenderSystem.h | 8 +++++++- 2 files changed, 37 insertions(+), 6 deletions(-) (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 10211a3..718036c 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include @@ -6,6 +8,7 @@ #include "../api/Transform.h" #include "../facade/SDLContext.h" #include "../util/log.h" +#include "Asset.h" #include "RenderSystem.h" @@ -29,7 +32,6 @@ void RenderSystem::present_screen() const { } void RenderSystem::update_camera() { ComponentManager & mgr = ComponentManager::get_instance(); - std::vector> cameras = mgr.get_components_by_type(); @@ -38,15 +40,38 @@ void RenderSystem::update_camera() { this->curr_cam = &cam; } } -void RenderSystem::render_sprites() const { + +std::vector> +RenderSystem::sort(std::vector> & objs) { + if (objs.empty()) return {}; + + std::vector> sorted_objs; + sorted_objs.insert(sorted_objs.end(), objs.begin(), objs.end()); + assert(sorted_objs.size() != objs.size()); + + std::sort(sorted_objs.begin(), sorted_objs.end(), + [](const std::reference_wrapper & a, + const std::reference_wrapper & b) { + const Sprite & sprite_a = a.get(); + const Sprite & sprite_b = b.get(); + if (sprite_a.sorting_in_layer == sprite_b.sorting_in_layer) { + return sprite_a.order_in_layer < sprite_b.order_in_layer; + } + return sprite_a.sorting_in_layer < sprite_b.sorting_in_layer; + }); + return sorted_objs; +} + +void RenderSystem::render_sprites() { ComponentManager & mgr = ComponentManager::get_instance(); - std::vector> sprites - = mgr.get_components_by_type(); + auto sprites = mgr.get_components_by_type(); + auto sorted_sprites = this->sort(sprites); + SDLContext & render = SDLContext::get_instance(); - for (const Sprite & sprite : sprites) { + for (const Sprite & sprite : sorted_sprites) { auto transforms = mgr.get_components_by_id(sprite.game_object_id); render.draw(sprite, transforms[0], *curr_cam); diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 70db21a..a83633a 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -3,6 +3,9 @@ #include "api/Camera.h" #include "System.h" +#include "api/Sprite.h" +#include +#include namespace crepe { @@ -44,7 +47,10 @@ private: void update_camera(); //! Renders all active sprites to the screen. - void render_sprites() const; + void render_sprites() ; + + std::vector> + sort(std::vector> & objs); /** * \todo Include color handling for sprites. -- cgit v1.2.3 From e25aa67c4263b3a940ae9e92f2fcd31a4486f407 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Thu, 14 Nov 2024 20:18:19 +0100 Subject: adjusted based on feedback of PR#31 --- src/crepe/facade/SDLContext.cpp | 33 +++++++++++---------------------- src/crepe/facade/SDLContext.h | 9 ++------- src/crepe/system/RenderSystem.cpp | 6 +++--- src/crepe/system/RenderSystem.h | 6 ++++++ 4 files changed, 22 insertions(+), 32 deletions(-) (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index cacf238..febbe4b 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -7,10 +7,9 @@ #include #include #include -#include #include +#include #include -#include #include "../api/Sprite.h" #include "../api/Texture.h" @@ -32,31 +31,23 @@ SDLContext::SDLContext() { // FIXME: read window defaults from config manager if (SDL_Init(SDL_INIT_VIDEO) < 0) { - // FIXME: throw exception - std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() - << std::endl; - return; + throw std::runtime_error("SDL could not initialize!"); } + SDL_Window * tmp_window = SDL_CreateWindow( "Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 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; - return; + throw std::runtime_error("Window could not be created!"); } + this->game_window = {tmp_window, [](SDL_Window * window) { SDL_DestroyWindow(window); }}; SDL_Renderer * tmp_renderer = SDL_CreateRenderer( this->game_window.get(), -1, SDL_RENDERER_ACCELERATED); if (!tmp_renderer) { - // FIXME: throw exception - std::cerr << "Renderer could not be created! SDL_Error: " - << SDL_GetError() << std::endl; - SDL_DestroyWindow(this->game_window.get()); - return; + throw std::runtime_error("Renderer could not be created!"); } this->game_renderer = {tmp_renderer, [](SDL_Renderer * renderer) { @@ -65,9 +56,7 @@ SDLContext::SDLContext() { int img_flags = IMG_INIT_PNG; if (!(IMG_Init(img_flags) & img_flags)) { - // FIXME: throw exception - std::cout << "SDL_image could not initialize! SDL_image Error: " - << IMG_GetError() << std::endl; + throw std::runtime_error("SDL_image could not initialize!"); } } @@ -145,8 +134,8 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, void SDLContext::camera(const Camera & cam) { this->viewport.w = static_cast(cam.aspect_width); this->viewport.h = static_cast(cam.aspect_height); - this->viewport.x = static_cast(cam.x) - (SCREEN_WIDTH / 2); - this->viewport.y = static_cast(cam.y) - (SCREEN_HEIGHT / 2); + this->viewport.x = static_cast(cam.x) - (this->viewport.w / 2); + this->viewport.y = static_cast(cam.y) - (this->viewport.h / 2); SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g, cam.bg_color.b, cam.bg_color.a); @@ -181,12 +170,12 @@ SDLContext::texture_from_path(const std::string & path) { return img_texture; } -int SDLContext::get_width(const Texture & ctx) { +int SDLContext::get_width(const Texture & ctx) const { int w; SDL_QueryTexture(ctx.texture.get(), NULL, NULL, &w, NULL); return w; } -int SDLContext::get_height(const Texture & ctx) { +int SDLContext::get_height(const Texture & ctx) const { int h; SDL_QueryTexture(ctx.texture.get(), NULL, NULL, NULL, &h); return h; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index c4392bd..0614036 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -10,11 +10,6 @@ #include "../api/Sprite.h" #include "../api/Transform.h" #include "api/Camera.h" -#include "api/Vector2.h" - -// FIXME: this needs to be removed -const int SCREEN_WIDTH = 640; -const int SCREEN_HEIGHT = 480; namespace crepe { @@ -109,14 +104,14 @@ private: * \param texture Reference to the Texture object. * \return Width of the texture as an integer. */ - int get_width(const Texture &) ; + int get_width(const 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 &) ; + int get_height(const Texture &) const; private: //! Will use draw,clear_screen, present_screen, camera. diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 17a2337..6ecd604 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -85,13 +85,13 @@ void RenderSystem::render() { auto sprites = mgr.get_components_by_type(); for (const Sprite & sprite : sprites) { if (!sprite.active) continue; - auto transform = mgr.get_components_by_id(sprite.game_object_id); + auto transform = mgr.get_components_by_id(sprite.game_object_id).front().get(); - bool rendered_particles = this->render_particle(sprite, transform[0].get()); + bool rendered_particles = this->render_particle(sprite, transform); if (rendered_particles) continue; - this->render_normal(sprite, transform[0].get()); + this->render_normal(sprite, transform); } } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 6a87eec..3d6286f 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -31,6 +31,12 @@ public: */ void update() override; + + RenderSystem(const RenderSystem &) = delete; + RenderSystem(RenderSystem &&) = delete; + RenderSystem & operator=(const RenderSystem &) = delete; + RenderSystem & operator=(RenderSystem &&) = delete; + private: // Private constructor to enforce singleton pattern. RenderSystem(); -- cgit v1.2.3 From c9aac3ea385213b7b9a9a4e7e48e45e6a1268902 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 18 Nov 2024 15:03:39 +0100 Subject: working sorting --- src/crepe/system/RenderSystem.cpp | 37 +++++++++++++++++++------------------ src/example/rendering.cpp | 25 +++++++++++++++++++++---- 2 files changed, 40 insertions(+), 22 deletions(-) (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index aa9910b..ba804d4 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -1,13 +1,13 @@ #include #include #include +#include #include #include "../ComponentManager.h" #include "../api/Sprite.h" #include "../api/Transform.h" #include "../facade/SDLContext.h" -#include "../util/Log.h" #include "RenderSystem.h" @@ -27,38 +27,39 @@ void RenderSystem::update_camera() { } } +bool sorting_comparison(const Sprite & a, const Sprite & b) { + if (a.sorting_in_layer > b.sorting_in_layer) return true; + if (a.sorting_in_layer == b.sorting_in_layer) return a.order_in_layer > b.order_in_layer; + + return false; +} + std::vector> RenderSystem::sort(std::vector> & objs) { if (objs.empty()) return {}; - std::vector> sorted_objs; - sorted_objs.insert(sorted_objs.end(), objs.begin(), objs.end()); - assert(sorted_objs.size() != objs.size()); - - std::sort(sorted_objs.begin(), sorted_objs.end(), - [](const std::reference_wrapper & a, - const std::reference_wrapper & b) { - const Sprite & sprite_a = a.get(); - const Sprite & sprite_b = b.get(); - if (sprite_a.sorting_in_layer == sprite_b.sorting_in_layer) { - return sprite_a.order_in_layer < sprite_b.order_in_layer; - } - return sprite_a.sorting_in_layer < sprite_b.sorting_in_layer; - }); + std::vector> sorted_objs(objs); + std::sort(sorted_objs.begin(), sorted_objs.end(), sorting_comparison); + return sorted_objs; } void RenderSystem::render_sprites() { ComponentManager & mgr = this->component_manager; - ComponentManager & mgr = ComponentManager::get_instance(); - auto sprites = mgr.get_components_by_type(); + for (const Sprite & s : sprites) { + std::cout << s.game_object_id << " " << unsigned(s.sorting_in_layer) << " " + << unsigned(s.order_in_layer) << std::endl; + } + auto sorted_sprites = this->sort(sprites); - + SDLContext & render = SDLContext::get_instance(); for (const Sprite & sprite : sorted_sprites) { + std::cout << sprite.game_object_id << " " << unsigned(sprite.sorting_in_layer) << " " + << unsigned(sprite.order_in_layer) << std::endl; auto transforms = mgr.get_components_by_id(sprite.game_object_id); render.draw(sprite, transforms[0], *curr_cam); } diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index c9e62f1..ecd3f6a 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -30,14 +30,27 @@ int main() { // Normal adding components { Color color(0, 0, 0, 0); - obj.add_component(make_shared("../asset/texture/img.png"), color, - FlipSettings{false, false}); + Sprite & sprite + = obj.add_component(make_shared("../asset/texture/img.png"), + color, FlipSettings{false, false}); + sprite.sorting_in_layer = 2; + sprite.order_in_layer = 1; obj.add_component(Color::get_red()); } { Color color(0, 0, 0, 0); - obj1.add_component(make_shared("../asset/texture/second.png"), color, - FlipSettings{true, true}); + Sprite & sprite = obj1.add_component( + make_shared("../asset/texture/img.png"), color, FlipSettings{true, true}); + sprite.sorting_in_layer = 2; + sprite.order_in_layer = 2; + } + + { + Color color(0, 0, 0, 0); + Sprite & sprite = obj2.add_component( + make_shared("../asset/texture/img.png"), color, FlipSettings{true, true}); + sprite.sorting_in_layer = 1; + sprite.order_in_layer = 2; } /* @@ -48,8 +61,12 @@ int main() { } */ + sys.update(); + /* + auto start = std::chrono::steady_clock::now(); while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { sys.update(); } + */ } -- cgit v1.2.3 From bae2501c9e2604497db152d5e3df6a018cdd6858 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 18 Nov 2024 15:04:36 +0100 Subject: removed iostream & printing --- src/crepe/system/RenderSystem.cpp | 9 --------- 1 file changed, 9 deletions(-) (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index ba804d4..bf5dade 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include "../ComponentManager.h" @@ -48,18 +47,10 @@ void RenderSystem::render_sprites() { ComponentManager & mgr = this->component_manager; auto sprites = mgr.get_components_by_type(); - - for (const Sprite & s : sprites) { - std::cout << s.game_object_id << " " << unsigned(s.sorting_in_layer) << " " - << unsigned(s.order_in_layer) << std::endl; - } - auto sorted_sprites = this->sort(sprites); SDLContext & render = SDLContext::get_instance(); for (const Sprite & sprite : sorted_sprites) { - std::cout << sprite.game_object_id << " " << unsigned(sprite.sorting_in_layer) << " " - << unsigned(sprite.order_in_layer) << std::endl; auto transforms = mgr.get_components_by_id(sprite.game_object_id); render.draw(sprite, transforms[0], *curr_cam); } -- cgit v1.2.3 From 74fcf733c2bcdcbcbfc64d88270b12a39e9d91e0 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 18 Nov 2024 15:18:20 +0100 Subject: merged master --- src/crepe/system/RenderSystem.cpp | 18 +++++++----------- src/crepe/system/RenderSystem.h | 2 +- 2 files changed, 8 insertions(+), 12 deletions(-) (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 23ab738..37320ed 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -6,11 +6,9 @@ #include "../api/Sprite.h" #include "../api/Transform.h" #include "../facade/SDLContext.h" -#include "../util/log.h" #include "../api/ParticleEmitter.h" #include "../api/Vector2.h" -#include "Particle.h" #include "RenderSystem.h" using namespace crepe; @@ -30,17 +28,15 @@ void RenderSystem::update_camera() { } bool RenderSystem::render_particle(const Sprite & sprite, - const Transform & tm) { + Transform tm) { - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; SDLContext & render = SDLContext::get_instance(); auto emitters = mgr.get_components_by_id(sprite.game_object_id); bool rendering_particles = false; - Transform tmp(0, Vector2{0, 0}, 0, 0); - tmp.scale = tm.scale; for (const ParticleEmitter & em : emitters) { if (!em.active) continue; if (!(em.data.sprite.game_object_id == sprite.game_object_id)) continue; @@ -49,16 +45,16 @@ bool RenderSystem::render_particle(const Sprite & sprite, for (const Particle & p : em.data.particles) { if (!p.active) continue; - tmp.position = p.position; - tmp.rotation = p.angle; - render.draw(em.data.sprite, tmp, *curr_cam); + tm.position = p.position; + tm.rotation = p.angle; + render.draw(em.data.sprite, tm, *curr_cam); } } return rendering_particles; } void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; SDLContext & render = SDLContext::get_instance(); render.draw(sprite, tm, *curr_cam); @@ -66,7 +62,7 @@ void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { void RenderSystem::render() { - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; auto sprites = mgr.get_components_by_type(); for (const Sprite & sprite : sprites) { diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index ad44716..c18b80b 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -51,7 +51,7 @@ private: * \param tm the Transform component for scale * \return true if particles have been rendered */ - bool render_particle(const Sprite &, const Transform & tm); + bool render_particle(const Sprite &, Transform tm); /** * \brief renders a sprite with a Transform component on the screen -- cgit v1.2.3 From 193e837fce92dcc169f995f670c9261f853ea1c3 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 18 Nov 2024 20:25:31 +0100 Subject: adjusted so that all gets are auto --- src/crepe/system/RenderSystem.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 37320ed..39e0e3f 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -1,5 +1,4 @@ #include -#include #include #include "../ComponentManager.h" @@ -19,7 +18,7 @@ void RenderSystem::present_screen() const { SDLContext::get_instance().present_s void RenderSystem::update_camera() { ComponentManager & mgr = this->component_manager; - std::vector> cameras = mgr.get_components_by_type(); + auto cameras = mgr.get_components_by_type(); for (Camera & cam : cameras) { SDLContext::get_instance().camera(cam); -- cgit v1.2.3 From 66bbea079bf1ae84c355349d337db468c18eac32 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 18 Nov 2024 21:07:26 +0100 Subject: feedback from PR#38 implemented --- src/crepe/facade/SDLContext.h | 5 ----- src/crepe/system/RenderSystem.cpp | 5 ++--- src/crepe/system/RenderSystem.h | 10 ++++++---- 3 files changed, 8 insertions(+), 12 deletions(-) (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 007092b..d437bc4 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -21,8 +21,6 @@ namespace crepe { // typedef is unusable when crepe is packaged. Wouter will fix this later. typedef SDL_Keycode CREPE_KEYCODES; -class Texture; -class LoopManager; /** * \class SDLContext @@ -91,9 +89,6 @@ private: //! Will use the funtions: texture_from_path, get_width,get_height. friend class Texture; - //! Will use the funtions: texture_from_path, get_width,get_height. - friend class Animator; - /** * \brief Loads a texture from a file path. * \param path Path to the image file. diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index bf5dade..11c20f5 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -22,7 +22,7 @@ void RenderSystem::update_camera() { for (Camera & cam : cameras) { SDLContext::get_instance().camera(cam); - this->curr_cam = &cam; + this->curr_cam_ref = &cam; } } @@ -35,7 +35,6 @@ bool sorting_comparison(const Sprite & a, const Sprite & b) { std::vector> RenderSystem::sort(std::vector> & objs) { - if (objs.empty()) return {}; std::vector> sorted_objs(objs); std::sort(sorted_objs.begin(), sorted_objs.end(), sorting_comparison); @@ -52,7 +51,7 @@ void RenderSystem::render_sprites() { SDLContext & render = SDLContext::get_instance(); for (const Sprite & sprite : sorted_sprites) { auto transforms = mgr.get_components_by_id(sprite.game_object_id); - render.draw(sprite, transforms[0], *curr_cam); + render.draw(sprite, transforms[0], *this->curr_cam_ref); } } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 7661cbd..1176b06 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -1,12 +1,14 @@ #pragma once -#include "api/Camera.h" -#include "System.h" -#include "api/Sprite.h" #include #include +#include "api/Sprite.h" +#include "api/Camera.h" + +#include "System.h" + namespace crepe { /** @@ -54,7 +56,7 @@ private: private: //! Pointer to the current active camera for rendering - Camera * curr_cam = nullptr; + Camera * curr_cam_ref = nullptr; // TODO: needs a better solution }; -- cgit v1.2.3 From 9fb2e3a1d697f4961379980651e5434395e372bd Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 19 Nov 2024 11:48:07 +0100 Subject: unit tests --- src/crepe/api/Color.h | 4 +- src/crepe/system/RenderSystem.cpp | 7 +- src/test/CMakeLists.txt | 1 + src/test/rendering.cpp | 174 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 182 insertions(+), 4 deletions(-) create mode 100644 src/test/rendering.cpp (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/src/crepe/api/Color.h b/src/crepe/api/Color.h index aa47bf4..c207ba7 100644 --- a/src/crepe/api/Color.h +++ b/src/crepe/api/Color.h @@ -21,13 +21,13 @@ public: static const Color & get_yellow(); static const Color & get_black(); -private: - // TODO: why are these private!? +public: uint8_t r; uint8_t g; uint8_t b; uint8_t a; +private: static Color white; static Color red; static Color green; diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 11c20f5..82257c4 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "../ComponentManager.h" @@ -20,6 +21,8 @@ void RenderSystem::update_camera() { std::vector> cameras = mgr.get_components_by_type(); + if (cameras.size() == 0) throw std::runtime_error("No cameras in current scene"); + for (Camera & cam : cameras) { SDLContext::get_instance().camera(cam); this->curr_cam_ref = &cam; @@ -27,8 +30,8 @@ void RenderSystem::update_camera() { } bool sorting_comparison(const Sprite & a, const Sprite & b) { - if (a.sorting_in_layer > b.sorting_in_layer) return true; - if (a.sorting_in_layer == b.sorting_in_layer) return a.order_in_layer > b.order_in_layer; + if (a.sorting_in_layer < b.sorting_in_layer) return true; + if (a.sorting_in_layer == b.sorting_in_layer) return a.order_in_layer < b.order_in_layer; return false; } diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 49c8151..0969a52 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -3,5 +3,6 @@ target_sources(test_main PUBLIC PhysicsTest.cpp ScriptTest.cpp ParticleTest.cpp + rendering.cpp ) diff --git a/src/test/rendering.cpp b/src/test/rendering.cpp new file mode 100644 index 0000000..4c5fb1d --- /dev/null +++ b/src/test/rendering.cpp @@ -0,0 +1,174 @@ +#include "api/Camera.h" +#include +#include +#include +#include + +#define private public +#define protected public + +#include +#include +#include +#include +#include + +#include + +using namespace std; +using namespace crepe; +using namespace testing; + +class RenderSystemTest : public Test { +public: + ComponentManager mgr{}; + RenderSystem sys{mgr}; + GameObject entity1 = this->mgr.new_object("name"); + GameObject entity2 = this->mgr.new_object("name"); + GameObject entity3 = this->mgr.new_object("name"); + 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; + 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; + + 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; + + 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; + EXPECT_EQ(sprite4.sorting_in_layer, 1); + EXPECT_EQ(sprite4.order_in_layer, 1); + } +}; + +TEST_F(RenderSystemTest, expected_throws) { + GameObject entity1 = this->mgr.new_object("NAME"); + + // no texture img + EXPECT_ANY_THROW({ + entity1.add_component(make_shared("NO_IMAGE"), Color(0, 0, 0, 0), + FlipSettings{false, false}); + }); + + // No camera + EXPECT_ANY_THROW({ this->sys.update(); }); +} + +TEST_F(RenderSystemTest, make_sprites) {} + +TEST_F(RenderSystemTest, sorting_sprites) { + vector> sprites = this->mgr.get_components_by_type(); + ASSERT_EQ(sprites.size(), 4); + + vector> sorted_sprites = this->sys.sort(sprites); + ASSERT_EQ(sorted_sprites.size(), 4); + + // Expected order after sorting: + // 1. sorting_in_layer: 1, order_in_layer: 1 (entity4) + // 2. sorting_in_layer: 1, order_in_layer: 2 (entity3) + // 3. sorting_in_layer: 2, order_in_layer: 1 (entity2) + // 4. sorting_in_layer: 5, order_in_layer: 5 (entity1) + + EXPECT_EQ(sorted_sprites[0].get().sorting_in_layer, 1); + EXPECT_EQ(sorted_sprites[0].get().order_in_layer, 1); + + EXPECT_EQ(sorted_sprites[1].get().sorting_in_layer, 1); + EXPECT_EQ(sorted_sprites[1].get().order_in_layer, 2); + + EXPECT_EQ(sorted_sprites[2].get().sorting_in_layer, 2); + EXPECT_EQ(sorted_sprites[2].get().order_in_layer, 1); + + EXPECT_EQ(sorted_sprites[3].get().sorting_in_layer, 5); + EXPECT_EQ(sorted_sprites[3].get().order_in_layer, 5); + + for (size_t i = 1; i < sorted_sprites.size(); ++i) { + const Sprite & prev = sorted_sprites[i - 1].get(); + const Sprite & curr = sorted_sprites[i].get(); + + if (prev.sorting_in_layer == curr.sorting_in_layer) { + EXPECT_LE(prev.order_in_layer, curr.order_in_layer); + } else { + EXPECT_LE(prev.sorting_in_layer, curr.sorting_in_layer); + } + } +} + +TEST_F(RenderSystemTest, Update) { + entity1.add_component(Color::get_white()); + { + vector> sprites = this->mgr.get_components_by_type(); + ASSERT_EQ(sprites.size(), 4); + + EXPECT_EQ(sprites[0].get().game_object_id, 0); + EXPECT_EQ(sprites[1].get().game_object_id, 1); + EXPECT_EQ(sprites[2].get().game_object_id, 2); + EXPECT_EQ(sprites[3].get().game_object_id, 3); + } + this->sys.update(); + { + vector> sprites = this->mgr.get_components_by_type(); + ASSERT_EQ(sprites.size(), 4); + + EXPECT_EQ(sprites[0].get().game_object_id, 0); + EXPECT_EQ(sprites[1].get().game_object_id, 1); + EXPECT_EQ(sprites[2].get().game_object_id, 2); + EXPECT_EQ(sprites[3].get().game_object_id, 3); + } +} + +TEST_F(RenderSystemTest, Camera) { + { + auto cameras = this->mgr.get_components_by_type(); + EXPECT_NE(cameras.size(), 1); + } + { + entity1.add_component(Color::get_white()); + auto cameras = this->mgr.get_components_by_type(); + EXPECT_EQ(cameras.size(), 1); + } + + //TODO improve with newer version +} +TEST_F(RenderSystemTest, Color) { + entity1.add_component(Color::get_white()); + auto & sprite = this->mgr.get_components_by_id(entity1.id).front().get(); + ASSERT_NE(sprite.sprite_image.get(), nullptr); + + sprite.color = Color::get_green(); + EXPECT_EQ(sprite.color.r, Color::get_green().r); + EXPECT_EQ(sprite.color.g, Color::get_green().g); + EXPECT_EQ(sprite.color.b, Color::get_green().b); + EXPECT_EQ(sprite.color.a, Color::get_green().a); + this->sys.update(); + EXPECT_EQ(sprite.color.r, Color::get_green().r); + EXPECT_EQ(sprite.color.g, Color::get_green().g); + EXPECT_EQ(sprite.color.b, Color::get_green().b); + EXPECT_EQ(sprite.color.a, Color::get_green().a); +} -- cgit v1.2.3 From 52e84ca0a81210cd1dc61e096c228586649c9bfc Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 20 Nov 2024 11:35:25 +0100 Subject: updated adjsuted rendering based on feedback --- src/crepe/facade/SDLContext.cpp | 51 +++++++++++++++++++++++++++++--------- src/crepe/facade/SDLContext.h | 26 +++++++++++++++++-- src/crepe/system/RenderSystem.cpp | 8 +++--- src/crepe/system/RenderSystem.h | 3 ++- src/example/rendering_particle.cpp | 11 ++++---- 5 files changed, 74 insertions(+), 25 deletions(-) (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 40189f6..8131df2 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -15,6 +16,8 @@ #include "../api/Texture.h" #include "../api/Transform.h" #include "../util/Log.h" +#include "api/Camera.h" +#include "api/Vector2.h" #include "SDLContext.h" @@ -93,30 +96,54 @@ 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 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; - SDL_Rect srcrect = { +SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) { + return SDL_Rect{ .x = sprite.sprite_rect.x, .y = sprite.sprite_rect.y, .w = sprite.sprite_rect.w, .h = sprite.sprite_rect.h, }; +} +SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, + const double & scale, const Camera & cam) { + + 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; - SDL_Rect dstrect = { + return SDL_Rect{ .x = static_cast(adjusted_x), .y = static_cast(adjusted_y), .w = static_cast(adjusted_w), .h = static_cast(adjusted_h), }; +} + +void SDLContext::draw_particle(const Sprite & sprite, const Vector2 & pos, + const double & angle, const double & scale, + const Camera & camera) { + + 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_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, transform.scale, cam); 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 78ac56b..fb09015 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -1,8 +1,10 @@ #pragma once #include +#include #include #include +#include #include #include #include @@ -10,6 +12,7 @@ #include "../api/Sprite.h" #include "../api/Transform.h" #include "api/Camera.h" +#include "api/Vector2.h" namespace crepe { @@ -122,8 +125,7 @@ private: * \param camera Reference to the Camera for view adjustments. */ void draw(const Sprite & sprite, const Transform & transform, const Camera & camera); - - void draw_particle(const Vector2 & pos, const Camera & camera); + void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, const double & scale, const Camera & camera); //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -137,6 +139,26 @@ private: */ void camera(const Camera & camera); +private: + /** + * \brief calculates the sqaure size of the image + * + * \param sprite Reference to the sprite to calculate the rectangle + * \return sdl rectangle to draw a src image + */ + SDL_Rect get_src_rect(const Sprite & sprite); + /** + * \brief calculates the sqaure size of the image for an 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 + * \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); + private: //! sdl Window std::unique_ptr> game_window; diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 39e0e3f..1ae5ca7 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -27,7 +27,7 @@ void RenderSystem::update_camera() { } bool RenderSystem::render_particle(const Sprite & sprite, - Transform tm) { + const double & scale) { ComponentManager & mgr = this->component_manager; SDLContext & render = SDLContext::get_instance(); @@ -44,9 +44,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, for (const Particle & p : em.data.particles) { if (!p.active) continue; - tm.position = p.position; - tm.rotation = p.angle; - render.draw(em.data.sprite, tm, *curr_cam); + render.draw_particle(sprite, p.position, p.angle, scale, *this->curr_cam); } } return rendering_particles; @@ -68,7 +66,7 @@ void RenderSystem::render() { if (!sprite.active) continue; auto transform = mgr.get_components_by_id(sprite.game_object_id).front().get(); - bool rendered_particles = this->render_particle(sprite, transform); + bool rendered_particles = this->render_particle(sprite, transform.scale); if (rendered_particles) continue; diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index c18b80b..6643084 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -5,6 +5,7 @@ #include "api/Transform.h" #include "System.h" +#include namespace crepe { @@ -51,7 +52,7 @@ private: * \param tm the Transform component for scale * \return true if particles have been rendered */ - bool render_particle(const Sprite &, Transform tm); + bool render_particle(const Sprite &, const double & scale); /** * \brief renders a sprite with a Transform component on the screen diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 33013ef..71b50ba 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -21,7 +20,11 @@ using namespace crepe; using namespace std; int main(int argc, char * argv[]) { - GameObject game_object(0, "", "", Vector2{100, 100}, 0, 0.1); + ComponentManager mgr; + GameObject game_object = mgr.new_object("", "", Vector2{100, 100}, 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, @@ -47,11 +50,9 @@ int main(int argc, char * argv[]) { }); game_object.add_component(Color::get_white()); - auto & sys = crepe::RenderSystem::get_instance(); - auto sys_part = crepe::ParticleSystem(); auto start = std::chrono::steady_clock::now(); while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { - sys_part.update(); + psys.update(); sys.update(); SDL_Delay(10 ); } -- cgit v1.2.3 From 5121e95e5ba98ca5cadfb63b7603731dfb41a322 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 20 Nov 2024 11:53:17 +0100 Subject: implemented feedback --- makefile | 2 +- mwe/events/include/event.h | 2 +- src/crepe/api/Sprite.h | 3 ++- src/crepe/facade/SDLContext.h | 1 - src/crepe/system/RenderSystem.cpp | 15 +++++++-------- src/crepe/system/RenderSystem.h | 20 +++++++++++--------- 6 files changed, 22 insertions(+), 21 deletions(-) (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/makefile b/makefile index dd7c587..32e68c0 100644 --- a/makefile +++ b/makefile @@ -6,5 +6,5 @@ doxygen: Doxyfile FORCE FMT += $(shell git ls-files '*.c' '*.cpp' '*.h' '*.hpp') format: FORCE clang-format -i $(FMT) - $(MAKE) -C src $@ + #$(MAKE) -C src $@ 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/Sprite.h b/src/crepe/api/Sprite.h index 0192793..74a55d4 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -2,8 +2,9 @@ #include +#include "../Component.h" + #include "Color.h" -#include "Component.h" #include "Texture.h" namespace crepe { diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index d437bc4..652a83e 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -21,7 +21,6 @@ namespace crepe { // typedef is unusable when crepe is packaged. Wouter will fix this later. typedef SDL_Keycode CREPE_KEYCODES; - /** * \class SDLContext * \brief Facade for the SDL library diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 82257c4..96c5f27 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -12,10 +12,11 @@ #include "RenderSystem.h" using namespace crepe; +using namespace std; -void RenderSystem::clear_screen() const { SDLContext::get_instance().clear_screen(); } +void RenderSystem::clear_screen() { this->context.clear_screen(); } -void RenderSystem::present_screen() const { SDLContext::get_instance().present_screen(); } +void RenderSystem::present_screen() { this->context.present_screen(); } void RenderSystem::update_camera() { ComponentManager & mgr = this->component_manager; @@ -24,7 +25,7 @@ void RenderSystem::update_camera() { if (cameras.size() == 0) throw std::runtime_error("No cameras in current scene"); for (Camera & cam : cameras) { - SDLContext::get_instance().camera(cam); + this->context.camera(cam); this->curr_cam_ref = &cam; } } @@ -47,14 +48,12 @@ RenderSystem::sort(std::vector> & objs) { void RenderSystem::render_sprites() { ComponentManager & mgr = this->component_manager; + vector> sprites = mgr.get_components_by_type(); + vector> sorted_sprites = this->sort(sprites); - auto sprites = mgr.get_components_by_type(); - auto sorted_sprites = this->sort(sprites); - - SDLContext & render = SDLContext::get_instance(); for (const Sprite & sprite : sorted_sprites) { auto transforms = mgr.get_components_by_id(sprite.game_object_id); - render.draw(sprite, transforms[0], *this->curr_cam_ref); + this->context.draw(sprite, transforms[0], *this->curr_cam_ref); } } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 1176b06..8914b96 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -1,23 +1,23 @@ #pragma once - #include #include -#include "api/Sprite.h" -#include "api/Camera.h" +#include "facade/SDLContext.h" #include "System.h" namespace crepe { +class Camera; +class Sprite; + /** * \class RenderSystem * \brief Manages rendering operations for all game objects. * - * RenderSystem is responsible for rendering sprites, clearing and presenting the screen, and - * managing the active camera. It functions as a singleton, providing centralized rendering - * services for the application. + * RenderSystem is responsible for rendering, clearing and presenting the screen, and + * managing the active camera. */ class RenderSystem : public System { public: @@ -30,16 +30,16 @@ public: private: //! Clears the screen in preparation for rendering. - void clear_screen() const; + void clear_screen(); //! Presents the rendered frame to the display. - void present_screen() const; + void present_screen(); //! Updates the active camera used for rendering. void update_camera(); //! Renders all active sprites to the screen. - void render_sprites() ; + void render_sprites(); std::vector> sort(std::vector> & objs); @@ -58,6 +58,8 @@ private: //! Pointer to the current active camera for rendering Camera * curr_cam_ref = nullptr; // TODO: needs a better solution + + SDLContext & context = SDLContext::get_instance(); }; } // namespace crepe -- cgit v1.2.3 From 3344e0df01435be903e38ccbd3b9cee608d574e7 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 20 Nov 2024 14:06:54 +0100 Subject: implemented feedback --- src/crepe/facade/SDLContext.cpp | 18 +++++++++--------- src/crepe/facade/SDLContext.h | 10 ++++++---- src/crepe/system/RenderSystem.cpp | 18 +++++++++--------- src/crepe/system/RenderSystem.h | 14 ++++---------- src/example/rendering_particle.cpp | 14 ++++++-------- 5 files changed, 34 insertions(+), 40 deletions(-) (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index eacb10a..daf0050 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -12,12 +12,12 @@ #include #include +#include "../api/Camera.h" #include "../api/Sprite.h" #include "../api/Texture.h" #include "../api/Transform.h" +#include "../api/Vector2.h" #include "../util/Log.h" -#include "api/Camera.h" -#include "api/Vector2.h" #include "SDLContext.h" @@ -96,8 +96,7 @@ 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()); } - -SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) { +SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { return SDL_Rect{ .x = sprite.sprite_rect.x, .y = sprite.sprite_rect.y, @@ -106,7 +105,7 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) { }; } SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, - const double & scale, const Camera & cam) { + const double & scale, const Camera & cam) const { double adjusted_x = (pos.x - cam.x) * cam.zoom; double adjusted_y = (pos.y - cam.y) * cam.zoom; @@ -123,20 +122,21 @@ 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 double & scale, - const Camera & camera) { + const Camera & camera) const { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) - | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); + | (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_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, - &dstrect, angle, NULL, render_flip); + &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 Camera & cam) const { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 287ad5d..718c40f 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -124,9 +124,10 @@ 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 Camera & camera) const; - 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 double & scale, const Camera & camera) const; //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -147,7 +148,7 @@ private: * \param sprite Reference to the sprite to calculate the rectangle * \return sdl rectangle to draw a src image */ - SDL_Rect get_src_rect(const Sprite & sprite); + SDL_Rect get_src_rect(const Sprite & sprite) const; /** * \brief calculates the sqaure size of the image for an destination * @@ -158,7 +159,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 double & scale, const Camera & cam); + SDL_Rect get_dst_rect(const Sprite & sprite, const Vector2 & pos, const double & scale, + const Camera & cam) const; private: //! sdl Window diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 1ae5ca7..28bcf56 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -2,11 +2,11 @@ #include #include "../ComponentManager.h" +#include "../api/ParticleEmitter.h" #include "../api/Sprite.h" #include "../api/Transform.h" -#include "../facade/SDLContext.h" -#include "../api/ParticleEmitter.h" #include "../api/Vector2.h" +#include "../facade/SDLContext.h" #include "RenderSystem.h" @@ -21,13 +21,12 @@ void RenderSystem::update_camera() { auto cameras = mgr.get_components_by_type(); for (Camera & cam : cameras) { - SDLContext::get_instance().camera(cam); + SDLContext::get_instance().set_camera(cam); this->curr_cam = &cam; } } -bool RenderSystem::render_particle(const Sprite & sprite, - const double & scale) { +bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) const { ComponentManager & mgr = this->component_manager; SDLContext & render = SDLContext::get_instance(); @@ -49,22 +48,23 @@ bool RenderSystem::render_particle(const Sprite & sprite, } return rendering_particles; } -void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { +void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) const { ComponentManager & mgr = this->component_manager; SDLContext & render = SDLContext::get_instance(); - + render.draw(sprite, tm, *curr_cam); } -void RenderSystem::render() { +void RenderSystem::render() const { ComponentManager & mgr = this->component_manager; auto sprites = mgr.get_components_by_type(); for (const Sprite & sprite : sprites) { if (!sprite.active) continue; - auto transform = mgr.get_components_by_id(sprite.game_object_id).front().get(); + const Transform & transform + = mgr.get_components_by_id(sprite.game_object_id).front().get(); bool rendered_particles = this->render_particle(sprite, transform.scale); diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 6643084..8841f72 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -25,13 +25,7 @@ public: * This method is called to perform all rendering operations for the current game frame. */ void update() override; - - - RenderSystem(const RenderSystem &) = delete; - RenderSystem(RenderSystem &&) = delete; - RenderSystem & operator=(const RenderSystem &) = delete; - RenderSystem & operator=(RenderSystem &&) = delete; - + private: //! Clears the screen in preparation for rendering. void clear_screen() const; @@ -43,7 +37,7 @@ private: void update_camera(); //! Renders the whole screen - void render(); + void render() const; /** * \brief Renders all the particles on the screen from a given sprite. @@ -52,7 +46,7 @@ private: * \param tm the Transform component for scale * \return true if particles have been rendered */ - bool render_particle(const Sprite &, const double & scale); + bool render_particle(const Sprite & sprite, const double & scale) const; /** * \brief renders a sprite with a Transform component on the screen @@ -60,7 +54,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 &, const Transform & tm); + void render_normal(const Sprite & sprite, const Transform & tm) const; /** * \todo Include color handling for sprites. diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 71b50ba..3f71750 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -4,15 +4,15 @@ #include #include +#include #include #include #include +#include #include #include -#include -#include -#include #include +#include #include @@ -20,15 +20,14 @@ using namespace crepe; using namespace std; int main(int argc, char * argv[]) { - ComponentManager mgr; + ComponentManager mgr; GameObject game_object = mgr.new_object("", "", Vector2{100, 100}, 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/img.png"), color, FlipSettings{false, false}); game_object.add_component(ParticleEmitter::Data{ .position = {0, 0}, .max_particles = 10, @@ -54,9 +53,8 @@ int main(int argc, char * argv[]) { while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { psys.update(); sys.update(); - SDL_Delay(10 ); + SDL_Delay(10); } return 0; } - -- cgit v1.2.3 From d623b13dab63408cf5e99dbc453636f174bc6fe8 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 20 Nov 2024 14:33:08 +0100 Subject: fixxed bug with particleemiiter and sprite in single component --- src/crepe/system/RenderSystem.cpp | 4 ++-- src/example/rendering_particle.cpp | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 28bcf56..c137de1 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -36,10 +36,10 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) bool rendering_particles = false; for (const ParticleEmitter & em : emitters) { + if (!(&em.data.sprite == &sprite)) continue; + rendering_particles = true; if (!em.active) continue; - if (!(em.data.sprite.game_object_id == sprite.game_object_id)) continue; - rendering_particles = true; for (const Particle & p : em.data.particles) { if (!p.active) continue; diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 3f71750..bcf95b8 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -15,6 +15,7 @@ #include #include +#include using namespace crepe; using namespace std; @@ -26,10 +27,12 @@ int main(int argc, char * argv[]) { ParticleSystem psys{mgr}; Color color(255, 255, 255, 255); + game_object.add_component(make_shared("../asset/texture/img.png"), color, FlipSettings{false,false}); + Sprite test_sprite = game_object.add_component( make_shared("../asset/texture/img.png"), color, FlipSettings{false, false}); game_object.add_component(ParticleEmitter::Data{ - .position = {0, 0}, + .position = {100, 0}, .max_particles = 10, .emission_rate = 0.1, .min_speed = 6, @@ -46,9 +49,10 @@ int main(int argc, char * argv[]) { .reset_on_exit = false, }, .sprite = test_sprite, - }); + }).active = false; game_object.add_component(Color::get_white()); + auto start = std::chrono::steady_clock::now(); while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { psys.update(); -- cgit v1.2.3 From a0f39be4510665614d25d23d88fe1d0f0b5cc740 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 20 Nov 2024 15:16:06 +0100 Subject: rendering system bug --- src/crepe/system/RenderSystem.cpp | 29 ++++++++++++++--------------- src/crepe/system/RenderSystem.h | 12 ++++++------ src/example/rendering_particle.cpp | 8 ++++---- 3 files changed, 24 insertions(+), 25 deletions(-) (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index bc5422d..c599729 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include #include @@ -48,8 +50,6 @@ RenderSystem::sort(std::vector> & objs) { return sorted_objs; } - - void RenderSystem::update() { this->clear_screen(); this->update_camera(); @@ -57,43 +57,42 @@ void RenderSystem::update() { this->present_screen(); } -bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) const { +bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) { ComponentManager & mgr = this->component_manager; - auto emitters = mgr.get_components_by_id(sprite.game_object_id); + vector> emitters + = mgr.get_components_by_id(sprite.game_object_id); bool rendering_particles = false; for (const ParticleEmitter & em : emitters) { + cout << &em.data.sprite << " " << &sprite << endl; if (!(&em.data.sprite == &sprite)) continue; rendering_particles = true; if (!em.active) continue; - for (const Particle & p : em.data.particles) { if (!p.active) continue; - this->context.draw_particle(sprite, p.position, p.angle, scale, *this->curr_cam); + this->context.draw_particle(sprite, p.position, p.angle, scale, + *this->curr_cam_ref); } } return rendering_particles; } -void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) const { - - ComponentManager & mgr = this->component_manager; - - this->context.draw(sprite, tm, *this->curr_cam); +void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { + this->context.draw(sprite, tm, *this->curr_cam_ref); } -void RenderSystem::render() const { +void RenderSystem::render() { ComponentManager & mgr = this->component_manager; vector> sprites = mgr.get_components_by_type(); - vector> sorted_sprites = this->sort(sprites); + //vector> sorted_sprites = this->sort(sprites); - for (const Sprite & sprite : sorted_sprites) { + for (const Sprite & sprite : sprites) { if (!sprite.active) continue; - const Transform & transform + Transform & transform = mgr.get_components_by_id(sprite.game_object_id).front().get(); bool rendered_particles = this->render_particle(sprite, transform.scale); diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index b9033fb..0393f2f 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -31,16 +31,16 @@ public: private: //! Clears the screen in preparation for rendering. - void clear_screen(); + void clear_screen() ; //! Presents the rendered frame to the display. - void present_screen(); + void present_screen() ; //! Updates the active camera used for rendering. void update_camera(); //! Renders the whole screen - void render() const; + void render() ; /** * \brief Renders all the particles on the screen from a given sprite. @@ -49,7 +49,7 @@ private: * \param tm the Transform component for scale * \return true if particles have been rendered */ - bool render_particle(const Sprite & sprite, const double & scale) const; + bool render_particle(const Sprite & sprite, const double & scale) ; /** * \brief renders a sprite with a Transform component on the screen @@ -57,7 +57,7 @@ private: * \param sprite the sprite component that holds all the data * \param tm the Transform component that holds the position,rotation and scale */ - void render_normal(const Sprite & sprite, const Transform & tm); + void render_normal(const Sprite & sprite, const Transform & tm) ; /** * \brief sort a vector sprite objects with @@ -66,7 +66,7 @@ private: * \return returns a sorted reference vector */ std::vector> - sort(std::vector> & objs); + sort(std::vector> & objs) ; /** * \todo Include color handling for sprites. diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index bcf95b8..062f11f 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -27,12 +27,12 @@ int main(int argc, char * argv[]) { ParticleSystem psys{mgr}; Color color(255, 255, 255, 255); - game_object.add_component(make_shared("../asset/texture/img.png"), color, FlipSettings{false,false}); + //game_object.add_component(make_shared("../asset/texture/img.png"), color, FlipSettings{false,false}); Sprite test_sprite = game_object.add_component( make_shared("../asset/texture/img.png"), color, FlipSettings{false, false}); game_object.add_component(ParticleEmitter::Data{ - .position = {100, 0}, + .position = {0, 0}, .max_particles = 10, .emission_rate = 0.1, .min_speed = 6, @@ -49,8 +49,8 @@ int main(int argc, char * argv[]) { .reset_on_exit = false, }, .sprite = test_sprite, - }).active = false; - game_object.add_component(Color::get_white()); + }); + game_object.add_component(Color::WHITE); auto start = std::chrono::steady_clock::now(); -- cgit v1.2.3 From ccb9dd63c6fc172fed9acaea31a6d67c457b37fe Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 20 Nov 2024 15:42:55 +0100 Subject: fixed the reference bug in render particle, it was in the example problem --- mwe/events/include/event.h | 2 +- src/crepe/system/RenderSystem.cpp | 19 +++++++++---------- src/crepe/system/RenderSystem.h | 14 +++++++------- src/example/rendering_particle.cpp | 12 +++++++++--- 4 files changed, 26 insertions(+), 21 deletions(-) (limited to 'src/crepe/system/RenderSystem.cpp') 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/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index c599729..05b7337 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -18,9 +18,9 @@ using namespace crepe; using namespace std; -void RenderSystem::clear_screen() { this->context.clear_screen(); } +void RenderSystem::clear_screen() const { this->context.clear_screen(); } -void RenderSystem::present_screen() { this->context.present_screen(); } +void RenderSystem::present_screen() const { this->context.present_screen(); } void RenderSystem::update_camera() { ComponentManager & mgr = this->component_manager; @@ -42,7 +42,7 @@ bool sorting_comparison(const Sprite & a, const Sprite & b) { } std::vector> -RenderSystem::sort(std::vector> & objs) { +RenderSystem::sort(std::vector> & objs) const { std::vector> sorted_objs(objs); std::sort(sorted_objs.begin(), sorted_objs.end(), sorting_comparison); @@ -57,7 +57,7 @@ void RenderSystem::update() { this->present_screen(); } -bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) { +bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) const { ComponentManager & mgr = this->component_manager; @@ -67,7 +67,6 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) bool rendering_particles = false; for (const ParticleEmitter & em : emitters) { - cout << &em.data.sprite << " " << &sprite << endl; if (!(&em.data.sprite == &sprite)) continue; rendering_particles = true; if (!em.active) continue; @@ -80,19 +79,19 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) } return rendering_particles; } -void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { +void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) const { this->context.draw(sprite, tm, *this->curr_cam_ref); } -void RenderSystem::render() { +void RenderSystem::render() const { ComponentManager & mgr = this->component_manager; vector> sprites = mgr.get_components_by_type(); - //vector> sorted_sprites = this->sort(sprites); + vector> sorted_sprites = this->sort(sprites); - for (const Sprite & sprite : sprites) { + for (const Sprite & sprite : sorted_sprites) { if (!sprite.active) continue; - Transform & transform + const Transform & transform = mgr.get_components_by_id(sprite.game_object_id).front().get(); bool rendered_particles = this->render_particle(sprite, transform.scale); diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 0393f2f..1a11d5b 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -28,19 +28,19 @@ public: * This method is called to perform all rendering operations for the current game frame. */ void update() override; - + private: //! Clears the screen in preparation for rendering. - void clear_screen() ; + void clear_screen() const; //! Presents the rendered frame to the display. - void present_screen() ; + void present_screen() const; //! Updates the active camera used for rendering. void update_camera(); //! Renders the whole screen - void render() ; + void render() const; /** * \brief Renders all the particles on the screen from a given sprite. @@ -49,7 +49,7 @@ private: * \param tm the Transform component for scale * \return true if particles have been rendered */ - bool render_particle(const Sprite & sprite, const double & scale) ; + bool render_particle(const Sprite & sprite, const double & scale) const; /** * \brief renders a sprite with a Transform component on the screen @@ -57,7 +57,7 @@ private: * \param sprite the sprite component that holds all the data * \param tm the Transform component that holds the position,rotation and scale */ - void render_normal(const Sprite & sprite, const Transform & tm) ; + void render_normal(const Sprite & sprite, const Transform & tm) const; /** * \brief sort a vector sprite objects with @@ -66,7 +66,7 @@ private: * \return returns a sorted reference vector */ std::vector> - sort(std::vector> & objs) ; + sort(std::vector> & objs) const; /** * \todo Include color handling for sprites. diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 062f11f..5030bc6 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -15,6 +15,7 @@ #include #include +#include #include using namespace crepe; @@ -27,11 +28,11 @@ int main(int argc, char * argv[]) { ParticleSystem psys{mgr}; Color color(255, 255, 255, 255); - //game_object.add_component(make_shared("../asset/texture/img.png"), color, FlipSettings{false,false}); - Sprite test_sprite = game_object.add_component( + Sprite & test_sprite = game_object.add_component( make_shared("../asset/texture/img.png"), color, FlipSettings{false, false}); - game_object.add_component(ParticleEmitter::Data{ + test_sprite.order_in_layer = 5; + auto test = game_object.add_component(ParticleEmitter::Data{ .position = {0, 0}, .max_particles = 10, .emission_rate = 0.1, @@ -52,6 +53,11 @@ int main(int argc, char * argv[]) { }); game_object.add_component(Color::WHITE); + game_object + .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 700892e9a2801902da31835c0069ae27da54cf4c Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 20 Nov 2024 19:21:46 +0100 Subject: implemented feedback --- src/crepe/facade/SDLContext.h | 4 ++-- src/crepe/system/RenderSystem.cpp | 11 ++++++----- src/crepe/system/RenderSystem.h | 10 +++++----- 3 files changed, 13 insertions(+), 12 deletions(-) (limited to 'src/crepe/system/RenderSystem.cpp') diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 9a514cb..3b17c0b 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -118,10 +118,10 @@ 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) const; + void draw(const Sprite & sprite, const Transform & transform, const Camera & camera) ; void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, - const double & scale, const Camera & camera) const; + const double & scale, const Camera & camera) ; //! Clears the screen, preparing for a new frame. void clear_screen(); diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 05b7337..e379771 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -18,9 +18,9 @@ using namespace crepe; using namespace std; -void RenderSystem::clear_screen() const { this->context.clear_screen(); } +void RenderSystem::clear_screen() { this->context.clear_screen(); } -void RenderSystem::present_screen() const { this->context.present_screen(); } +void RenderSystem::present_screen() { this->context.present_screen(); } void RenderSystem::update_camera() { ComponentManager & mgr = this->component_manager; @@ -29,6 +29,7 @@ void RenderSystem::update_camera() { if (cameras.size() == 0) throw std::runtime_error("No cameras in current scene"); for (Camera & cam : cameras) { + if (!cam.active) continue; this->context.set_camera(cam); this->curr_cam_ref = &cam; } @@ -57,7 +58,7 @@ void RenderSystem::update() { this->present_screen(); } -bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) const { +bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) { ComponentManager & mgr = this->component_manager; @@ -79,11 +80,11 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) } return rendering_particles; } -void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) const { +void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { this->context.draw(sprite, tm, *this->curr_cam_ref); } -void RenderSystem::render() const { +void RenderSystem::render() { ComponentManager & mgr = this->component_manager; vector> sprites = mgr.get_components_by_type(); diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 1a11d5b..5a2af17 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -31,16 +31,16 @@ public: private: //! Clears the screen in preparation for rendering. - void clear_screen() const; + void clear_screen() ; //! Presents the rendered frame to the display. - void present_screen() const; + void present_screen() ; //! Updates the active camera used for rendering. void update_camera(); //! Renders the whole screen - void render() const; + void render(); /** * \brief Renders all the particles on the screen from a given sprite. @@ -49,7 +49,7 @@ private: * \param tm the Transform component for scale * \return true if particles have been rendered */ - bool render_particle(const Sprite & sprite, const double & scale) const; + bool render_particle(const Sprite & sprite, const double & scale); /** * \brief renders a sprite with a Transform component on the screen @@ -57,7 +57,7 @@ private: * \param sprite the sprite component that holds all the data * \param tm the Transform component that holds the position,rotation and scale */ - void render_normal(const Sprite & sprite, const Transform & tm) const; + void render_normal(const Sprite & sprite, const Transform & tm); /** * \brief sort a vector sprite objects with -- cgit v1.2.3