Recent

Author Topic: SDL2 resize window  (Read 401 times)

Pe3s

  • Hero Member
  • *****
  • Posts: 590
SDL2 resize window
« on: November 15, 2024, 11:50:23 pm »
Hello forum members, how can I get rid of the window copy effect when resizing (empty window) without an open image?
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, ExtCtrls,
  9.   StdCtrls, SDL2, SDL2_image, Math;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     Button3: TButton;
  19.     Button4: TButton;
  20.     OpenDialog1: TOpenDialog;
  21.     Panel1: TPanel;
  22.     Panel2: TPanel;
  23.     StatusBar1: TStatusBar;
  24.     procedure Button1Click(Sender: TObject);
  25.     procedure FormDestroy(Sender: TObject);
  26.     procedure FormResize(Sender: TObject);
  27.     procedure FormShow(Sender: TObject);
  28.   private
  29.       SDLWindow: PSDL_Window;
  30.       SDLRenderer: PSDL_Renderer;
  31.       CurrentTexture: PSDL_Texture;
  32.       procedure InitializeSDL;
  33.       procedure ShutdownSDL;
  34.       procedure LoadAndDisplayImage(const FileName: string);
  35.       procedure RenderImage;
  36.  
  37.   public
  38.  
  39.   end;
  40.  
  41. var
  42.   Form1: TForm1;
  43.  
  44. implementation
  45.  
  46. {$R *.lfm}
  47.  
  48. { TForm1 }
  49.  
  50. procedure TForm1.Button1Click(Sender: TObject);
  51. begin
  52.   if OpenDialog1.Execute then
  53.     LoadAndDisplayImage(OpenDialog1.FileName);
  54. end;
  55.  
  56. procedure TForm1.FormDestroy(Sender: TObject);
  57. begin
  58.   ShutdownSDL;
  59. end;
  60.  
  61. procedure TForm1.FormResize(Sender: TObject);
  62. begin
  63.   RenderImage;
  64. end;
  65.  
  66. procedure TForm1.FormShow(Sender: TObject);
  67. begin
  68.    InitializeSDL;
  69. end;
  70.  
  71. procedure TForm1.InitializeSDL;
  72. begin
  73.   if SDL_Init(SDL_INIT_VIDEO) < 0 then
  74.     raise Exception.Create('Could not initialize SDL: ' + SDL_GetError);
  75.  
  76.   SDLWindow := SDL_CreateWindowFrom(Pointer(Panel1.Handle));
  77.   if SDLWindow = nil then
  78.     raise Exception.Create('Could not create SDL window: ' + SDL_GetError);
  79.  
  80.   SDLRenderer := SDL_CreateRenderer(SDLWindow, -1, SDL_RENDERER_ACCELERATED);
  81.   if SDLRenderer = nil then
  82.     raise Exception.Create('Could not create SDL renderer: ' + SDL_GetError);
  83.  
  84.   if IMG_Init(IMG_INIT_PNG or IMG_INIT_JPG) = 0 then
  85.     raise Exception.Create('Could not initialize SDL_image: ' + IMG_GetError);
  86. end;
  87.  
  88. procedure TForm1.ShutdownSDL;
  89. begin
  90.    if CurrentTexture <> nil then
  91.     SDL_DestroyTexture(CurrentTexture);
  92.   if SDLRenderer <> nil then
  93.     SDL_DestroyRenderer(SDLRenderer);
  94.   if SDLWindow <> nil then
  95.     SDL_DestroyWindow(SDLWindow);
  96.   IMG_Quit;
  97.   SDL_Quit;
  98. end;
  99.  
  100. procedure TForm1.LoadAndDisplayImage(const FileName: string);
  101. begin
  102.    // Clean up the old texture if it exists
  103.   if CurrentTexture <> nil then
  104.     SDL_DestroyTexture(CurrentTexture);
  105.  
  106.   // Load the new image as a texture
  107.   CurrentTexture := IMG_LoadTexture(SDLRenderer, PChar(FileName));
  108.   if CurrentTexture = nil then
  109.     raise Exception.Create('Could not load texture: ' + IMG_GetError);
  110.  
  111.   // Render the image to fit the new size
  112.   RenderImage;
  113. end;
  114.  
  115. procedure TForm1.RenderImage;
  116. var
  117.   SrcRect, DstRect: TSDL_Rect;
  118.   ImgWidth, ImgHeight: Integer;
  119.   PanelWidth, PanelHeight: Integer;
  120.   AspectRatio: Double;
  121. begin
  122.   if CurrentTexture = nil then
  123.     Exit;
  124.  
  125.   // Get image dimensions
  126.   SDL_QueryTexture(CurrentTexture, nil, nil, @ImgWidth, @ImgHeight);
  127.  
  128.   // Get panel dimensions
  129.   PanelWidth := Panel1.Width;
  130.   PanelHeight := Panel1.Height;
  131.  
  132.   // Calculate aspect ratio to fit the image proportionally
  133.   AspectRatio := Min(PanelWidth / ImgWidth, PanelHeight / ImgHeight);
  134.  
  135.   DstRect.w := Round(ImgWidth * AspectRatio);
  136.   DstRect.h := Round(ImgHeight * AspectRatio);
  137.   DstRect.x := (PanelWidth - DstRect.w) div 2;
  138.   DstRect.y := (PanelHeight - DstRect.h) div 2;
  139.  
  140.   // Render the image
  141.   SDL_RenderClear(SDLRenderer);
  142.   SDL_RenderCopy(SDLRenderer, CurrentTexture, nil, @DstRect);
  143.   SDL_RenderPresent(SDLRenderer);
  144.  
  145. end;
  146.  
  147. end.
  148.  
  149.  

 

TinyPortal © 2005-2018