diff options
| author | jaroWMR <jarorutjes07@gmail.com> | 2024-10-01 19:10:24 +0200 | 
|---|---|---|
| committer | jaroWMR <jarorutjes07@gmail.com> | 2024-10-01 19:10:24 +0200 | 
| commit | bd24f87e18bef29679023ee9a60028f2afdbb470 (patch) | |
| tree | aa17d05db8ba6d5f959e676aa2c51c67743108b3 /src | |
| parent | a78f7bbfcdabad9550afe22f615b973b92cb074f (diff) | |
particle POC
Diffstat (limited to 'src')
| -rw-r--r-- | src/CMakeLists.txt | 7 | ||||
| -rw-r--r-- | src/crepe/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | src/crepe/Particle.cpp | 14 | ||||
| -rw-r--r-- | src/crepe/Particle.hpp | 19 | ||||
| -rw-r--r-- | src/crepe/ParticleEmitter.cpp | 40 | ||||
| -rw-r--r-- | src/crepe/ParticleEmitter.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/crepe/main.cpp | 84 | 
11 files changed, 394 insertions, 5 deletions
| diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0090188..b8ed359 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,13 +4,16 @@ 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)  project(crepe C CXX) +find_package(SDL2 REQUIRED) +include_directories(${SDL2_INCLUDE_DIRS}) +  add_executable(main) -add_subdirectory(crepe) +target_link_libraries(main ${SDL2_LIBRARIES}) +add_subdirectory(crepe)
\ No newline at end of file diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt index 392b7d7..c825529 100644 --- a/src/crepe/CMakeLists.txt +++ b/src/crepe/CMakeLists.txt @@ -1,3 +1,7 @@  target_sources(main PUBLIC -	main.cpp +    main.cpp +	Particle.cpp +	ParticleEmitter.cpp  ) + +target_include_directories(main PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/crepe/Particle.cpp b/src/crepe/Particle.cpp new file mode 100644 index 0000000..90957db --- /dev/null +++ b/src/crepe/Particle.cpp @@ -0,0 +1,14 @@ +#include "Particle.hpp" + +Particle::Particle(float lifespan, Position position, Position velocity) +    : lifespan(lifespan), position(position), velocity(velocity), timeInLife(0.0f) {} + +void Particle::update(float deltaTime) { +    timeInLife += deltaTime; +    position.x += static_cast<int>(velocity.x * deltaTime); +    position.y += static_cast<int>(velocity.y * deltaTime); +} + +bool Particle::isAlive() const { +    return timeInLife < lifespan; +} diff --git a/src/crepe/Particle.hpp b/src/crepe/Particle.hpp new file mode 100644 index 0000000..f8d2770 --- /dev/null +++ b/src/crepe/Particle.hpp @@ -0,0 +1,19 @@ +#pragma once + +class Particle { +public: +    struct Position { +        int x; +        int y; +    }; + +    Position position; +    Position velocity; +    float lifespan; + +    Particle(float lifespan, Position position, Position velocity); +    void update(float deltaTime); +    bool isAlive() const; +private: +    float timeInLife; +}; diff --git a/src/crepe/ParticleEmitter.cpp b/src/crepe/ParticleEmitter.cpp new file mode 100644 index 0000000..13aaeae --- /dev/null +++ b/src/crepe/ParticleEmitter.cpp @@ -0,0 +1,40 @@ +#include "ParticleEmitter.hpp" + +ParticleEmitter::ParticleEmitter(int maxParticles, float emissionRate) +    : maxParticles(maxParticles), emissionRate(emissionRate), elapsedTime(0.0f) { +    position = { 0, 0 };   +    std::srand(static_cast<unsigned int>(std::time(nullptr))); / +} + +void ParticleEmitter::update(float deltaTime) { +    elapsedTime += deltaTime; + +    while (elapsedTime >= (1.0f / emissionRate) && particles.size() < maxParticles) { +        elapsedTime -= (1.0f / emissionRate); +        emitParticle(); +    } + +    for (auto it = particles.begin(); it != particles.end();) { +        it->update(deltaTime); +        if (!it->isAlive()) { +            it = particles.erase(it);  +        } else { +            ++it; +        } +    } +} + +void ParticleEmitter::emitParticle() { +    Particle::Position initialPosition = { position.x, position.y }; +    Particle::Position initialVelocity = { +        static_cast<int>(randFloat(-50.0f, 50.0f, 10.0f, 100.0f)),  +        static_cast<int>(randFloat(-50.0f, 50.0f, 10.0f, 100.0f)) +    }; +    particles.emplace_back(2.0f, initialPosition, initialVelocity);  +} + +float ParticleEmitter::randFloat(float minangle, float maxangle, float minVel, float maxVel) { +    float angle = static_cast<float>(rand()) / RAND_MAX * (maxangle - minangle) + minangle; +    float velocity = static_cast<float>(rand()) / RAND_MAX * (maxVel - minVel) + minVel; +    return velocity * std::cos(angle);  +} diff --git a/src/crepe/ParticleEmitter.hpp b/src/crepe/ParticleEmitter.hpp new file mode 100644 index 0000000..682a2ae --- /dev/null +++ b/src/crepe/ParticleEmitter.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include <vector> +#include <cstdlib>   +#include <ctime>    +#include "Particle.hpp" +#include <cmath> + +class ParticleEmitter { +public: +    std::vector<Particle> particles; + +    struct Position { +        int x; +        int y; +    } position; + +    int maxParticles; +    float emissionRate; +    float elapsedTime; + +    ParticleEmitter(int maxParticles, float emissionRate); +    void update(float deltaTime); + +private: +    void emitParticle(); +    float randFloat(float minangle, float maxangle, float minVel, float maxVel); +}; 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/main.cpp b/src/crepe/main.cpp index 8e9a184..73d041c 100644 --- a/src/crepe/main.cpp +++ b/src/crepe/main.cpp @@ -1,3 +1,83 @@ -#include <stdio.h> +#include <iostream> +#include <thread> +#include <chrono> +#include <SDL.h> +#include "ParticleEmitter.hpp" -int main() { printf("Hello World!\n"); } +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; +    } + +    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(); +        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 };  + +    float deltaTime = 0.1f; + +    bool running = true; +    SDL_Event event; +    while (running) { + +        while (SDL_PollEvent(&event)) { +            if (event.type == SDL_QUIT) { +                running = false; +            } +        } + +        emitter.update(deltaTime); +        emitter2.update(deltaTime); + +        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);  +        SDL_RenderClear(renderer); + +        renderParticles(renderer, emitter); +        renderParticles(renderer, emitter2); + +        SDL_RenderPresent(renderer); + +         +    } + +   +    SDL_DestroyRenderer(renderer); +    SDL_DestroyWindow(window); +    SDL_Quit(); + +    return 0; +} |