Hamish Hamish - 1 month ago 7
C Question

Border/Titlebar not properly displaying in SDL OSX

I was just following lazyfoo's SDL tutorial and I ran the sample code as shown here:

#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( "Failed to initialise SDL! SDL_Error: %s\n", SDL_GetError() );
//Create window
if( window == NULL )
printf( "Failed to create window! SDL_Error: %s\n", SDL_GetError() );
//Get window surface
screenSurface = SDL_GetWindowSurface( window );

//Fill the surface white
SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 255, 0, 0 ) );

//Update the surface
SDL_UpdateWindowSurface( window );

//Wait two seconds
SDL_Delay( 2000 );

//Destroy window
SDL_DestroyWindow( window );

//Quit SDL subsystems

return 0;

But for some reason no real border or title bar is being shown, it just displays a white screen. I tried using
SDL_SetWindowBordered but it did nothing. Next I set the background colour to red and from this image you can see there is a titlebar but there is no close or minimize button.Window
Does anyone know why this is happening. Is it just me or is it a problem with mac's?


Since getting rid of SDL_Delay seemed to help, I will try to elaborate a little. If we look at the code of SDL_Delay we can see that it basically does two things:

  • if nanosleep() can be utilized, it does sleep for a time interval;
  • else, it runs in an infinite while loop, checking how much time has passed @ each iteration, and breaking out of the loop after enough time has passed.

Now, I must say that I have never personally coded for osx, so I do not know how exactly does it draw it's windows. However I can assume that for some reason SDL_Delay in your code gets called (and effectively blocks the thread it's called from) before the OS manages to draw the header of the window, and after the delay finishes you immediately destroy the window yourself, thus the header is never properly drawn.