random freezing

Started by
2 comments, last by abso 18 years, 7 months ago

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
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 ...
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
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
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.

This topic is closed to new replies.

Advertisement