1

I installed SDL according to these instructions on Ubuntu 22.04.2.

In VS Code, I managed to include SDL2 libraries properly so the code would compile. However, the window will never appear when I actually run the program. The program seems to run, looking at the terminal, but nothing happens:

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

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

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

//Starts up SDL and creates window
bool init();

//Loads media
bool loadMedia();

//Frees media and shuts down SDL
void close();

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

//The image we will load and show on the screen
SDL_Surface* gHelloWorld = NULL;

bool init()
{
    //Initialization flag
    bool success = true;

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

    return success;
}

bool loadMedia()
{
    //Loading success flag
    bool success = true;

    //Load splash image
    gHelloWorld = SDL_LoadBMP( "hello_world.bmp" );
    if( gHelloWorld == NULL )
    {
        printf( "Unable to load image %s! SDL Error: %s\n", "02_getting_an_image_on_the_screen/hello_world.bmp", SDL_GetError() );
        success = false;
    }

    return success;
}

void close()
{
    //Deallocate surface
    SDL_FreeSurface( gHelloWorld );
    gHelloWorld = NULL;

    //Destroy window
    SDL_DestroyWindow( gWindow );
    gWindow = NULL;

    //Quit SDL subsystems
    SDL_Quit();
}

int main( int argc, char* args[] )
{
    //Start up SDL and create window
    if( !init() )
    {
        printf( "Failed to initialize!\n" );
    }
    else
    {
        //Load media
        if( !loadMedia() )
        {
            printf( "Failed to load media!\n" );
        }
        else
        {
            //Apply the image
            SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
            
            //Update the surface
            SDL_UpdateWindowSurface( gWindow );

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

    //Free resources and close SDL
    close();

    return 0;
}

I don't get any error messages.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • This is the time to learn how to run your program under a debugger such that you can trace through the execution and find out what goes wrong. – Botje Apr 05 '23 at 07:00
  • What are you running this under? X11? Wayland? Linux console? What does [this test program](https://stackoverflow.com/a/57684805/44729) output? – genpfault Apr 05 '23 at 18:08
  • Why did you build SDL from source instead of using Ubuntu's [`libsdl2-dev` package](https://packages.ubuntu.com/jammy/libsdl2-dev)? Which tag/branch did you build? [`release-2.26.4`](https://github.com/libsdl-org/SDL/releases/tag/release-2.26.4)? [`main`](https://github.com/libsdl-org/SDL/tree/main)? Or something else? – genpfault Apr 05 '23 at 18:14
  • @genpfault I think I am running it through the Linux console. I don't really know what you mean. I tried running the test program, but nothing happens. I should add that I installed the dev package first and it didn't work using that either, so I assumed I had to install it some other way; using release 2.26.4, but that did not work either. – user21569762 Apr 05 '23 at 20:10
  • @Botje I'm not sure how to use a debugger for this, as it does not recognize the included SDL objects. I get a bunch of errors and it does not begin debugging. – user21569762 Apr 05 '23 at 20:18
  • You seem to want to render pictures by blitting surfaces to the window's surface, is that right? I have never seen SDL2 rendering being done this way. Usually, you create a [renderer](https://wiki.libsdl.org/SDL2/SDL_Renderer) that'll be attached to your window and will render textures to your window. Not sure if this has an impact on the windows displaying though. Besides that, what do you compile your code with and with which flags? There might be a window flag that you missed somewhere, or a missing lib. – RedStoneMatt Jul 05 '23 at 09:34

0 Answers0