简体   繁体   English

SDL_CreateWindow 错误:windows 不可用

[英]SDL_CreateWindow Error: windows not available

When calling SDL_CreateWindow("Hello World,", 100, 100, 640, 480; SDL_WINDOW_SHOWN);调用时SDL_CreateWindow("Hello World,", 100, 100, 640, 480; SDL_WINDOW_SHOWN); the window is not created and calling SDL_GetError returns an error that reads exactly as shown in the title. window 未创建,调用 SDL_GetError 返回一个错误,其内容与标题中显示的完全相同。 I had set my SDL_VIDEODRIVER to 'windows' at one point, but changing this, rebuilding my application, and attempting to run again did not change the error.我曾一度将我的 SDL_VIDEODRIVER 设置为“windows”,但更改它、重建我的应用程序并尝试再次运行并没有改变错误。 I did not find any documentation about the error, even in a couple of directories listing SDL error codes.即使在列出 SDL 错误代码的几个目录中,我也没有找到任何有关该错误的文档。 I am on Eclipse C++ and my compiler is cygwin.我在 Eclipse C++ 上,我的编译器是 cygwin。 Why am I getting this error, and how do I solve it?为什么我会收到此错误,我该如何解决? Is there any other information I need to provide to get to the bottom of this?我还需要提供任何其他信息才能查明真相吗?

Edit: Here is the minimum reproducible example:编辑:这是最小的可重现示例:

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

I don't have alot of experience using SDL but when im creating a window a usualy do it like this我没有很多使用 SDL 的经验,但是当我创建 window 时,通常会这样做

SDL_Window *win = SDL_CreateWindow("Hello World!", SDL_WINDOWPOS_UNDEFINED, 
SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);

i don't know if this will fix your problem but you can try it我不知道这是否能解决你的问题,但你可以试试

as shown here https://wiki.libsdl.org/SDL_CreateWindow the second and the thrid argument can take SDL_WINDOWPOS_CENTERED, or SDL_WINDOWPOS_UNDEFINED如此处所示https://wiki.libsdl.org/SDL_CreateWindow第二个和第三个参数可以采用 SDL_WINDOWPOS_CENTERED 或 SDL_WINDOWPOS_UNDEFINED

The answer is very simple, you are not running the test with the X server running答案很简单,你不是在运行 X 服务器的情况下运行测试

from mintty:来自薄荷:

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

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

now we start the X server现在我们启动 X 服务器

$ startxwin

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

and a XTerm和一个 XTerm

在此处输入图像描述

now from the XTerm现在来自 XTerm

$ ./prova.exe 
 
$ echo $?
0

the program completed without errors but so fast to not see the window.该程序完成没有错误,但速度如此之快以至于看不到 window。 Add to the code添加到代码

#include <unistd.h>

and

sleep(20);

before return 0 .return 0之前。 The Window will show up with the "Hello World" in the title bar. Window 将在标题栏中显示“Hello World”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM