0

I have the following code to create a window, fill it with red, and display it. This was mostly taken directly from some other answer on here, so should be perfectly fine.

// test.cpp
#include <SDL2/SDL.h>

int main(int argc, char** argv)
{
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Window* window = NULL;
    window = SDL_CreateWindow(
            "Title",
            SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
            640, 480,
            SDL_WINDOW_SHOWN
            );

    SDL_Renderer* renderer = NULL;
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);

    SDL_Delay(5000);

    SDL_DestroyWindow(window);
    SDL_Quit();

    return EXIT_SUCCESS;
}

I compile with g++ -std=c++20 test.cpp -lSDL2, no errors are raised. Then I run the resulting executable, and all I see is this: enter image description here for 5 seconds. The basic entity of a window is shown I guess, but neither are its dimensions nor its contents.

Anyone have any idea what might be happening? I'm on Ubuntu Studio 22.04.


EDIT: I have now changed the SDL_RenderPresent(renderer); to a code block containing an event loop:

    SDL_bool quit = SDL_FALSE;
    while(!quit)
    {
        SDL_RenderPresent(renderer);
        SDL_Event event;
        SDL_WaitEvent(&event);
        if(event.type == SDL_QUIT)
            quit = SDL_TRUE;
    }

Now a red window is shown if the mouse is in the window, but the window is black if the mouse is outside of it. Which is a whole new problem, because nowhere does my code state that it should do that.

Jo Mo
  • 155
  • 7
  • You need to process all the events prior to rendering a frame. You need to invoke at least `SDL_RenderClear` prior to each `SDL_RenderPresent`. Right now you are presenting frames with undefined content. – user7860670 May 21 '23 at 08:48
  • Ok, I didn't know that I had to RenderClear for each RenderPresent. I guess I have to brush up on how renderers work. Thanks – Jo Mo May 21 '23 at 08:56
  • Please don't stack "edit"s at the bottom of the question, this is conusing. Edit the question directly, so that the code is always in the up-to-date state. (I was going to jump in with "you need an event loop" based on the original code, then noticed the "edit".) – HolyBlackCat May 21 '23 at 09:00

1 Answers1

0

Thanks to the comment below the question, the following works as expected:

// test.cpp
#include <SDL2/SDL.h>

int main(int argc, char** argv)
{
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Window* window = NULL;
    window = SDL_CreateWindow(
            "Title",
            SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
            640, 480,
            SDL_WINDOW_SHOWN
            );

    SDL_Renderer* renderer = NULL;
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    
    SDL_bool quit = SDL_FALSE;
    while(!quit)
    {
        SDL_Event event;
        while(SDL_PollEvent(&e))
        {
            if(event.type == SDL_QUIT)
                quit = SDL_TRUE;
        }
        SDL_RenderClear(renderer);
        SDL_RenderPresent(renderer);
    }

    SDL_Delay(5000);

    SDL_DestroyWindow(window);
    SDL_Quit();

    return EXIT_SUCCESS;
}

There are some weird side-effects on other windows, such as firefox, which get banged up a bit, but this is a known issue between SDL2 and various KDE-friendly window managers.

Jo Mo
  • 155
  • 7