From 95e874e74e9710c1af70118e71f1b39a018a82d2 Mon Sep 17 00:00:00 2001 From: Niklas Birk Date: Tue, 17 Dec 2019 22:29:36 +0100 Subject: [PATCH] Init particle creation --- CMakeLists.txt | 2 +- main.c | 61 ++++++++++++++++++++++++++++++++++++++++++++- particlesystem.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ particlesystem.h | 55 +++++++++++++++++++++++++++------------- 4 files changed, 164 insertions(+), 19 deletions(-) create mode 100644 particlesystem.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 27462cd..8b2f283 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,4 @@ project(Informatikprojekt C) set(CMAKE_C_STANDARD 11) -add_executable(Informatikprojekt main.c particlesystem.h) \ No newline at end of file +add_executable(Informatikprojekt main.c particlesystem.h particlesystem.c) \ No newline at end of file diff --git a/main.c b/main.c index 7d38eaf..07b2934 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,66 @@ #include +#include +#include +#include "particlesystem.h" + +char *printVector(vector3f *v); +void printParticle(particle *v); +void printEmitter(emitter *e); +void initRandomParticles(emitter *e); int main() { - printf("Hello, World!\n"); + vector3f *epos = initVector3f(0, 0, 0); + emitter *e = initEmitter(epos, 10); + + initRandomParticles(e); + + printEmitter(e); + + for (int i = 0; i < e->pamount; i++) + { + free((e->particles)[i]); + } + + free(epos); + free(e); return 0; } + +void initRandomParticles(emitter *e) +{ + for (int i = 0; i < e->pamount; i++) + { + vector3f *pos = initVector3f(e->position->x, e->position->y, e->position->z); + vector3f *dir = initVector3f((float) rand() / RAND_MAX, (float) rand() / RAND_MAX, (float) rand() / RAND_MAX); + (e->particles)[i] = initParticle(pos, dir); + } +} + +char *printVector(vector3f *v) +{ + char *c = malloc(100); + sprintf(c, "(%f, %f, %f)", v->x, v->y, v->z); + return c; +} + +void printParticle(particle *v) +{ + printf(" Particle {\n"); + printf(" position = %s", printVector(v->position)); + printf("\n direction = %s", printVector(v->direction)); + printf("\n }"); +} + +void printEmitter(emitter *e) +{ + printf("Emitter {\n"); + + for (int i = 0; i < e->pamount; i++) + { + printParticle((e->particles)[i]); + printf("\n"); + } + + printf("\n}"); +} diff --git a/particlesystem.c b/particlesystem.c new file mode 100644 index 0000000..0008f6c --- /dev/null +++ b/particlesystem.c @@ -0,0 +1,65 @@ +#include "particlesystem.h" +#include + +/* + * Initializes a particle + */ +particle *initParticle(vector3f *pos, vector3f *dir) +{ + particle *p = malloc(sizeof(particle)); + p->position = pos; + p->direction = dir; + return p; +} + +/* + * Initializes an emitter + */ +emitter *initEmitter(vector3f *pos, int pamount) +{ + emitter *e = malloc(sizeof(emitter)); + e->position = pos; + e->particles = malloc(sizeof(particle *) * pamount); + e->pamount = pamount; + return e; +} + +/* + * Initializes a particle system + */ +particle_system *initParticleSystem(int eamount) +{ + particle_system *ps = malloc(sizeof(particle_system)); + ps->emitters = malloc(sizeof(emitter *) * eamount); + ps->eamount = eamount; + return ps; +} + +/* + * Updates particles + */ +int updateParticles(float dt, particle_system *particleSystem) +{ + +} + +/* + * Draws particles + */ +int drawParticles(particle_system *particleSystem) +{ + +} + +/* + * Initializes a vector + * For that it allocates memory that must be freed later + */ +vector3f *initVector3f(float x, float y, float z) +{ + vector3f *vector = malloc(sizeof(vector3f)); + vector->x = x; + vector->y = y; + vector->z = z; + return vector; +} \ No newline at end of file diff --git a/particlesystem.h b/particlesystem.h index 3256cf7..5f99776 100644 --- a/particlesystem.h +++ b/particlesystem.h @@ -1,49 +1,70 @@ /* * A vector with three floats */ -struct vector3f +typedef struct vector3f { float x; float y; float z; -}; +} vector3f; /* * A particle has a position and a direction */ -struct particle +typedef struct particle { - struct vector3f position; - struct vector3f direction; -}; + vector3f *position; + vector3f *direction; +} particle; /* * An emitter has a position and contains an array of particles */ -struct emitter +typedef struct emitter { - struct vector3f position; - struct particle *particles; + vector3f *position; + particle **particles; int pamount; -}; +} emitter; /* * A particle system consists of one or more emitter */ -struct particle_system +typedef struct particle_system { - struct emitter *emitters; + emitter **emitters; int eamount; -}; +} particle_system; /* - * + * Initializes a particle */ -int initParticle(struct vector3f pos, struct vector3f dir); +particle *initParticle(vector3f *pos, vector3f *dir); /* - * + * Initializes an emitter */ -int initVector3f(int x, int y, int z); +emitter *initEmitter(vector3f *pos, int pamount); + +/* + * Initializes a particle system + */ +particle_system *initParticleSystem(int eamount); + +/* + * Updates particle + */ +int updateParticles(float dt, particle_system *particleSystem); + +/* + * Updates particle + */ +int drawParticles(particle_system *particleSystem); + +/* + * Initializes a vector + * For that it allocates memory that must be freed later + */ +vector3f *initVector3f(float x, float y, float z);