SDL_CreateWindow Error: windows not available

SDL_CreateWindow Error: windows not available

当调用 SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN); 时,未创建 window 并且调用 SDL_GetError returns 时出现错误,如标题所示。我曾一度将我的 SDL_VIDEODRIVER 设置为 'windows',但更改此设置、重建我的应用程序并再次尝试 运行 并没有改变错误。我没有找到任何关于错误的文档,即使在列出 SDL 错误代码的几个目录中也是如此。我在 Eclipse C++ 上,我的编译器是 cygwin。为什么会出现此错误,我该如何解决?我还需要提供任何其他信息才能弄清这个问题吗?

编辑: 这是最小的可重现示例:

#define SDL_MAIN_HANDLED
#include <iostream>
#include <SDL.h>
using namespace std;

int main() {

//This if statement's condition is not met. I left it here in its full form to
//show that SDL_Init() is not causing the error.
    if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
        cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
            SDL_Quit();
            return 1;
        }

//It seems as though win is being set to nullptr here.
    SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);

//This if statement's condition is met. The error is written to the console from this cout statement.
    if (win == nullptr){
        cout << "SDL_CreateWindow Error: " << SDL_GetError();
        SDL_Quit();
        return 1;
    }
    return 0;
}

答案很简单,你是不是运行用X server测试的运行

来自薄荷糖:

$ g++ prova.cc -o prova -lSDL2 -I/usr/include/SDL2

$ ./prova.exe
SDL_Init Error: No available video device

现在我们启动 X 服务器

$ startxwin

Welcome to the XWin X Server
Vendor: The Cygwin/X Project
Release: 1.21.1.2
...

和一个 XTerm

现在来自 XTerm

$ ./prova.exe 
 
$ echo $?
0

程序完成且没有错误,但速度太快以至于看不到 window。 添加到代码

#include <unistd.h>

sleep(20);

return 0 之前。 Window 将在标题栏中显示“Hello World”。

Cygwin/X User's Guide
https://x.cygwin.com/docs/ug/cygwin-x-ug.html