32blit / 32blit-sdk

32blit SDK
https://32blit.com
MIT License
191 stars 67 forks source link

How does/should ParticleGenerator work? #354

Open Gadgetoid opened 4 years ago

Gadgetoid commented 4 years ago

Looking over ParticleGenerator and the particle example reveal several wrinkles that I think should be fixed.

Perhaps I'm overthinking the role of the particle generator here, but presently in the particle example it seems almost completely unused.

ParticleGenerator

It feels like ParticleGenerator should handle expiring old particles and calling the necessary function to generate new ones, but then call out to an anonymous function for the "update remaining particles" portion of its functionality.

Perhaps update should be update_particle and render should be render_particle and these user functions can then only have to worry about operating upon a single particle:

    // update remaining particles
    for (auto p : particles) {
      update_particle(p);  // Call out to user supplied function
      p->age_ms += elapsed_ms;
      p->age = p->age_ms / float(lifetime_ms);
    }

Particle Example

I would expect the code to feel something like this:

struct RainParticle : Particle {
    float wetness;

    rain_particle(Vec2 pos, Vec2 vel, float wetness) : Particle(pos, vel), wetness(wetness) {};
}

void update_rain_particle(RainParticle p){
    // Update physics nonsense
}

void render_rain_particle(RainParticle p){
    // Draw stuff
}

RainParticle generate_rain_particle(){
    return new RainParticle(
        // THIIIIIIIIIIIINGGGGGGGGGGGGGSSSSSSSSSSSS
    );
}

ParticleGenerator rain(250, 4000, generate_rain_particle, update_rain_particle, render_rain_particle);

void render() {
    rain.render();
}

void update(uint32_t time_ms) {
    rain.update(); // Possibly managed by engine, like Timers/Tweens?
}
Daft-Freak commented 3 years ago

Looks like the particle example has two copies of rain/smoke, one using the generator and one not (and the generator ones aren't used).

generator:

no generator: