SDL2 window not showing up on Ubuntu

I am trying to make a simple blank window using SDL2 in C. Here is my code:

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

#include <SDL2/SDL.h>

void manageEvents(bool* launched) {
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
        switch (event.type) {
            case SDL_QUIT:
                *launched = false;
                break;

            default:
                break;
        }
    }
}

int main() {
    if(SDL_Init(SDL_INIT_VIDEO) < 0){
        printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
        return 1;
    }

    SDL_Window* window = SDL_CreateWindow("Mandelbrot :)", 0, 0, 800, 600, SDL_WINDOW_SHOWN);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);


    if (window == NULL) {
        printf("No window pointer\n");
        return 1;
    }
    
    bool launched = true;

    while (launched) {
        manageEvents(&launched);
    }

    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);

    SDL_Quit();

    return 0;
}

When I compile my code, no errors show up. When I run it from the terminal, nothing happens and no window shows up. The while loop seems to run indefinitely, but it does run.

I am using Ubuntu 22.04.2 LTS and SDL 2.28.0.

I have installed SDL using the following commands:

sudo apt-get install libsdl2-2.0-0
sudo apt-get install libsdl2-dev

I have also tried compiling the source code myself by following the instructions on this page, with no success: https://wiki.libsdl.org/SDL2/Installation

The window pointer is not NULL, I am sure.

Adding SDL_ShowWindow(window); before the while loop has not helped.

I tried adding this in my while loop, after the manageEvents(&launched); call:

        SDL_RenderClear(renderer);
        SDL_RenderPresent(renderer);

Here is the command I use to compile it:

gcc ./main.c -o out -Wextra -Wall -Werror -lSDL2

It works here. The problem is neither your source code or your compile command. Maybe your window manager is putting the window somewhere you can’t see it? Do you have multiple monitors?

I’m also on Ubuntu 22.04.2 LTS.

No window seems to show up, I checked using alt + tab, and I only have one monitor. The window also doesn’t seem to show up on other desktops. I’m not sure how I could verify further that it actually doesn’t show up, but it doesn’t seem like it.

Hi buddy. I have actually the same problem. Did you solve yours ? I wanna know what’s wrong

Hey. I did not find a way to fix this issue, I gave up and moved on with my life. Do you also have a recently installed windows/Ubuntu dual boot? I did find this StackOverflow thread, which mentions deleting and reinstalling your Linux image: c - SDL2 window does not show up after running on Ubuntu - Stack Overflow
I hope that helps! If you do find a fix, please answer with your solution to help others as well!

Hi,
your program eats a lot of cpu power.

You can try to change the following lines:

>  SDL_Window* window = SDL_CreateWindow("Mandelbrot :)", 0, 0, 800, 600, 0);
>  SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

My loop:

while (launched) {
        manageEvents(&launched);
        SDL_RenderClear(renderer);
        SDL_RenderPresent(renderer);
}

Best regards
Mik

Thks for your answer. I already have the latest release of Ubutu 22.04.3 LTS.
I need to reinstall also?
Capture d’écran du 2023-12-17 12-39-13

Put the SDL_Delay call with some value at the and of while and your program will runs a little fine. Search for “frajmerate control sdl” and you can see some cool algorithms to learn.