0

i was following an online SDL tutorial linked here: https://lazyfoo.net/tutorials/SDL/ following the linux version with the g++ compiler.

at the end of the hello SDL course i compiled my program and nothing happened although it seemed to be doing something. i then downloaded the source files included and compiled and ran them too, which didn't open a new window or do anything it was supposed to like the program i made.

the source files are available here: https://lazyfoo.net/tutorials/SDL/01_hello_SDL/index2.php at the bottom of the page in a download link. and my exact code is:

/*This source code copyrighted by Lazy Foo' Productions 2004-2023
and may not be redistributed without written permission.*/

//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>
#include <iostream>



//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

int main( int argc, char* args[] )
{

    //The window we'll be rendering to
    SDL_Window* window = NULL;
    
    //The surface contained by the window
    SDL_Surface* screenSurface = NULL;

    //Initialize SDL
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
    }
    else
    {
        //Create window
        window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
        if( window == NULL )
        {
            printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
        }
        else
        {
            //Get window surface
            screenSurface = SDL_GetWindowSurface( window );

            //Fill the surface white
            SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );
            
            //Update the surface
            SDL_UpdateWindowSurface( window );
            
            //Hack to get window to stay up
            SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } }
        }
    }
    
    return 0;
}

My SDL2 was obtained through the apt package manager.

i compiled the program with this command:

g++ -Wall 01_hello_SDL.cpp -o game `sdl2-config --cflags --libs`

i am running debian 11 with a kernel version of 5.10.0-23-amd64.

I expected the executed c++ program to open a window or in fact do anything, which it did not.

I just used the provided source code from the tutorial website which still didn't open a window.

I am not very experienced with Linux or c++ so I have decided to learn both on a dedicated laptop running Debian.

Tinigame
  • 1
  • 3
  • 1
    Please add your exact code to the question itself as text. Do you use SDL2 from the package manager, or did you compile it yourself? What does your debugger say? – HolyBlackCat Jun 25 '23 at 20:26
  • 2
    Fwiw, your code shows a window with a white surface for me – Ted Lyngmo Jun 25 '23 at 20:36
  • @HolyBlackCat added what you had asked for. Im not quite sure what my debugger would be, i write my code using sublimetext and compile with g++ which doesnt report anything. The program supposedly runs but no window is created. – Tinigame Jun 25 '23 at 20:36
  • 1
    Are you sitting at the console of your debian machine or how are you connected to it? Does it print out anything to the console window when you start the program? – Ted Lyngmo Jun 25 '23 at 20:37
  • @TedLyngmo it does not print anything to the console. it doesnt even indicate it had finished or anything. – Tinigame Jun 25 '23 at 20:39
  • Ok, that probably means that it's running. If you run the command `xterm` instead, does a new terminal window show up? What is your `$DISPLAY` variable set to? – Ted Lyngmo Jun 25 '23 at 20:40
  • @TedLyngmo doing the xterm command opens a new window indeed. where would i see the $DISPLAY variable though? – Tinigame Jun 25 '23 at 20:47
  • Ok, odd. Then I guess that SDL is not using the X11 backend. If it did, you would have seen a window. I connect remotely to my linux-machine and use X11 so your program shows up fine for me. To see `$DISPLAY` just do `echo $DISPLAY` – Ted Lyngmo Jun 25 '23 at 20:48
  • @TedLyngmo it resulted in a :0 – Tinigame Jun 25 '23 at 20:51
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Jun 25 '23 at 20:51
  • `:0` sounds reasonable (and if it wasn't, `xterm` wouldn't have worked). Hmm, sorry, I haven't used SDL on linux _without_ X11 so I'm not sure what the issue might be. – Ted Lyngmo Jun 25 '23 at 20:52
  • @JesperJuhl would the return codes it should spit out act like a debugger? either way i get nothing in my console after executing the program. – Tinigame Jun 25 '23 at 20:55
  • @Tinigame Ehh, what? Return codes are just that, values returned from main when a program terminates (to print the exit code of a program, run `echo $?` after the program terminates). The debugger is a standalone tool that lets you run the program line by line, step into functions you call, set break points, inspect variable values and memory contents etc. It will let you figure out what your program is actually doing. – Jesper Juhl Jun 25 '23 at 21:54
  • Try to change `SDL_PollEvent()` to `SDL_WaitEvent()` first. – dimich Jun 25 '23 at 21:59
  • @dimich did that. still nothing has changed. – Tinigame Jun 25 '23 at 22:04
  • When you run program from command line, does it stay in foreground or exit immediately? Try to change `SDL_WINDOWPOS_CENTERED` (both) to `SDL_WINDOWPOS_UNDEFINED` or to `0`. Add some debug printfs before event loop and in event loop too. Are they printed? – dimich Jun 25 '23 at 22:20
  • @dimich i added a printf right before window creation, right after it, and right before the window surface lines. They all printed successfully but nothing had changed. i also made the SDL_WINDOWPOS_CENTERED back into SDL_WINDOWPOS_UNDEFINED which didnt change anything. making them 0 also did nothing. The commandline stayed in the foreground. – Tinigame Jun 25 '23 at 22:39
  • The example code creates and displays SDL window with white surface on my system. What window system do you use? Try with another window manager. Also if `SDL_VIDEODRIVER` is set, check it is set properly. – dimich Jun 26 '23 at 08:11
  • @dimich i used the default kde WM (forgot what it was called exactly) where of course it didn't work. I then changed into the Openbox WM. trying it changed nothing. In the code SDL_VIDEODRIVER is not present and im not quite sure how or where to set it in the code. – Tinigame Jun 26 '23 at 14:22
  • `SDL_VIDEODRIVER` is an environment variable: [How do I choose a specific video driver?](https://wiki.libsdl.org/SDL2/FAQUsingSDL#how_do_i_choose_a_specific_video_driver). Just a guess that your issue is caused by composite windowing system or something like that. – dimich Jun 26 '23 at 16:19
  • @dimich I found a command labeled SDL_GetCurrentVideoDriver that i set to printf before and after the SDL initialising. before it returned a NULL and after it returned a "offscreen", would that mean anything significant? Also im not quite sure how to set the SDL_VIDEODRIVER variable. would i set it in the program? or is it during compiling? – Tinigame Jun 26 '23 at 19:25
  • Of course, it's significant. Good catch. For some reason it uses offscreen rendering driver instead of `x11` (or `wayland`, i don't know how modern KDE works). Try to run your executable with `SDL_VIDEODRIVER=x11 ./game` or `SDL_VIDEODRIVER=wayland ./game`. If it fails, pay attention what `SDL_GetError()` returns. Also read about environment variables if you are going to use GNU/Linux forward, this is "must know". – dimich Jun 26 '23 at 19:55
  • @dimich it returns an error stating that wayland is not available or x11 is not available altough they should be? – Tinigame Jun 26 '23 at 22:53
  • For me it looks like `libsdl2-dev` package is broken. [Here](https://stackoverflow.com/q/76559748/14772619) is similar issue in Ubuntu. Sorry, i have no Debian or Ubuntu installed to check. – dimich Jun 27 '23 at 09:39

1 Answers1

0

I fixed my issue by re-installing Debian, which i also upgraded to Debian 12, which was 11 before. Re-downloading SDL took longer and did more than before, and then compiling the same program worked as expected.

The issue must've been a broken debian-11 install on my laptop with missing firmware or otherwise, which is quite likely since i was even less experienced when i installed it at the time.

Tinigame
  • 1
  • 3