Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c++ - SDL_Window not displayed in windowed mode in macOS

Running the following code, I'm not able to view a window unless I set SDL_WINDOW_FULLSCREEN in SDL_CreateWindow. With any other settings, I can see the process running, but no window appears. I'd like to be able to create a windowed SDL_Window. Am I missing something?

Running macOS Sierra, SDL v2.0.7

#include <SDL2/SDL.h>

int main() {
    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
        SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
        return 1;
    }

    SDL_Window *window = SDL_CreateWindow(
        "Title",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        640,
        800,
        SDL_WINDOW_SHOWN
    );

    if (window == NULL) {
        SDL_Log("Unable to create window: %s", SDL_GetError());
        return 1;
    }

    SDL_Delay(5000);

    // Cleanup.
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Can you try adding an event loop instead of a SDL_Delay?

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

So now I've tried your code on my Mac machine at work and it indeed does not show a window. Your code is not giving SDL enough time to even show a screen, it just goes to sleep and exits. With the event loop is showing a non-fullscreen non-maximized window.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...