尝试在玩家死亡时切换纹理(OpenGL + C++)
我正在创建一个 2D 游戏,当玩家死亡时,我希望纹理切换到另一个(以显示爆炸),我还希望游戏暂停一两秒,以便用户可以看到纹理已更改。
我的纹理加载正确,因为我可以将其应用到形状,并且如果我说将其与玩家原始纹理切换,我可以看到它。
我认为一定是它只在一帧中渲染然后消失或类似的情况。这是代码。
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;
}
}
我怎样才能让它切换纹理,显示它,然后休眠一段时间?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你想看到任何东西,你需要在 Sleep() 之前交换缓冲区。
更一般地说,将 Sleep() 替换为 ExplodeStart,并将其设置为 CurrentTimeInMilliseconds()。然后每次通过渲染循环检查 CurrentTimeInMilliseconds()-ExplodeStart > 是否为1000。如果是,请再次切换到常规播放器纹理。
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.