Jump to content
  • Advertisement
Sign in to follow this  
WWakerFAN

random freezing

This topic is 4815 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

emitter add_particle(emitter emit) {
	particle* temp;
	temp = new particle[emit.partNum+1];
	for(int i=0;i<emit.partNum;i++)
		temp = emit.particles;
	delete [] emit.particles;
	emit.particles = temp;
	emit.particles[emit.partNum].x=emit.x+int(emit.width * float(rand())/float(RAND_MAX))-emit.width/2;
	emit.particles[emit.partNum].y=emit.y+int(emit.height * float(rand())/float(RAND_MAX))-emit.height/2;
	emit.particles[emit.partNum].speed= emit.speed_min + (emit.speed_max-emit.speed_min) * float(rand())/float(RAND_MAX);
	emit.particles[emit.partNum].fade = emit.fade_min + (emit.fade_max-emit.fade_min) * float(rand())/float(RAND_MAX);
	emit.particles[emit.partNum].size = emit.size_min + (emit.size_max-emit.size_min) * float(rand())/float(RAND_MAX);
	emit.particles[emit.partNum].direction = emit.angle_min + (emit.angle_max-emit.angle_min) * float(rand())/float(RAND_MAX);
	emit.particles[emit.partNum].life = 0;

	emit.particles[emit.partNum].r=float(rand())/float(RAND_MAX);
	emit.particles[emit.partNum].g=float(rand())/float(RAND_MAX);
	emit.particles[emit.partNum].b=float(rand())/float(RAND_MAX);
	emit.partNum++;
	return emit;
}
emitter destroy_particle(emitter emit,GLint partNum) {
	particle* temp;
	temp = new particle[emit.partNum-1];
	for(int i=0;i<partNum;i++) {
		temp = emit.particles;
	}

	for(i=partNum+1;i<emit.partNum;i++) {
		temp[i-1] = emit.particles;
	}

	delete [] emit.particles;
	emit.particles = temp;
	emit.partNum--;
	return emit;
}
i have a feeling it might have something to do with those 2 functions and the way i'm handling the memory in them

Share this post


Link to post
Share on other sites
Advertisement
Hmm, the functions will take more and more time, if the number of particles is large. You should fix the size of the array at the beginning, but handle only
the 'living' particles, i.e. you allocate space for all particles, but keep the number of currently active parameters to process them ...

Share this post


Link to post
Share on other sites
You may also want to try sending emmiter pointers instead of copies. If those classes get really large and get called alot at once, it could contribute to choppy frame rates.

Matt Hughson

Share this post


Link to post
Share on other sites
You may want to look into a more efficient growable array type as well. Rather than increasing the memory requirement by 1 each time, just double the array size. That way you can spend less time on copying and reallocating space, and more time on particle generation. Alternatively, you could have a max number of particles at a given time, and just allocate your array to be the max possible number of particles, as stated previously.

Share this post


Link to post
Share on other sites
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!