SDL 程序冻结

发布于 2024-09-27 23:19:33 字数 1551 浏览 3 评论 0原文

我正在编写一个简单的程序来测试鼠标。它编译得很好,但不起作用。当我启动它时,窗口冻结。我做错了什么?

#include <SDL/SDL.h>
#undef main

int main()
{
    if (SDL_Init (SDL_INIT_EVERYTHING) != 0)
        return 1;
    SDL_Surface* Scr;
    if ((Scr = SDL_SetVideoMode (300, 200, 32, 0)) == 0)
        return 2;

    SDL_Rect Mouse1 = {50, 50, 50, 100};
    SDL_Rect Mouse3 = {150, 50, 50, 100};
    SDL_Rect Mouse2 = {250, 50, 50, 100};
    SDL_Surface Colors;
    SDL_Rect Click = {0, 0, 50, 100};
    SDL_Rect NoClick = {50, 0, 50, 100};

    SDL_FillRect (Scr, 0, SDL_MapRGB (Scr->format, 255, 255, 255));
    SDL_FillRect (&Colors, &Click, SDL_MapRGB (Colors.format, 255, 0, 0));
    SDL_FillRect (&Colors, &NoClick, SDL_MapRGB (Colors.format, 0, 0, 255));

    while (true)
    {
        if (SDL_GetMouseState (0, 0) & SDL_BUTTON(1))
            SDL_BlitSurface (&Colors, &Click, Scr, &Mouse1);
        else
            SDL_BlitSurface (&Colors, &NoClick, Scr, &Mouse1);

        if (SDL_GetMouseState (0, 0) & SDL_BUTTON(2))
            SDL_BlitSurface (&Colors, &Click, Scr, &Mouse2);
        else
            SDL_BlitSurface (&Colors, &NoClick, Scr, &Mouse2);

        if (SDL_GetMouseState (0, 0) & SDL_BUTTON(3))
            SDL_BlitSurface (&Colors, &Click, Scr, &Mouse3);
        else
            SDL_BlitSurface (&Colors, &NoClick, Scr, &Mouse3);

        if (SDL_GetKeyState (0) [SDLK_ESCAPE])
            return 0;

        SDL_Delay (17);
    }
}

I'm writing a simple program for testing mouse. It compiles fine, but doesn't work. When I launch it, the window freezes. What am I doing wrong?

#include <SDL/SDL.h>
#undef main

int main()
{
    if (SDL_Init (SDL_INIT_EVERYTHING) != 0)
        return 1;
    SDL_Surface* Scr;
    if ((Scr = SDL_SetVideoMode (300, 200, 32, 0)) == 0)
        return 2;

    SDL_Rect Mouse1 = {50, 50, 50, 100};
    SDL_Rect Mouse3 = {150, 50, 50, 100};
    SDL_Rect Mouse2 = {250, 50, 50, 100};
    SDL_Surface Colors;
    SDL_Rect Click = {0, 0, 50, 100};
    SDL_Rect NoClick = {50, 0, 50, 100};

    SDL_FillRect (Scr, 0, SDL_MapRGB (Scr->format, 255, 255, 255));
    SDL_FillRect (&Colors, &Click, SDL_MapRGB (Colors.format, 255, 0, 0));
    SDL_FillRect (&Colors, &NoClick, SDL_MapRGB (Colors.format, 0, 0, 255));

    while (true)
    {
        if (SDL_GetMouseState (0, 0) & SDL_BUTTON(1))
            SDL_BlitSurface (&Colors, &Click, Scr, &Mouse1);
        else
            SDL_BlitSurface (&Colors, &NoClick, Scr, &Mouse1);

        if (SDL_GetMouseState (0, 0) & SDL_BUTTON(2))
            SDL_BlitSurface (&Colors, &Click, Scr, &Mouse2);
        else
            SDL_BlitSurface (&Colors, &NoClick, Scr, &Mouse2);

        if (SDL_GetMouseState (0, 0) & SDL_BUTTON(3))
            SDL_BlitSurface (&Colors, &Click, Scr, &Mouse3);
        else
            SDL_BlitSurface (&Colors, &NoClick, Scr, &Mouse3);

        if (SDL_GetKeyState (0) [SDLK_ESCAPE])
            return 0;

        SDL_Delay (17);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

半枫 2024-10-04 23:19:33

您没有处理任何事件。在您的情况下,调用 SDL_PumpEvents 以使 SDL 处理它们并更新其所有内部状态:

while (true)
{
  SDL_PumpEvents();

  // The rest is the same ...
}

You are not processing any events. In your case, call SDL_PumpEvents to make SDL process them and update all its internal states:

while (true)
{
  SDL_PumpEvents();

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