From 27879d12f5a9f00b7c6b0d8fb9047710d82046bf Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 8 Oct 2022 20:33:41 +0200 Subject: Fix SDL2 window creation on Wayland See this post: https://github.com/libsdl-org/SDL/issues/6074 It seems like SDL2 does not show an empty window on Wayland systems if there is nothing drawn on this. Thus this commit adds an empty white pixel buffer. --- sdl2.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/sdl2.c b/sdl2.c index e8a12f9..6b30891 100644 --- a/sdl2.c +++ b/sdl2.c @@ -2,8 +2,6 @@ /* mention the SDL2 documentation at http://wiki.libsdl.org/APIByCategory */ -/* WARNING this seems to be not working on Wayland! */ - #include #include @@ -17,6 +15,9 @@ void gui_run(void) { bool quit = false; SDL_Event event; SDL_Window* window; + SDL_Renderer* renderer; + SDL_Texture* texture; + uint32_t* pixels; // init SDL2 and create window SDL_Init(SDL_INIT_VIDEO); @@ -28,12 +29,20 @@ void gui_run(void) { // output video driver to stderr fprintf(stderr, "SDL2 video driver: %s\n", SDL_GetCurrentVideoDriver()); + // create and initialize renderer, texture and pixel buffer + renderer = SDL_CreateRenderer(window, -1, 0); + texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, + SDL_TEXTUREACCESS_STATIC, 640, 480); + pixels = malloc(sizeof(uint32_t) * 640 * 480); + memset(pixels, 255, 640 * 480 * sizeof(uint32_t)); + // handle failed window creation if (window == NULL) { fprintf(stderr, "Failed to create SDL2 window!\n"); return; } else { while (!quit) { + SDL_UpdateTexture(texture, NULL, pixels, 640 * sizeof(uint32_t)); SDL_WaitEvent(&event); switch (event.type) { @@ -41,10 +50,16 @@ void gui_run(void) { quit = true; break; } + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, texture, NULL, NULL); + SDL_RenderPresent(renderer); } SDL_DestroyWindow(window); } + free(pixels); + SDL_DestroyTexture(texture); + SDL_DestroyRenderer(renderer); SDL_Quit(); } -- cgit v1.2.3-70-g09d2