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 --- asset/texture/ERROR.png | Bin 0 -> 3581 bytes src/crepe/system/RenderSystem.cpp | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 asset/texture/ERROR.png diff --git a/asset/texture/ERROR.png b/asset/texture/ERROR.png new file mode 100644 index 0000000..2af3548 Binary files /dev/null and b/asset/texture/ERROR.png differ 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 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(-) 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(-) 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(-) 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 8524865e9a62974f77f63d70929000fc63c679d7 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 12 Nov 2024 19:55:11 +0100 Subject: makefile --- src/makefile | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/makefile b/src/makefile index 356179e..21903a7 100644 --- a/src/makefile +++ b/src/makefile @@ -28,18 +28,18 @@ MAX += crepe/api/Metadata.cpp MAX += crepe/api/Metadata.h JARO += crepe/Particle.cpp JARO += crepe/Particle.h -TODO += crepe/Position.h -TODO += crepe/api/AssetManager.cpp -TODO += crepe/api/AssetManager.h -TODO += crepe/api/AssetManager.hpp +NIELS += crepe/Position.h +NIELS += crepe/api/AssetManager.cpp +NIELS += crepe/api/AssetManager.h +NIELS += crepe/api/AssetManager.hpp LOEK += crepe/api/AudioSource.cpp LOEK += crepe/api/AudioSource.h LOEK += crepe/api/BehaviorScript.cpp LOEK += crepe/api/BehaviorScript.h LOEK += crepe/api/BehaviorScript.hpp TODO += crepe/api/CircleCollider.h -TODO += crepe/api/Color.cpp -TODO += crepe/api/Color.h +NIELS += crepe/api/Color.cpp +NIELS += crepe/api/Color.h LOEK += crepe/api/Config.h MAX += crepe/api/GameObject.cpp MAX += crepe/api/GameObject.h @@ -53,14 +53,14 @@ JARO += crepe/api/Rigidbody.h LOEK += crepe/api/Script.cpp LOEK += crepe/api/Script.h LOEK += crepe/api/Script.hpp -TODO += crepe/api/Sprite.cpp -TODO += crepe/api/Sprite.h -TODO += crepe/api/Texture.cpp -TODO += crepe/api/Texture.h +NIELS += crepe/api/Sprite.cpp +NIELS += crepe/api/Sprite.h +NIELS += crepe/api/Texture.cpp +NIELS += crepe/api/Texture.h MAX += crepe/api/Transform.cpp MAX += crepe/api/Transform.h -TODO += crepe/facade/SDLContext.cpp -TODO += crepe/facade/SDLContext.h +NIELS += crepe/facade/SDLContext.cpp +NIELS += crepe/facade/SDLContext.h LOEK += crepe/facade/Sound.cpp LOEK += crepe/facade/Sound.h LOEK += crepe/facade/SoundContext.cpp @@ -71,8 +71,8 @@ JARO += crepe/system/ParticleSystem.cpp JARO += crepe/system/ParticleSystem.h JARO += crepe/system/PhysicsSystem.cpp JARO += crepe/system/PhysicsSystem.h -TODO += crepe/system/RenderSystem.cpp -TODO += crepe/system/RenderSystem.h +NIELS += crepe/system/RenderSystem.cpp +NIELS += crepe/system/RenderSystem.h LOEK += crepe/system/ScriptSystem.cpp LOEK += crepe/system/ScriptSystem.h LOEK += crepe/system/System.h @@ -82,21 +82,21 @@ LOEK += crepe/util/fmt.cpp LOEK += crepe/util/fmt.h LOEK += crepe/util/log.cpp LOEK += crepe/util/log.h -TODO += example/asset_manager.cpp +NIELS += example/asset_manager.cpp LOEK += example/audio_internal.cpp TODO += example/components_internal.cpp MAX += example/ecs.cpp LOEK += example/log.cpp JARO += example/particles.cpp JARO += example/physics.cpp -TODO += example/rendering.cpp +NIELS += example/rendering.cpp LOEK += example/script.cpp LOEK += test/audio.cpp LOEK += test/dummy.cpp JARO += test/PhysicsTest.cpp JARO += test/ParticleTest.cpp -FMT := $(JARO) #<<< CHANGE THIS TO YOUR NAME FOR STEP 2 +FMT := $(NIELS) #<<< CHANGE THIS TO YOUR NAME FOR STEP 2 format: FORCE clang-tidy -p build/compile_commands.json --fix-errors $(FMT) -- cgit v1.2.3 From 032f8560b91b76cad5f32a6a9ebfc0ac845ed69a Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Thu, 14 Nov 2024 10:37:57 +0100 Subject: forget a doxygen docs on a function --- src/crepe/system/RenderSystem.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 468f79b..6a87eec 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -57,6 +57,12 @@ private: */ bool render_particle(const Sprite &, const Transform & tm); + /** + * \brief renders a sprite with a Transform component on the screen + * + * \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); /** -- 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(-) 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(-) 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 ed1403aea4412bd479244eae8e2940190e71cd28 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 15 Nov 2024 09:48:04 +0100 Subject: example --- src/example/rendering_particle.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index dd08354..33013ef 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -22,14 +22,14 @@ using namespace std; int main(int argc, char * argv[]) { GameObject game_object(0, "", "", Vector2{100, 100}, 0, 0.1); - Color color(0, 0, 0, 0); + Color color(255, 255, 255, 255); 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 = 10, - .emission_rate = 0.5, + .emission_rate = 0.1, .min_speed = 6, .max_speed = 20, .min_angle = -20, -- 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(-) 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(-) 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(-) 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(-) 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(-) 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 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 0476a8e9dbe7afb422862f7b1c15aaed7f3c416e Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 19 Nov 2024 12:11:11 +0100 Subject: add some more doxygen --- .editorconfig | 2 +- Doxyfile | 9 +- src/doc/feature/script.dox | 62 +++++++++++ src/doc/features.dox | 10 ++ src/doc/index.dox | 10 ++ src/doc/installing.dox | 9 ++ src/doc/layout.xml | 252 +++++++++++++++++++++++++++++++++++++++++++++ src/doc/style.css | 4 + 8 files changed, 356 insertions(+), 2 deletions(-) create mode 100644 src/doc/feature/script.dox create mode 100644 src/doc/features.dox create mode 100644 src/doc/index.dox create mode 100644 src/doc/installing.dox create mode 100644 src/doc/layout.xml create mode 100644 src/doc/style.css diff --git a/.editorconfig b/.editorconfig index 191871f..65f5034 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,5 +10,5 @@ max_line_length = 95 indent_style = space indent_size = 2 -[*.md] +[*.{md,dox}] max_line_length = 80 diff --git a/Doxyfile b/Doxyfile index 8ba90fc..9328b24 100644 --- a/Doxyfile +++ b/Doxyfile @@ -14,12 +14,19 @@ RECURSIVE = YES GENERATE_LATEX = NO +LAYOUT_FILE = src/doc/layout.xml +TAB_SIZE = 2 + +HTML_INDEX_NUM_ENTRIES = 2 +HTML_EXTRA_STYLESHEET = src/doc/style.css + USE_MDFILE_AS_MAINPAGE = ./readme.md -HTML_INDEX_NUM_ENTRIES = 1 # collapse trees by default REPEAT_BRIEF = NO INTERNAL_DOCS = YES EXTRACT_STATIC = YES +HIDE_UNDOC_NAMESPACES = YES +HIDE_UNDOC_CLASSES = YES QUIET = YES diff --git a/src/doc/feature/script.dox b/src/doc/feature/script.dox new file mode 100644 index 0000000..d25a63b --- /dev/null +++ b/src/doc/feature/script.dox @@ -0,0 +1,62 @@ +// vim:ft=doxygen +namespace crepe { +/** + +\defgroup feature_script Scripting +\ingroup feature +\brief User-defined scripts for game objects + +Scripts can be used to implement game behavior, and allow arbitrary code to run +as part of the game loop. Scripts are implemented as derivative classes of +Script, which are added to game objects using the BehaviorScript \ref Component +"component". + +\todo This section is incomplete: +- Utility functions to get components/events/etc inside script +- How to listen for events +- Extensions of script (keylistener) + +\see Script +\see BehaviorScript +\see GameObject + +\par Example + +First, define a class that inherits from Script. This class acts as an +interface, and has two functions (\ref Script::init "\c init()" and \ref +Script::update "\c update()"), which may be implemented (they are empty by +default). From now on, this derivative class will be referred to as a *concrete +script*. + +```cpp +#include +#include + +class MyScript : public crepe::Script { + void init() { + // called once + } + void update() { + // called on fixed update + } +}; +``` + +Concrete scripts can be instantiated and attached to \ref GameObject +"game objects" using the BehaviorScript \ref Component "component". + +```cpp +using namespace crepe; +GameObject obj = component_manager.new_object("name"); + +// create BehaviorScript instance +BehaviorScript & behavior_script = obj.add_component(); +// attach (and instantiate) MyScript to behavior_script +behavior_script.set_script(); + +// the above can also be done in a single call for convenience: +obj.add_component().set_script(); +``` + +*/ +} diff --git a/src/doc/features.dox b/src/doc/features.dox new file mode 100644 index 0000000..4786bed --- /dev/null +++ b/src/doc/features.dox @@ -0,0 +1,10 @@ +// vim:ft=doxygen +/** + +\defgroup feature Features +\brief Engine components + +This page lists engine features and contains usage instructions for each +feature. + +*/ diff --git a/src/doc/index.dox b/src/doc/index.dox new file mode 100644 index 0000000..5ec7889 --- /dev/null +++ b/src/doc/index.dox @@ -0,0 +1,10 @@ +// vim:ft=doxygen +/** + +\mainpage crêpe game engine + +Welcome to the documentation for the crêpe game engine. + +\see feature + +*/ diff --git a/src/doc/installing.dox b/src/doc/installing.dox new file mode 100644 index 0000000..48b27d7 --- /dev/null +++ b/src/doc/installing.dox @@ -0,0 +1,9 @@ +// vim:ft=doxygen +/** + +\defgroup install Installation +\brief Engine installation instructions + +\todo This entire page + +*/ diff --git a/src/doc/layout.xml b/src/doc/layout.xml new file mode 100644 index 0000000..2244fa7 --- /dev/null +++ b/src/doc/layout.xml @@ -0,0 +1,252 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/doc/style.css b/src/doc/style.css new file mode 100644 index 0000000..08bc9f5 --- /dev/null +++ b/src/doc/style.css @@ -0,0 +1,4 @@ +#titlearea, +address { + display: none; +} -- cgit v1.2.3 From 85a15e4f43941b5d47fc4be2fee754f0a3d88d15 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 20 Nov 2024 10:41:01 +0100 Subject: Added ECS tests --- src/example/CMakeLists.txt | 2 - src/example/components_internal.cpp | 51 -------- src/example/ecs.cpp | 53 -------- src/test/CMakeLists.txt | 1 + src/test/ECSTest.cpp | 236 ++++++++++++++++++++++++++++++++++++ 5 files changed, 237 insertions(+), 106 deletions(-) delete mode 100644 src/example/components_internal.cpp delete mode 100644 src/example/ecs.cpp create mode 100644 src/test/ECSTest.cpp diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 3a5b543..bbeec09 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -17,7 +17,6 @@ function(add_example target_name) endfunction() add_example(audio_internal) -# add_example(components_internal) add_example(script) add_example(log) add_example(rendering) @@ -26,7 +25,6 @@ add_example(physics) add_example(savemgr) add_example(proxy) add_example(db) -add_example(ecs) add_example(scene_manager) add_example(particles) add_example(gameloop) diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp deleted file mode 100644 index 2a232a9..0000000 --- a/src/example/components_internal.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/** \file - * - * Standalone example for usage of the internal ECS - */ - -#include -#include - -#include -#include - -#include -#include -#include - -#include - -using namespace crepe; -using namespace std; - -#define OBJ_COUNT 100000 - -int main() { - dbg_trace(); - - ComponentManager mgr{}; - - auto start_adding = chrono::high_resolution_clock::now(); - - for (int i = 0; i < OBJ_COUNT; ++i) { - GameObject obj = mgr.new_object("Name", "Tag"); - obj.add_component("test"); - obj.add_component(0, 0, i); - } - - auto stop_adding = chrono::high_resolution_clock::now(); - - auto sprites = mgr.get_components_by_type(); - for (auto sprite : sprites) { - assert(true); - } - - auto stop_looping = chrono::high_resolution_clock::now(); - - auto add_time = chrono::duration_cast(stop_adding - start_adding); - auto loop_time = chrono::duration_cast(stop_looping - stop_adding); - printf("add time: %ldus\n", add_time.count()); - printf("loop time: %ldus\n", loop_time.count()); - - return 0; -} diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp deleted file mode 100644 index d5ba51b..0000000 --- a/src/example/ecs.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include - -#include -#include -#include -#include - -using namespace crepe; -using namespace std; - -int main() { - ComponentManager mgr{}; - - // Create a few GameObjects - try { - GameObject body = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); - GameObject right_leg = mgr.new_object("rightLeg", "person", Vector2{1, 1}, 0, 1); - GameObject left_leg = mgr.new_object("leftLeg", "person", Vector2{1, 1}, 0, 1); - GameObject right_foot = mgr.new_object("rightFoot", "person", Vector2{2, 2}, 0, 1); - GameObject left_foot = mgr.new_object("leftFoot", "person", Vector2{2, 2}, 0, 1); - - // Set the parent of each GameObject - right_foot.set_parent(right_leg); - left_foot.set_parent(left_leg); - right_leg.set_parent(body); - left_leg.set_parent(body); - - // Adding a second Transform component is not allowed and will invoke an exception - body.add_component(Vector2{10, 10}, 0, 1); - } catch (const exception & e) { - cerr << e.what() << endl; - } - - // Get the Metadata and Transform components of each GameObject - vector> metadata = mgr.get_components_by_type(); - vector> transform = mgr.get_components_by_type(); - - // Print the Metadata and Transform components - for (auto & m : metadata) { - cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name - << " Tag: " << m.get().tag << " Parent: " << m.get().parent << " Children: "; - for (auto & c : m.get().children) { - cout << c << " "; - } - cout << endl; - } - for (auto & t : transform) { - cout << "Id: " << t.get().game_object_id << " Position: [" << t.get().position.x - << ", " << t.get().position.y << "]" << endl; - } - - return 0; -} diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 49c8151..0849b98 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 + ECSTest.cpp ) diff --git a/src/test/ECSTest.cpp b/src/test/ECSTest.cpp new file mode 100644 index 0000000..a245879 --- /dev/null +++ b/src/test/ECSTest.cpp @@ -0,0 +1,236 @@ +#include +#include +#define protected public + +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace crepe; + +class ECSTest : public ::testing::Test { +public: + ComponentManager mgr{}; +}; + +TEST_F(ECSTest, createGameObject) { + GameObject obj = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + + vector> metadata = mgr.get_components_by_type(); + vector> transform = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 1); + EXPECT_EQ(transform.size(), 1); + + EXPECT_EQ(metadata[0].get().name, "body"); + EXPECT_EQ(metadata[0].get().tag, "person"); + EXPECT_EQ(metadata[0].get().parent, -1); + EXPECT_EQ(metadata[0].get().children.size(), 0); + + EXPECT_EQ(transform[0].get().position.x, 0); + EXPECT_EQ(transform[0].get().position.y, 0); + EXPECT_EQ(transform[0].get().rotation, 0); + EXPECT_EQ(transform[0].get().scale, 1); +} + +TEST_F(ECSTest, deleteAllGameObjects) { + GameObject obj0 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + GameObject obj1 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + + mgr.delete_all_components(); + + vector> metadata = mgr.get_components_by_type(); + vector> transform = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 0); + EXPECT_EQ(transform.size(), 0); + + GameObject obj2 = mgr.new_object("body2", "person2", Vector2{1, 0}, 5, 1); + + metadata = mgr.get_components_by_type(); + transform = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 1); + EXPECT_EQ(transform.size(), 1); + + EXPECT_EQ(metadata[0].get().game_object_id, 0); + EXPECT_EQ(metadata[0].get().name, "body2"); + EXPECT_EQ(metadata[0].get().tag, "person2"); + EXPECT_EQ(metadata[0].get().parent, -1); + EXPECT_EQ(metadata[0].get().children.size(), 0); + + EXPECT_EQ(transform[0].get().game_object_id, 0); + EXPECT_EQ(transform[0].get().position.x, 1); + EXPECT_EQ(transform[0].get().position.y, 0); + EXPECT_EQ(transform[0].get().rotation, 5); + EXPECT_EQ(transform[0].get().scale, 1); +} + +TEST_F(ECSTest, deleteGameObject) { + GameObject obj0 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + GameObject obj1 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + + mgr.delete_all_components_of_id(0); + + vector> metadata = mgr.get_components_by_type(); + vector> transform = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 1); + EXPECT_EQ(transform.size(), 1); + + EXPECT_EQ(metadata[0].get().game_object_id, 1); + EXPECT_EQ(metadata[0].get().name, "body"); + EXPECT_EQ(metadata[0].get().tag, "person"); + EXPECT_EQ(metadata[0].get().parent, -1); + EXPECT_EQ(metadata[0].get().children.size(), 0); + + EXPECT_EQ(transform[0].get().game_object_id, 1); + EXPECT_EQ(transform[0].get().position.x, 0); + EXPECT_EQ(transform[0].get().position.y, 0); + EXPECT_EQ(transform[0].get().rotation, 0); + EXPECT_EQ(transform[0].get().scale, 1); +} + +TEST_F(ECSTest, manyGameObjects) { + for(int i = 0; i < 5000; i++) { + GameObject obj = mgr.new_object("body", "person", Vector2{0, 0}, 0, i); + } + + vector> metadata = mgr.get_components_by_type(); + vector> transform = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 5000); + EXPECT_EQ(transform.size(), 5000); + for(int i = 0; i < 5000; i++) { + EXPECT_EQ(metadata[i].get().game_object_id, i); + EXPECT_EQ(metadata[i].get().name, "body"); + EXPECT_EQ(metadata[i].get().tag, "person"); + EXPECT_EQ(metadata[i].get().parent, -1); + EXPECT_EQ(metadata[i].get().children.size(), 0); + + EXPECT_EQ(transform[i].get().game_object_id, i); + EXPECT_EQ(transform[i].get().position.x, 0); + EXPECT_EQ(transform[i].get().position.y, 0); + EXPECT_EQ(transform[i].get().rotation, 0); + EXPECT_EQ(transform[i].get().scale, i); + } + + mgr.delete_components(); + + metadata = mgr.get_components_by_type(); + transform = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 0); + EXPECT_EQ(transform.size(), 5000); + + for(int i = 0; i < 10000 - 5000; i++) { + string tag = "person" + to_string(i); + GameObject obj = mgr.new_object("body", tag, Vector2{0, 0}, i, 0); + } + + metadata = mgr.get_components_by_type(); + transform = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 10000 - 5000); + EXPECT_EQ(transform.size(), 10000); +} + +TEST_F(ECSTest, getComponentsByID) { + GameObject obj0 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + GameObject obj1 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + + vector> metadata = mgr.get_components_by_id(0); + vector> transform = mgr.get_components_by_id(1); + + EXPECT_EQ(metadata.size(), 1); + EXPECT_EQ(transform.size(), 1); + + EXPECT_EQ(metadata[0].get().game_object_id, 0); + EXPECT_EQ(metadata[0].get().name, "body"); + EXPECT_EQ(metadata[0].get().tag, "person"); + EXPECT_EQ(metadata[0].get().parent, -1); + EXPECT_EQ(metadata[0].get().children.size(), 0); + + EXPECT_EQ(transform[0].get().game_object_id, 1); + EXPECT_EQ(transform[0].get().position.x, 0); + EXPECT_EQ(transform[0].get().position.y, 0); + EXPECT_EQ(transform[0].get().rotation, 0); + EXPECT_EQ(transform[0].get().scale, 1); +} + +TEST_F(ECSTest, tooMuchComponents) { + try { + GameObject obj0 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + obj0.add_component(Vector2{10, 10}, 0, 1); + } catch (const exception & e) { + EXPECT_EQ(e.what(), string("Exceeded maximum number of instances for this component type")); + } + + try { + GameObject obj1 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + obj1.add_component("body", "person"); + } catch (const exception & e) { + EXPECT_EQ(e.what(), string("Exceeded maximum number of instances for this component type")); + } + + vector> metadata = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 2); + EXPECT_EQ(metadata[0].get().name, "body"); + EXPECT_EQ(metadata[1].get().name, "body"); +} + +TEST_F(ECSTest, partentChild) { + { + GameObject body = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + GameObject right_leg = mgr.new_object("rightLeg", "person", Vector2{1, 1}, 0, 1); + GameObject left_leg = mgr.new_object("leftLeg", "person", Vector2{1, 1}, 0, 1); + GameObject right_foot = mgr.new_object("rightFoot", "person", Vector2{2, 2}, 0, 1); + GameObject left_foot = mgr.new_object("leftFoot", "person", Vector2{2, 2}, 0, 1); + + // Set the parent of each GameObject + right_foot.set_parent(right_leg); + left_foot.set_parent(left_leg); + right_leg.set_parent(body); + left_leg.set_parent(body); + } + + // Get the Metadata and Transform components of each GameObject + vector> metadata = mgr.get_components_by_type(); + + // Check IDs + EXPECT_EQ(metadata[0].get().game_object_id, 0); + EXPECT_EQ(metadata[1].get().game_object_id, 1); + EXPECT_EQ(metadata[2].get().game_object_id, 2); + EXPECT_EQ(metadata[3].get().game_object_id, 3); + EXPECT_EQ(metadata[4].get().game_object_id, 4); + + // Check the parent-child relationships + EXPECT_EQ(metadata[0].get().name, "body"); + EXPECT_EQ(metadata[1].get().name, "rightLeg"); + EXPECT_EQ(metadata[2].get().name, "leftLeg"); + EXPECT_EQ(metadata[3].get().name, "rightFoot"); + EXPECT_EQ(metadata[4].get().name, "leftFoot"); + + EXPECT_EQ(metadata[0].get().parent, -1); + EXPECT_EQ(metadata[1].get().parent, 0); + EXPECT_EQ(metadata[2].get().parent, 0); + EXPECT_EQ(metadata[3].get().parent, 1); + EXPECT_EQ(metadata[4].get().parent, 2); + + EXPECT_EQ(metadata[0].get().children.size(), 2); + EXPECT_EQ(metadata[1].get().children.size(), 1); + EXPECT_EQ(metadata[2].get().children.size(), 1); + EXPECT_EQ(metadata[3].get().children.size(), 0); + EXPECT_EQ(metadata[4].get().children.size(), 0); + + EXPECT_EQ(metadata[0].get().children[0], 1); + EXPECT_EQ(metadata[0].get().children[1], 2); + EXPECT_EQ(metadata[1].get().children[0], 3); + EXPECT_EQ(metadata[2].get().children[0], 4); +} -- cgit v1.2.3 From e714303c486fb81851116710ba5d68e1b469df02 Mon Sep 17 00:00:00 2001 From: max-001 Date: Wed, 20 Nov 2024 10:59:34 +0100 Subject: Added SceneManager tests --- src/example/CMakeLists.txt | 1 - src/example/scene_manager.cpp | 79 --------------------------- src/test/CMakeLists.txt | 1 + src/test/ECSTest.cpp | 3 -- src/test/SceneManagerTest.cpp | 120 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 83 deletions(-) delete mode 100644 src/example/scene_manager.cpp create mode 100644 src/test/SceneManagerTest.cpp diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index bbeec09..30432e5 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -25,7 +25,6 @@ add_example(physics) add_example(savemgr) add_example(proxy) add_example(db) -add_example(scene_manager) add_example(particles) add_example(gameloop) diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp deleted file mode 100644 index accec7d..0000000 --- a/src/example/scene_manager.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#include - -#include -#include -#include -#include -#include -#include - -using namespace crepe; -using namespace std; - -class ConcreteScene1 : public Scene { -public: - using Scene::Scene; - - void load_scene() { - auto & mgr = this->component_manager; - GameObject object1 = mgr.new_object("scene_1", "tag_scene_1", Vector2{0, 0}, 0, 1); - GameObject object2 = mgr.new_object("scene_1", "tag_scene_1", Vector2{1, 0}, 0, 1); - GameObject object3 = mgr.new_object("scene_1", "tag_scene_1", Vector2{2, 0}, 0, 1); - } -}; - -class ConcreteScene2 : public Scene { -public: - using Scene::Scene; - - void load_scene() { - auto & mgr = this->component_manager; - GameObject object1 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 0}, 0, 1); - GameObject object2 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 1}, 0, 1); - GameObject object3 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 2}, 0, 1); - GameObject object4 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 3}, 0, 1); - } -}; - -int main() { - ComponentManager component_mgr{}; - SceneManager scene_mgr{component_mgr}; - - // Add the scenes to the scene manager - scene_mgr.add_scene("scene1"); - scene_mgr.add_scene("scene2"); - - // There is no need to call set_next_scene() at the beginnen, because the first scene will be - // automatically set as the next scene - - // Load scene1 (the first scene added) - scene_mgr.load_next_scene(); - - // Get the Metadata components of each GameObject of Scene1 - vector> metadata - = component_mgr.get_components_by_type(); - - cout << "Metadata components of Scene1:" << endl; - // Print the Metadata - for (auto & m : metadata) { - cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name - << " Tag: " << m.get().tag << endl; - } - - // Set scene2 as the next scene - scene_mgr.set_next_scene("scene2"); - // Load scene2 - scene_mgr.load_next_scene(); - - // Get the Metadata components of each GameObject of Scene2 - metadata = component_mgr.get_components_by_type(); - - cout << "Metadata components of Scene2:" << endl; - // Print the Metadata - for (auto & m : metadata) { - cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name - << " Tag: " << m.get().tag << endl; - } - - return 0; -} diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 0849b98..d56d80f 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -4,5 +4,6 @@ target_sources(test_main PUBLIC ScriptTest.cpp ParticleTest.cpp ECSTest.cpp + SceneManagerTest.cpp ) diff --git a/src/test/ECSTest.cpp b/src/test/ECSTest.cpp index a245879..eaa2dbc 100644 --- a/src/test/ECSTest.cpp +++ b/src/test/ECSTest.cpp @@ -1,5 +1,3 @@ -#include -#include #define protected public #include @@ -7,7 +5,6 @@ #include #include #include -#include #include using namespace std; diff --git a/src/test/SceneManagerTest.cpp b/src/test/SceneManagerTest.cpp new file mode 100644 index 0000000..98b2a23 --- /dev/null +++ b/src/test/SceneManagerTest.cpp @@ -0,0 +1,120 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace crepe; + +class ConcreteScene1 : public Scene { +public: + using Scene::Scene; + + void load_scene() { + auto & mgr = this->component_manager; + GameObject object1 = mgr.new_object("scene_1", "tag_scene_1", Vector2{0, 0}, 0, 1); + GameObject object2 = mgr.new_object("scene_1", "tag_scene_1", Vector2{1, 0}, 0, 1); + GameObject object3 = mgr.new_object("scene_1", "tag_scene_1", Vector2{2, 0}, 0, 1); + } +}; + +class ConcreteScene2 : public Scene { +public: + using Scene::Scene; + + void load_scene() { + auto & mgr = this->component_manager; + GameObject object1 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 0}, 0, 1); + GameObject object2 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 1}, 0, 1); + GameObject object3 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 2}, 0, 1); + GameObject object4 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 3}, 0, 1); + } +}; + +class SceneManagerTest : public ::testing::Test { +public: + ComponentManager component_mgr{}; + SceneManager scene_mgr{component_mgr}; +}; + +TEST_F(SceneManagerTest, loadScene) { + scene_mgr.add_scene("scene1"); + scene_mgr.add_scene("scene2"); + + scene_mgr.load_next_scene(); + + vector> metadata = component_mgr.get_components_by_type(); + vector> transform = component_mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 3); + EXPECT_EQ(transform.size(), 3); + + EXPECT_EQ(metadata[0].get().game_object_id, 0); + EXPECT_EQ(metadata[0].get().name, "scene_1"); + EXPECT_EQ(metadata[0].get().tag, "tag_scene_1"); + EXPECT_EQ(metadata[0].get().parent, -1); + EXPECT_EQ(metadata[0].get().children.size(), 0); + EXPECT_EQ(transform[0].get().position.x, 0); + EXPECT_EQ(transform[0].get().position.y, 0); + + EXPECT_EQ(metadata[1].get().game_object_id, 1); + EXPECT_EQ(metadata[1].get().name, "scene_1"); + EXPECT_EQ(metadata[1].get().tag, "tag_scene_1"); + EXPECT_EQ(metadata[1].get().parent, -1); + EXPECT_EQ(metadata[1].get().children.size(), 0); + EXPECT_EQ(transform[1].get().position.x, 1); + EXPECT_EQ(transform[1].get().position.y, 0); + + EXPECT_EQ(metadata[2].get().game_object_id, 2); + EXPECT_EQ(metadata[2].get().name, "scene_1"); + EXPECT_EQ(metadata[2].get().tag, "tag_scene_1"); + EXPECT_EQ(metadata[2].get().parent, -1); + EXPECT_EQ(metadata[2].get().children.size(), 0); + EXPECT_EQ(transform[2].get().position.x, 2); + EXPECT_EQ(transform[2].get().position.y, 0); + + scene_mgr.set_next_scene("scene2"); + scene_mgr.load_next_scene(); + + metadata = component_mgr.get_components_by_type(); + transform = component_mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 4); + EXPECT_EQ(transform.size(), 4); + + EXPECT_EQ(metadata[0].get().game_object_id, 0); + EXPECT_EQ(metadata[0].get().name, "scene_2"); + EXPECT_EQ(metadata[0].get().tag, "tag_scene_2"); + EXPECT_EQ(metadata[0].get().parent, -1); + EXPECT_EQ(metadata[0].get().children.size(), 0); + EXPECT_EQ(transform[0].get().position.x, 0); + EXPECT_EQ(transform[0].get().position.y, 0); + + EXPECT_EQ(metadata[1].get().game_object_id, 1); + EXPECT_EQ(metadata[1].get().name, "scene_2"); + EXPECT_EQ(metadata[1].get().tag, "tag_scene_2"); + EXPECT_EQ(metadata[1].get().parent, -1); + EXPECT_EQ(metadata[1].get().children.size(), 0); + EXPECT_EQ(transform[1].get().position.x, 0); + EXPECT_EQ(transform[1].get().position.y, 1); + + EXPECT_EQ(metadata[2].get().game_object_id, 2); + EXPECT_EQ(metadata[2].get().name, "scene_2"); + EXPECT_EQ(metadata[2].get().tag, "tag_scene_2"); + EXPECT_EQ(metadata[2].get().parent, -1); + EXPECT_EQ(metadata[2].get().children.size(), 0); + EXPECT_EQ(transform[2].get().position.x, 0); + EXPECT_EQ(transform[2].get().position.y, 2); + + EXPECT_EQ(metadata[3].get().game_object_id, 3); + EXPECT_EQ(metadata[3].get().name, "scene_2"); + EXPECT_EQ(metadata[3].get().tag, "tag_scene_2"); + EXPECT_EQ(metadata[3].get().parent, -1); + EXPECT_EQ(metadata[3].get().children.size(), 0); + EXPECT_EQ(transform[3].get().position.x, 0); + EXPECT_EQ(transform[3].get().position.y, 3); +} -- 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(-) 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(-) 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 d4dd3ceab2b95c26f3b07092a5c80f47eab18110 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 20 Nov 2024 13:02:08 +0100 Subject: split format/lint --- makefile | 3 ++- src/makefile | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/makefile b/makefile index dd7c587..c46e8a5 100644 --- a/makefile +++ b/makefile @@ -6,5 +6,6 @@ doxygen: Doxyfile FORCE FMT += $(shell git ls-files '*.c' '*.cpp' '*.h' '*.hpp') format: FORCE clang-format -i $(FMT) - $(MAKE) -C src $@ +lint: FORCE + $(MAKE) -C src $@ diff --git a/src/makefile b/src/makefile index 5f80204..a0e8f02 100644 --- a/src/makefile +++ b/src/makefile @@ -1,6 +1,9 @@ .PHONY: FORCE -FMT := $(shell git ls-files '*.c' '*.cpp' '*.h' '*.hpp') format: FORCE - clang-tidy -p build/compile_commands.json --fix-errors $(FMT) + $(MAKE) -C .. $@ + +LINT := $(shell git ls-files '*.c' '*.cpp' '*.h' '*.hpp') +lint: FORCE + clang-tidy -p build/compile_commands.json --fix-errors $(LINT) -- cgit v1.2.3 From 158022890cb9abd308a6a445588cc2fca8d84e67 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 20 Nov 2024 13:25:44 +0100 Subject: implementing feedback --- src/crepe/facade/SDLContext.cpp | 3 ++- src/crepe/facade/SDLContext.h | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 8131df2..eacb10a 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -149,7 +149,7 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const &dstrect, transform.rotation, NULL, render_flip); } -void SDLContext::camera(const Camera & cam) { +void SDLContext::set_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) - (this->viewport.w / 2); @@ -167,6 +167,7 @@ SDLContext::texture_from_path(const std::string & path) { SDL_Surface * tmp = IMG_Load(path.c_str()); if (tmp == nullptr) { tmp = IMG_Load("../asset/texture/ERROR.png"); + if (tmp == nullptr) throw runtime_error("cannot load image"); } std::unique_ptr> img_surface; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index fb09015..287ad5d 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -125,6 +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 Sprite & sprite, const Vector2 & pos, const double & angle, const double & scale, const Camera & camera); //! Clears the screen, preparing for a new frame. @@ -134,10 +135,10 @@ private: void present_screen(); /** - * \brief Sets the current camera for rendering. + * \brief sets the background of the camera (will be adjusted in future PR) * \param camera Reference to the Camera object. */ - void camera(const Camera & camera); + void set_camera(const Camera & camera); private: /** -- cgit v1.2.3 From 26ddec125cd93e9613331872f8e22c79c6f3a720 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 20 Nov 2024 13:30:33 +0100 Subject: update contributing.md --- contributing.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/contributing.md b/contributing.md index 5b0c79d..9c95851 100644 --- a/contributing.md +++ b/contributing.md @@ -20,7 +20,7 @@ that you can click on to open them. # Code style - Formatting nitty-gritty is handled by clang-format/clang-tidy (run `make - format` in the root folder of this repository to format all sources files) + format` or `make lint`) -
ASCII only
GoodBad
@@ -798,6 +798,27 @@ that you can click on to open them. resolving merge conflicts when multiple sources were added by different people to the same CMakeLists.txt easier. +## GoogleTest-specific + +- Unit tests are not *required* to follow all code standards +-
+ Private/protected members may be accessed using preprocessor tricks + + + ```cpp + // include unrelated headers before + + #define private public + #define protected public + + // headers included after *will* be affected + ``` +
+- Each test source file defines tests within a single test suite (first + parameter of `TEST()` / `TEST_F()` macro) +- Test source files match their suite name (or test fixture name in the case of + tests that use a fixture) + # Structure - Files are placed in the appropriate directory: -- cgit v1.2.3 From c6d7623cc0bc17aa171bc5078f2f36c15be28683 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 20 Nov 2024 13:39:44 +0100 Subject: implemented feedback from @lonkaars --- src/crepe/api/Color.cpp | 37 ++------- src/crepe/api/Color.h | 49 ++++------- src/crepe/system/RenderSystem.h | 6 ++ src/test/CMakeLists.txt | 2 +- src/test/RenderSystemTest.cpp | 174 ++++++++++++++++++++++++++++++++++++++++ src/test/rendering.cpp | 174 ---------------------------------------- 6 files changed, 203 insertions(+), 239 deletions(-) create mode 100644 src/test/RenderSystemTest.cpp delete mode 100644 src/test/rendering.cpp diff --git a/src/crepe/api/Color.cpp b/src/crepe/api/Color.cpp index 9e5f187..29bd77a 100644 --- a/src/crepe/api/Color.cpp +++ b/src/crepe/api/Color.cpp @@ -2,32 +2,11 @@ using namespace crepe; -Color Color::white = Color(255, 255, 255, 0); -Color Color::red = Color(255, 0, 0, 0); -Color Color::green = Color(0, 255, 0, 0); -Color Color::blue = Color(0, 0, 255, 0); -Color Color::black = Color(0, 0, 0, 0); -Color Color::cyan = Color(0, 255, 255, 0); -Color Color::yellow = Color(255, 255, 0, 0); -Color Color::magenta = Color(255, 0, 255, 0); - -Color::Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) { - this->a = alpha; - this->r = red; - this->g = green; - this->b = blue; -}; - -const Color & Color::get_white() { return Color::white; }; - -const Color & Color::get_red() { return Color::red; }; -const Color & Color::get_green() { return Color::green; }; -const Color & Color::get_blue() { return Color::blue; }; - -const Color & Color::get_black() { return Color::black; }; - -const Color & Color::get_cyan() { return Color::cyan; }; - -const Color & Color::get_yellow() { return Color::yellow; }; - -const Color & Color::get_magenta() { return Color::magenta; }; +const Color Color::WHITE{0xff, 0xff, 0xff}; +const Color Color::RED{0xff, 0x00, 0x00}; +const Color Color::GREEN{0x00, 0xff, 0x00}; +const Color Color::BLUE{0x00, 0x00, 0xff}; +const Color Color::BLACK{0x00, 0x00, 0x00}; +const Color Color::CYAN{0x00, 0xff, 0xff}; +const Color Color::YELLOW{0xff, 0xff, 0x00}; +const Color Color::MAGENTA{0xff, 0x00, 0xff}; diff --git a/src/crepe/api/Color.h b/src/crepe/api/Color.h index c207ba7..84edb5c 100644 --- a/src/crepe/api/Color.h +++ b/src/crepe/api/Color.h @@ -4,41 +4,20 @@ namespace crepe { -// TODO: make Color a struct w/o constructors/destructors -class Color { - - // FIXME: can't these colors be defined as a `static constexpr const Color` - // instead? - -public: - Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha); - static const Color & get_white(); - static const Color & get_red(); - static const Color & get_green(); - static const Color & get_blue(); - static const Color & get_cyan(); - static const Color & get_magenta(); - static const Color & get_yellow(); - static const Color & get_black(); - -public: - uint8_t r; - uint8_t g; - uint8_t b; - uint8_t a; - -private: - static Color white; - static Color red; - static Color green; - static Color blue; - static Color cyan; - static Color magenta; - static Color yellow; - static Color black; - -private: - friend class SDLContext; +struct Color { + uint8_t r = 0x00; + uint8_t g = 0x00; + uint8_t b = 0x00; + uint8_t a = 0xff; + + static const Color WHITE; + static const Color RED; + static const Color GREEN; + static const Color BLUE; + static const Color CYAN; + static const Color MAGENTA; + static const Color YELLOW; + static const Color BLACK; }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 8914b96..57b9c73 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -41,6 +41,12 @@ private: //! Renders all active sprites to the screen. void render_sprites(); + /** + * \brief sort a vector sprite objects with + * + * \param objs the vector that will do a sorting algorithm on + * \return returns a sorted reference vector + */ std::vector> sort(std::vector> & objs); diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 0969a52..1c93220 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -3,6 +3,6 @@ target_sources(test_main PUBLIC PhysicsTest.cpp ScriptTest.cpp ParticleTest.cpp - rendering.cpp + RenderSystemTest.cpp ) diff --git a/src/test/RenderSystemTest.cpp b/src/test/RenderSystemTest.cpp new file mode 100644 index 0000000..ac479d3 --- /dev/null +++ b/src/test/RenderSystemTest.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::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::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::WHITE); + auto & sprite = this->mgr.get_components_by_id(entity1.id).front().get(); + ASSERT_NE(sprite.sprite_image.get(), nullptr); + + sprite.color = Color::GREEN; + EXPECT_EQ(sprite.color.r, Color::GREEN.r); + EXPECT_EQ(sprite.color.g, Color::GREEN.g); + EXPECT_EQ(sprite.color.b, Color::GREEN.b); + EXPECT_EQ(sprite.color.a, Color::GREEN.a); + this->sys.update(); + EXPECT_EQ(sprite.color.r, Color::GREEN.r); + EXPECT_EQ(sprite.color.g, Color::GREEN.g); + EXPECT_EQ(sprite.color.b, Color::GREEN.b); + EXPECT_EQ(sprite.color.a, Color::GREEN.a); +} diff --git a/src/test/rendering.cpp b/src/test/rendering.cpp deleted file mode 100644 index 4c5fb1d..0000000 --- a/src/test/rendering.cpp +++ /dev/null @@ -1,174 +0,0 @@ -#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 e25547c26c8d852ba7cc9fa3981aa75daf0f2287 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 20 Nov 2024 13:57:23 +0100 Subject: show private members in doxygen --- Doxyfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Doxyfile b/Doxyfile index 9328b24..e0a31df 100644 --- a/Doxyfile +++ b/Doxyfile @@ -24,6 +24,7 @@ USE_MDFILE_AS_MAINPAGE = ./readme.md REPEAT_BRIEF = NO INTERNAL_DOCS = YES +EXTRACT_PRIVATE = YES EXTRACT_STATIC = YES HIDE_UNDOC_NAMESPACES = YES HIDE_UNDOC_CLASSES = YES -- cgit v1.2.3 From 22741a7adc6ff5ce53324d6ef8ef36acf666a46c Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 20 Nov 2024 13:57:28 +0100 Subject: `make format` --- mwe/events/include/event.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index e1b220b..ee1bf52 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) -- 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(-) 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(-) 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 fb35ca64eee9afdd72f2cf8d279c4e745444baf9 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 20 Nov 2024 14:51:26 +0100 Subject: `make format` --- src/test/ECSTest.cpp | 15 +++++++++------ src/test/SceneManagerTest.cpp | 10 ++++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/test/ECSTest.cpp b/src/test/ECSTest.cpp index eaa2dbc..d5a5826 100644 --- a/src/test/ECSTest.cpp +++ b/src/test/ECSTest.cpp @@ -1,3 +1,5 @@ +#include + #define protected public #include @@ -5,7 +7,6 @@ #include #include #include -#include using namespace std; using namespace crepe; @@ -94,7 +95,7 @@ TEST_F(ECSTest, deleteGameObject) { } TEST_F(ECSTest, manyGameObjects) { - for(int i = 0; i < 5000; i++) { + for (int i = 0; i < 5000; i++) { GameObject obj = mgr.new_object("body", "person", Vector2{0, 0}, 0, i); } @@ -103,7 +104,7 @@ TEST_F(ECSTest, manyGameObjects) { EXPECT_EQ(metadata.size(), 5000); EXPECT_EQ(transform.size(), 5000); - for(int i = 0; i < 5000; i++) { + for (int i = 0; i < 5000; i++) { EXPECT_EQ(metadata[i].get().game_object_id, i); EXPECT_EQ(metadata[i].get().name, "body"); EXPECT_EQ(metadata[i].get().tag, "person"); @@ -125,7 +126,7 @@ TEST_F(ECSTest, manyGameObjects) { EXPECT_EQ(metadata.size(), 0); EXPECT_EQ(transform.size(), 5000); - for(int i = 0; i < 10000 - 5000; i++) { + for (int i = 0; i < 10000 - 5000; i++) { string tag = "person" + to_string(i); GameObject obj = mgr.new_object("body", tag, Vector2{0, 0}, i, 0); } @@ -165,14 +166,16 @@ TEST_F(ECSTest, tooMuchComponents) { GameObject obj0 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); obj0.add_component(Vector2{10, 10}, 0, 1); } catch (const exception & e) { - EXPECT_EQ(e.what(), string("Exceeded maximum number of instances for this component type")); + EXPECT_EQ(e.what(), + string("Exceeded maximum number of instances for this component type")); } try { GameObject obj1 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); obj1.add_component("body", "person"); } catch (const exception & e) { - EXPECT_EQ(e.what(), string("Exceeded maximum number of instances for this component type")); + EXPECT_EQ(e.what(), + string("Exceeded maximum number of instances for this component type")); } vector> metadata = mgr.get_components_by_type(); diff --git a/src/test/SceneManagerTest.cpp b/src/test/SceneManagerTest.cpp index 98b2a23..69e1171 100644 --- a/src/test/SceneManagerTest.cpp +++ b/src/test/SceneManagerTest.cpp @@ -1,10 +1,10 @@ #include #include #include -#include -#include #include #include +#include +#include #include using namespace std; @@ -47,8 +47,10 @@ TEST_F(SceneManagerTest, loadScene) { scene_mgr.load_next_scene(); - vector> metadata = component_mgr.get_components_by_type(); - vector> transform = component_mgr.get_components_by_type(); + vector> metadata + = component_mgr.get_components_by_type(); + vector> transform + = component_mgr.get_components_by_type(); EXPECT_EQ(metadata.size(), 3); EXPECT_EQ(transform.size(), 3); -- 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(-) 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(-) 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 d9a51069366650051e644453f5d3ac90f2ab29b5 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 20 Nov 2024 16:29:48 +0100 Subject: turn examples into unit tests --- src/crepe/util/Proxy.h | 2 ++ src/crepe/util/Proxy.hpp | 6 +++++ src/example/CMakeLists.txt | 3 --- src/example/log.cpp | 28 ------------------- src/example/proxy.cpp | 45 ------------------------------- src/example/script.cpp | 49 --------------------------------- src/test/CMakeLists.txt | 1 + src/test/ValueBrokerTest.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 73 insertions(+), 125 deletions(-) delete mode 100644 src/example/log.cpp delete mode 100644 src/example/proxy.cpp delete mode 100644 src/example/script.cpp create mode 100644 src/test/ValueBrokerTest.cpp diff --git a/src/crepe/util/Proxy.h b/src/crepe/util/Proxy.h index b34f7c6..789144a 100644 --- a/src/crepe/util/Proxy.h +++ b/src/crepe/util/Proxy.h @@ -15,6 +15,8 @@ namespace crepe { template class Proxy { public: + //! Set operator + Proxy & operator=(Proxy &); //! Set operator Proxy & operator=(const T &); //! Get operator diff --git a/src/crepe/util/Proxy.hpp b/src/crepe/util/Proxy.hpp index b9923db..ef2b69f 100644 --- a/src/crepe/util/Proxy.hpp +++ b/src/crepe/util/Proxy.hpp @@ -13,6 +13,12 @@ Proxy & Proxy::operator=(const T & val) { return *this; } +template +Proxy & Proxy::operator=(Proxy & proxy) { + this->broker.set(T(proxy)); + return *this; +} + template Proxy::operator const T &() { return this->broker.get(); diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 30432e5..f21bd24 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -17,13 +17,10 @@ function(add_example target_name) endfunction() add_example(audio_internal) -add_example(script) -add_example(log) add_example(rendering) add_example(asset_manager) add_example(physics) add_example(savemgr) -add_example(proxy) add_example(db) add_example(particles) add_example(gameloop) diff --git a/src/example/log.cpp b/src/example/log.cpp deleted file mode 100644 index 5baa021..0000000 --- a/src/example/log.cpp +++ /dev/null @@ -1,28 +0,0 @@ -/** \file - * - * Standalone example for usage of the logging functions - */ - -#include -#include - -using namespace crepe; - -// unrelated setup code -int _ = []() { - // make sure all log messages get printed - auto & cfg = Config::get_instance(); - cfg.log.level = Log::Level::TRACE; - - return 0; // satisfy compiler -}(); - -int main() { - dbg_trace(); - dbg_log("debug message"); - Log::logf("info message with variable: {}", 3); - Log::logf(Log::Level::WARNING, "warning"); - Log::logf(Log::Level::ERROR, "error"); - - return 0; -} diff --git a/src/example/proxy.cpp b/src/example/proxy.cpp deleted file mode 100644 index 69451f8..0000000 --- a/src/example/proxy.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/** \file - * - * Standalone example for usage of the proxy type - */ - -#include -#include -#include -#include - -using namespace std; -using namespace crepe; - -void test_ro_ref(const int & val) {} -void test_rw_ref(int & val) {} -void test_ro_val(int val) {} - -int main() { - auto & cfg = Config::get_instance(); - cfg.log.level = Log::Level::DEBUG; - - int real_value = 0; - - ValueBroker broker{ - [&real_value](const int & target) { - dbg_logf("set {} to {}", real_value, target); - real_value = target; - }, - [&real_value]() -> const int & { - dbg_logf("get {}", real_value); - return real_value; - }, - }; - - Proxy proxy{broker}; - - broker.set(54); - proxy = 84; - - test_ro_ref(proxy); // this is allowed - // test_rw_ref(proxy); // this should throw a compile error - test_ro_val(proxy); - - return 0; -} diff --git a/src/example/script.cpp b/src/example/script.cpp deleted file mode 100644 index a23295b..0000000 --- a/src/example/script.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/** \file - * - * Standalone example for usage of the script component and system - */ - -#include -#include -#include - -#include -#include -#include -#include -#include - -using namespace crepe; -using namespace std; - -// Unrelated stuff that is not part of this POC -int _ = []() { - // Show dbg_trace() output - auto & cfg = Config::get_instance(); - cfg.log.level = Log::Level::TRACE; - - return 0; // satisfy compiler -}(); - -// User-defined script: -class MyScript : public Script { - void update() { - // Retrieve component from the same GameObject this script is on - Transform & test = get_component(); - dbg_logf("Transform({:.2f}, {:.2f})", test.position.x, test.position.y); - } -}; - -int main() { - ComponentManager component_manager{}; - ScriptSystem system{component_manager}; - - // Create game object with Transform and BehaviorScript components - GameObject obj = component_manager.new_object("name"); - obj.add_component().set_script(); - - // Update all scripts. This should result in MyScript::update being called - system.update(); - - return EXIT_SUCCESS; -} diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index d56d80f..c1f935d 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -5,5 +5,6 @@ target_sources(test_main PUBLIC ParticleTest.cpp ECSTest.cpp SceneManagerTest.cpp + ValueBrokerTest.cpp ) diff --git a/src/test/ValueBrokerTest.cpp b/src/test/ValueBrokerTest.cpp new file mode 100644 index 0000000..10a4654 --- /dev/null +++ b/src/test/ValueBrokerTest.cpp @@ -0,0 +1,64 @@ +#include + +#include +#include + +using namespace std; +using namespace crepe; +using namespace testing; + +class ValueBrokerTest : public Test { +public: + int read_count = 0; + int write_count = 0; + int value = 0; + + ValueBroker broker { + [this](const int & target) -> void { + this->write_count++; + this->value = target; + }, + [this]() -> const int & { + this->read_count++; + return this->value; + }, + }; + Proxy proxy{broker}; + + void SetUp() override { + ASSERT_EQ(read_count, 0); + ASSERT_EQ(write_count, 0); + } +}; + +TEST_F(ValueBrokerTest, BrokerWrite) { + broker.set(0); + EXPECT_EQ(read_count, 0); + EXPECT_EQ(write_count, 1); +} + +TEST_F(ValueBrokerTest, BrokerRead) { + broker.get(); + EXPECT_EQ(read_count, 1); + EXPECT_EQ(write_count, 0); +} + +TEST_F(ValueBrokerTest, ProxyWrite) { + proxy = 0; + EXPECT_EQ(read_count, 0); + EXPECT_EQ(write_count, 1); +} + +void dummy(int) { } +TEST_F(ValueBrokerTest, ProxyRead) { + dummy(proxy); + EXPECT_EQ(read_count, 1); + EXPECT_EQ(write_count, 0); +} + +TEST_F(ValueBrokerTest, ProxyReadWrite) { + proxy = proxy; + ASSERT_EQ(read_count, 1); + ASSERT_EQ(write_count, 1); +} + -- cgit v1.2.3 From fdf91122ddc1fa9942c8790d2c8c845f877df11d Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 20 Nov 2024 16:46:00 +0100 Subject: convert more examples to unit tests --- src/crepe/facade/DB.cpp | 4 +-- src/crepe/facade/DB.h | 4 ++- src/example/CMakeLists.txt | 5 --- src/example/audio_internal.cpp | 56 -------------------------------- src/example/db.cpp | 30 ------------------ src/example/particles.cpp | 43 ------------------------- src/example/physics.cpp | 24 -------------- src/example/rendering.cpp | 72 ------------------------------------------ src/test/CMakeLists.txt | 1 + src/test/DBTest.cpp | 29 +++++++++++++++++ 10 files changed, 35 insertions(+), 233 deletions(-) delete mode 100644 src/example/audio_internal.cpp delete mode 100644 src/example/db.cpp delete mode 100644 src/example/particles.cpp delete mode 100644 src/example/physics.cpp delete mode 100644 src/example/rendering.cpp create mode 100644 src/test/DBTest.cpp diff --git a/src/crepe/facade/DB.cpp b/src/crepe/facade/DB.cpp index d5d19dc..95cf606 100644 --- a/src/crepe/facade/DB.cpp +++ b/src/crepe/facade/DB.cpp @@ -18,8 +18,8 @@ DB::DB(const string & path) { this->db = {db, [](libdb::DB * db) { db->close(db, 0); }}; // load or create database file - ret = this->db->open(this->db.get(), NULL, path.c_str(), NULL, libdb::DB_BTREE, DB_CREATE, - 0); + const char * file = path.empty() ? NULL : path.c_str(); + ret = this->db->open(this->db.get(), NULL, file, NULL, libdb::DB_BTREE, DB_CREATE, 0); if (ret != 0) throw runtime_error(format("db->open: {}", libdb::db_strerror(ret))); // create cursor diff --git a/src/crepe/facade/DB.h b/src/crepe/facade/DB.h index 629b0eb..115c0f1 100644 --- a/src/crepe/facade/DB.h +++ b/src/crepe/facade/DB.h @@ -22,8 +22,10 @@ class DB { public: /** * \param path The path of the database (created if nonexistant) + * + * \note If \p path is empty, the database is entirely in-memory */ - DB(const std::string & path); + DB(const std::string & path = ""); virtual ~DB() = default; public: diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index f21bd24..c75a4b5 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -16,12 +16,7 @@ function(add_example target_name) add_dependencies(examples ${target_name}) endfunction() -add_example(audio_internal) -add_example(rendering) add_example(asset_manager) -add_example(physics) add_example(savemgr) -add_example(db) -add_example(particles) add_example(gameloop) diff --git a/src/example/audio_internal.cpp b/src/example/audio_internal.cpp deleted file mode 100644 index 661161a..0000000 --- a/src/example/audio_internal.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/** \file - * - * Standalone example for usage of the internal \c Sound class. - */ - -#include -#include -#include - -#include - -using namespace crepe; -using namespace std; -using namespace std::chrono_literals; -using std::make_unique; - -// Unrelated stuff that is not part of this POC -int _ = []() { - // Show dbg_trace() output - auto & cfg = Config::get_instance(); - cfg.log.level = Log::Level::TRACE; - - return 0; // satisfy compiler -}(); - -int main() { - // Load a background track (Ogg Vorbis) - auto bgm = Sound("../mwe/audio/bgm.ogg"); - // Load three short samples (WAV) - auto sfx1 = Sound("../mwe/audio/sfx1.wav"); - auto sfx2 = Sound("../mwe/audio/sfx2.wav"); - auto sfx3 = Sound("../mwe/audio/sfx3.wav"); - - // Start the background track - bgm.play(); - - // Play each sample sequentially while pausing and resuming the background track - this_thread::sleep_for(500ms); - sfx1.play(); - this_thread::sleep_for(500ms); - sfx2.play(); - bgm.pause(); - this_thread::sleep_for(500ms); - sfx3.play(); - bgm.play(); - this_thread::sleep_for(500ms); - - // Play all samples simultaniously - sfx1.play(); - sfx2.play(); - sfx3.play(); - this_thread::sleep_for(1000ms); - - // Stop all audio and exit - return EXIT_SUCCESS; -} diff --git a/src/example/db.cpp b/src/example/db.cpp deleted file mode 100644 index ee4e8fc..0000000 --- a/src/example/db.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include - -using namespace crepe; -using namespace std; - -// run before main -static auto _ = []() { - auto & cfg = Config::get_instance(); - cfg.log.level = Log::Level::TRACE; - return 0; -}(); - -int main() { - dbg_trace(); - - DB db("file.db"); - - const char * test_key = "test-key"; - string test_data = "Hello world!"; - - dbg_logf("DB has key = {}", db.has(test_key)); - - db.set(test_key, test_data); - - dbg_logf("key = \"{}\"", db.get(test_key)); - - return 0; -} diff --git a/src/example/particles.cpp b/src/example/particles.cpp deleted file mode 100644 index 3d5f676..0000000 --- a/src/example/particles.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace crepe; -using namespace std; - -int main(int argc, char * argv[]) { - ComponentManager mgr{}; - GameObject game_object = mgr.new_object("", "", Vector2{0, 0}, 0, 0); - Color color(0, 0, 0, 0); - Sprite test_sprite = game_object.add_component( - make_shared("../asset/texture/img.png"), color, FlipSettings{true, true}); - game_object.add_component(ParticleEmitter::Data{ - .position = {0, 0}, - .max_particles = 100, - .emission_rate = 0, - .min_speed = 0, - .max_speed = 0, - .min_angle = 0, - .max_angle = 0, - .begin_lifespan = 0, - .end_lifespan = 0, - .force_over_time = Vector2{0, 0}, - .boundary{ - .width = 0, - .height = 0, - .offset = Vector2{0, 0}, - .reset_on_exit = false, - }, - .sprite = test_sprite, - }); - - return 0; -} diff --git a/src/example/physics.cpp b/src/example/physics.cpp deleted file mode 100644 index ad663a0..0000000 --- a/src/example/physics.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include -#include -#include -#include -#include - -using namespace crepe; -using namespace std; - -int main(int argc, char * argv[]) { - ComponentManager mgr{}; - - GameObject game_object = mgr.new_object("Name", "Tag", Vector2{0, 0}, 0, 0); - game_object.add_component(Rigidbody::Data{ - .mass = 1, - .gravity_scale = 1, - .body_type = Rigidbody::BodyType::DYNAMIC, - .constraints = {0, 0, 0}, - .use_gravity = true, - .bounce = false, - }); - return 0; -} diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp deleted file mode 100644 index ecd3f6a..0000000 --- a/src/example/rendering.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include "api/Camera.h" -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include -#include - -using namespace std; -using namespace crepe; - -int main() { - dbg_trace(); - - ComponentManager mgr{}; - RenderSystem sys{mgr}; - - GameObject obj = mgr.new_object("name", "tag", Vector2{0, 0}, 1, 1); - GameObject obj1 = mgr.new_object("name", "tag", Vector2{500, 0}, 1, 0.1); - GameObject obj2 = mgr.new_object("name", "tag", Vector2{800, 0}, 1, 0.1); - - // Normal adding components - { - Color color(0, 0, 0, 0); - 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); - 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; - } - - /* - { - Color color(0, 0, 0, 0); - auto img = mgr.cache("../asset/texture/second.png"); - obj2.add_component(img, color, FlipSettings{true, true}); - } - */ - - sys.update(); - /* - - auto start = std::chrono::steady_clock::now(); - while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { - sys.update(); - } - */ -} diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index c1f935d..61dd2d9 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -6,5 +6,6 @@ target_sources(test_main PUBLIC ECSTest.cpp SceneManagerTest.cpp ValueBrokerTest.cpp + DBTest.cpp ) diff --git a/src/test/DBTest.cpp b/src/test/DBTest.cpp new file mode 100644 index 0000000..b57eba9 --- /dev/null +++ b/src/test/DBTest.cpp @@ -0,0 +1,29 @@ +#include +#include + +using namespace std; +using namespace crepe; +using namespace testing; + +class DBTest : public Test { +public: + DB db; +}; + +TEST_F(DBTest, ReadWrite) { + db.set("foo", "bar"); + EXPECT_EQ(db.get("foo"), "bar"); +} + +TEST_F(DBTest, Nonexistant) { + EXPECT_THROW(db.get("foo"), std::out_of_range); + db.set("foo", "bar"); + EXPECT_NO_THROW(db.get("foo")); +} + +TEST_F(DBTest, Has) { + EXPECT_EQ(db.has("foo"), false); + db.set("foo", "bar"); + EXPECT_EQ(db.has("foo"), true); +} + -- cgit v1.2.3 From 80712f111e5b703f4f91b9bd55f4dbb14cb14829 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 20 Nov 2024 19:00:36 +0100 Subject: remove copy/move constructors/operators on all components --- src/crepe/Component.h | 5 +++++ src/test/ParticleTest.cpp | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 5279fb3..6d405c7 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -27,6 +27,11 @@ protected: //! Only the ComponentManager can create components friend class ComponentManager; + Component(const Component &) = delete; + Component(Component &&) = delete; + virtual Component & operator=(const Component &) = delete; + virtual Component & operator=(Component &&) = delete; + public: virtual ~Component() = default; diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 4e655a9..d9bbba0 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -28,7 +28,7 @@ public: GameObject game_object = mgr.new_object("", "", Vector2{0, 0}, 0, 0); Color color(0, 0, 0, 0); - Sprite test_sprite = game_object.add_component( + Sprite & test_sprite = game_object.add_component( make_shared("../asset/texture/img.png"), color, FlipSettings{true, true}); -- 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(-) 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 From c23c7d03522456ac580cb7acd93fbfd95d5b846d Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 20 Nov 2024 19:27:30 +0100 Subject: fixed compile warning and tested the prerequisite that user needs to set explicity and reference --- src/crepe/facade/SDLContext.cpp | 4 ++-- src/example/rendering_particle.cpp | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index daf0050..f78ec8f 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -122,7 +122,7 @@ 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 { + const Camera & camera) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) @@ -136,7 +136,7 @@ void SDLContext::draw_particle(const Sprite & sprite, const Vector2 & pos, } void SDLContext::draw(const Sprite & sprite, const Transform & transform, - const Camera & cam) const { + const Camera & cam) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 5030bc6..4571afb 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -32,7 +32,8 @@ int main(int argc, char * argv[]) { Sprite & test_sprite = game_object.add_component( make_shared("../asset/texture/img.png"), color, FlipSettings{false, false}); test_sprite.order_in_layer = 5; - auto test = game_object.add_component(ParticleEmitter::Data{ + + auto & test = game_object.add_component(ParticleEmitter::Data{ .position = {0, 0}, .max_particles = 10, .emission_rate = 0.1, -- cgit v1.2.3 From 30492d9eee7ee47a42e2471dd0716044f59df705 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 20 Nov 2024 19:30:34 +0100 Subject: make format --- src/crepe/Component.h | 8 ++++---- src/crepe/facade/SDLContext.cpp | 3 +-- src/crepe/facade/SDLContext.h | 4 ++-- src/crepe/system/RenderSystem.h | 4 ++-- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 6d405c7..dc17721 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -27,10 +27,10 @@ protected: //! Only the ComponentManager can create components friend class ComponentManager; - Component(const Component &) = delete; - Component(Component &&) = delete; - virtual Component & operator=(const Component &) = delete; - virtual Component & operator=(Component &&) = delete; + Component(const Component &) = delete; + Component(Component &&) = delete; + virtual Component & operator=(const Component &) = delete; + virtual Component & operator=(Component &&) = delete; public: virtual ~Component() = default; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index f78ec8f..26a1bdc 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -135,8 +135,7 @@ void SDLContext::draw_particle(const Sprite & sprite, const Vector2 & pos, &dstrect, angle, NULL, render_flip); } -void SDLContext::draw(const Sprite & sprite, const Transform & transform, - const Camera & cam) { +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) diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 3b17c0b..841ffc9 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) ; + 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 double & scale, const Camera & camera); //! Clears the screen, preparing for a new frame. void clear_screen(); diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 5a2af17..d25a6e3 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -31,10 +31,10 @@ 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(); -- cgit v1.2.3 From b6aea9de9c9c8d86856bcd6c3b521e385bc00a69 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 20 Nov 2024 20:19:10 +0100 Subject: nitpick #31 --- src/crepe/facade/SDLContext.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 26a1bdc..d352ea6 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -33,21 +33,21 @@ SDLContext::SDLContext() { dbg_trace(); // FIXME: read window defaults from config manager - if (SDL_Init(SDL_INIT_VIDEO) < 0) { - throw std::runtime_error("SDL could not initialize!"); + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError())); } SDL_Window * tmp_window = SDL_CreateWindow("Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, this->viewport.w, this->viewport.h, 0); if (!tmp_window) { - throw std::runtime_error("Window could not be created!"); + throw runtime_error(format("SDLContext: SDL_Window error: {}", SDL_GetError())); } 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) { - throw std::runtime_error("Renderer could not be created!"); + throw runtime_error(format("SDLContext: SDL_CreateRenderer error: {}", SDL_GetError())); } this->game_renderer @@ -55,7 +55,7 @@ SDLContext::SDLContext() { int img_flags = IMG_INIT_PNG; if (!(IMG_Init(img_flags) & img_flags)) { - throw std::runtime_error("SDL_image could not initialize!"); + throw runtime_error("SDLContext: SDL_image could not initialize!"); } } @@ -164,10 +164,8 @@ std::unique_ptr> SDLContext::texture_from_path(const std::string & path) { SDL_Surface * tmp = IMG_Load(path.c_str()); - if (tmp == nullptr) { - tmp = IMG_Load("../asset/texture/ERROR.png"); - if (tmp == nullptr) throw runtime_error("cannot load image"); - } + if (tmp == nullptr) + throw runtime_error(format("SDLContext: IMG_Load error: {}", SDL_GetError())); std::unique_ptr> img_surface; img_surface = {tmp, [](SDL_Surface * surface) { SDL_FreeSurface(surface); }}; @@ -176,7 +174,7 @@ SDLContext::texture_from_path(const std::string & path) { = SDL_CreateTextureFromSurface(this->game_renderer.get(), img_surface.get()); if (tmp_texture == nullptr) { - throw runtime_error(format("Texture cannot be load from {}", path)); + throw runtime_error(format("SDLContext: Texture cannot be load from {}", path)); } std::unique_ptr> img_texture; -- cgit v1.2.3