Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top