SDL window does not show

限于喜欢 提交于 2019-11-27 02:54:29

问题


This is my code:

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

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

SDL_Init(SDL_INIT_VIDEO);

SDL_Window *_window;
_window = SDL_CreateWindow("Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 700, 500, SDL_WINDOW_RESIZABLE);

SDL_Delay(20000);

SDL_DestroyWindow(_window);
SDL_Quit();

return 0;

}

Im working in Xcode. I've downloaded SDL2 and imported the library to the projects build phases. I've tested that the SDL2 works correctly.

The problem is that window never shows up. I just get a "spinning-mac-wheel" and then the program quits after the delay. I've made sure that the window is not hidden behind somewhere.

Ideas?


回答1:


You have to give the system a chance to have it's event loop run.

easiest is to poll for events yourself:

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;
        }
    }
}

instead of the wait loop




回答2:


You have to load a bitmap image, or display something on the window, for Xcode to start displaying the window.

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

using namespace std;

int main() {
    SDL_Window * window = nullptr;

    SDL_Surface * window_surface = nullptr;
    SDL_Surface * image_surface = nullptr;

    SDL_Init(SDL_INIT_VIDEO);

    window = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);

    window_surface = SDL_GetWindowSurface(window);
    image_surface = SDL_LoadBMP("image.bmp");

    SDL_BlitSurface(image_surface, NULL, window_surface, NULL);

    SDL_UpdateWindowSurface(window);

    SDL_Delay(5000);

    SDL_DestroyWindow(window);
    SDL_FreeSurface(image_surface);
    SDL_Quit();
}



回答3:


You need to initialize SDL with SDL_Init(SDL_INIT_VIDEO) before creating the window.




回答4:


Please remove the sdl_delay() and replace it with the below mentioned code. I don't have any reason for it but I tried on my own and it works

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


来源:https://stackoverflow.com/questions/34424816/sdl-window-does-not-show

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!