c++ - 为什么 mac os sdl2 程序窗口没有响应?

标签 c++ sdl

我刚刚在我的 mac 上设置了 SDL2 框架,但是编译和运行程序成功了,窗口没有响应(我复制了创建一些矩形的代码)。

我使用 xcode,并按照此处的教程进行操作 http://lazyfoo.net/tutorials/SDL/01_hello_SDL/mac/xcode/index.php 一步一步。

SDL_Window* window = NULL;

//The surface contained by the window
SDL_Surface* screenSurface = NULL;

//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
    printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
else
{
    //Create window
    window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
    if( window == NULL )
    {
        printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
    }
    else
    {
        //Get window surface
        screenSurface = SDL_GetWindowSurface( window );

        //Fill the surface white
        SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );

        //Update the surface
        SDL_UpdateWindowSurface( window );
        cout << " Ok" << endl;
        //Wait two seconds
        SDL_Delay( 20000 );
    }
}

//Destroy window
SDL_DestroyWindow( window );

//Quit SDL subsystems
SDL_Quit();

return 0;

为什么会出现这个问题? 提前谢谢你

最佳答案

为了让用 SDL 编写的程序“响应”操作系统,您应该将控制权交还给 SDL,以便它处理系统消息,并将它们作为 SDL 事件(鼠标事件、键盘事件等)交还给您.

为此,您必须添加一个使用 SDL_PollEvent 的循环,看起来应该是这样的

while(true)
{
    SDL_Event e;
    while (SDL_PollEvent(&e)) 
    {
        // Decide what to do with events here
    }

    // Put the code that is executed every "frame". 
    // Under "frame" I mean any logic that is run every time there is no app events to process       
}

您需要处理一些特殊事件,例如 SDL_QuiEvent 才能关闭您的应用程序。如果你想处理它,你应该修改你的代码看起来像这样:

while(true)
{
    SDL_Event e;
    while (SDL_PollEvent(&e)) 
    {
        if(e.type == SDL_QUIT)
        {
            break;
        }
        // Handle events
    }

    // "Frame" logic     
}

关于c++ - 为什么 mac os sdl2 程序窗口没有响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55340608/

相关文章:

c++ - 初始化 boost::shared_ptr 的正确方法

c++ - 全局变量类 c++

c++ - 不识别键盘输入

c++ - 移除 stdc++ 库依赖

c++ - OpenGL 纹理不渲染

c++ - 如何通过 Gradle 将链接器标志传递给 Android Studio 中的 cmake

c++ - 当传递给对象时,SDL_Surface 会在内存中存储图像的新实例,还是指向传递给它的图像?

c++ - 在 Apache Thrift 中使用 seqid 的目的是什么?

c - SDL_net没有收到UDP数据包

c - 调试器报告 SDL 在初始化时生成的那些线程是什么?