From 094ce1806156e191ffb554d4e88e636421cb0242 Mon Sep 17 00:00:00 2001 From: jaroWMR Date: Mon, 7 Oct 2024 10:04:23 +0200 Subject: particle direction is random --- src/crepe/main.cpp | 82 +++++++++++++++++------------------------------------- 1 file changed, 26 insertions(+), 56 deletions(-) (limited to 'src/crepe/main.cpp') diff --git a/src/crepe/main.cpp b/src/crepe/main.cpp index 73d041c..fc307cc 100644 --- a/src/crepe/main.cpp +++ b/src/crepe/main.cpp @@ -1,83 +1,53 @@ #include #include #include -#include +#include "SDLApp.hpp" #include "ParticleEmitter.hpp" const int WINDOW_WIDTH = 800; const int WINDOW_HEIGHT = 600; -void renderParticles(SDL_Renderer* renderer, const ParticleEmitter& emitter) { - for (const auto& particle : emitter.particles) { - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - SDL_Rect rect = { particle.position.x, particle.position.y, 5, 5 }; - SDL_RenderFillRect(renderer, &rect); - } -} - int main(int argc, char* argv[]) { - - if (SDL_Init(SDL_INIT_VIDEO) != 0) { - std::cerr << "SDL Initialization Error: " << SDL_GetError() << std::endl; - return 1; - } + SDLApp app(WINDOW_WIDTH, WINDOW_HEIGHT); - SDL_Window* window = SDL_CreateWindow("Particle System", - SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, - WINDOW_WIDTH, - WINDOW_HEIGHT, - SDL_WINDOW_SHOWN); - if (!window) { - std::cerr << "Window Creation Error: " << SDL_GetError() << std::endl; - SDL_Quit(); + if (!app.initialize()) { + std::cerr << "Failed to initialize SDLApp." << std::endl; return 1; } - // Create SDL renderer - SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); - if (!renderer) { - std::cerr << "Renderer Creation Error: " << SDL_GetError() << std::endl; - SDL_DestroyWindow(window); - SDL_Quit(); - return 1; - } - ParticleEmitter emitter(100, 10.0f); - emitter.position = { 400, 300 }; - ParticleEmitter emitter2(100, 10.0f); - emitter2.position = { 600, 600 }; + 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 = 180; //base angle of particle emission + unsigned int angleOffset = 30; //random offset for particle angle - float deltaTime = 0.1f; - bool running = true; - SDL_Event event; - while (running) { + ParticleEmitter emitter(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset); - while (SDL_PollEvent(&event)) { - if (event.type == SDL_QUIT) { - running = false; - } - } + emitter.setPosition(400, 300); + + float deltaTime = 0.1f; + bool running = true; + while (running) { + app.handleEvents(running); emitter.update(deltaTime); - emitter2.update(deltaTime); - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); - SDL_RenderClear(renderer); + app.clearScreen(); - renderParticles(renderer, emitter); - renderParticles(renderer, emitter2); + //render particles using the drawSquare method + const std::vector& particles = emitter.getParticles(); + for (std::size_t i = 0; i < particles.size(); ++i) { + app.drawSquare(particles[i].position.x, particles[i].position.y, 5); + } - SDL_RenderPresent(renderer); + app.presentScreen(); - + std::this_thread::sleep_for(std::chrono::milliseconds(17)); } - - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); - SDL_Quit(); - + app.cleanUp(); return 0; } -- cgit v1.2.3