I'm learning to create my first window following LazyFoo's tutorial, everything compile and run, but the surface is not updating when SDL_UpdateWindowSurface() is called, like the image below:
As you can see, the last thing that appearing it's my background. I already test this program on my other laptop, and everything works fine, and i use the same system, both updated.
My code:
#include <SDL2/SDL.h>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main(){
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
if(SDL_Init(SDL_INIT_VIDEO) < 0){
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
}else{
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{
screenSurface = SDL_GetWindowSurface(window);
if(screenSurface == NULL){
printf("Surface could not be loaded SDL_Error: %s\n", SDL_GetError());
}else{
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 255, 255, 255));
SDL_UpdateWindowSurface(window);
SDL_Delay(3000);
}
}
}
}
A Strange thing: If i add SDL_Delay(1000) before SDL_UpdateWindowSurface(), after 1 second i could see the surface filled with white, but this not seems the correct solution since the program work perfectly on my laptop.
Edit 1:
Just realize one thing, my desktop enviroment where i am developing this program, is using as window manager the awesomewm. My laptop is using gnome as DE. I just installed gnome on my desktop and voilá, the program works as expected. Now I'm just curious why this happens. Screenshot of the same program running on Gnome:
Edit 2:
As keltar mentioned, the SDL was updating the surface before my window manager load them. The solution is to handle window event EXPOSED that is when the window loaded by the window manager.
SDL_Event event;
while(SDL_WaitEvent(&event))){
switch(event.type){
case SDL_WINDOWEVENT:
switch(event.window.event){
case SDL_WINDOWEVENT_EXPOSED:
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 255, 255, 255));
SDL_UpdateWindowSurface(window);
SDL_Delay(3000);
SDL_Quit();
break;
}
}
}