2 Replies - 2707 Views - Last Post: 24 February 2011 - 05:10 PM Rate Topic: -----

#1 Guest_x_5632*


Reputation:

C++ and SDL Particle Emitter

Posted 23 February 2011 - 03:03 PM

I'm writing a very simple 2d particle emitter in C++ and SDL and for some reason I cannot get it to work.

I've debugged it with breakpoints and stepped through, but I can't seem to figure out what's wrong.

When you click, the particles seem to continue to emit until you click somewhere else, and I can't figure out why.
Changing the lifetime of them seems to reduce the amount created, but changing the number of them doesn't.

Defenitions

Emitter.h
#ifndef _EMITTER_H_
#define _EMITTER_H_

#include "SDL.h"
#include "Particle.h"

class Emitter
{
public:
	Emitter();
	Emitter(int x, int y);
	~Emitter();

	void emit( int noOfParticles, float xDirection, float yDirection, int speed, int lifetime, bool repeat, SDL_Surface* image );
	void set_position( int x, int y );

	void update( int ticks );
	void draw( SDL_Surface* screen );

private:

	int x;
	int y;

	Particle* particles;
	int noOfParticles;
	bool repeat;
};

#endif



Particle.h
#ifndef _PARTICLE_H_
#define _PARTICLE_H_

#include "SDL.h"

class Particle
{
public:
	Particle();
	~Particle();

	void emit( int x, int y, float xDirection, float yDirection, int speed, int lifetime, SDL_Surface* image );
	void reemit();
	void draw( SDL_Surface* screen );
	void update( int ticks );

	bool is_dead();
private:
	int x;
	int y;

	int startX; // used for reemit
	int startY;
	float xDirection;
	float yDirection;
	int speed;
	int lifetime;

	int startTicks;
	bool dead;

	SDL_Surface* sprite;
};

#endif



The other files are pretty big so I've included them in spoilers to reduce the post size.

main.cpp
Spoiler


Emitter.cpp
Spoiler


Particle.cpp
Spoiler


Thanks in advance for any replies :)

Is This A Good Question/Topic? 0

Replies To: C++ and SDL Particle Emitter

#2 Guest_hype261*


Reputation:

Re: C++ and SDL Particle Emitter

Posted 24 February 2011 - 10:36 AM

View Postx_5632, on 23 February 2011 - 03:03 PM, said:

I'm writing a very simple 2d particle emitter in C++ and SDL and for some reason I cannot get it to work.

I've debugged it with breakpoints and stepped through, but I can't seem to figure out what's wrong.

When you click, the particles seem to continue to emit until you click somewhere else, and I can't figure out why.
Changing the lifetime of them seems to reduce the amount created, but changing the number of them doesn't.

Defenitions

Emitter.h
#ifndef _EMITTER_H_
#define _EMITTER_H_

#include "SDL.h"
#include "Particle.h"

class Emitter
{
public:
	Emitter();
	Emitter(int x, int y);
	~Emitter();

	void emit( int noOfParticles, float xDirection, float yDirection, int speed, int lifetime, bool repeat, SDL_Surface* image );
	void set_position( int x, int y );

	void update( int ticks );
	void draw( SDL_Surface* screen );

private:

	int x;
	int y;

	Particle* particles;
	int noOfParticles;
	bool repeat;
};

#endif



Particle.h
#ifndef _PARTICLE_H_
#define _PARTICLE_H_

#include "SDL.h"

class Particle
{
public:
	Particle();
	~Particle();

	void emit( int x, int y, float xDirection, float yDirection, int speed, int lifetime, SDL_Surface* image );
	void reemit();
	void draw( SDL_Surface* screen );
	void update( int ticks );

	bool is_dead();
private:
	int x;
	int y;

	int startX; // used for reemit
	int startY;
	float xDirection;
	float yDirection;
	int speed;
	int lifetime;

	int startTicks;
	bool dead;

	SDL_Surface* sprite;
};

#endif



The other files are pretty big so I've included them in spoilers to reduce the post size.

main.cpp
Spoiler


Emitter.cpp
Spoiler


Particle.cpp
Spoiler


Thanks in advance for any replies :)



The problem is in your main.cpp

if ( tEvent.button.button == SDL_BUTTON_LEFT )
				{
					// left mouse click
					int x = tEvent.button.x;
					int y = tEvent.button.y;

					emitter = new Emitter( x, y );
					emitter->emit( 1, 1, 1, 1, 10000000000, false, particle_image ); //Emit the particles at the point clicked
					printf( "Emitted at (%i, %i)\n", x, y );
				}




When you click the left button your pointer emitter is now pointing to a new Emitter and the old Emitter value is now lost. You are also leaking memory here. If you want to display multiple emitters you should use some sort of data structure to store the emitters you have created ie vector.
Was This Post Helpful? 0

#3 Guest_x_5631*


Reputation:

Re: C++ and SDL Particle Emitter

Posted 24 February 2011 - 05:10 PM

Thanks for the reply.
I actually didn't want to have more than one emitter, I wanted to have the "old" one destroyed and replaced with the new one each time the user clicks.
Thanks for pointing out the memory leak though, I'll make sure I delete the emitter before creating another now :)

However, I'm still having the problem with the particles continuing to be created non-stop until a new emitter is created.
:(
Was This Post Helpful? 0

Page 1 of 1