Skip to content

SDl window resize problem  #151

Description

@pst-82

Hello how can I fix the problem that is visualized in the picture when resizing the sdl window.
What do I need to do to make it scale with the window ?
SDL_WINDOWEVENT: if Event.window.event = SDL_WINDOWEVENT_RESIZED then begin WindowWidth := Event.window.data1; WindowHeight := Event.window.data2; UpdateDestRect(); end;
Code not working

sdl2_screen

Activity

  1. suve commented on Jan 1, 2025

    @suve
    Collaborator

    You need to post more code.

    • How do you set up the SDL window?
    • What does your rendering function look like?
  2. pst-82 commented on Jan 1, 2025

    @pst-82
    Author
    program project1;
    
    {$APPTYPE GUI}
    
    uses
      sdl2, sdl2_image, Windows;
    
    var
      SDLWindow: PSDL_Window;
      SDLRenderer: PSDL_Renderer;
      Running: Boolean;
      Event: TSDL_Event;
    
      WindowWidth, WindowHeight: Integer;
    
    begin
      // Inicjalizacja SDL
      if SDL_Init(SDL_INIT_VIDEO) < 0 then
      begin
        MessageBox(0, 'SDL could not initialize!', 'Error', MB_ICONERROR or MB_OK);
        Halt(1);
      end;
    
      // Ustawienia początkowe okna
      WindowWidth := 800;
      WindowHeight := 600;
    
      // Tworzenie okna SDL
      SDLWindow := SDL_CreateWindow('SDL window', SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WindowWidth, WindowHeight, SDL_WINDOW_RESIZABLE);
      if SDLWindow = nil then
      begin
        MessageBox(0, 'Window could not be created!', 'Error', MB_ICONERROR or MB_OK);
        SDL_Quit;
        Halt(1);
      end;
    
      // Tworzenie renderera
      SDLRenderer := SDL_CreateRenderer(SDLWindow, -1, SDL_RENDERER_ACCELERATED or SDL_RENDERER_PRESENTVSYNC);
      if SDLRenderer = nil then
      begin
        MessageBox(0, 'Renderer could not be created!', 'Error', MB_ICONERROR or MB_OK);
        SDL_DestroyWindow(SDLWindow);
        SDL_Quit;
        Halt(1);
      end;
    
    
       // Pętla główna aplikacji
      Running := True;
      while Running do
      begin
        // Obsługa zdarzeń
        while SDL_PollEvent(@Event) <> 0 do
        begin
          case Event.type_ of
            SDL_QUITEV:
              Running := False;
            SDL_KEYDOWN:
              if Event.key.keysym.sym = SDLK_ESCAPE then
                Running := False;
            SDL_WINDOWEVENT:
              case Event.window.event of
                SDL_WINDOWEVENT_RESIZED:
                  begin
    
    
                  end;
              end;
        end;
    
       // Renderowanie tła
        SDL_SetRenderDrawColor(SDLRenderer, 72, 69, 71, 255); // Szary kolor
        SDL_RenderClear(SDLRenderer); // Wypełnij całe okno
    
        // Zaktualizuj ekran
        SDL_RenderPresent(SDLRenderer);
      end;
    
      // Sprzątanie zasobów
      SDL_DestroyRenderer(SDLRenderer);
      SDL_DestroyWindow(SDLWindow);
      SDL_Quit;
    end.
  3. pst-82 commented on Jan 2, 2025

    @pst-82
    Author

    Does anyone have any idea how this problem can be solved ?

  4. suve commented on Jan 2, 2025

    @suve
    Collaborator

    Is this the actual code, though? It fails to compile for me because of an unclosed block.

    while Running do
    begin // <-- 1st block opened
        while SDL_PollEvent(@Event) <> 0 do
        begin // <-- 2nd block opened
          case Event.type_ of // <-- 3rd block opened
            SDL_QUITEV:
              Running := False;
            SDL_KEYDOWN:
              if Event.key.keysym.sym = SDLK_ESCAPE then
                Running := False;
            SDL_WINDOWEVENT:
              case Event.window.event of // <-- 4th block opened
                SDL_WINDOWEVENT_RESIZED:
                  begin // <-- 5th block opened
    
    
                  end; // <-- close 5th block
              end; // <-- close 4th block
        end; // <-- close 3rd block
    
        SDL_SetRenderDrawColor(SDLRenderer, 72, 69, 71, 255);
        SDL_RenderClear(SDLRenderer);
    
        // Zaktualizuj ekran
        SDL_RenderPresent(SDLRenderer);
      end; <-- close 2nd block

    After I added another end; to the event handling bit, it works and resizes fine.

  5. pst-82 commented on Jan 2, 2025

    @pst-82
    Author
    program project1;
    
    {$APPTYPE GUI}
    
    uses
      sdl2, sdl2_image, Windows;
    
    var
      SDLWindow: PSDL_Window;
      SDLRenderer: PSDL_Renderer;
      Running: Boolean;
      Event: TSDL_Event;
    
      WindowWidth, WindowHeight: Integer;
    
    begin
      // Inicjalizacja SDL
      if SDL_Init(SDL_INIT_VIDEO) < 0 then
      begin
        MessageBox(0, 'SDL could not initialize!', 'Error', MB_ICONERROR or MB_OK);
        Halt(1);
      end;
    
      // Ustawienia początkowe okna
      WindowWidth := 800;
      WindowHeight := 600;
    
      // Tworzenie okna SDL
      SDLWindow := SDL_CreateWindow('SDL window', SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WindowWidth, WindowHeight, SDL_WINDOW_RESIZABLE);
      if SDLWindow = nil then
      begin
        MessageBox(0, 'Window could not be created!', 'Error', MB_ICONERROR or MB_OK);
        SDL_Quit;
        Halt(1);
      end;
    
      // Tworzenie renderera
      SDLRenderer := SDL_CreateRenderer(SDLWindow, -1, SDL_RENDERER_ACCELERATED or SDL_RENDERER_PRESENTVSYNC);
      if SDLRenderer = nil then
      begin
        MessageBox(0, 'Renderer could not be created!', 'Error', MB_ICONERROR or MB_OK);
        SDL_DestroyWindow(SDLWindow);
        SDL_Quit;
        Halt(1);
      end;
    
    
       // Pętla główna aplikacji
      Running := True;
      while Running do
      begin
        // Obsługa zdarzeń
        while SDL_PollEvent(@Event) <> 0 do
        begin
          case Event.type_ of
            SDL_QUITEV:
              Running := False;
            SDL_KEYDOWN:
              if Event.key.keysym.sym = SDLK_ESCAPE then
                Running := False;
            SDL_WINDOWEVENT:
              case Event.window.event of
                SDL_WINDOWEVENT_RESIZED:
                  begin
    
    
                  end;
              end;
        end;
    
        end;
    
       // Renderowanie tła
        SDL_SetRenderDrawColor(SDLRenderer, 72, 69, 71, 255); // Szary kolor
        SDL_RenderClear(SDLRenderer); // Wypełnij całe okno
    
        // Zaktualizuj ekran
        SDL_RenderPresent(SDLRenderer);
      end;
    
    
    
      // Sprzątanie zasobów
      SDL_DestroyRenderer(SDLRenderer);
      SDL_DestroyWindow(SDLWindow);
      SDL_Quit;
    end.

    I mean that the window scales only the background does not scale at the same time as the window

  6. suve commented on Jan 2, 2025

    @suve
    Collaborator

    Hmm. I'm getting the exact same behaviour with an analogue C program - SDL does not create any events while dragging the window corner, only when I finish resizing the window (i.e. let go of the dragging action). You might be better off asking about this in the SDL forums.

  7. pst-82 commented on Jan 2, 2025

    @pst-82
    Author

    Thanks for the hint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions