0

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
Yolwoocle
  • 42
  • 1
  • 3
  • Your code works for me, on Ubuntu 22.04 LTS. Apparently I have libsdl2 2.0.20. – pmacfarlane Jun 26 '23 at 19:44
  • Similar issue? https://stackoverflow.com/q/76552222/14772619 – dimich Jun 26 '23 at 20:07
  • This should be easy. AFAICT, you're doing all the right steps. Note that you probably _do_ need the `SDL_RenderClear/SDL_RenderPresent` calls. There are _many_ `SDL` questions/tutorials on SO. Here's one of mine: [How can I display a sudoku solution as an image using C](https://stackoverflow.com/a/67726303/5382650) – Craig Estey Jun 26 '23 at 20:19
  • I have to concur: I downloaded, built, and ran your program unchanged. I got a blank/black window. So, I'm unable to reproduce the issue (i.e. your program is correct). Are you sure SDL2 is installed correctly? The command I used came from: `pkg-config --cflags --libs sdl2` and the final command is: `gcc ./main.c -o out -Wextra -Wall -Werror -I/usr/include/SDL2 -D_REENTRANT -lSDL2` – Craig Estey Jun 26 '23 at 20:24
  • Thank you for your answers. The issue @dimich linked mentions deleting and reinstalling my Linux image, which somehow solved the issue. I am on a recently installed dual boot Windows/Ubuntu system, so maybe there was a problem while installing Ubuntu. – Yolwoocle Jun 27 '23 at 07:38

0 Answers0