PascalGameDevelopment/SDL2-for-Pascal

Resizing the window right after turning off video mode sometimes does not work

flowCRANE opened this issue · 4 comments

In my test program I implemented the toggle fullscreen function. After pressing a given button, the video mode is activated, after pressing a second time it returns to the window mode. However, resizing the window with SDL_SetWindowSize right after SDL_SetWindowFullScreen has no effect in most cases — the window size remains as used in video mode. Most of the time, because sometimes it works.

Code to enable video mode:

procedure TurnOnFullScreen(AWindow: PSDL_Window);
begin
  SDL_SetWindowSize(AWindow, 800, 600);
  SDL_SetWindowFullScreen(AWindow, SDL_WINDOW_FULLSCREEN);
  SDL_ShowCursor(0);
end;

Code to disable video mode, restore window size and center it on the screen:

procedure TurnOffFullScreen(AWindow: PSDL_Window);
var
  DisplayBounds: TSDL_Rect;
  DisplayIndex: SInt32;
begin
  SDL_SetWindowFullscreen(AWindow, 0);
  SDL_ShowCursor(1);

  DisplayIndex := SDL_GetWindowDisplayIndex(AWindow);

  if SDL_GetDisplayBounds(DisplayIndex, @DisplayBounds) = 0 then
  begin
    SDL_SetWindowSize(AWindow, 640, 480);
    SDL_SetWindowPosition(
      AWindow,
      DisplayBounds.X + (DisplayBounds.W - 640) div 2,
      DisplayBounds.Y + (DisplayBounds.H - 480) div 2
    );
  end;
end;

The effect is that the window is sometimes centered correctly and the size does not always change from 800×600 to 640×480. This problem occurs on at least Windows 10.

This problem seems to exists only when the size and position is changed beeing isnide event loop.

For me, your example code works like a charm, also in the event loop.

  while QuitLoop = False do
  begin

    SDL_PumpEvents;
    sdlKeyboardState := SDL_GetKeyboardState(nil);

    // ESC pressed
    if sdlKeyboardState[SDL_SCANCODE_ESCAPE] = 1 then
      QuitLoop := True;

    // Toggle screen
    if sdlKeyboardState[SDL_SCANCODE_1] = 1 then TurnOnFullscreen(sdlWindow1);
    if sdlKeyboardState[SDL_SCANCODE_2] = 1 then TurnOffFullscreen(sdlWindow1);

    // render to window for 0.03 seconds
    SDL_RenderPresent(sdlRenderer);
    SDL_Delay(30);

  end;      

The strange thing is that everything works like a charm in my main project, but not in the small test program. It looks like my tester is wrong but I have to test this again — this time carefully. I will give you a feedback soon.

Sorry for the late reply, but the problem does not exist, and even if it does, it is not the fault of this bindings, but the SDL itself. Apparently, the error was on my side, so it makes no sense to figure it out here.