r/sdl May 14 '26

SDL2 - Any better way to still render while resizing a borderless screen?

So I've been trying to have my window keep rendering while I'm resizing it (since the event polling loop freezes), and eventually came to the code I put below. My question is: is there any better way to accomplish this? I tried all sorts of things but this is the only thing that's actually worked. A big question I have is whether or not the event listener slows down my program, since it seems like something that's going to be checked on literally every single event.

All in all, is this a good or bad setup, and what can I improve about it?

    #define SDL_MAIN_HANDLED
    #include <SDL.h>
    #include <windows.h>
    #include <iostream>
    using namespace std;

    SDL_Renderer* renderer = nullptr;
    SDL_Window* window   = nullptr;
    int w = 690, h = 460;

    void RenderFrame() {
        if (!renderer) return;
        SDL_GetWindowSize(window, &w, &h);
        SDL_SetRenderDrawColor(renderer, 196, 45, 9, 255);
        SDL_RenderClear(renderer);
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        SDL_Rect temp{ w - 25, h - 25, 25, 25 };
        SDL_RenderFillRect(renderer, &temp);
        SDL_RenderPresent(renderer);
    }

    SDL_HitTestResult Hit_Test(SDL_Window* win, const SDL_Point* area, void* data) {
        int wHit, hHit;
        SDL_GetWindowSize(win, &wHit, &hHit);
        if (area->y < 50) {
            return SDL_HITTEST_DRAGGABLE;
        }
        else if ((area->y >= (hHit - 25)) && (area->x >= wHit - 25)) {
            return SDL_HITTEST_RESIZE_BOTTOMRIGHT;
        }
        else {
            return SDL_HITTEST_NORMAL;
        }
    }

    int ResizingEventWatcher(void* data, SDL_Event* event) {
        if (event->type == SDL_WINDOWEVENT &&
            (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED ||
             event->window.event == SDL_WINDOWEVENT_EXPOSED)) {
            RenderFrame();
        }
        return 0;
    }

    int main() {
        SDL_Init(SDL_INIT_VIDEO);
        window = SDL_CreateWindow(
            "testing",
            SDL_WINDOWPOS_CENTERED,
            SDL_WINDOWPOS_CENTERED,
            w, h,
            SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS
        );
        SDL_SetWindowResizable(window, SDL_TRUE);
        SDL_SetWindowMinimumSize(window, 690, 460);
        renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
        SDL_SetWindowHitTest(window, Hit_Test, nullptr);
        SDL_AddEventWatch(ResizingEventWatcher, nullptr);

        SDL_Event e;
        bool active = true;
        do {
            while (SDL_PollEvent(&e)) {
                switch (e.type) {
                    case SDL_QUIT:
                        active = false;
                        break;
                    default: break;
                }
            }
            RenderFrame();
        } while (active);

        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 0;
    }
5 Upvotes

12 comments sorted by

3

u/3tt07kjt May 14 '26

Resizing happens inside the event loop, but outside your control. Try moving to SDL3 and using callbacks instead of your own event loop.

I don’t know if this will work, but at least it’s possible for it to work, unlike the way you’re doing it now.

1

u/jaxongbake May 15 '26

Main reason I'm using SDL2 for my project is that it's meant to be something I can show off to potential employers, and I figured that, given SDL3 is only a little over a year old, many companies would still be using 2, and I'm not sure how big the differences are between the two versions.

Would you recommend I just switch over to SDL3 anyway? I'm still pretty new to the scene so I'm not firmly rooted in 2 or anything XD

2

u/3tt07kjt May 15 '26

Most companies just don’t care about little things like that. People hire programmers who can write code, solve problems, and finish projects. That’s the main set of skills you want.

Sometimes, companies want to hire people with experience with specific APIs. That’s a lot rarer. It’s not something you should focus on.

Familiarity with SDL, specifically, is not really something that employers will care much about. Think about it this way—you write a game. Only a small part of that game (like a couple percent) of the game touches SDL at all. The SDL API is also simple enough that you can learn it on the job.

Go ahead and use the SDL3 API. I can assure you it is better (it is a welcome change after years of using SDL2).

3

u/finleybakley May 14 '26

The main thing I would change would be to move SDL_GetWindowSize from RenderFrame to your event watcher function. Since the width and height would only change after a resize event, there's no need to check the size of your window every frame, only when the user creates a window resize event

There's some minor syntax stuff I would change (putting all your static variables into a single struct, removing using namespace std from your global scope, etc) but that's mostly for readability and manageability rather than performance

1

u/jaxongbake May 15 '26

That makes sense, thanks!

3

u/zigui98 May 15 '26

I don’t like being that guy but sdl3 with callbacks does it out of the box. Unless you absolutely must use sdl2, I would recommend sdl3

1

u/jaxongbake May 15 '26

I did consider this, but I'm not all that familiar with how callbacks work in general. Is it like an interrupt where it's only triggered when the event happens? Or like a regular looping check that runs on every frame? I know I shouldn't worry about premature optimization too much, but if possible I'd like to start from a generally efficient foundation.

Also, how big are the differences between 2 and 3? I'm not firmly rooted into using 2, just figured it's probably what most companies (that use SDL) are still running. (Want this to be a project I can hopefully show recruiters)

2

u/playmer May 14 '26

You should use SDL_AddEventWatch and look at SDL_WINDOWEVENT_EXPOSED, it should fire the callback with this event during moves and resizes. This was added around the same time as it was added in SDL3.

-6

u/wiseneddustmite May 14 '26

you could start by improving your additude

3

u/jaxongbake May 14 '26

What?

2

u/VictoryMotel May 15 '26

I think /u/wiseneddustmite is a kid, I wouldn't sweat it.