1
\$\begingroup\$

A tutorial's code from LazyFoo wonderful place's 01_hello_SDL page doesn't show a white form, but a desktop's screen, like a screenshot. Something like:

img_1

The code:

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

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_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 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 );

            //Wait two seconds
            SDL_Delay( 2000 );
        }
    }

    //Destroy window
    SDL_DestroyWindow( window );

    //Quit SDL subsystems
    SDL_Quit();

    return 0;
}

Putting PumpEvents(); or while(SDL_PollEvent(&event))... didn't work, unfortunately.

The machine's characteristics:

  • CPU: i7 M620
  • GPU: Intel Core Processor Integrated Graphics Controller
  • GPU Driver: i915
  • RAM: 8GB
  • OS: KDE Neon 5.15.4 64bit, 4.15.0-47 Kernel, fully updated
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

By experimenting, we found that adding a short delay(~100ms) at the very start(after SDL_CreateWindow) fixes the issue. This might mean that there was a conflict between the OS's Dekstop/Window Manager and SDL when the latter renders too quickly. A short delay forces SDL to wait some time while OS's Dekstop/Window Manager handles the window's creation. This might also be something related to the compositor, which in our case - compiz.

In other words, the changes:

...
// Create window
window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
SDL_Delay(100); // A short delay should be put here(if doesn't work try increasing delay)

if(window == NULL)
{
...

With these changes, the white window opens successfully.

'Already wrote to the author of LazyFoo and hopefully they will alter the tutorial.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Hi V.7, I'm glad you found a solution to your problem, but for future readers I'd like to point out this is not a proper solution, just a workaround. \$\endgroup\$ Commented Jan 10, 2020 at 10:16
  • \$\begingroup\$ Sure thing @TomTsagk \$\endgroup\$ Commented Jan 11, 2020 at 22:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.