Search code examples
c++cygwinsdlsdl-2

SDL_CreateWindow Error: windows not available


When calling 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. 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. I did not find any documentation about the error, even in a couple of directories listing SDL error codes. I am on Eclipse C++ and my compiler is 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;
}

Solution

  • The answer is very simple, you are not running the test with the X server running

    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

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

    and a XTerm

    enter image description here

    now from the XTerm

    $ ./prova.exe 
     
    $ echo $?
    0
    

    the program completed without errors but so fast to not see the window. Add to the code

    #include <unistd.h>
    

    and

    sleep(20);
    

    before return 0. The Window will show up with the "Hello World" in the title bar.

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