SDL窗口仅在使用macOS 10.14.2的扩展坞中显示

时间:2019-01-22 18:34:28

标签: c++ macos sdl

我正在尝试使用macOS 10.14.2一起学习lazyfoo SDL教程。

我已经从练习1中获得了代码来进行编译和启动,但是我无法真正显示该窗口。

运行程序时,停靠窗口中会出现一个窗口图标,直到程序结束。

单击图标无济于事。

编辑:使用以下命令进行编译:

g++ -o window window.cpp -L/usr/local/Cellar/ -lSDL2

右键单击该图标,然后单击“显示所有窗口”,将显示消息:“无可用窗口”。

/*This source code copyrighted by Lazy Foo' Productions (2004-2019)
and may not be redistributed without written permission.*/

//Using SDL and standard IO
#include <SDL2/SDL.h> // Adjusted for macOS
#include <stdio.h>

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

int main( int argc, char* args[] )
{
    //The window we'll be rendering to
    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 );

            //Wait two seconds
            SDL_Delay( 20000 );
        }
    }

    //Destroy window
    SDL_DestroyWindow( window );

    //Quit SDL subsystems
    SDL_Quit();

    return 0;
}

已解决:

感谢VTT的提示。

通过替换以下内容,我可以使窗口正常显示:

SDL_Delay( 20000 );
SDL_DestroyWindow( window );
SDL_Quit()

包含Toad在this线程中找到的摘录:

SDL_Event e;
bool quit = false;
while (!quit){
    while (SDL_PollEvent(&e)){
        if (e.type == SDL_QUIT){
            quit = true;
        }
        if (e.type == SDL_KEYDOWN){
            quit = true;
        }
        if (e.type == SDL_MOUSEBUTTONDOWN){
            quit = true;
        }
    }
}

感谢所有提供帮助的人。

0 个答案:

没有答案