SDL窗口完全没有显示出来。

4

我现在正在学习SDL,已经下载了库文件并使用MinGW将它们添加到了链接器中。我试图运行一个简单的演示程序来显示一个窗口,但是窗口根本没有出现。我没有收到任何错误信息,窗口就是不会显示。

#include "SDL.h"
#include <stdio.h>

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

SDL_Window *window;                    // Declare a pointer

SDL_Init(SDL_INIT_VIDEO);              // Initialize SDL2

// Create an application window with the following settings:
window = SDL_CreateWindow(
    "An SDL2 window",                  // window title
    SDL_WINDOWPOS_UNDEFINED,           // initial x position
    SDL_WINDOWPOS_UNDEFINED,           // initial y position
    640,                               // width, in pixels
    480,                               // height, in pixels
    SDL_WINDOW_OPENGL                  // flags - see below
);

// Check that the window was successfully created
if (window == NULL) {
    // In the case that the window could not be made...
    printf("Could not create window: %s\n", SDL_GetError());
    return 1;
}

// The window is open: could enter program loop here (see SDL_PollEvent())

SDL_Delay(3000);  // Pause execution for 3000 milliseconds, for example

// Close and destroy the window
SDL_DestroyWindow(window);

// Clean up
SDL_Quit();
return 0;

}


2
实际上,这对我来说完全正常运行。 - cmourglia
1个回答

14

我刚在Linux和MinGW上测试了这个。可能是SDL_Delay阻塞窗口显示之前的问题。尝试添加一个基本的主循环看看是否有效。这将创建一个空白窗口。

#include "SDL.h"
#include <stdio.h>

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

SDL_Window *window;                    // Declare a pointer

SDL_Init(SDL_INIT_VIDEO);              // Initialize SDL2

// Create an application window with the following settings:
window = SDL_CreateWindow(
    "An SDL2 window",                  // window title
    SDL_WINDOWPOS_UNDEFINED,           // initial x position
    SDL_WINDOWPOS_UNDEFINED,           // initial y position
    640,                               // width, in pixels
    480,                               // height, in pixels
    SDL_WINDOW_OPENGL                  // flags - see below
);

// Check that the window was successfully created
if (window == NULL) {
    // In the case that the window could not be made...
    printf("Could not create window: %s\n", SDL_GetError());
    return 1;
}

// A basic main loop to prevent blocking
bool is_running = true;
SDL_Event event;
while (is_running) {
    while (SDL_PollEvent(&event)) {
        if (event.type == SDL_QUIT) {
            is_running = false;
        }
    }
    SDL_Delay(16);
}

// Close and destroy the window
SDL_DestroyWindow(window);

// Clean up
SDL_Quit();
return 0;

}

我在Mac OS Sierra (10.12.6) 上遇到了相同的问题,使用基本循环解决了这个问题。谢谢! - AdamInTheOculus
在Mac OS Catalina上有同样的问题,延迟确实是问题所在,循环运行得非常完美。非常感谢! - harveydf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接