本文介绍了C++ 游戏循环示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以为一个只有游戏循环"的程序编写一个源代码,它会一直循环直到您按 Esc 键,并且程序会显示一个基本图像.这是我现在拥有的源代码,但我必须使用 SDL_Delay(2000); 使程序保持活动状态 2 秒,在此期间程序被冻结.

Can someone write up a source for a program that just has a "game loop", which just keeps looping until you press Esc, and the program shows a basic image. Heres the source I have right now but I have to use SDL_Delay(2000); to keep the program alive for 2 seconds, during which the program is frozen.

#include "SDL.h"

int main(int argc, char* args[]) {

    SDL_Surface* hello = NULL;
    SDL_Surface* screen = NULL;

    SDL_Init(SDL_INIT_EVERYTHING);

    screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);

    hello = SDL_LoadBMP("hello.bmp");

    SDL_BlitSurface(hello, NULL, screen, NULL);

    SDL_Flip(screen);

    SDL_Delay(2000);

    SDL_FreeSurface(hello);

    SDL_Quit();

    return 0;

}

我只想打开程序,直到我按下 Esc.我知道循环是如何工作的,我只是不知道我是在 main() 函数内部还是在它外部实现.我两个都试过,两次都失败了.如果你能帮助我,那就太好了:P

I just want the program to be open until I press Esc. I know how the loop works, I just don't know if I implement inside the main() function, or outside of it. I've tried both, and both times it failed. If you could help me out that would be great :P

推荐答案

尝试过类似的东西

  SDL_Event e;
  while( SDL_WaitEvent(&e) )
  {
    if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE) break;
  }

?您可以在那里找到许多教程和示例;只是一个快速搜索示例.

? You can find many tutorials and example out there; just a fast-search example.

添加注释:WaitEvent冻结"程序,因此您无法执行任何操作..您只需等待;可能需要其他等待技术(如 PollEvent,或定时器初始化后的 WaitEvent).

Added note: WaitEvent "freezes" the program so you can't do anything .. you just wait; other waiting technics can be desired (as PollEvent, or WaitEvent again after the initializtion of a timer).

这篇关于C++ 游戏循环示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:43