Can not get SDL_Window to show up in Pop!_OS 22.04

I have the following simple C++ code:

#include <iostream>
#include <sstream>

#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>

int main(int argc, char* argv[])
{
    SDL_LogSetPriority(SDL_LOG_CATEGORY_ERROR, SDL_LOG_PRIORITY_ERROR);
    
    if (SDL_Init(SDL_INIT_VIDEO) == -1) 
    {
        SDL_LogError(SDL_LOG_CATEGORY_ERROR, "[SDL Initialization] Error during SDL initialization: %s", SDL_GetError());
        return 1;
    }
    std::atexit(SDL_Quit);

    SDL_Window* win = nullptr;
    win = SDL_CreateWindow("Hello SDL!", 100, 100, 800, 600, SDL_WINDOW_SHOWN);
    if (win == nullptr) 
    {
        SDL_LogError(SDL_LOG_CATEGORY_ERROR, "[Windows creation] Error during SDL initialization: %s", SDL_GetError());
    }

    SDL_Renderer* ren = nullptr;
    ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (ren == nullptr) 
    {
        SDL_LogError(SDL_LOG_CATEGORY_ERROR, "[Renderer creation] Error during the creation of an SDL renderer: %s", SDL_GetError());
        SDL_DestroyWindow(win);
        return 1;
    }

    SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
    SDL_RenderClear(ren);

    SDL_SetRenderDrawColor(ren, 0, 255, 0, 255);
    SDL_RenderDrawLine(ren, 10, 10, 10, 60);

    SDL_RenderPresent(ren);

    SDL_Delay(2000);

    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);

    return 0;
}

I installed SDL2 using vcpkg and I’m using CMake to simplify the build process. Here’s CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)

project(SDL2_HelloWorld LANGUAGES CXX)

find_package(SDL2 CONFIG REQUIRED)

include_directories(${SDL2_INCLUDE_DIRS})

add_executable(SDL2_HelloWorld src/main.cpp)

target_link_libraries(SDL2_HelloWorld PRIVATE SDL2::SDL2 SDL2::SDL2main)

Building finishes without any errors. Running it produces a two-second delay (due to the SDL_Delay call) and then the application quits. No window ever shows up. No runtime errors either. Any ideas?

Wayland? See SDL2 can not show window in Ubuntu 22.04 - #5 by rtrussell

Nah, I’m using the default X Window that comes with the distro. I also tried the example code at Lazy Foo' Productions - Hello SDL: Your First Graphics Window which uses event polling, but it just hangs forever without displaying a window.

Share the output with

--- a.cpp       2024-09-18 08:59:00.622025344 +0000
+++ b.cpp       2024-09-18 09:19:56.805093942 +0000
@@ -8,13 +8,18 @@
 {
     SDL_LogSetPriority(SDL_LOG_CATEGORY_ERROR, SDL_LOG_PRIORITY_ERROR);
     
-    if (SDL_Init(SDL_INIT_VIDEO) == -1) 
+    if (SDL_Init(SDL_INIT_VIDEO) < 0) 
     {
         SDL_LogError(SDL_LOG_CATEGORY_ERROR, "[SDL Initialization] Error during SDL initialization: %s", SDL_GetError());
         return 1;
     }
     std::atexit(SDL_Quit);
 
+    int nvd = SDL_GetNumVideoDrivers();
+    for (int i = 0; i < nvd; i++) {
+        std::cout << "driver: " << SDL_GetVideoDriver(i) << '\n';
+    }
+
     SDL_Window* win = nullptr;
     win = SDL_CreateWindow("Hello SDL!", 100, 100, 800, 600, SDL_WINDOW_SHOWN);
     if (win == nullptr) 

It prints the following:

driver: offscreen
driver: dummy
driver: evdev

wtf man… “offscreen” “dummy”. Is this normal??

I have a Ryzen 5 7535HS CPU with an integrated Radeon GPU and a dedicated Nvidia RTX 3050 by the way. I tried forcing Pop!_OS to only use the dedicated Nvidia card, but it didn’t help.

It is normal but not for you I guess :grin:. vcpkg installs libraries from source AFAIK. SDL will enable features on its own only if you have required dependencies (and it looks like you miss something to build it with x11 support). You should install development dependencies and rebuild your SDL.

I don’t know Pop OS, but can’t you just install the SDL development package (libsdl2-dev probably)? You will get a prebuild with all essentials and dependencies handled.

I installed libsdl2-dev through apt, and interestingly it works that way (compiling directly with g++ without using CMake). I probably did not set up the build system correctly, or there is something going on with the vcpkg version of SDL. I’ll look into it. Still, it’s kinda weird that it didn’t throw any errors, even if it’s due to missing dependencies.

Anyway, thanks for the help!

If the dependency for the feature is not found, the feature will simply not be enabled. You still have several video drivers enabled, so SDL will choose the optimal one (offscreen for you). You can guess what offscreen render do.
As for vcpkg you should delete vcpkg’s SDL or rebuild it with x11 dependencies installed.