libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
9.88k stars 1.83k forks source link

SDL changes window size even with SDL_WINDOW_RESIZABLE not set (Windows) #7086

Open madebr opened 1 year ago

madebr commented 1 year ago

Tested with current main (SDL3), but may also be present on SDL2.

Consider the source at the bottom of this post that switches the window between full screen and windowed mode every second.

Common in all issues, is that SDL_RestoreWindow loses the position of the window. After a few iterations, it ends up in the top left location, without the title bar visible. I expect it to be restored in a similar location before SDL_MaximizeWindow.

#include "SDL.h"
#include <stdio.h>

static void SetNextColor(SDL_Renderer *renderer) {
    static int c = 0;
    SDL_SetRenderDrawColor(renderer, (c&0x1)*0xff, (c&0x2)*0xff, (c&0x4)*0xff, 0xff);
    c += 1;
}

static SDL_bool flipped = SDL_FALSE;

static Uint32 second_passed(Uint32 interval, void *data) {
    flipped = SDL_TRUE;
    return interval;
}

static int wait_second_while_handling_messages(void) {
    int res = 0;
    SDL_Event event;
    while (!flipped) {
        while (SDL_PollEvent(&event)) {
            switch (event.type) {
            case SDL_QUIT:
                res = 1;
                break;
            default:
                break;
            }
        }
        SDL_Delay(10);
    }
    flipped = SDL_FALSE;
    return res;
}

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

    SDL_Window *window = SDL_CreateWindow("title", 0, 0, 120, 200, 0); // 1
    //SDL_Window *window = SDL_CreateWindow("title", 0, 0, 120, 200, SDL_WINDOW_RESIZABLE); // 2
    //SDL_Window *window = SDL_CreateWindow("title", 0, 0, 120, 200, SDL_WINDOW_BORDERLESS); // 3
    SDL_assert(window != NULL);
    SDL_Renderer *renderer = SDL_CreateRenderer(window, NULL, 0);
    SDL_assert(renderer != NULL);

    SDL_TimerID timer = SDL_AddTimer(1000, second_passed, NULL);

    int w=0;
    int h=0;

    while (1) {
        SetNextColor(renderer);
        SDL_RenderClear(renderer);
        SDL_RenderPresent(renderer);
        SDL_GetWindowSize(window, &w, &h);
        SDL_Log("Window Size    : width: %d, height: %d", w, h);
        if (wait_second_while_handling_messages()) {break;}

        SDL_MaximizeWindow(window);
        SetNextColor(renderer);
        SDL_RenderClear(renderer);
        SDL_RenderPresent(renderer);
        SDL_GetWindowSize(window, &w, &h);
        SDL_Log("Maximized Size : width: %d, height: %d", w, h);
        if (wait_second_while_handling_messages()) {break;}

        SDL_RestoreWindow(window);
    }
    SDL_RemoveTimer(timer);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}
slime73 commented 1 year ago

5240 is probably related too.