Window not showing

Hi there,

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);

SDL_Delay(5000);

SDL_DestroyWindow(window);
SDL_Quit();
}

And I’m compiling with this command:

g++ -o GameOfLife GameOfLife.cpp sdl2-config --cflags --libs

I don’t get any errors, and when I execute my binary, the proccess is running but after 5 seconds it stops, and no window is shown.

Best regards,
Daniel.

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

1 Like

According to the Wayland README you might also have to call SDL_RenderPresent (or equivalent) before the window is shown.

Nothing happens with that insertion. I have checked that both Init and window creation succeeded.

Now, I moved to windows. I have instaled SDL2 library and it works.

g++ version: 14.2.0
SDL2 version: 2.30.11

I have this code:

try.cpp

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

const int WIDTH = 900;
const int HEIGHT = 600;

int main(int argc, char* argv[]) 
{
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window* window = SDL_CreateWindow("Try", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, 0);

    SDL_Delay(5000);
    
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

I compile with this:
g++ try.cpp -o try -I C:\SDL2\include -L C:\SDL2\lib -lmingw32 -lSDL2main -lSDL2

And when I execute my binary, the window is showing:

But I cannot move or minimize/maximize/close the window, it’s like the window interaction is bugged.

What do you suggest ?

Best regards,
Daniel.

Hey, Welcome Erdaniejabe

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.

If you want the window to be resizable you should use the SDL_WINDOW_RESIZABLE flag when creating the window.

SDL_Window* window = SDL_CreateWindow("Try", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_RESIZABLE);

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