diff options
46 files changed, 890 insertions, 69 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8a47905..b60a0cd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -38,4 +38,3 @@ target_link_libraries(test_main PRIVATE gtest_main PUBLIC crepe ) - diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 6633696..afaa74c 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -1,14 +1,18 @@ + target_sources(crepe PUBLIC Asset.cpp Sound.cpp SoundContext.cpp - SdlContext.cpp + Particle.cpp + ParticleSystem.cpp + SDLApp.cpp ComponentManager.cpp Component.cpp - GameObject.cpp - Collider.cpp - Rigidbody.cpp ScriptSystem.cpp + PhysicsSystem.cpp + CollisionSystem.cpp + Collider.cpp + SdlContext.cpp RenderSystem.cpp ) @@ -16,16 +20,18 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Asset.h Sound.h SoundContext.h - SdlContext.h + ParticleSystem.h + Particle.h + SDLApp.h ComponentManager.h ComponentManager.hpp Component.h - GameObject.h - GameObject.hpp - Collider.h - Rigidbody.h System.h ScriptSystem.h + PhysicsSystem.h + CollisionSystem.h + Collider.h + SdlContext.h RenderSystem.h ) diff --git a/src/crepe/Collider.cpp b/src/crepe/Collider.cpp index 3f12afd..311ffb3 100644 --- a/src/crepe/Collider.cpp +++ b/src/crepe/Collider.cpp @@ -2,4 +2,4 @@ using namespace crepe; -Collider::Collider(int size) : size(size) {} +Collider::Collider(uint32_t gameObjectId) : Component(gameObjectId){} diff --git a/src/crepe/Collider.h b/src/crepe/Collider.h index 120da05..cfc044c 100644 --- a/src/crepe/Collider.h +++ b/src/crepe/Collider.h @@ -6,7 +6,7 @@ namespace crepe { class Collider : public Component { public: - Collider(int size); + Collider(uint32_t gameObjectId); int size; }; diff --git a/src/crepe/CollisionSystem.cpp b/src/crepe/CollisionSystem.cpp new file mode 100644 index 0000000..a0c6dca --- /dev/null +++ b/src/crepe/CollisionSystem.cpp @@ -0,0 +1,13 @@ +#include "CollisionSystem.h" +#include "ComponentManager.h" +#include <iostream> + +using namespace crepe; + +CollisionSystem::CollisionSystem() { + +} + +void CollisionSystem::update() { + +} diff --git a/src/crepe/CollisionSystem.h b/src/crepe/CollisionSystem.h new file mode 100644 index 0000000..70d7237 --- /dev/null +++ b/src/crepe/CollisionSystem.h @@ -0,0 +1,15 @@ +#pragma once + +namespace crepe { + +class CollisionSystem +{ + private: + /* data */ + public: + CollisionSystem(/* args */); + void update(); +}; + +} + diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index bce90f1..e2cd7e0 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -1,3 +1,5 @@ #include "Component.h" using namespace crepe; + +Component::Component(uint32_t id) : gameObjectId(id), active(true) {} diff --git a/src/crepe/Component.h b/src/crepe/Component.h index d9a01ac..9483dad 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -1,17 +1,16 @@ #pragma once +#include <cstdint> namespace crepe { class Component { protected: - Component() = default; + Component(uint32_t id); public: + uint32_t gameObjectId; + bool active; virtual ~Component() = default; - // TODO: shouldn't this constructor be deleted because this class will never - // directly be instantiated? - - bool active = true; }; } // namespace crepe diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 2377a94..22dbc66 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -30,7 +30,7 @@ T & ComponentManager::add_component(uint32_t id, Args &&... args) { // Create a new component of type T (arguments directly forwarded). The // constructor must be called by ComponentManager. - T * instance = new T(forward<Args>(args)...); + T * instance = new T(id,forward<Args>(args)...); // store its unique_ptr in the vector<> components[type][id].push_back(unique_ptr<T>(instance)); diff --git a/src/crepe/GameObject.cpp b/src/crepe/GameObject.cpp deleted file mode 100644 index de3beb6..0000000 --- a/src/crepe/GameObject.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "GameObject.h" - -using namespace crepe; - -GameObject::GameObject(uint32_t id, std::string name, std::string tag, - int layer) - : id(id), name(name), tag(tag), active(true), layer(layer) {} diff --git a/src/crepe/Particle.cpp b/src/crepe/Particle.cpp new file mode 100644 index 0000000..aa33606 --- /dev/null +++ b/src/crepe/Particle.cpp @@ -0,0 +1,25 @@ +#include "Particle.h" +#include <iostream> + +using namespace crepe; + +Particle::Particle() +{ + this->active = false; +} + +void Particle::reset(float lifespan, Position position, Position velocity) { + this->timeInLife = 0; + this->lifespan = lifespan; + this->position = position; + this->velocity = velocity; + this->active = true; +} + +void Particle::update(float deltaTime) { + timeInLife += deltaTime; + position.x += velocity.x * deltaTime; + position.y += velocity.y * deltaTime; + if(timeInLife >= lifespan)this->active = false; +} + diff --git a/src/crepe/Particle.h b/src/crepe/Particle.h new file mode 100644 index 0000000..c408ed9 --- /dev/null +++ b/src/crepe/Particle.h @@ -0,0 +1,24 @@ +#pragma once + +namespace crepe { + +struct Position { + float x; + float y; + }; + +class Particle { +public: + + Position position; + Position velocity; + float lifespan; + bool active; + + Particle(); + void reset(float lifespan, Position position, Position velocity); + void update(float deltaTime); + float timeInLife; +}; + +} diff --git a/src/crepe/ParticleSystem.cpp b/src/crepe/ParticleSystem.cpp new file mode 100644 index 0000000..bf7f8fc --- /dev/null +++ b/src/crepe/ParticleSystem.cpp @@ -0,0 +1,59 @@ +#include "ParticleSystem.h" +#include <cmath> +#include <ctime> +#include "api/ParticleEmitter.h" +#include "Particle.h" +#include "ComponentManager.h" + +using namespace crepe; + +ParticleSystem::ParticleSystem() : m_elapsedTime(0.0f) {} // Initialize m_elapsedTime to 0 + +void ParticleSystem::update() { + ComponentManager& mgr = ComponentManager::get_instance(); + std::vector<std::reference_wrapper<ParticleEmitter>> emitters = mgr.get_components_by_type<ParticleEmitter>(); + float deltaTime = 0.10; + for (ParticleEmitter& emitter : emitters) { + float updateAmount = 1/static_cast<float>(emitter.m_emissionRate); + for (float i = 0; i < deltaTime; i += updateAmount) + { + emitParticle(emitter); + } + for (size_t j = 0; j < emitter.particles.size(); j++) + { + if(emitter.particles[j].active) + { + emitter.particles[j].update(deltaTime); + } + } + } +} + +void ParticleSystem::emitParticle(ParticleEmitter& emitter) { + Position initialPosition = { emitter.m_position.x, emitter.m_position.y}; + float randomAngle = 0.0f; + if(emitter.m_maxAngle < emitter.m_minAngle) + { + randomAngle = ((emitter.m_minAngle + (std::rand() % (static_cast<uint32_t>(emitter.m_maxAngle + 360 - emitter.m_minAngle + 1))))%360); + } + else + { + randomAngle = emitter.m_minAngle + (std::rand() % (static_cast<uint32_t>(emitter.m_maxAngle - emitter.m_minAngle + 1))); + } + float angleInRadians = randomAngle * (M_PI / 180.0f); + float randomSpeedOffset = (static_cast<float>(std::rand()) / RAND_MAX) * (2 * emitter.m_speedOffset) - emitter.m_speedOffset; + float velocityX = (emitter.m_speed + randomSpeedOffset) * std::cos(angleInRadians); + float velocityY = (emitter.m_speed + randomSpeedOffset) * std::sin(angleInRadians); + Position initialVelocity = { + velocityX, + velocityY + }; + for (size_t i = 0; i < emitter.particles.size(); i++) + { + if(!emitter.particles[i].active) + { + emitter.particles[i].reset(emitter.m_endLifespan, initialPosition, initialVelocity); + break; + } + } +} diff --git a/src/crepe/ParticleSystem.h b/src/crepe/ParticleSystem.h new file mode 100644 index 0000000..313e1dd --- /dev/null +++ b/src/crepe/ParticleSystem.h @@ -0,0 +1,19 @@ +#pragma once + +#include <vector> +#include "api/ParticleEmitter.h" + + +namespace crepe { + +class ParticleSystem { +public: + ParticleSystem(); + void update(); +private: + void emitParticle(ParticleEmitter &emitter); //emits a new particle + + float m_elapsedTime; //elapsed time since the last emission +}; + +} diff --git a/src/crepe/PhysicsSystem.cpp b/src/crepe/PhysicsSystem.cpp new file mode 100644 index 0000000..d9930a8 --- /dev/null +++ b/src/crepe/PhysicsSystem.cpp @@ -0,0 +1,56 @@ +#include "PhysicsSystem.h" +#include "ComponentManager.h" +#include "api/Rigidbody.h" +#include "api/Transform.h" +#include "api/Force.h" +#include <iostream> + +using namespace crepe; +using namespace crepe::api; + +PhysicsSystem::PhysicsSystem() { + +} + +void PhysicsSystem::update() { + ComponentManager& mgr = ComponentManager::get_instance(); + std::vector<std::reference_wrapper<Rigidbody>> rigidbodies = mgr.get_components_by_type<Rigidbody>(); + std::vector<std::reference_wrapper<Transform>> transforms = mgr.get_components_by_type<Transform>(); + + for (Rigidbody& rigidbody : rigidbodies) { + + switch (rigidbody.body_type) + { + case BodyType::Dynamic : + for (Transform& transform : transforms) { + if(transform.gameObjectId == rigidbody.gameObjectId) + { + rigidbody.velocity_x = 0; + rigidbody.velocity_y = 0; + std::vector<std::reference_wrapper<Force>> Forces = mgr.get_components_by_id<Force>(rigidbody.gameObjectId); + rigidbody.velocity_y += rigidbody.gravity_scale * 1 * rigidbody.mass; + + for (Force& force : Forces) + { + rigidbody.velocity_x += force.force_x; + rigidbody.velocity_y += force.force_y; + } + + std::cout << "before transform.postion.x " << transform.postion.x << std::endl; + std::cout << "before transform.postion.y " << transform.postion.y << std::endl; + transform.postion.x += rigidbody.velocity_x; + transform.postion.y += rigidbody.velocity_y; + std::cout << "after transform.postion.x " << transform.postion.x << std::endl; + std::cout << "after transform.postion.y " << transform.postion.y << std::endl; + } + } + break; + case BodyType::Kinematic : + break; //(scripts) + case BodyType::Static : + break; //(unmoveable objects) + default: + break; + } + } +} diff --git a/src/crepe/PhysicsSystem.h b/src/crepe/PhysicsSystem.h new file mode 100644 index 0000000..1899089 --- /dev/null +++ b/src/crepe/PhysicsSystem.h @@ -0,0 +1,17 @@ +#pragma once + +namespace crepe { + +class PhysicsSystem +{ + private: + /* data */ + public: + PhysicsSystem(/* args */); + void update(); +}; + +} + + + diff --git a/src/crepe/Rigidbody.cpp b/src/crepe/Rigidbody.cpp deleted file mode 100644 index 495d908..0000000 --- a/src/crepe/Rigidbody.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "Rigidbody.h" - -using namespace crepe; - -Rigidbody::Rigidbody(int mass, int gravityScale, int bodyType) - : mass(mass), gravity_scale(gravityScale), body_type(bodyType) {} diff --git a/src/crepe/Rigidbody.h b/src/crepe/Rigidbody.h deleted file mode 100644 index 63a8877..0000000 --- a/src/crepe/Rigidbody.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include "Component.h" - -namespace crepe { - -class Rigidbody : public Component { -public: - Rigidbody(int mass, int gravityScale, int bodyType); - - int mass; - int gravity_scale; - int body_type; -}; - -} // namespace crepe diff --git a/src/crepe/SDLApp.cpp b/src/crepe/SDLApp.cpp new file mode 100644 index 0000000..ca7e819 --- /dev/null +++ b/src/crepe/SDLApp.cpp @@ -0,0 +1,78 @@ +#include "SDLApp.h" +#include <iostream> +#include <vector> +#include "Particle.h" +#include "api/ParticleEmitter.h" + +SDLApp::SDLApp(int windowWidth, int windowHeight) + : windowWidth(windowWidth), windowHeight(windowHeight), window(nullptr), renderer(nullptr) {} + +SDLApp::~SDLApp() { + cleanUp(); +} + +bool SDLApp::initialize() { + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + std::cerr << "SDL Initialization Error: " << SDL_GetError() << std::endl; + return false; + } + + window = SDL_CreateWindow("Particle System", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + windowWidth, + windowHeight, + SDL_WINDOW_SHOWN); + if (!window) { + std::cerr << "Window Creation Error: " << SDL_GetError() << std::endl; + return false; + } + + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + if (!renderer) { + std::cerr << "Renderer Creation Error: " << SDL_GetError() << std::endl; + return false; + } + + return true; +} + +void SDLApp::handleEvents(bool& running) { + SDL_Event event; + while (SDL_PollEvent(&event)) { + if (event.type == SDL_QUIT) { + running = false; + } + } +} + +void SDLApp::clearScreen() { + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); +} + +void SDLApp::presentScreen() { + SDL_RenderPresent(renderer); +} + +void SDLApp::drawSquare(int x, int y, int size) { + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_Rect rect = { x, y, size, size }; + SDL_RenderFillRect(renderer, &rect); +} + +SDL_Texture* squareTexture = nullptr; // Load this with an image or create it + + + + + +void SDLApp::cleanUp() { + if (renderer) { + SDL_DestroyRenderer(renderer); + } + if (window) { + SDL_DestroyWindow(window); + } + SDL_Quit(); +} diff --git a/src/crepe/SDLApp.h b/src/crepe/SDLApp.h new file mode 100644 index 0000000..b536ac8 --- /dev/null +++ b/src/crepe/SDLApp.h @@ -0,0 +1,28 @@ +#ifndef SDLAPP_HPP +#define SDLAPP_HPP + +#include <SDL2/SDL.h> +#include "Particle.h" +#include "api/ParticleEmitter.h" + +class SDLApp { +public: + SDLApp(int windowWidth, int windowHeight); + ~SDLApp(); + + bool initialize(); + void handleEvents(bool& running); + void clearScreen(); + void presentScreen(); + void drawSquare(int x, int y, int size); + void cleanUp(); + void drawParticles(const std::vector<crepe::ParticleEmitter>& emitters); + void drawMultipleSquares(const std::vector<SDL_Rect>& squares); +private: + int windowWidth; + int windowHeight; + SDL_Window* window; + SDL_Renderer* renderer; +}; + +#endif diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp index 90c1b07..10b3b49 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -1,6 +1,8 @@ +#include <memory> + #include "AudioSource.h" -#include "Sound.h" +#include "../Sound.h" #include <memory> using namespace crepe::api; diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h index 2d26cda..7980212 100644 --- a/src/crepe/api/AudioSource.h +++ b/src/crepe/api/AudioSource.h @@ -2,8 +2,8 @@ #include <memory> -#include "Asset.h" -#include "Component.h" +#include "../Asset.h" +#include "../Component.h" namespace crepe { class Sound; diff --git a/src/crepe/api/BehaviorScript.cpp b/src/crepe/api/BehaviorScript.cpp index 1f236b4..74788ec 100644 --- a/src/crepe/api/BehaviorScript.cpp +++ b/src/crepe/api/BehaviorScript.cpp @@ -1,8 +1,7 @@ #include "../util/log.h" #include "BehaviorScript.h" -#include "Script.h" using namespace crepe::api; -BehaviorScript::BehaviorScript() { dbg_trace(); } +BehaviorScript::BehaviorScript() : Component(gameObjectId) { dbg_trace(); } diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h index 6ce6798..25d3cc5 100644 --- a/src/crepe/api/BehaviorScript.h +++ b/src/crepe/api/BehaviorScript.h @@ -2,7 +2,7 @@ #include <memory> -#include "Component.h" +#include "../Component.h" #include "Script.h" namespace crepe { diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index d29a771..f6de96e 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -2,10 +2,14 @@ target_sources(crepe PUBLIC # AudioSource.cpp BehaviorScript.cpp Script.cpp - Color.cpp - Texture.cpp + GameObject.cpp + Rigidbody.cpp Sprite.cpp + Force.cpp + ParticleEmitter.cpp Transform.cpp + Color.cpp + Texture.cpp AssetManager.cpp ) @@ -13,10 +17,15 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES # AudioSource.h BehaviorScript.h Script.h - Point.h - Transform.h - Color.h + GameObject.h + GameObject.hpp + Rigidbody.h Sprite.h + Force.h + ParticleEmitter.h + Transform.h + Point.h + Color.h Texture.h AssetManager.h ) diff --git a/src/crepe/api/CircleCollider.h b/src/crepe/api/CircleCollider.h new file mode 100644 index 0000000..4a4883c --- /dev/null +++ b/src/crepe/api/CircleCollider.h @@ -0,0 +1,12 @@ +#pragma once +#include "../Collider.h" + +namespace crepe::api { + +class CircleCollider : public Collider { +public: + CircleCollider(uint32_t gameObjectId,int radius) : Collider(gameObjectId), radius(radius) {} + int radius; +}; + +} // namespace crepe diff --git a/src/crepe/api/Force.cpp b/src/crepe/api/Force.cpp new file mode 100644 index 0000000..1b4c81a --- /dev/null +++ b/src/crepe/api/Force.cpp @@ -0,0 +1,14 @@ +#include "Force.h" +#include <cmath> + +namespace crepe::api { + +Force::Force(uint32_t gameObjectId, uint32_t forceMagnitude, uint32_t direction): Component(gameObjectId) +{ + // Convert direction from degrees to radians + float radian_direction = static_cast<float>(direction) * (M_PI / 180.0f); + force_x = static_cast<int32_t>(std::round(forceMagnitude * std::cos(radian_direction))); + force_y = static_cast<int32_t>(std::round(forceMagnitude * std::sin(radian_direction))); +} + +} // namespace crepe::api diff --git a/src/crepe/api/Force.h b/src/crepe/api/Force.h new file mode 100644 index 0000000..0b06da1 --- /dev/null +++ b/src/crepe/api/Force.h @@ -0,0 +1,17 @@ +#pragma once + +#include "../Component.h" +#include <cstdint> +#include <utility> + +namespace crepe::api { + +class Force : public Component { +public: + Force(uint32_t gameObjectId, uint32_t forceMagnitude, uint32_t direction); + + int32_t force_x; + int32_t force_y; +}; + +} // namespace crepe diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp new file mode 100644 index 0000000..b167187 --- /dev/null +++ b/src/crepe/api/GameObject.cpp @@ -0,0 +1,7 @@ +#include "GameObject.h" + +using namespace crepe::api; +using namespace std; + +GameObject::GameObject(uint32_t id, string name, string tag, int layer) + : id(id), name(name), tag(tag), active(true), layer(layer) {} diff --git a/src/crepe/GameObject.h b/src/crepe/api/GameObject.h index b5d6399..57508c5 100644 --- a/src/crepe/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -3,7 +3,7 @@ #include <cstdint> #include <string> -namespace crepe { +namespace crepe::api { class GameObject { public: @@ -19,6 +19,6 @@ public: int layer; }; -} // namespace crepe +} // namespace crepe::api #include "GameObject.hpp" diff --git a/src/crepe/api/GameObject.hpp b/src/crepe/api/GameObject.hpp new file mode 100644 index 0000000..8295ea3 --- /dev/null +++ b/src/crepe/api/GameObject.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "../ComponentManager.h" + +#include "GameObject.h" + +namespace crepe::api { + +template <typename T, typename... Args> +T & GameObject::add_component(Args &&... args) { + auto & mgr = ComponentManager::get_instance(); + return mgr.add_component<T>(id, std::forward<Args>(args)...); +} + +} // namespace crepe::api diff --git a/src/crepe/api/ParticleEmitter.cpp b/src/crepe/api/ParticleEmitter.cpp new file mode 100644 index 0000000..318c6db --- /dev/null +++ b/src/crepe/api/ParticleEmitter.cpp @@ -0,0 +1,28 @@ +#include "ParticleEmitter.h" +#include <ctime> +#include "Particle.h" +#include <iostream> + +using namespace crepe; + +ParticleEmitter::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) + : Component(gameObjectId), m_maxParticles(maxParticles), m_emissionRate(emissionRate), m_speed(speed), m_speedOffset(speedOffset), m_position{0, 0}, m_beginLifespan(m_beginLifespan),m_endLifespan(m_endLifespan) { + std::srand(static_cast<uint32_t>(std::time(nullptr))); // initialize random seed + std::cout << "Create emitter" << std::endl; + m_minAngle = (360 + angle - (angleOffset % 360)) % 360; // calculate minAngle + m_maxAngle = (360 + angle + (angleOffset % 360)) % 360; // calculate maxAngle + m_position.x = 400; + m_position.y = 400; + for (size_t i = 0; i < m_maxParticles; i++) + { + this->particles.emplace_back(); + } + +} + +ParticleEmitter::~ParticleEmitter() { + std::vector<Particle>::iterator it = this->particles.begin(); + while (it != this->particles.end()) { + it = this->particles.erase(it); + } +} diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h new file mode 100644 index 0000000..8cd78a9 --- /dev/null +++ b/src/crepe/api/ParticleEmitter.h @@ -0,0 +1,32 @@ +#pragma once + +#include <vector> +#include "Particle.h" +#include <cstdint> +#include "Component.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(); + + Position m_position; //position of the emitter + uint32_t m_maxParticles; //maximum number of particles + uint32_t m_emissionRate; //rate of particle emission + uint32_t m_speed; //base speed of the particles + uint32_t m_speedOffset; //offset for random speed variation + uint32_t m_minAngle; //min angle of particle emission + uint32_t m_maxAngle; //max angle of particle emission + float m_beginLifespan; //begin Lifespan of particle (only visual) + float m_endLifespan; //begin Lifespan of particle + + std::vector<Particle> particles; //collection of particles + +}; + +} + + diff --git a/src/crepe/api/Rigidbody.cpp b/src/crepe/api/Rigidbody.cpp new file mode 100644 index 0000000..30a2cff --- /dev/null +++ b/src/crepe/api/Rigidbody.cpp @@ -0,0 +1,7 @@ +#include "Rigidbody.h" + +using namespace crepe::api; + +Rigidbody::Rigidbody(uint32_t gameObjectId,int mass, int gravityScale, BodyType bodyType) + : Component(gameObjectId), mass(mass), gravity_scale(gravityScale), body_type(bodyType) {} + diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h new file mode 100644 index 0000000..b7a0064 --- /dev/null +++ b/src/crepe/api/Rigidbody.h @@ -0,0 +1,24 @@ +#pragma once + +#include "../Component.h" +#include <cstdint> + +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 ...) +}; + +class Rigidbody : public Component { +public: + Rigidbody(uint32_t gameObjectId,int mass, int gravityScale, BodyType bodyType); + int32_t velocity_x; + int32_t velocity_y; + int mass; + int gravity_scale; + BodyType body_type; +}; + +} // namespace crepe::api diff --git a/src/crepe/crepe/CMakeFiles/CMakeDirectoryInformation.cmake b/src/crepe/crepe/CMakeFiles/CMakeDirectoryInformation.cmake new file mode 100644 index 0000000..8d03506 --- /dev/null +++ b/src/crepe/crepe/CMakeFiles/CMakeDirectoryInformation.cmake @@ -0,0 +1,16 @@ +# CMAKE generated file: DO NOT EDIT! +# Generated by "Unix Makefiles" Generator, CMake Version 3.28 + +# Relative path conversion top directories. +set(CMAKE_RELATIVE_PATH_TOP_SOURCE "/home/jaro/crepe/src") +set(CMAKE_RELATIVE_PATH_TOP_BINARY "/home/jaro/crepe/src/crepe") + +# Force unix paths in dependencies. +set(CMAKE_FORCE_UNIX_PATHS 1) + + +# The C and CXX include file regular expressions for this directory. +set(CMAKE_C_INCLUDE_REGEX_SCAN "^.*$") +set(CMAKE_C_INCLUDE_REGEX_COMPLAIN "^$") +set(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN}) +set(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN ${CMAKE_C_INCLUDE_REGEX_COMPLAIN}) diff --git a/src/crepe/crepe/CMakeFiles/progress.marks b/src/crepe/crepe/CMakeFiles/progress.marks new file mode 100644 index 0000000..573541a --- /dev/null +++ b/src/crepe/crepe/CMakeFiles/progress.marks @@ -0,0 +1 @@ +0 diff --git a/src/crepe/crepe/Makefile b/src/crepe/crepe/Makefile new file mode 100644 index 0000000..094215a --- /dev/null +++ b/src/crepe/crepe/Makefile @@ -0,0 +1,140 @@ +# CMAKE generated file: DO NOT EDIT! +# Generated by "Unix Makefiles" Generator, CMake Version 3.28 + +# Default target executed when no arguments are given to make. +default_target: all +.PHONY : default_target + +# Allow only one "make -f Makefile2" at a time, but pass parallelism. +.NOTPARALLEL: + +#============================================================================= +# Special targets provided by cmake. + +# Disable implicit rules so canonical targets will work. +.SUFFIXES: + +# Disable VCS-based implicit rules. +% : %,v + +# Disable VCS-based implicit rules. +% : RCS/% + +# Disable VCS-based implicit rules. +% : RCS/%,v + +# Disable VCS-based implicit rules. +% : SCCS/s.% + +# Disable VCS-based implicit rules. +% : s.% + +.SUFFIXES: .hpux_make_needs_suffix_list + +# Command-line flag to silence nested $(MAKE). +$(VERBOSE)MAKESILENT = -s + +#Suppress display of executed commands. +$(VERBOSE).SILENT: + +# A target that is always out of date. +cmake_force: +.PHONY : cmake_force + +#============================================================================= +# Set environment variables for the build. + +# The shell in which to execute make rules. +SHELL = /bin/sh + +# The CMake executable. +CMAKE_COMMAND = /usr/bin/cmake + +# The command to remove a file. +RM = /usr/bin/cmake -E rm -f + +# Escaping for special characters. +EQUALS = = + +# The top-level source directory on which CMake was run. +CMAKE_SOURCE_DIR = /home/jaro/crepe/src + +# The top-level build directory on which CMake was run. +CMAKE_BINARY_DIR = /home/jaro/crepe/src/crepe + +#============================================================================= +# Targets provided globally by CMake. + +# Special rule for the target edit_cache +edit_cache: + @$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "No interactive CMake dialog available..." + /usr/bin/cmake -E echo No\ interactive\ CMake\ dialog\ available. +.PHONY : edit_cache + +# Special rule for the target edit_cache +edit_cache/fast: edit_cache +.PHONY : edit_cache/fast + +# Special rule for the target rebuild_cache +rebuild_cache: + @$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake to regenerate build system..." + /usr/bin/cmake --regenerate-during-build -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) +.PHONY : rebuild_cache + +# Special rule for the target rebuild_cache +rebuild_cache/fast: rebuild_cache +.PHONY : rebuild_cache/fast + +# The main all target +all: cmake_check_build_system + cd /home/jaro/crepe/src/crepe && $(CMAKE_COMMAND) -E cmake_progress_start /home/jaro/crepe/src/crepe/CMakeFiles /home/jaro/crepe/src/crepe/crepe//CMakeFiles/progress.marks + cd /home/jaro/crepe/src/crepe && $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 crepe/all + $(CMAKE_COMMAND) -E cmake_progress_start /home/jaro/crepe/src/crepe/CMakeFiles 0 +.PHONY : all + +# The main clean target +clean: + cd /home/jaro/crepe/src/crepe && $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 crepe/clean +.PHONY : clean + +# The main clean target +clean/fast: clean +.PHONY : clean/fast + +# Prepare targets for installation. +preinstall: all + cd /home/jaro/crepe/src/crepe && $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 crepe/preinstall +.PHONY : preinstall + +# Prepare targets for installation. +preinstall/fast: + cd /home/jaro/crepe/src/crepe && $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 crepe/preinstall +.PHONY : preinstall/fast + +# clear depends +depend: + cd /home/jaro/crepe/src/crepe && $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1 +.PHONY : depend + +# Help Target +help: + @echo "The following are some of the valid targets for this Makefile:" + @echo "... all (the default if no target is provided)" + @echo "... clean" + @echo "... depend" + @echo "... edit_cache" + @echo "... rebuild_cache" +.PHONY : help + + + +#============================================================================= +# Special targets to cleanup operation of make. + +# Special rule to run CMake to check the build system integrity. +# No rule that depends on this can have commands that come from listfiles +# because they might be regenerated. +cmake_check_build_system: + cd /home/jaro/crepe/src/crepe && $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0 +.PHONY : cmake_check_build_system + diff --git a/src/crepe/crepe/cmake_install.cmake b/src/crepe/crepe/cmake_install.cmake new file mode 100644 index 0000000..cd57803 --- /dev/null +++ b/src/crepe/crepe/cmake_install.cmake @@ -0,0 +1,44 @@ +# Install script for directory: /home/jaro/crepe/src/crepe + +# Set the install prefix +if(NOT DEFINED CMAKE_INSTALL_PREFIX) + set(CMAKE_INSTALL_PREFIX "/usr/local") +endif() +string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}") + +# Set the install configuration name. +if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME) + if(BUILD_TYPE) + string(REGEX REPLACE "^[^A-Za-z0-9_]+" "" + CMAKE_INSTALL_CONFIG_NAME "${BUILD_TYPE}") + else() + set(CMAKE_INSTALL_CONFIG_NAME "Debug") + endif() + message(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"") +endif() + +# Set the component getting installed. +if(NOT CMAKE_INSTALL_COMPONENT) + if(COMPONENT) + message(STATUS "Install component: \"${COMPONENT}\"") + set(CMAKE_INSTALL_COMPONENT "${COMPONENT}") + else() + set(CMAKE_INSTALL_COMPONENT) + endif() +endif() + +# Install shared libraries without execute permission? +if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE) + set(CMAKE_INSTALL_SO_NO_EXE "1") +endif() + +# Is this installation the result of a crosscompile? +if(NOT DEFINED CMAKE_CROSSCOMPILING) + set(CMAKE_CROSSCOMPILING "FALSE") +endif() + +# Set default install directory permissions. +if(NOT DEFINED CMAKE_OBJDUMP) + set(CMAKE_OBJDUMP "/usr/bin/objdump") +endif() + diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h index 3e36f0a..e2776c9 100644 --- a/src/crepe/util/log.h +++ b/src/crepe/util/log.h @@ -9,7 +9,7 @@ #define _crepe_logf_here(fmt, ...) \ crepe::util::logf(util::log_level::DEBUG, "%s%s (%s:%d)%s" fmt "\n", \ crepe::util::color::FG_WHITE, __PRETTY_FUNCTION__, \ - __FILE_NAME__, __LINE__, crepe::util::color::RESET, \ + __FILE__, __LINE__, crepe::util::color::RESET, \ __VA_ARGS__) // very illegal global function-style macros diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 1b148b3..475d31c 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -1,3 +1,6 @@ +# all examples +add_custom_target(examples) + # add_example(target_name [SOURCES...]) function(add_example target_name) # if SOURCES is not specified @@ -10,11 +13,16 @@ function(add_example target_name) add_executable(${target_name} EXCLUDE_FROM_ALL ${sources}) target_link_libraries(${target_name} PUBLIC crepe) + add_dependencies(examples ${target_name}) endfunction() add_example(audio_internal) add_example(components_internal) add_example(script) +add_example(particle) +add_example(Physics) +target_link_libraries(particle PUBLIC SDL2) +target_link_libraries(Physics PUBLIC SDL2) add_example(rendering) add_example(asset_manager) diff --git a/src/example/Physics.cpp b/src/example/Physics.cpp new file mode 100644 index 0000000..6499fb0 --- /dev/null +++ b/src/example/Physics.cpp @@ -0,0 +1,28 @@ +#include <iostream> +#include <thread> +#include <chrono> +#include "Transform.h" +#include <crepe/api/Rigidbody.h> +#include <crepe/api/Force.h> +#include "PhysicsSystem.h" +#include <crepe/Component.h> +#include <crepe/ComponentManager.h> +#include <crepe/api/GameObject.h> + +using namespace crepe::api; +using namespace crepe; +using namespace std; + + +int main(int argc, char* argv[]) { + PhysicsSystem physicsSystem; + GameObject * game_object[2]; + game_object[1] = new GameObject(2, "Name", "Tag", 0); // not found not used + game_object[0] = new GameObject(5, "Name", "Tag", 0); + Position position = {0, 0}; + game_object[0]->add_component<Transform>(position,0,0); + game_object[0]->add_component<Rigidbody>(1, 1 , BodyType::Dynamic); + game_object[0]->add_component<Force>(1 , 0); + physicsSystem.update(); + return 0; +} diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp index 54ce295..ae6c765 100644 --- a/src/example/components_internal.cpp +++ b/src/example/components_internal.cpp @@ -6,14 +6,17 @@ #include <cassert> #include <chrono> -#include <crepe/Collider.h> #include <crepe/Component.h> #include <crepe/ComponentManager.h> -#include <crepe/GameObject.h> -#include <crepe/Rigidbody.h> -#include <crepe/Sprite.h> + +#include <crepe/api/Collider.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/Rigidbody.h> +#include <crepe/api/Sprite.h> + #include <crepe/util/log.h> +using namespace crepe::api; using namespace crepe; using namespace std; @@ -39,8 +42,8 @@ int main() { auto stop_adding = chrono::high_resolution_clock::now(); auto sprites = mgr.get_components_by_type<Sprite>(); - for (auto sprite : sprites) { - assert(sprite.get().path == "test"); + for (auto & sprite : sprites) { + assert(sprite.path == "test"); } auto stop_looping = chrono::high_resolution_clock::now(); diff --git a/src/example/particle.cpp b/src/example/particle.cpp new file mode 100644 index 0000000..fb89a3d --- /dev/null +++ b/src/example/particle.cpp @@ -0,0 +1,103 @@ +#include <iostream> +#include <thread> +#include <chrono> +#include "SDLApp.h" +#include "api/ParticleEmitter.h" +#include "ParticleSystem.h" +#include "Particle.h" +#include <crepe/Component.h> +#include <crepe/ComponentManager.h> +#include <crepe/api/GameObject.h> +#include <chrono> + +using namespace crepe::api; +using namespace crepe; +using namespace std; + +const int WINDOW_WIDTH = 800; +const int WINDOW_HEIGHT = 600; + +int main(int argc, char* argv[]) { + SDLApp app(WINDOW_WIDTH, WINDOW_HEIGHT); + + if (!app.initialize()) { + cerr << "Failed to initialize SDLApp." << endl; + return 1; + } + + + GameObject * game_object[1]; + game_object[0] = new GameObject(0, "Name", "Tag", 0); + + + ParticleSystem particleSystem; + + unsigned int maxParticles = 100; // maximum number of particles + unsigned int emissionRate = 10; // particles created per second + unsigned int speed = 50; // base speed of particles + unsigned int speedOffset = 10; // random offset for particle speed + unsigned int angle = 270; // base angle of particle emission + unsigned int angleOffset = 30; // random offset for particle angle + float beginLifespan = 0.0f; // beginning lifespan of particles + float endLifespan = 6.0f; // ending lifespan of particles + + // Vector to hold all the emitters + // vector<ParticleEmitter> emitters; + game_object[0]->add_component<ParticleEmitter>(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset, beginLifespan, endLifespan); + + + + + // Loop to create 1000 emitters + // for (unsigned int i = 0; i < 1000; ++i) { + // ParticleEmitter emitter(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset, beginLifespan, endLifespan); + + // // Set a position for each emitter, modifying the position for demonstration + // emitter.m_position = {static_cast<float>(200 + (i % 100)), static_cast<float>(200 + (i / 100) * 10)}; // Adjust position for each emitter + + // emitters.push_back(emitter); // Add the emitter to the vector + // } + float deltaTime = 0.1f; + bool running = true; + cout << "start loop " << endl; + while (running) { + app.handleEvents(running); + + // Start timing + auto start = chrono::high_resolution_clock::now(); + + + // POC CODE + particleSystem.update(); + // POC CODE + + // End timing + auto end = chrono::high_resolution_clock::now(); + chrono::duration<float, milli> duration = end - start; // get duration in milliseconds + + cout << "Update took " << duration.count() << " ms" << endl; + app.clearScreen(); + + start = chrono::high_resolution_clock::now(); + // render particles using the drawSquare method from SDLApp + ComponentManager& mgr = ComponentManager::get_instance(); + std::vector<std::reference_wrapper<ParticleEmitter>> emitters = mgr.get_components_by_type<ParticleEmitter>(); + for (const ParticleEmitter& emitter : emitters) { + for (const Particle& particle : emitter.particles) { + if(particle.active)app.drawSquare(particle.position.x, particle.position.y, 5); // draw each particle + } + } + + + app.presentScreen(); + end = chrono::high_resolution_clock::now(); + duration = end - start; // get duration in milliseconds + + cout << "screen took " << duration.count() << " ms" << endl; + + this_thread::sleep_for(chrono::milliseconds(20)); // simulate ~50 FPS + } + + app.cleanUp(); + return 0; +} diff --git a/src/example/script.cpp b/src/example/script.cpp index cc585db..6e5563c 100644 --- a/src/example/script.cpp +++ b/src/example/script.cpp @@ -4,11 +4,11 @@ */ #include <crepe/ComponentManager.h> -#include <crepe/GameObject.h> #include <crepe/ScriptSystem.h> #include <crepe/util/log.h> #include <crepe/api/BehaviorScript.h> +#include <crepe/api/GameObject.h> #include <crepe/api/Script.h> using namespace crepe; diff --git a/src/readme.md b/src/readme.md index a4a71e7..a8ffc51 100644 --- a/src/readme.md +++ b/src/readme.md @@ -18,7 +18,8 @@ running the build command: $ ninja -C build test_main ``` -Each source file in the example/ folder corresponds to a CMake target as well: +Each source file in the example/ folder corresponds to a CMake target as well +(all examples can be built at once by specifying the `examples` target): ``` $ ninja -C build audio_internal components_internal |