diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/crepe/CMakeLists.txt | 9 | ||||
-rw-r--r-- | src/crepe/Particle.cpp | 25 | ||||
-rw-r--r-- | src/crepe/Particle.h | 24 | ||||
-rw-r--r-- | src/crepe/ParticleEmitter.cpp | 28 | ||||
-rw-r--r-- | src/crepe/ParticleEmitter.h | 32 | ||||
-rw-r--r-- | src/crepe/ParticleSystem.cpp | 59 | ||||
-rw-r--r-- | src/crepe/ParticleSystem.h | 19 | ||||
-rw-r--r-- | src/crepe/SDLApp.cpp | 78 | ||||
-rw-r--r-- | src/crepe/SDLApp.h | 28 | ||||
-rw-r--r-- | src/crepe/Transform.cpp | 6 | ||||
-rw-r--r-- | src/crepe/Transform.h | 16 | ||||
-rw-r--r-- | src/crepe/crepe/CMakeFiles/CMakeDirectoryInformation.cmake | 16 | ||||
-rw-r--r-- | src/crepe/crepe/CMakeFiles/progress.marks | 1 | ||||
-rw-r--r-- | src/crepe/crepe/Makefile | 140 | ||||
-rw-r--r-- | src/crepe/crepe/cmake_install.cmake | 44 | ||||
-rw-r--r-- | src/crepe/util/log.h | 2 | ||||
-rw-r--r-- | src/example/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/example/components_internal.cpp | 4 | ||||
-rw-r--r-- | src/example/particle.cpp | 102 |
20 files changed, 632 insertions, 5 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 09c60bd..446433c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -36,4 +36,3 @@ target_link_libraries(test_main PRIVATE gtest_main PUBLIC crepe ) - diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index d85aef0..399200b 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -1,7 +1,12 @@ + target_sources(crepe PUBLIC Asset.cpp Sound.cpp SoundContext.cpp + Particle.cpp + ParticleEmitter.cpp + ParticleSystem.cpp + SDLApp.cpp ComponentManager.cpp Component.cpp GameObject.cpp @@ -16,6 +21,10 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Asset.h Sound.h SoundContext.h + ParticleEmitter.h + ParticleSystem.h + Particle.h + SDLApp.h ComponentManager.h ComponentManager.hpp Component.h 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/ParticleEmitter.cpp b/src/crepe/ParticleEmitter.cpp new file mode 100644 index 0000000..30cba7c --- /dev/null +++ b/src/crepe/ParticleEmitter.cpp @@ -0,0 +1,28 @@ +#include "ParticleEmitter.h" +#include <ctime> +#include "Particle.h" +#include <iostream> + +using namespace crepe; + +ParticleEmitter::ParticleEmitter(uint32_t maxParticles, uint32_t emissionRate, uint32_t speed, uint32_t speedOffset, uint32_t angle, uint32_t angleOffset, float m_beginLifespan, float m_endLifespan) + : 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/ParticleEmitter.h b/src/crepe/ParticleEmitter.h new file mode 100644 index 0000000..477f492 --- /dev/null +++ b/src/crepe/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 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/ParticleSystem.cpp b/src/crepe/ParticleSystem.cpp new file mode 100644 index 0000000..0dad4cd --- /dev/null +++ b/src/crepe/ParticleSystem.cpp @@ -0,0 +1,59 @@ +#include "ParticleSystem.h" +#include <cmath> +#include <ctime> +#include "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..c8777f5 --- /dev/null +++ b/src/crepe/ParticleSystem.h @@ -0,0 +1,19 @@ +#pragma once + +#include <vector> +#include "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/SDLApp.cpp b/src/crepe/SDLApp.cpp new file mode 100644 index 0000000..715dd6f --- /dev/null +++ b/src/crepe/SDLApp.cpp @@ -0,0 +1,78 @@ +#include "SDLApp.h" +#include <iostream> +#include <vector> +#include "Particle.h" +#include "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..8915d30 --- /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 "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/Transform.cpp b/src/crepe/Transform.cpp new file mode 100644 index 0000000..0e4b0cf --- /dev/null +++ b/src/crepe/Transform.cpp @@ -0,0 +1,6 @@ +#include "Transform.h" + +using namespace crepe; + +Transform::Transform(int mass, int gravityScale, int bodyType) + : mass(mass), gravity_scale(gravityScale), body_type(bodyType) {} diff --git a/src/crepe/Transform.h b/src/crepe/Transform.h new file mode 100644 index 0000000..c72ebc4 --- /dev/null +++ b/src/crepe/Transform.h @@ -0,0 +1,16 @@ +#pragma once + +#include "Component.h" + +namespace crepe { + +class Transform : public Component { +public: + Transform(int mass, int gravityScale, int bodyType); + + int mass; + int gravity_scale; + int body_type; +}; + +} // namespace crepe 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 2b0fbe1..763b314 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)" 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 6df4ce7..cbf8e31 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -15,4 +15,5 @@ endfunction() add_example(audio_internal) add_example(components_internal) add_example(script) - +add_example(particle) +target_link_libraries(particle PUBLIC SDL2) diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp index 54ce295..3ba5b5a 100644 --- a/src/example/components_internal.cpp +++ b/src/example/components_internal.cpp @@ -39,8 +39,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..df1dbfd --- /dev/null +++ b/src/example/particle.cpp @@ -0,0 +1,102 @@ +#include <iostream> +#include <thread> +#include <chrono> +#include "SDLApp.h" +#include "ParticleEmitter.h" +#include "ParticleSystem.h" +#include "Particle.h" +#include <crepe/Component.h> +#include <crepe/ComponentManager.h> +#include <crepe/GameObject.h> +#include <chrono> + +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; +} |