【问题标题】:SDL Window Not Showing Up at allSDL 窗口根本不显示
【发布时间】:2016-06-16 00:41:11
【问题描述】:

我刚刚学习 SDL 并下载了库并将它们添加到我的链接器等中,我正在尝试运行一个简单的演示程序来显示一个窗口,但它根本不会显示。我没有收到任何错误,只是没有显示窗口。

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

}

【问题讨论】:

  • 其实这对我来说工作得很好

标签: c++ sdl


【解决方案1】:

我刚刚在 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) 上遇到了同样的问题,基本循环解决了这个问题。谢谢!
  • Mac OS Catalina 上的同样问题,延迟确实是问题所在,循环运行完美。非常感谢!
猜你喜欢
  • 2016-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多