I’m new on this forum, so I hope someone can help me with this…
I have fedora distro 2 hours ago, and I installed g++ and SDL2 and SDL2-devel. The first program I wrote to try is this:
#include <SDL2/SDL.h> #include
const int WIDTH = 900;
const int HEIGHT = 600;
int main()
{
//Initializing SDL
SDL_Init(SDL_INIT_VIDEO);
//Generating a window
SDL_Window* window = SDL_CreateWindow(“Conway’s Game of Life”, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
First of all, make sure you’re checking that SDL_Init() succeeded, and that window creation succeeded.
If that’s not the problem, you might need to run through any events that got added to the event queue during window creation. Between creating the window and the SDL_Delay(5000); line, insert:
SDL_Event event;
while(SDL_PollEvent(&event)) {
// do nothing
}
edit: just FYI, you can post code samples by starting with three backticks on their own line, then end with three backticks on their own line
With the code that you present, the window should automatically close after 5 seconds (the SDL_Delay function pauses your code to allow the CPU to do other tasks, but that also means your code can’t process events in those 5 seconds either).
Rather than SDL_Delay, you will want to use a game loop, and you will need to poll for events as SJR hinted.
One of the best tutorials for SDL2 is the lazyfoo tutorial series, please take a day or two and run through some of those pages from the beginning with “Hello SDL”. You will have so many questions answered in those tutorials.
The reason nothing happens when you try to close the window is because you don’t handle the SDL_QUIT event. If you want the program to end, you need to write the code to make that happen.
To answer your question, many OSes won’t let you do that if you don’t process events. So you’ll need to call WaitEvent or PollEvent. The other thing is chatgpt (you can use it for free, chat.com should bring you to the site) is trained on a lot of SDL code and can give you examples fairly quickly. You should read the documentation of every line you put into your real code but it’s definitely an easy way to get started.
I recommend clicking “offline html” at the the bottom of the sdl wiki to get a copy of the API. It’ll load faster then the wiki website. It should give you docs for sdl2, sdl3 and libs like image, mixer and tff