Hello,
I have made a smoke grenade in a top-down shooter and I just started on smoke grenades. My current way of rendering smoke is to use a png file of a grey smoke like cloud and then have around 100 of them spin out of the grenade. It looks really good but it does lag quite a lot and I was wandering if there was a way to speed up my code.
This is the smoke class:
class Smoke
{
private:
double x,y,alpha,size,speed,direction, rotation;
public:
void Move();
void Draw();
Smoke(double, double);
};
std::vector<Smoke> Smoke_Vector;
void Smoke::Move(void)
{
x += cos(direction) * speed;
y += sin(direction) * speed;
size += 0.1;
alpha -= alpha*0.001;
rotation += 0.1;
if (alpha < 0.05)
{
for (std::vector<Smoke>::iterator it = Smoke_Vector.begin(); it != Smoke_Vector.end(); ++it)
{
if (&*it == this)
{
Smoke_Vector.erase(it);
break;
}
}
}
}
void Smoke::Draw(void)
{
glColor4d(1, 1, 1, alpha);
Draw_Texture(x, y, size, size, rotation, Tex_Smoke);
}
Smoke::Smoke(double ax, double ay)
{
x = ax;
y = ay;
size = 0;
direction = Random(0, 360, current_time);
rotation = Random(0, 360, current_time * direction);
speed = 0.05;
alpha = 20;
}
This is how the texture is loaded:
Tex_Smoke = SOIL_load_OGL_texture("Data/Smoke.png",SOIL_LOAD_AUTO,SOIL_CREATE_NEW_ID,SOIL_FLAG_MIPMAPS | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT);
This is how it’s drawn:
void Draw_Texture(double x, double y, double width, double height, double Angle, GLuint texture)
{
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); //GL_NEAREST = no smoothing
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D,texture);
glTranslated(x, y, 0);
glRotated(Angle, 0, 0, 1);
glBegin(GL_QUADS);
double x_centre = width/2;
double y_centre = height/2;
glTexCoord2d(0.0,0.0); glVertex2d(-x_centre, -y_centre);
glTexCoord2d(1.0,0.0); glVertex2d(x_centre, -y_centre);
glTexCoord2d(1.0,1.0); glVertex2d(x_centre, y_centre);
glTexCoord2d(0.0,1.0); glVertex2d(-x_centre, y_centre);
glEnd();
glPopMatrix();
}
And this is how they are rendered in the display funtion:
for( int i = 0 ; i < Smoke_Vector.size() ; ++i )
{
Smoke_Vector[i].Draw();
Smoke_Vector[i].Move();
}
Any help is much appreciated.
Thanks,
Rowan