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