From f1857fc2d4ddec71b3f0395903f8446cf96b8d0c Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 22 Oct 2024 15:37:00 +0200 Subject: fixed everything and can now work with new compiler, example rendering and made it work with component manager --- src/crepe/api/Transform.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/crepe/api/Transform.cpp (limited to 'src/crepe/api/Transform.cpp') diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp new file mode 100644 index 0000000..c83461f --- /dev/null +++ b/src/crepe/api/Transform.cpp @@ -0,0 +1,13 @@ + + +#include "Transform.h" +#include "api/Point.h" +#include "util/log.h" + +using namespace crepe::api; + +Transform::Transform(Point & point, double rot, double scale) + : position(point), rotation(rot), scale(scale) { + dbg_trace(); +} +Transform::~Transform() { dbg_trace(); } -- cgit v1.2.3 From c9246515fe785563411e9170aedd0231165ab988 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 23 Oct 2024 19:56:28 +0200 Subject: rendering and assetmanager --- src/crepe/CMakeLists.txt | 26 ++++++++++++-------------- src/crepe/PhysicsSystem.cpp | 5 +++-- src/crepe/RenderSystem.cpp | 23 ++++++++++++----------- src/crepe/SdlContext.cpp | 2 +- src/crepe/Transform.cpp | 6 ------ src/crepe/Transform.h | 22 ---------------------- src/crepe/api/Rigidbody.h | 2 +- src/crepe/api/Sprite.cpp | 6 ++++-- src/crepe/api/Sprite.h | 2 +- src/crepe/api/Transform.cpp | 6 ++++-- src/crepe/api/Transform.h | 3 ++- src/example/CMakeLists.txt | 2 -- src/example/rendering.cpp | 17 +++++++++-------- 13 files changed, 49 insertions(+), 73 deletions(-) delete mode 100644 src/crepe/Transform.cpp delete mode 100644 src/crepe/Transform.h (limited to 'src/crepe/api/Transform.cpp') diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 497423e..245df31 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -3,37 +3,35 @@ target_sources(crepe PUBLIC Asset.cpp Sound.cpp SoundContext.cpp - Particle.cpp - ParticleEmitter.cpp - ParticleSystem.cpp + #Particle.cpp + #ParticleEmitter.cpp + #ParticleSystem.cpp SdlContext.cpp ComponentManager.cpp Component.cpp ScriptSystem.cpp RenderSystem.cpp - PhysicsSystem.cpp - Transform.cpp - Force.cpp - CollisionSystem.cpp + #PhysicsSystem.cpp + #Force.cpp + #CollisionSystem.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES Asset.h Sound.h SoundContext.h - ParticleEmitter.h - ParticleSystem.h - Particle.h + #ParticleEmitter.h + #ParticleSystem.h + #Particle.h SdlContext.h ComponentManager.h ComponentManager.hpp Component.h System.h ScriptSystem.h - PhysicsSystem.h - Transform.h - Force.h - CollisionSystem.h + #PhysicsSystem.h + #Force.h + #CollisionSystem.h RenderSystem.h ) diff --git a/src/crepe/PhysicsSystem.cpp b/src/crepe/PhysicsSystem.cpp index 7d16044..3ed84f8 100644 --- a/src/crepe/PhysicsSystem.cpp +++ b/src/crepe/PhysicsSystem.cpp @@ -1,11 +1,12 @@ #include "PhysicsSystem.h" #include "ComponentManager.h" -#include "Rigidbody.h" -#include "Transform.h" +#include "api/Rigidbody.h" +#include "api/Transform.h" #include "Force.h" #include using namespace crepe; +using namespace crepe::api; PhysicsSystem::PhysicsSystem() { diff --git a/src/crepe/RenderSystem.cpp b/src/crepe/RenderSystem.cpp index 6aae3bb..b5503d3 100644 --- a/src/crepe/RenderSystem.cpp +++ b/src/crepe/RenderSystem.cpp @@ -6,7 +6,6 @@ #include "api/Sprite.h" #include "api/Transform.h" #include "util/log.h" -#include #include #include @@ -17,25 +16,27 @@ RenderSystem::RenderSystem() { dbg_trace(); } RenderSystem::~RenderSystem() { dbg_trace(); } -RenderSystem& RenderSystem::get_instance(){ +RenderSystem & RenderSystem::get_instance() { static RenderSystem instance; return instance; } void RenderSystem::update() { - ComponentManager& mgr = ComponentManager::get_instance(); - - std::vector> sprites = mgr.get_components_by_type(); - std::vector> transforms = mgr.get_components_by_type(); + ComponentManager & mgr = ComponentManager::get_instance(); - SdlContext& render = SdlContext::get_instance(); + std::vector> sprites + = mgr.get_components_by_type(); + + SdlContext & render = SdlContext::get_instance(); render.clear_screen(); - for (size_t i = 0; i < sprites.size(); ++i) { - render.draw(sprites[i].get(), transforms[i].get()); - } + for (const Sprite & sprite : sprites) { + std::vector> transforms = mgr.get_components_by_id(sprite.gameObjectId); + for (const Transform& transform : transforms) { + render.draw(sprite, transform); + } + } render.present_screen(); - } diff --git a/src/crepe/SdlContext.cpp b/src/crepe/SdlContext.cpp index cc5148c..09d9c9b 100644 --- a/src/crepe/SdlContext.cpp +++ b/src/crepe/SdlContext.cpp @@ -57,7 +57,7 @@ SdlContext::SdlContext() { m_game_window = SDL_CreateWindow( "Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - 1920, 1080, SDL_WINDOW_HIDDEN); + 1920, 1080, SDL_WINDOW_SHOWN); if (!m_game_window) { std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl; diff --git a/src/crepe/Transform.cpp b/src/crepe/Transform.cpp deleted file mode 100644 index 2c39523..0000000 --- a/src/crepe/Transform.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "Transform.h" - -using namespace crepe; - -Transform::Transform(uint32_t gameObjectId,Position position, int rotation, int scale) - : Component(gameObjectId), postion(postion), rotation(rotation), scale(scale) {} diff --git a/src/crepe/Transform.h b/src/crepe/Transform.h deleted file mode 100644 index 3e8d142..0000000 --- a/src/crepe/Transform.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include "Component.h" - -namespace crepe { - -struct Position -{ - int x; - int y; -}; - - -class Transform : public Component { -public: - Transform(uint32_t gameObjectId,Position position, int rotation, int scale); - Position postion; - int rotation; - int scale; -}; - -} // namespace crepe diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h index 548650a..c16a300 100644 --- a/src/crepe/api/Rigidbody.h +++ b/src/crepe/api/Rigidbody.h @@ -3,7 +3,7 @@ #include "Component.h" #include -namespace crepe { +namespace crepe::api{ enum class BodyType { Static, // Does not move (e.g. walls, ground ...) diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index d9e26ab..3cc263e 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -1,8 +1,10 @@ #include "Sprite.h" +#include "Component.h" #include "api/Texture.h" #include "util/log.h" +#include #include #include @@ -10,8 +12,8 @@ using namespace std; using namespace crepe; using namespace crepe::api; -Sprite::Sprite(shared_ptr image, const Color & color, - const flip_settings & flip) : color(color), flip(flip), sprite_image(image) { +Sprite::Sprite(uint32_t id, shared_ptr image, const Color & color, + const flip_settings & flip) : Component(id), color(color), flip(flip), sprite_image(image) { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 920f91e..3d9e911 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -17,7 +17,7 @@ struct flip_settings{ class Sprite : public Component { public: - Sprite(std::shared_ptr image, const Color& color, const flip_settings& flip ); + Sprite(uint32_t game_id, std::shared_ptr image, const Color& color, const flip_settings& flip ); ~Sprite(); std::shared_ptr sprite_image; Color color; diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index c83461f..4f22843 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -1,13 +1,15 @@ #include "Transform.h" +#include "Component.h" #include "api/Point.h" #include "util/log.h" +#include using namespace crepe::api; -Transform::Transform(Point & point, double rot, double scale) - : position(point), rotation(rot), scale(scale) { +Transform::Transform(uint32_t game_id, Point & point, double rot, double scale) + : Component(game_id), position(point), rotation(rot), scale(scale) { dbg_trace(); } Transform::~Transform() { dbg_trace(); } diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index a34ebb1..29e2a3d 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -2,11 +2,12 @@ #include "Component.h" #include "api/Point.h" +#include namespace crepe::api { class Transform : public Component { public: - Transform(Point&, double, double); + Transform(uint32_t id, Point&, double, double); ~Transform(); Point position; // Translation (shift) double rotation; // Rotation, in radians diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 368aa2e..4295d19 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -23,5 +23,3 @@ add_example(rendering) add_example(asset_manager) add_example(particle) add_example(Physics) -add_example(particle) -add_example(Physics) diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index 34d9f66..1d83004 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -1,7 +1,7 @@ #include -#include +#include #include #include @@ -24,8 +24,8 @@ int main() { dbg_trace(); auto obj = GameObject(0, "name", "tag", 0); - auto obj1= GameObject(0, "name", "tag", 0); - auto obj2 = GameObject(0, "name", "tag", 0); + auto obj1= GameObject(1, "name", "tag", 0); + auto obj2 = GameObject(2, "name", "tag", 0); auto& mgr = AssetManager::get_instance(); // Normal adding components @@ -40,17 +40,20 @@ int main() { make_shared("../asset/texture/img.png"), color, flip_settings{true, true}); } + + { Color color(0, 0, 0, 0); Point point = { .x = 500, .y = 0, }; - obj.add_component(point, 0, 0.1); + obj1.add_component(point, 0, 0.1); auto img = mgr.cache("../asset/texture/second.png"); - obj.add_component(img, color, + obj1.add_component(img, color, flip_settings{true, true}); } + { Color color(0, 0, 0, 0); Point point = { @@ -59,13 +62,11 @@ int main() { }; //obj.add_component(point, 0, 0.1); auto img = mgr.cache("../asset/texture/second.png"); - obj.add_component(img, color, + obj2.add_component(img, color, flip_settings{true, true}); } - - auto & sys = crepe::RenderSystem::get_instance(); auto start = std::chrono::steady_clock::now(); while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { -- cgit v1.2.3 From 6e2c5e1b57210b10f8781f103e5c46308544339f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 23 Oct 2024 22:23:36 +0200 Subject: more nitpicking --- contributing.md | 2 ++ src/crepe/Collider.h | 2 +- src/crepe/CollisionSystem.cpp | 2 -- src/crepe/CollisionSystem.h | 5 +--- src/crepe/Component.h | 1 + src/crepe/ComponentManager.h | 1 - src/crepe/ParticleSystem.cpp | 46 ++++++++++++++++++------------------ src/crepe/ParticleSystem.h | 3 +-- src/crepe/PhysicsSystem.cpp | 8 ++++--- src/crepe/PhysicsSystem.h | 5 +--- src/crepe/RenderSystem.cpp | 12 +++++----- src/crepe/RenderSystem.h | 1 - src/crepe/SDLApp.cpp | 10 ++++---- src/crepe/SDLApp.h | 11 ++++----- src/crepe/ScriptSystem.cpp | 5 ++-- src/crepe/SdlContext.cpp | 49 ++++++++++++++++++++------------------- src/crepe/SdlContext.h | 12 ++++++---- src/crepe/Sound.h | 3 +-- src/crepe/api/AssetManager.cpp | 3 +-- src/crepe/api/AssetManager.h | 1 + src/crepe/api/AudioSource.cpp | 5 ++-- src/crepe/api/BehaviorScript.hpp | 1 + src/crepe/api/CircleCollider.h | 4 ++-- src/crepe/api/Color.cpp | 2 -- src/crepe/api/Force.cpp | 11 +++++---- src/crepe/api/Force.h | 6 ++--- src/crepe/api/ParticleEmitter.cpp | 35 ++++++++++++++-------------- src/crepe/api/ParticleEmitter.h | 44 +++++++++++++++++++++-------------- src/crepe/api/Rigidbody.cpp | 4 ++-- src/crepe/api/Rigidbody.h | 16 ++++++++----- src/crepe/api/Sprite.cpp | 11 ++++----- src/crepe/api/Sprite.h | 9 ++++--- src/crepe/api/Texture.cpp | 19 ++++++++------- src/crepe/api/Texture.h | 5 ++-- src/crepe/api/Transform.cpp | 8 +++---- src/crepe/api/Transform.h | 9 +++++-- src/example/particle.cpp | 2 +- 37 files changed, 193 insertions(+), 180 deletions(-) (limited to 'src/crepe/api/Transform.cpp') diff --git a/contributing.md b/contributing.md index 59adefb..a6e5074 100644 --- a/contributing.md +++ b/contributing.md @@ -71,6 +71,8 @@ possible (i.e. if you only need a reference or pointer type). - Template functions are only declared in a `.h` header, and defined in a matching `.hpp` header. +- Where possible, end (initializer) lists with a trailing comma (e.g. with + structs, enums) ## CMakeLists specific diff --git a/src/crepe/Collider.h b/src/crepe/Collider.h index cfc044c..68a7d1d 100644 --- a/src/crepe/Collider.h +++ b/src/crepe/Collider.h @@ -6,7 +6,7 @@ namespace crepe { class Collider : public Component { public: - Collider(uint32_t gameObjectId); + Collider(uint32_t game_object_id); int size; }; diff --git a/src/crepe/CollisionSystem.cpp b/src/crepe/CollisionSystem.cpp index 77110a2..55e0fdc 100644 --- a/src/crepe/CollisionSystem.cpp +++ b/src/crepe/CollisionSystem.cpp @@ -1,6 +1,4 @@ #include "CollisionSystem.h" -#include "ComponentManager.h" -#include using namespace crepe; diff --git a/src/crepe/CollisionSystem.h b/src/crepe/CollisionSystem.h index 4a222eb..1e9f1aa 100644 --- a/src/crepe/CollisionSystem.h +++ b/src/crepe/CollisionSystem.h @@ -3,11 +3,8 @@ namespace crepe { class CollisionSystem { -private: - /* data */ - public: - CollisionSystem(/* args */); + CollisionSystem(); void update(); }; diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 9e10c42..bc44865 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -1,4 +1,5 @@ #pragma once + #include namespace crepe { diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 38f32e4..2b5e1df 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -4,7 +4,6 @@ #include #include #include -#include #include #include "Component.h" diff --git a/src/crepe/ParticleSystem.cpp b/src/crepe/ParticleSystem.cpp index f0220a1..af6c550 100644 --- a/src/crepe/ParticleSystem.cpp +++ b/src/crepe/ParticleSystem.cpp @@ -1,14 +1,14 @@ -#include "ParticleSystem.h" -#include "ComponentManager.h" -#include "Particle.h" -#include "api/ParticleEmitter.h" #include #include +#include "api/ParticleEmitter.h" + +#include "ComponentManager.h" +#include "ParticleSystem.h" + using namespace crepe; -ParticleSystem::ParticleSystem() - : m_elapsed_time(0.0f) {} // Initialize m_elapsedTime to 0 +ParticleSystem::ParticleSystem() : elapsed_time(0.0f) {} void ParticleSystem::update() { ComponentManager & mgr = ComponentManager::get_instance(); @@ -16,7 +16,7 @@ void ParticleSystem::update() { = mgr.get_components_by_type(); float delta_time = 0.10; for (ParticleEmitter & emitter : emitters) { - float update_amount = 1 / static_cast(emitter.m_emission_rate); + float update_amount = 1 / static_cast(emitter.emission_rate); for (float i = 0; i < delta_time; i += update_amount) { emit_particle(emitter); } @@ -29,32 +29,32 @@ void ParticleSystem::update() { } void ParticleSystem::emit_particle(ParticleEmitter & emitter) { - Position initial_position = {emitter.m_position.x, emitter.m_position.y}; + Position initial_position = {emitter.position.x, emitter.position.y}; float random_angle = 0.0f; - if (emitter.m_max_angle < emitter.m_min_angle) { - random_angle = ((emitter.m_min_angle - + (std::rand() - % (static_cast(emitter.m_max_angle + 360 - - emitter.m_min_angle + 1)))) - % 360); + if (emitter.max_angle < emitter.min_angle) { + random_angle = ((emitter.min_angle + + (std::rand() + % (static_cast(emitter.max_angle + 360 + - emitter.min_angle + 1)))) + % 360); } else { - random_angle = emitter.m_min_angle - + (std::rand() - % (static_cast(emitter.m_max_angle - - emitter.m_min_angle + 1))); + random_angle = emitter.min_angle + + (std::rand() + % (static_cast(emitter.max_angle + - emitter.min_angle + 1))); } float angle_in_radians = random_angle * (M_PI / 180.0f); float random_speed_offset = (static_cast(std::rand()) / RAND_MAX) - * (2 * emitter.m_speed_offset) - - emitter.m_speed_offset; + * (2 * emitter.speed_offset) + - emitter.speed_offset; float velocity_x - = (emitter.m_speed + random_speed_offset) * std::cos(angle_in_radians); + = (emitter.speed + random_speed_offset) * std::cos(angle_in_radians); float velocity_y - = (emitter.m_speed + random_speed_offset) * std::sin(angle_in_radians); + = (emitter.speed + random_speed_offset) * std::sin(angle_in_radians); Position initial_velocity = {velocity_x, velocity_y}; for (size_t i = 0; i < emitter.particles.size(); i++) { if (!emitter.particles[i].active) { - emitter.particles[i].reset(emitter.m_end_lifespan, initial_position, + emitter.particles[i].reset(emitter.end_lifespan, initial_position, initial_velocity); break; } diff --git a/src/crepe/ParticleSystem.h b/src/crepe/ParticleSystem.h index 071eec4..ad96eb0 100644 --- a/src/crepe/ParticleSystem.h +++ b/src/crepe/ParticleSystem.h @@ -1,7 +1,6 @@ #pragma once #include "api/ParticleEmitter.h" -#include namespace crepe { @@ -13,7 +12,7 @@ public: private: void emit_particle(ParticleEmitter & emitter); //emits a new particle - float m_elapsed_time; //elapsed time since the last emission + float elapsed_time; //elapsed time since the last emission }; } // namespace crepe diff --git a/src/crepe/PhysicsSystem.cpp b/src/crepe/PhysicsSystem.cpp index 4bb931d..16f4c10 100644 --- a/src/crepe/PhysicsSystem.cpp +++ b/src/crepe/PhysicsSystem.cpp @@ -1,9 +1,11 @@ -#include "PhysicsSystem.h" -#include "ComponentManager.h" +#include + #include "api/Force.h" #include "api/Rigidbody.h" #include "api/Transform.h" -#include + +#include "ComponentManager.h" +#include "PhysicsSystem.h" using namespace crepe; using namespace crepe::api; diff --git a/src/crepe/PhysicsSystem.h b/src/crepe/PhysicsSystem.h index dd242c7..33b4072 100644 --- a/src/crepe/PhysicsSystem.h +++ b/src/crepe/PhysicsSystem.h @@ -3,11 +3,8 @@ namespace crepe { class PhysicsSystem { -private: - /* data */ - public: - PhysicsSystem(/* args */); + PhysicsSystem(); void update(); }; diff --git a/src/crepe/RenderSystem.cpp b/src/crepe/RenderSystem.cpp index b8789fc..1139359 100644 --- a/src/crepe/RenderSystem.cpp +++ b/src/crepe/RenderSystem.cpp @@ -1,13 +1,13 @@ +#include +#include - -#include "RenderSystem.h" -#include "ComponentManager.h" -#include "SdlContext.h" #include "api/Sprite.h" #include "api/Transform.h" #include "util/log.h" -#include -#include + +#include "ComponentManager.h" +#include "RenderSystem.h" +#include "SdlContext.h" using namespace crepe; using namespace crepe::api; diff --git a/src/crepe/RenderSystem.h b/src/crepe/RenderSystem.h index 5e86dce..4b910a4 100644 --- a/src/crepe/RenderSystem.h +++ b/src/crepe/RenderSystem.h @@ -1,4 +1,3 @@ - #pragma once #include "System.h" diff --git a/src/crepe/SDLApp.cpp b/src/crepe/SDLApp.cpp index 198c0d0..f408595 100644 --- a/src/crepe/SDLApp.cpp +++ b/src/crepe/SDLApp.cpp @@ -1,8 +1,6 @@ -#include "SDLApp.h" -#include "Particle.h" -#include "api/ParticleEmitter.h" #include -#include + +#include "SDLApp.h" SDLApp::SDLApp(int window_width, int window_height) : window_width(window_width), window_height(window_height), window(nullptr), @@ -18,8 +16,8 @@ bool SDLApp::initialize() { } window = SDL_CreateWindow("Particle System", SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, window_width, window_height, - SDL_WINDOW_SHOWN); + SDL_WINDOWPOS_CENTERED, window_width, + window_height, SDL_WINDOW_SHOWN); if (!window) { std::cerr << "Window Creation Error: " << SDL_GetError() << std::endl; return false; diff --git a/src/crepe/SDLApp.h b/src/crepe/SDLApp.h index cdba4c3..e67947b 100644 --- a/src/crepe/SDLApp.h +++ b/src/crepe/SDLApp.h @@ -1,13 +1,12 @@ -#ifndef SDLAPP_HPP -#define SDLAPP_HPP +#pragma once -#include "Particle.h" -#include "api/ParticleEmitter.h" #include +#include "api/ParticleEmitter.h" + class SDLApp { public: - SDLApp(int windowWidth, int windowHeight); + SDLApp(int window_width, int window_height); ~SDLApp(); bool initialize(); @@ -25,5 +24,3 @@ private: SDL_Window * window; SDL_Renderer * renderer; }; - -#endif diff --git a/src/crepe/ScriptSystem.cpp b/src/crepe/ScriptSystem.cpp index 5d882be..d00d474 100644 --- a/src/crepe/ScriptSystem.cpp +++ b/src/crepe/ScriptSystem.cpp @@ -2,12 +2,13 @@ #include #include -#include "ComponentManager.h" -#include "ScriptSystem.h" #include "api/BehaviorScript.h" #include "api/Script.h" #include "util/log.h" +#include "ComponentManager.h" +#include "ScriptSystem.h" + using namespace std; using namespace crepe; using namespace crepe::api; diff --git a/src/crepe/SdlContext.cpp b/src/crepe/SdlContext.cpp index f00dade..2b49283 100644 --- a/src/crepe/SdlContext.cpp +++ b/src/crepe/SdlContext.cpp @@ -1,11 +1,3 @@ - - -#include "SdlContext.h" - -#include "api/Sprite.h" -#include "api/Texture.h" -#include "api/Transform.h" -#include "util/log.h" #include #include #include @@ -15,6 +7,13 @@ #include #include +#include "api/Sprite.h" +#include "api/Texture.h" +#include "api/Transform.h" +#include "util/log.h" + +#include "SdlContext.h" + using namespace crepe; SdlContext & SdlContext::get_instance() { @@ -34,17 +33,18 @@ void SdlContext::handle_events(bool & running) { SdlContext::~SdlContext() { dbg_trace(); - if (m_game_renderer != nullptr) SDL_DestroyRenderer(m_game_renderer); + if (this->game_renderer != nullptr) + SDL_DestroyRenderer(this->game_renderer); - if (m_game_window != nullptr) { - SDL_DestroyWindow(m_game_window); + if (this->game_window != nullptr) { + SDL_DestroyWindow(this->game_window); } IMG_Quit(); SDL_Quit(); } -void SdlContext::clear_screen() { SDL_RenderClear(this->m_game_renderer); } +void SdlContext::clear_screen() { SDL_RenderClear(this->game_renderer); } SdlContext::SdlContext() { dbg_trace(); @@ -55,20 +55,20 @@ SdlContext::SdlContext() { return; } - m_game_window = SDL_CreateWindow( + this->game_window = SDL_CreateWindow( "Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1920, 1080, SDL_WINDOW_SHOWN); - if (!m_game_window) { + if (!this->game_window) { std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl; } - m_game_renderer - = SDL_CreateRenderer(m_game_window, -1, SDL_RENDERER_ACCELERATED); - if (!m_game_renderer) { + this->game_renderer + = SDL_CreateRenderer(this->game_window, -1, SDL_RENDERER_ACCELERATED); + if (!this->game_renderer) { std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << std::endl; - SDL_DestroyWindow(m_game_window); + SDL_DestroyWindow(this->game_window); return; } @@ -78,7 +78,8 @@ SdlContext::SdlContext() { << IMG_GetError() << std::endl; } } -void SdlContext::present_screen() { SDL_RenderPresent(this->m_game_renderer); } + +void SdlContext::present_screen() { SDL_RenderPresent(this->game_renderer); } void SdlContext::draw(const api::Sprite & sprite, const api::Transform & transform) { @@ -88,7 +89,7 @@ void SdlContext::draw(const api::Sprite & sprite, | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); int w, h; - SDL_QueryTexture(sprite.sprite_image->m_texture, NULL, NULL, &w, &h); + SDL_QueryTexture(sprite.sprite_image->texture, NULL, NULL, &w, &h); // needs maybe camera for position SDL_Rect dstrect = { .x = static_cast(transform.position.x), @@ -98,8 +99,8 @@ void SdlContext::draw(const api::Sprite & sprite, }; double degrees = transform.rotation * 180 / M_PI; - SDL_RenderCopyEx(this->m_game_renderer, sprite.sprite_image->m_texture, - NULL, &dstrect, degrees, NULL, render_flip); + SDL_RenderCopyEx(this->game_renderer, sprite.sprite_image->texture, NULL, + &dstrect, degrees, NULL, render_flip); } /* @@ -117,7 +118,7 @@ SDL_Texture * SdlContext::setTextureFromPath(const char * path, SDL_Rect & clip, clip.h = tmp->h / row; SDL_Texture * CreatedTexture - = SDL_CreateTextureFromSurface(m_game_renderer, tmp); + = SDL_CreateTextureFromSurface(this->game_renderer, tmp); if (!CreatedTexture) { std::cerr << "Error could not create texture " << IMG_GetError @@ -137,7 +138,7 @@ SDL_Texture * SdlContext::texture_from_path(const char * path) { std::cerr << "Error surface " << IMG_GetError << std::endl; } SDL_Texture * created_texture - = SDL_CreateTextureFromSurface(m_game_renderer, tmp); + = SDL_CreateTextureFromSurface(this->game_renderer, tmp); if (!created_texture) { std::cerr << "Error could not create texture " << IMG_GetError diff --git a/src/crepe/SdlContext.h b/src/crepe/SdlContext.h index 97adfa2..31ba3a6 100644 --- a/src/crepe/SdlContext.h +++ b/src/crepe/SdlContext.h @@ -1,11 +1,13 @@ #pragma once -#include "RenderSystem.h" -#include "api/Sprite.h" -#include "api/Transform.h" #include #include +#include "api/Sprite.h" +#include "api/Transform.h" + +#include "RenderSystem.h" + namespace crepe::api { class Texture; } @@ -43,8 +45,8 @@ private: void present_screen(); private: - SDL_Window * m_game_window = nullptr; - SDL_Renderer * m_game_renderer = nullptr; + SDL_Window * game_window = nullptr; + SDL_Renderer * game_renderer = nullptr; }; } // namespace crepe diff --git a/src/crepe/Sound.h b/src/crepe/Sound.h index b7cfbb8..917b57e 100644 --- a/src/crepe/Sound.h +++ b/src/crepe/Sound.h @@ -1,10 +1,9 @@ #pragma once +#include #include #include -#include - #include "Asset.h" namespace crepe { diff --git a/src/crepe/api/AssetManager.cpp b/src/crepe/api/AssetManager.cpp index bf4217f..560df6c 100644 --- a/src/crepe/api/AssetManager.cpp +++ b/src/crepe/api/AssetManager.cpp @@ -1,7 +1,6 @@ - +#include "util/log.h" #include "AssetManager.h" -#include "util/log.h" using namespace crepe::api; diff --git a/src/crepe/api/AssetManager.h b/src/crepe/api/AssetManager.h index f3f1307..d9d9b44 100644 --- a/src/crepe/api/AssetManager.h +++ b/src/crepe/api/AssetManager.h @@ -43,4 +43,5 @@ public: return new_asset; } }; + } // namespace crepe::api diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp index 10b3b49..35b8d83 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -1,9 +1,8 @@ #include -#include "AudioSource.h" - #include "../Sound.h" -#include + +#include "AudioSource.h" using namespace crepe::api; diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp index a6bd81c..6fdf605 100644 --- a/src/crepe/api/BehaviorScript.hpp +++ b/src/crepe/api/BehaviorScript.hpp @@ -3,6 +3,7 @@ #include #include "../util/log.h" + #include "BehaviorScript.h" namespace crepe::api { diff --git a/src/crepe/api/CircleCollider.h b/src/crepe/api/CircleCollider.h index 96d98ed..762574b 100644 --- a/src/crepe/api/CircleCollider.h +++ b/src/crepe/api/CircleCollider.h @@ -5,8 +5,8 @@ namespace crepe::api { class CircleCollider : public Collider { public: - CircleCollider(uint32_t gameObjectId, int radius) - : Collider(gameObjectId), radius(radius) {} + CircleCollider(uint32_t game_object_id, int radius) + : Collider(game_object_id), radius(radius) {} int radius; }; diff --git a/src/crepe/api/Color.cpp b/src/crepe/api/Color.cpp index 5c7d8cc..71592da 100644 --- a/src/crepe/api/Color.cpp +++ b/src/crepe/api/Color.cpp @@ -1,5 +1,3 @@ - - #include "Color.h" using namespace crepe::api; diff --git a/src/crepe/api/Force.cpp b/src/crepe/api/Force.cpp index ff82afa..98649c1 100644 --- a/src/crepe/api/Force.cpp +++ b/src/crepe/api/Force.cpp @@ -1,16 +1,17 @@ -#include "Force.h" #include +#include "Force.h" + namespace crepe::api { -Force::Force(uint32_t gameObjectId, uint32_t forceMagnitude, uint32_t direction) - : Component(gameObjectId) { +Force::Force(uint32_t game_object_id, uint32_t magnitude, uint32_t direction) + : Component(game_object_id) { // Convert direction from degrees to radians float radian_direction = static_cast(direction) * (M_PI / 180.0f); force_x = static_cast( - std::round(forceMagnitude * std::cos(radian_direction))); + std::round(magnitude * std::cos(radian_direction))); force_y = static_cast( - std::round(forceMagnitude * std::sin(radian_direction))); + std::round(magnitude * std::sin(radian_direction))); } } // namespace crepe::api diff --git a/src/crepe/api/Force.h b/src/crepe/api/Force.h index 1d30af4..8da9a00 100644 --- a/src/crepe/api/Force.h +++ b/src/crepe/api/Force.h @@ -1,14 +1,14 @@ #pragma once -#include "../Component.h" #include -#include + +#include "../Component.h" namespace crepe::api { class Force : public Component { public: - Force(uint32_t gameObjectId, uint32_t forceMagnitude, uint32_t direction); + Force(uint32_t game_object_id, uint32_t magnitude, uint32_t direction); int32_t force_x; int32_t force_y; diff --git a/src/crepe/api/ParticleEmitter.cpp b/src/crepe/api/ParticleEmitter.cpp index 298a5ec..2e07562 100644 --- a/src/crepe/api/ParticleEmitter.cpp +++ b/src/crepe/api/ParticleEmitter.cpp @@ -1,29 +1,28 @@ -#include "ParticleEmitter.h" -#include "Particle.h" #include #include +#include "Particle.h" +#include "ParticleEmitter.h" + using namespace crepe; -ParticleEmitter::ParticleEmitter(uint32_t gameObjectId, uint32_t max_particles, - uint32_t emission_rate, uint32_t speed, - uint32_t speed_offset, uint32_t angle, - uint32_t angleOffset, float m_begin_lifespan, - float m_end_lifespan) - : Component(gameObjectId), m_max_particles(max_particles), - m_emission_rate(emission_rate), m_speed(speed), m_speed_offset(speed_offset), - m_position{0, 0}, m_begin_lifespan(m_begin_lifespan), - m_end_lifespan(m_end_lifespan) { +ParticleEmitter::ParticleEmitter(uint32_t game_object_id, + uint32_t max_particles, uint32_t emission_rate, + uint32_t speed, uint32_t speed_offset, + uint32_t angle, uint32_t angleOffset, + float begin_lifespan, float end_lifespan) + : Component(game_object_id), max_particles(max_particles), + emission_rate(emission_rate), speed(speed), speed_offset(speed_offset), + position{0, 0}, begin_lifespan(begin_lifespan), + end_lifespan(end_lifespan) { std::srand( static_cast(std::time(nullptr))); // initialize random seed std::cout << "Create emitter" << std::endl; - m_min_angle - = (360 + angle - (angleOffset % 360)) % 360; // calculate minAngle - m_max_angle - = (360 + angle + (angleOffset % 360)) % 360; // calculate maxAngle - m_position.x = 400; - m_position.y = 400; - for (size_t i = 0; i < m_max_particles; i++) { + min_angle = (360 + angle - (angleOffset % 360)) % 360; // calculate minAngle + max_angle = (360 + angle + (angleOffset % 360)) % 360; // calculate maxAngle + position.x = 400; + position.y = 400; + for (size_t i = 0; i < max_particles; i++) { this->particles.emplace_back(); } } diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index 23f02f6..2e2e95b 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -1,32 +1,42 @@ #pragma once -#include "Component.h" -#include "Particle.h" #include #include -# + +#include "Component.h" +#include "Particle.h" namespace crepe { class ParticleEmitter : public Component { public: - ParticleEmitter(uint32_t gameObjectId, uint32_t maxParticles, - uint32_t emissionRate, uint32_t speed, uint32_t speedOffset, - uint32_t angle, uint32_t angleOffset, float m_beginLifespan, - float m_endLifespan); + ParticleEmitter(uint32_t game_object_id, uint32_t max_particles, + uint32_t emission_rate, uint32_t speed, + uint32_t speed_offset, uint32_t angle, uint32_t angleOffset, + float begin_lifespan, float end_lifespan); ~ParticleEmitter(); - Position m_position; //position of the emitter - uint32_t m_max_particles; //maximum number of particles - uint32_t m_emission_rate; //rate of particle emission - uint32_t m_speed; //base speed of the particles - uint32_t m_speed_offset; //offset for random speed variation - uint32_t m_min_angle; //min angle of particle emission - uint32_t m_max_angle; //max angle of particle emission - float m_begin_lifespan; //begin Lifespan of particle (only visual) - float m_end_lifespan; //begin Lifespan of particle + //! position of the emitter + Position position; + //! maximum number of particles + uint32_t max_particles; + //! rate of particle emission + uint32_t emission_rate; + //! base speed of the particles + uint32_t speed; + //! offset for random speed variation + uint32_t speed_offset; + //! min angle of particle emission + uint32_t min_angle; + //! max angle of particle emission + uint32_t max_angle; + //! begin Lifespan of particle (only visual) + float begin_lifespan; + //! begin Lifespan of particle + float end_lifespan; - std::vector particles; //collection of particles + //! collection of particles + std::vector particles; }; } // namespace crepe diff --git a/src/crepe/api/Rigidbody.cpp b/src/crepe/api/Rigidbody.cpp index b35d5b8..ebf9fb9 100644 --- a/src/crepe/api/Rigidbody.cpp +++ b/src/crepe/api/Rigidbody.cpp @@ -2,7 +2,7 @@ using namespace crepe::api; -Rigidbody::Rigidbody(uint32_t gameObjectId, int mass, int gravityScale, +Rigidbody::Rigidbody(uint32_t game_object_id, int mass, int gravity_scale, BodyType bodyType) - : Component(gameObjectId), mass(mass), gravity_scale(gravityScale), + : Component(game_object_id), mass(mass), gravity_scale(gravity_scale), body_type(bodyType) {} diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h index dc242c1..05cbb03 100644 --- a/src/crepe/api/Rigidbody.h +++ b/src/crepe/api/Rigidbody.h @@ -1,20 +1,24 @@ #pragma once -#include "../Component.h" #include +#include "../Component.h" + namespace crepe::api { enum class BodyType { - STATIC, // Does not move (e.g. walls, ground ...) - DYNAMIC, // Moves and responds to forces (e.g. player, physics objects ...) - KINEMATIC // Moves but does not respond to forces (e.g. moving platforms ...) + //! Does not move (e.g. walls, ground ...) + STATIC, + //! Moves and responds to forces (e.g. player, physics objects ...) + DYNAMIC, + //! Moves but does not respond to forces (e.g. moving platforms ...) + KINEMATIC, }; class Rigidbody : public Component { public: - Rigidbody(uint32_t gameObjectId, int mass, int gravityScale, - BodyType bodyType); + Rigidbody(uint32_t game_object_id, int mass, int gravity_scale, + BodyType body_type); int32_t velocity_x; int32_t velocity_y; int mass; diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index cdece28..806f147 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -1,12 +1,11 @@ +#include +#include - -#include "Sprite.h" -#include "Component.h" #include "api/Texture.h" #include "util/log.h" -#include -#include -#include + +#include "Component.h" +#include "Sprite.h" using namespace std; using namespace crepe; diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 7a701e3..b06125e 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -1,18 +1,21 @@ #pragma once -#include "Component.h" -#include "api/Color.h" -#include "api/Texture.h" #include #include #include +#include "api/Color.h" +#include "api/Texture.h" + +#include "Component.h" + namespace crepe::api { struct FlipSettings { bool flip_x = 1; bool flip_y = 1; }; + class Sprite : public Component { public: diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index c755fa9..7791b5b 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -1,31 +1,32 @@ +#include +#include "util/log.h" #include "Asset.h" #include "SdlContext.h" -#include "util/log.h" - #include "Texture.h" -#include using namespace crepe::api; +using namespace std; -Texture::Texture(std::unique_ptr res) { +Texture::Texture(unique_ptr res) { dbg_trace(); this->load(std::move(res)); } Texture::Texture(const char * src) { dbg_trace(); - this->load(std::make_unique(src)); + this->load(make_unique(src)); } Texture::~Texture() { dbg_trace(); - if (this->m_texture != nullptr) { - SDL_DestroyTexture(m_texture); + if (this->texture != nullptr) { + SDL_DestroyTexture(this->texture); } } -void Texture::load(std::unique_ptr res) { + +void Texture::load(unique_ptr res) { SdlContext & ctx = SdlContext::get_instance(); - m_texture = ctx.texture_from_path(res->canonical()); + this->texture = ctx.texture_from_path(res->canonical()); } diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h index 6d3fb40..993f72b 100644 --- a/src/crepe/api/Texture.h +++ b/src/crepe/api/Texture.h @@ -1,9 +1,10 @@ #pragma once -#include "Asset.h" #include #include +#include "Asset.h" + namespace crepe { class SdlContext; } @@ -21,7 +22,7 @@ private: void load(std::unique_ptr res); private: - SDL_Texture * m_texture = nullptr; + SDL_Texture * texture = nullptr; friend class crepe::SdlContext; }; diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index 4f22843..626cd67 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -1,10 +1,10 @@ +#include - -#include "Transform.h" -#include "Component.h" #include "api/Point.h" #include "util/log.h" -#include + +#include "Component.h" +#include "Transform.h" using namespace crepe::api; diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index c9d46c5..7b74e43 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -1,11 +1,15 @@ #pragma once -#include "Component.h" -#include "api/Point.h" #include + +#include "api/Point.h" + +#include "Component.h" + namespace crepe::api { class Transform : public Component { + public: Transform(uint32_t id, Point &, double, double); ~Transform(); @@ -13,4 +17,5 @@ public: double rotation; // Rotation, in radians double scale; // Multiplication factoh }; + } // namespace crepe::api diff --git a/src/example/particle.cpp b/src/example/particle.cpp index 66b1441..943f83b 100644 --- a/src/example/particle.cpp +++ b/src/example/particle.cpp @@ -83,7 +83,7 @@ int main(int argc, char * argv[]) { for (const Particle & particle : emitter.particles) { if (particle.active) app.draw_square(particle.position.x, particle.position.y, - 5); // draw each particle + 5); // draw each particle } } -- cgit v1.2.3 From 35ef3ba91ce9e00466508f2388f4c1dd2321b505 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 31 Oct 2024 18:40:45 +0100 Subject: minor script system fixes --- src/crepe/ScriptSystem.cpp | 1 + src/crepe/api/Transform.cpp | 2 +- src/crepe/api/Transform.h | 2 +- src/example/script.cpp | 31 +++++++++++++++++++++---------- 4 files changed, 24 insertions(+), 12 deletions(-) (limited to 'src/crepe/api/Transform.cpp') diff --git a/src/crepe/ScriptSystem.cpp b/src/crepe/ScriptSystem.cpp index d00d474..171b490 100644 --- a/src/crepe/ScriptSystem.cpp +++ b/src/crepe/ScriptSystem.cpp @@ -37,6 +37,7 @@ forward_list