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 have found that adding a short delay(~100ms) at the very start(after SDL_CreateWindow) fixes issue. This might mean that there is a conflict between OS's Dekstop/Window Manager and SDL when last renders too quickly. So, adding a short delay tells SDL to wait some time while OS's Dekstop/Window Manager handles window's creation. Also, this might be something related to OS's (D/E)M or its compositor(i.e. compiz) in our case KDE Plasma.

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 )
        {
...

After that, a white window opens successfully.

Already wrote to author of LazyFoo and hopefully he'll commit tutorial.

\$\endgroup\$
2
  • 1
    \$\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\$
    – TomTsagk
    Jan 10, 2020 at 10:16
  • \$\begingroup\$ Sure thing @TomTsagk \$\endgroup\$
    – Faither
    Jan 11, 2020 at 22:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .