1

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;
}
  • 1
    Please edit your question to include a [mcve] so we can see what you're doing. – Retired Ninja Jan 08 '22 at 00:30
  • Do you really need to compile for cygwin? Apps built with the regular MinGW don't need an X server. I don't know if cygwin gcc has flags to compile in non-cygwin mode, but there's [MSYS2](https://stackoverflow.com/q/30069830/2752075) which can do that (it has a cygwin-based shell and utilities, but a different compiler). – HolyBlackCat Jan 08 '22 at 06:07

1 Answers1

0

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

matzeri
  • 8,062
  • 2
  • 15
  • 16
  • Thank you so much! I looked all over and had found out that I was supposed to start the X server, but I had no idea about opening an XTerm. Your instructions were also very, very clear, which I appreciate. Thanks a million! – santapacman Jan 08 '22 at 20:54
  • Than you should accept it https://stackoverflow.com/help/someone-answers – matzeri Jan 08 '22 at 22:10
  • Running from XTerm is the best way to be sure that the X Server is running and DISPLAY variable is properly set. – matzeri Jan 09 '22 at 04:27