diff options
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/CMakeLists.txt | 10 | ||||
-rw-r--r-- | src/crepe/Particle.cpp | 23 | ||||
-rw-r--r-- | src/crepe/Particle.hpp | 20 | ||||
-rw-r--r-- | src/crepe/ParticleEmitter.cpp | 24 | ||||
-rw-r--r-- | src/crepe/ParticleEmitter.hpp | 25 | ||||
-rw-r--r-- | src/crepe/ParticleSystem.cpp | 77 | ||||
-rw-r--r-- | src/crepe/ParticleSystem.hpp | 14 | ||||
-rw-r--r-- | src/crepe/SDLApp.cpp | 78 | ||||
-rw-r--r-- | src/crepe/SDLApp.hpp | 28 | ||||
-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/example/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/example/particel.cpp | 82 |
16 files changed, 586 insertions, 1 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 62ca9a0..f11402a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,7 +4,6 @@ set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 20) set(CMAKE_EXPORT_COMPILE_COMMANDS 1) -# enable debug features set(CMAKE_BUILD_TYPE Debug) add_compile_definitions(DEBUG) @@ -39,3 +38,4 @@ target_link_libraries(test_main PUBLIC crepe ) +add_subdirectory(crepe)
\ No newline at end of file diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 208ba1f..c7f18cd 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -1,13 +1,23 @@ + target_sources(crepe PUBLIC Asset.cpp Sound.cpp SoundContext.cpp + main.cpp + Particle.cpp + ParticleEmitter.cpp + ParticleSystem.cpp + SDLApp.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES Asset.h Sound.h SoundContext.h + ParticleEmitter.hpp + ParticleSystem.hpp + Particle.hpp + SDLApp.hpp ) add_subdirectory(api) diff --git a/src/crepe/Particle.cpp b/src/crepe/Particle.cpp new file mode 100644 index 0000000..8cf7f7e --- /dev/null +++ b/src/crepe/Particle.cpp @@ -0,0 +1,23 @@ +#include "Particle.hpp" +#include <iostream> + +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.hpp b/src/crepe/Particle.hpp new file mode 100644 index 0000000..669a8ab --- /dev/null +++ b/src/crepe/Particle.hpp @@ -0,0 +1,20 @@ +#pragma once + +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..3c9cc4e --- /dev/null +++ b/src/crepe/ParticleEmitter.cpp @@ -0,0 +1,24 @@ +#include "ParticleEmitter.hpp" +#include <ctime> +#include "Particle.hpp" +#include <iostream> + +ParticleEmitter::ParticleEmitter(unsigned int maxParticles, unsigned int emissionRate, unsigned int speed, unsigned int speedOffset, unsigned int angle, unsigned int 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<unsigned int>(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 + 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.hpp b/src/crepe/ParticleEmitter.hpp new file mode 100644 index 0000000..5de5e1e --- /dev/null +++ b/src/crepe/ParticleEmitter.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include <vector> +#include "Particle.hpp" + + + +class ParticleEmitter { +public: + ParticleEmitter(unsigned int maxParticles, unsigned int emissionRate, unsigned int speed, unsigned int speedOffset, unsigned int angle, unsigned int angleOffset,float m_beginLifespan,float m_endLifespan); + ~ParticleEmitter(); + + Position m_position; //position of the emitter + unsigned int m_maxParticles; //maximum number of particles + unsigned int m_emissionRate; //rate of particle emission + unsigned int m_speed; //base speed of the particles + unsigned int m_speedOffset; //offset for random speed variation + unsigned int m_minAngle; //min angle of particle emission + unsigned int 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..aff7a30 --- /dev/null +++ b/src/crepe/ParticleSystem.cpp @@ -0,0 +1,77 @@ +#include "ParticleSystem.hpp" +#include <cmath> +// #include <cstdlib> +#include <ctime> +#include <iostream> // include iostream for std::cout +#include "ParticleEmitter.hpp" +#include "Particle.hpp" + +ParticleSystem::ParticleSystem() : m_elapsedTime(0.0f) {} // Initialize m_elapsedTime to 0 + +void ParticleSystem::update(float deltaTime, std::vector<ParticleEmitter>& emitters) { + // std::cout << "ParticleSystem update" << std::endl; + for (ParticleEmitter& emitter : emitters) { + float updateAmount = 1/static_cast<float>(emitter.m_emissionRate); + for (float i = 0; i < deltaTime; i += updateAmount) + { + emitParticle(emitter); + } + // std::cout << "after emit" << std::endl; + + //update/move particles afterwards delete if not alive. + for (size_t j = 0; j < emitter.particles.size(); j++) + { + // std::cout << "update" << std::endl; + if(emitter.particles[j].active) + { + emitter.particles[j].update(deltaTime); + } + } + + + } +} + +void ParticleSystem::emitParticle(ParticleEmitter& emitter) { + // std::cout << "new emitter:" << std::endl; + Position initialPosition = { emitter.m_position.x, emitter.m_position.y }; + float randomAngle = 0.0f; + //check if value is overthe 360 degrees + if(emitter.m_maxAngle < emitter.m_minAngle) + { + randomAngle = ((emitter.m_minAngle + (std::rand() % (static_cast<int>(emitter.m_maxAngle + 360 - emitter.m_minAngle + 1))))%360); + } + else + { + randomAngle = emitter.m_minAngle + (std::rand() % (static_cast<int>(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 + }; + // std::cout << "emitter.m_endLifespan:" << emitter.m_endLifespan << std::endl; + for (size_t i = 0; i < emitter.particles.size(); i++) + { + if(!emitter.particles[i].active) + { + // std::cout << "active " << emitter.particles[i].active << std::endl; + // std::cout << "lifespan " << emitter.particles[i].lifespan << std::endl; + // std::cout << "timeInLife " << emitter.particles[i].timeInLife << std::endl; + // std::cout << "emitter.m_endLifespan" << emitter.m_endLifespan << std::endl; + // std::cout << "initialPositionx" << initialPosition.x << std::endl; + // std::cout << "initialPositiony" << initialPosition.y << std::endl; + // std::cout << "initialVelocityx" << initialVelocity.x << std::endl; + // std::cout << "initialVelocityy" << initialVelocity.y << std::endl; + emitter.particles[i].reset(emitter.m_endLifespan, initialPosition, initialVelocity); + break; + } + } + + //emitter.particles.emplace_back(emitter.m_endLifespan, initialPosition, initialVelocity); +} diff --git a/src/crepe/ParticleSystem.hpp b/src/crepe/ParticleSystem.hpp new file mode 100644 index 0000000..f670415 --- /dev/null +++ b/src/crepe/ParticleSystem.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include <vector> +#include "ParticleEmitter.hpp" + +class ParticleSystem { +public: + ParticleSystem(); + void update(float deltaTime, std::vector<ParticleEmitter>& emitters); +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..0779af1 --- /dev/null +++ b/src/crepe/SDLApp.cpp @@ -0,0 +1,78 @@ +#include "SDLApp.hpp" +#include <iostream> +#include <vector> +#include "Particle.hpp" +#include "ParticleEmitter.hpp" + +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.hpp b/src/crepe/SDLApp.hpp new file mode 100644 index 0000000..f95d4bc --- /dev/null +++ b/src/crepe/SDLApp.hpp @@ -0,0 +1,28 @@ +#ifndef SDLAPP_HPP +#define SDLAPP_HPP + +#include <SDL2/SDL.h> +#include "Particle.hpp" +#include "ParticleEmitter.hpp" + +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<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/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/example/CMakeLists.txt b/src/example/CMakeLists.txt index bcc9271..17e995b 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -1,3 +1,6 @@ add_executable(audio_internal EXCLUDE_FROM_ALL audio_internal.cpp) target_link_libraries(audio_internal PUBLIC crepe) +add_executable(particel EXCLUDE_FROM_ALL particel.cpp) +target_link_libraries(particel PUBLIC crepe) + diff --git a/src/example/particel.cpp b/src/example/particel.cpp new file mode 100644 index 0000000..58480a9 --- /dev/null +++ b/src/example/particel.cpp @@ -0,0 +1,82 @@ +#include <iostream> +#include <thread> +#include <chrono> +#include "SDLApp.hpp" +#include "ParticleEmitter.hpp" +#include "ParticleSystem.hpp" +#include "Particle.hpp" +#include <chrono> + +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()) { + std::cerr << "Failed to initialize SDLApp." << std::endl; + return 1; + } + + ParticleSystem particleSystem; + + unsigned int maxParticles = 100; // maximum number of particles + unsigned int emissionRate = 1; // particles created per second + unsigned int speed = 50; // base speed of particles + unsigned int speedOffset = 10; // random offset for particle speed + unsigned int angle = 90; // base angle of particle emission + unsigned int angleOffset = 30; // random offset for particle angle + float beginLifespan = 0.0f; // beginning lifespan of particles + float endLifespan = 2.0f; // ending lifespan of particles + + // Vector to hold all the emitters + std::vector<ParticleEmitter> emitters; + + // 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; + std::cout << "start loop " << std::endl; + while (running) { + app.handleEvents(running); + + // Start timing + auto start = std::chrono::high_resolution_clock::now(); + + particleSystem.update(deltaTime, emitters); // update particle system with delta time and emitters + + // End timing + auto end = std::chrono::high_resolution_clock::now(); + std::chrono::duration<float, std::milli> duration = end - start; // get duration in milliseconds + + std::cout << "Update took " << duration.count() << " ms" << std::endl; + app.clearScreen(); + + start = std::chrono::high_resolution_clock::now(); + // render particles using the drawSquare method from SDLApp + 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 = std::chrono::high_resolution_clock::now(); + duration = end - start; // get duration in milliseconds + + std::cout << "screen took " << duration.count() << " ms" << std::endl; + + std::this_thread::sleep_for(std::chrono::milliseconds(20)); // simulate ~50 FPS + } + + app.cleanUp(); + return 0; +} |