views:

78

answers:

1

I'm creating a 2D game and when the player dies I want the texture I to switch to another (to show an explosion) I also want the game to pause for a second or two so the user can see that the texture has changed.

My textures are loading correctly because I can apply it to a shape and i can see it if I say switched it with the players original texture.

I think that it must be that it is only rendering in one frame and then disappearing or something like that. Here is the code.

void Player::die(){
if(Player::lives > 0){

    glPushMatrix();
    glEnable(GL_TEXTURE_2D);    
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glBindTexture(GL_TEXTURE_2D, explosionTex);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

    glTranslatef(200, 200, 0.0);
    glRotatef(heading, 0,0,1);
    glColor3f(1.0,0.0,0.0);
        glBegin(GL_POLYGON);
            glTexCoord2f(0.0, 1.0); glVertex2f(-40,40);
            glTexCoord2f(0.0, 0.0); glVertex2f(-40,-40);
            glTexCoord2f(1.0, 0.0); glVertex2f(40,-40);
            glTexCoord2f(1.0, 1.0); glVertex2f(40,40);
        glEnd();


    glDisable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);
    glPopMatrix();

    Sleep ( 1000 );

    *xscroll = 0;
    *yscroll = 0;
    Player::lives--;
    Player::XPos = 0;
    Player::YPos = 0;
    Player::heading = 0;
    Player::speed = 0;
}
}

How can I get it to switch texture, display that and then sleep for a time?

+2  A: 

You need to swap your buffers before you Sleep() if you want to see anything.

More generally, replace the Sleep() with a ExplodeStart, which you set to CurrentTimeInMilliseconds(). Then each time through your render loop check if CurrentTimeInMilliseconds()-ExplodeStart > 1000. If it is, switch to your regular player texture again.

genpfault
i did SwapBuffers(hDC); before the sleep but nothing changed
Chris
Ok i got that to work now, but some other things are disappearing?
Chris
Don't worry I worked it out, realised I was swapping the buffers before the rest had been done.
Chris
Cool. For the future "other things are disappearing" is extremely vague. Mindreaders and all that.
genpfault
That is true, was written in a bit of a haste. not the most helpful comment i appreciate :)
Chris