Jump to content
  • Advertisement
Sign in to follow this  
Phineas

2D Special Effects with a Particle System

This topic is 4583 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

After studying 2D special affects and particle systems, I came up with a class called ParticleSystem. Below is the code for ParticleSystem, as well as derived class FireEffect. Here is an example program using these classes. The API used for graphics is Allegro and the fblend add-on library. I'm afraid the design will prevent me from creating advanced effects like clouds and wormholes (I know nothing about these). How can I improve upon this design? Thanks for your help. particle2d.h
///////////////////////////////////////////////////////////
/*
    Filename:		particle2d.h

    Description:	2D particle system
*/
///////////////////////////////////////////////////////////
//
#ifndef PARTICLE2D_H
#define PARTICLE2D_H
//
//  Header Files  /////////////////////////////////////////
//
#include "allegro.h"

#include "vector2d.h" // personal Vector2D struct and functions
//
//  Structs  //////////////////////////////////////////////
//
struct Particle2D
{
	Vector2D pos;
	Vector2D vel;
	int color;
	int life;
	int alpha;
};
//
//  Base Classes  /////////////////////////////////////////
//
class ParticleSystem
{
public:
	ParticleSystem(Vector2D position, int particles);
	virtual ~ParticleSystem();

	void setPos(Vector2D position);
	// Set emitter position

	virtual void reset() = 0;
	virtual void update() = 0;
	virtual void draw(BITMAP *bmp) = 0;
	// Core functionality

protected:
	Vector2D pos;  // Position of emitter
	Particle2D *p; // Array of Particle2D's
	int size;      // Number of items in array
};
//
//  Derived Classes  //////////////////////////////////////
//
class FireEffect : public ParticleSystem
{
public:
	FireEffect(Vector2D position, int particles) : ParticleSystem(position, particles) { reset(); }

	void reset();
	void update();
	void draw(BITMAP *bmp);

private:
	void reset(Particle2D &particle);
	// Reset individual particles
};
//
///////////////////////////////////////////////////////////
//
#endif
//





particle2d.cpp
///////////////////////////////////////////////////////////
/*
    Filename:		particle2d.cpp

    Description:	2D particle system
*/
//  Header Files  /////////////////////////////////////////
//
#include "particle2d.h"

#include "allegro.h"
#include "vector2d.h"
#include "common.h"
#include "fblend.h"
//
//  ParticleSystem  ///////////////////////////////////////
//
ParticleSystem::ParticleSystem(Vector2D position, int particles)
{
	pos  = position;
	size = particles;

	p = new Particle2D[particles];
}

ParticleSystem::~ParticleSystem()
{
	delete [] p;
}

void ParticleSystem::setPos(Vector2D position)
{
	pos = position;
}
//
//  FireEffect  ///////////////////////////////////////////
//
void FireEffect::reset()
{
	int i;

	for (i = 0; i < size; i++)
	{
		reset(p);
	}
}

void FireEffect::update()
{
	int i;

	for (i = 0; i < size; i++)
	{
		// Resurrect particles if dead
		if (p.life < 0)
		{
			reset(p);
		}

		// Update position
		p.pos += p.vel;

		// Change color attributes
		p.color = makecol(255, 50+(p.life/50.0*200), 0);
		p.alpha -= 5;

		// Life is short :(
		p.life--;
	}
}

void FireEffect::draw(BITMAP *bmp)
{
	int i;

	for (i = 0; i < size; i++)
	{
		fblend_rect_trans(bmp, p.pos.x, p.pos.y, p.life/50.0*4, p.life/50.0*4, p.color, p.alpha);
	}
}

void FireEffect::reset(Particle2D &particle)
{
	particle.pos   = Vector2D( random(pos.x-2, pos.x+2), random(pos.y-2, pos.y+2) );
	particle.vel   = Vector2D( randomSign() * random(0, 10)/10.0, randomSign() * random(0, 10)/10.0 );
	particle.color = makecol(255, 255, 0);
	particle.life  = random(20, 50);
	particle.alpha = 255;
}
//





Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!