C++游戏循环示例

发布于 2024-09-05 22:45:53 字数 672 浏览 10 评论 0原文

有人可以编写一个只有“游戏循环”的程序的源代码,它会一直循环直到您按 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薄情伤 2024-09-12 22:45:53

这是一个完整且有效的示例。除了使用帧时间调节之外,您还可以使用 SDL_WaitEvent。

#include <SDL/SDL.h>
#include <cstdlib>
#include <iostream>

using namespace std;

const Uint32 fps = 40;
const Uint32 minframetime = 1000 / fps;

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

  if (SDL_Init (SDL_INIT_VIDEO) != 0)
  {
    cout << "Error initializing SDL: " << SDL_GetError () << endl;
    return 1;
  }

  atexit (&SDL_Quit);
  SDL_Surface *screen = SDL_SetVideoMode (640, 480, 32, SDL_DOUBLEBUF);

  if (screen == NULL)
  {
    cout << "Error setting video mode: " << SDL_GetError () << endl;
    return 1;
  }

  SDL_Surface *pic = SDL_LoadBMP ("hello.bmp");

  if (pic == NULL)
  {
    cout << "Error loading image: " << SDL_GetError () << endl;
    return 1;
  }

  bool running = true;
  SDL_Event event;
  Uint32 frametime;

  while (running)
  {

    frametime = SDL_GetTicks ();

    while (SDL_PollEvent (&event) != 0)
    {
      switch (event.type)
      {
        case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE)
                            running = false;
                          break;
      }
    }

    if (SDL_GetTicks () - frametime < minframetime)
      SDL_Delay (minframetime - (SDL_GetTicks () - frametime));

  }

  SDL_BlitSurface (pic, NULL, screen, NULL);
  SDL_Flip (screen);
  SDL_FreeSurface (pic);
  SDL_Delay (2000);

  return 0;

}

Here is a complete and working example. Instead of using a frame-time regulation you can also use SDL_WaitEvent.

#include <SDL/SDL.h>
#include <cstdlib>
#include <iostream>

using namespace std;

const Uint32 fps = 40;
const Uint32 minframetime = 1000 / fps;

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

  if (SDL_Init (SDL_INIT_VIDEO) != 0)
  {
    cout << "Error initializing SDL: " << SDL_GetError () << endl;
    return 1;
  }

  atexit (&SDL_Quit);
  SDL_Surface *screen = SDL_SetVideoMode (640, 480, 32, SDL_DOUBLEBUF);

  if (screen == NULL)
  {
    cout << "Error setting video mode: " << SDL_GetError () << endl;
    return 1;
  }

  SDL_Surface *pic = SDL_LoadBMP ("hello.bmp");

  if (pic == NULL)
  {
    cout << "Error loading image: " << SDL_GetError () << endl;
    return 1;
  }

  bool running = true;
  SDL_Event event;
  Uint32 frametime;

  while (running)
  {

    frametime = SDL_GetTicks ();

    while (SDL_PollEvent (&event) != 0)
    {
      switch (event.type)
      {
        case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE)
                            running = false;
                          break;
      }
    }

    if (SDL_GetTicks () - frametime < minframetime)
      SDL_Delay (minframetime - (SDL_GetTicks () - frametime));

  }

  SDL_BlitSurface (pic, NULL, screen, NULL);
  SDL_Flip (screen);
  SDL_FreeSurface (pic);
  SDL_Delay (2000);

  return 0;

}
单身狗的梦 2024-09-12 22:45:53

尝试过类似的东西

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

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

添加注释:WaitEvent“冻结”程序,因此您无法执行任何操作..您只需等待;可能需要其他等待技术(如 PollEvent,或在计时器初始化后再次等待事件)。

Tried with something like

  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.

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).

人间不值得 2024-09-12 22:45:53

由于您已经在使用 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 of mySDL_Event.key.keysym.sym == SDLK_ESCAPE.

野味少女 2024-09-12 22:45:53
#include <conio.h>

...

while (!kbhit())
{
    hello = SDL_LoadBMP("hello.bmp");

    SDL_BlitSurface(hello, NULL, screen, NULL);

    SDL_Flip(screen);
}

...
#include <conio.h>

...

while (!kbhit())
{
    hello = SDL_LoadBMP("hello.bmp");

    SDL_BlitSurface(hello, NULL, screen, NULL);

    SDL_Flip(screen);
}

...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文