C++游戏循环示例
有人可以编写一个只有“游戏循环”的程序的源代码,它会一直循环直到您按 Esc 键,并且程序会显示基本图像。这是我现在拥有的源代码,但我必须使用 SDL_Delay(2000);
来使程序保持活动状态 2 秒,在此期间程序被冻结。
#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
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;
}
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个完整且有效的示例。除了使用帧时间调节之外,您还可以使用 SDL_WaitEvent。
Here is a complete and working example. Instead of using a frame-time regulation you can also use SDL_WaitEvent.
尝试过类似的东西
?您可以在那里找到许多教程和示例;只是一个快速搜索示例。
添加注释:WaitEvent“冻结”程序,因此您无法执行任何操作..您只需等待;可能需要其他等待技术(如 PollEvent,或在计时器初始化后再次等待事件)。
Tried with something like
? You can find many tutorials and example out there; just a fast-search example.
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).
由于您已经在使用 SDL,因此可以使用
SDL_PollEvent
函数< /a> 运行事件循环,检查是否按下了按键事件是 ESC。看起来这将类似于 mySDL_Event.key.keysym.sym == SDLK_ESCAPE。Since you're already using SDL, you could use the
SDL_PollEvent
function to run an event loop, checking to see if the key press event was ESC. Looks like this would be along the lines ofmySDL_Event.key.keysym.sym == SDLK_ESCAPE
.