Recent

Author Topic: sdl2 & win10 64bits problem  (Read 622 times)

fcu

  • Full Member
  • ***
  • Posts: 104
sdl2 & win10 64bits problem
« on: November 07, 2024, 03:03:12 pm »
Hi

this simple program using sdl2 from (https://github.com/PascalGameDevelopment/SDL2-for-Pascal)
crashes at startup , but if i explicitly specify opengl as default driver it works fine   

can some one please confirm this on windows 64bits & sdl2 64bits
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. {$apptype gui}
  3. program demo;
  4. uses
  5.  sdl2;
  6.  
  7. var
  8.  win : psdl_window;
  9.  render: psdl_renderer;
  10.  ev : tsdl_event;
  11.  done: boolean = false;
  12.  
  13. begin
  14. // uncomment this to work ok  
  15. //sdl_sethint(sdl_hint_render_driver,'opengl');
  16.  
  17.  sdl_init(sdl_init_video);
  18.  win := sdl_createwindow('demo',50,50,800,600,sdl_window_shown);
  19.  render := sdl_createrenderer(win,-1,sdl_renderer_accelerated);
  20.  
  21.  while not done do begin
  22.   if boolean(sdl_pollevent(@ev)) then
  23.   begin
  24.    case ev.type_ of
  25.     sdl_quitev:begin
  26.      done:= true;
  27.     end;
  28.    end;
  29.   end;
  30.   sdl_setrenderdrawcolor(render, 32, 38, 42, 255);
  31.   sdl_renderclear(render);
  32.   sdl_renderpresent(render);
  33.  end;
  34.  
  35.  sdl_destroyrenderer(render);
  36.  sdl_destroywindow(win);
  37. end.
  38.  
  39.  



Fibonacci

  • Hero Member
  • *****
  • Posts: 615
  • Internal Error Hunter
Re: sdl2 & win10 64bits problem
« Reply #1 on: November 07, 2024, 03:19:26 pm »
Tested on Windows 10 x64 with 64 bit app. No crash. DLL version 2.0.20 as reported by SDL_GetVersion().

But with the line uncommented the window shows, hides instantly and then shows again after ~500ms maybe.
« Last Edit: November 07, 2024, 03:28:24 pm by Fibonacci »

MKGilby

  • Newbie
  • Posts: 1
Re: sdl2 & win10 64bits problem
« Reply #2 on: November 07, 2024, 03:22:33 pm »
Hi! I compiled and ran your code without problems on Windows 11 24H2 with Lazarus 4.0 rc1, SDL2-for-pascal (from GitHub) and SDL2.dll V2.30.6.0

fcu

  • Full Member
  • ***
  • Posts: 104
Re: sdl2 & win10 64bits problem
« Reply #3 on: November 07, 2024, 04:30:28 pm »
Tested on Windows 10 x64 with 64 bit app. No crash. DLL version 2.0.20 as reported by SDL_GetVersion().

But with the line uncommented the window shows, hides instantly and then shows again after ~500ms maybe.

strange 
the same program in c++ works fine  in my win10 64bits
this is the error message

fcu

  • Full Member
  • ***
  • Posts: 104
Re: sdl2 & win10 64bits problem
« Reply #4 on: November 07, 2024, 04:34:58 pm »
i think the default renderer driver in windows is direct3d   
because if i set sdl_sethint(sdl_hint_render_driver,'direct3d'); the app crashes but works with sdl_sethint(sdl_hint_render_driver,'opengl');

DrakkTheSeafarer

  • Newbie
  • Posts: 5
Re: sdl2 & win10 64bits problem
« Reply #5 on: November 07, 2024, 10:31:02 pm »
you need to call SDL_Quit;

https://wiki.libsdl.org/SDL2/SDL_Quit

this should be called every time the program quits.

you could use exceptions + try.. finally to clean up everything fine.

https://www.freepascal-meets-sdl.net/error-handling/

greetings

flowCRANE

  • Hero Member
  • *****
  • Posts: 899
Re: sdl2 & win10 64bits problem
« Reply #6 on: November 18, 2024, 02:23:20 am »
@fcu: you are processing the SDL event queue incorrectly. In each frame of the game, you should process all events that exist in the queue, i.e. use a loop:

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. {$apptype gui}
  3.  
  4. program Demo;
  5. uses
  6.   SDL2;
  7. var
  8.   Window:   PSDL_Window;
  9.   Renderer: PSDL_Renderer;
  10.   Event:    TSDL_Event;
  11.   Done:     Boolean = False;
  12. begin
  13.   SDL_Init(SDL_INIT_VIDEO);
  14.  
  15.   Window   := SDL_CreateWindow  ('Demo', 50, 50, 800, 600, SDL_WINDOW_SHOWN);
  16.   Renderer := SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED);
  17.  
  18.   while not Done do
  19.   begin
  20.     while SDL_PollEvent(@Event) = 1 do
  21.     case Event.Type_ of
  22.       SDL_QUITEV: Done := True;
  23.     end;
  24.  
  25.     SDL_SetRenderDrawColor(Renderer, 32, 38, 42, 255);
  26.     SDL_RenderClear       (Renderer);
  27.     SDL_RenderPresent     (Renderer);
  28.   end;
  29.  
  30.   SDL_DestroyRenderer(Renderer);
  31.   SDL_DestroyWindow  (Window);
  32.   SDL_Quit();
  33. end.

Instead of the Done flag, you can simply jump to the label. After all, rendering the frame when the loop is about to end is unnecessary:

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. {$apptype gui}
  3.  
  4. program Demo;
  5. uses
  6.   SDL2;
  7. label
  8.   Cleanup;
  9. var
  10.   Window:   PSDL_Window;
  11.   Renderer: PSDL_Renderer;
  12.   Event:    TSDL_Event;
  13. begin
  14.   SDL_Init(SDL_INIT_VIDEO);
  15.  
  16.   Window   := SDL_CreateWindow  ('Demo', 50, 50, 800, 600, SDL_WINDOW_SHOWN);
  17.   Renderer := SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED);
  18.  
  19.   repeat
  20.     while SDL_PollEvent(@Event) = 1 do
  21.     case Event.Type_ of
  22.       SDL_QUITEV: goto Cleanup;
  23.     end;
  24.  
  25.     SDL_SetRenderDrawColor(Renderer, 32, 38, 42, 255);
  26.     SDL_RenderClear       (Renderer);
  27.     SDL_RenderPresent     (Renderer);
  28.   until False;
  29.  
  30. Cleanup:
  31.   SDL_DestroyRenderer(Renderer);
  32.   SDL_DestroyWindow  (Window);
  33.   SDL_Quit();
  34. end.

Pro tip: start using the Shift key and format your code properly. I know it's a test program, but that's how you can recognize a professional, that they always write readable and formatted code.

By the way, SDL3 is already available (preview version) — take an interest in it, because it is much better than SDL2 and soon it will be the standard. Of course, if you can write your own headers, because you won't find them on the net for a long time (I wrote these myself, for my own game — nothing difficult).
« Last Edit: November 18, 2024, 02:25:38 am by flowCRANE »
Lazarus 3.6 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on a retro-style action/adventure game (pixel art), programming the engine from scratch, using Free Pascal and SDL3.

 

TinyPortal © 2005-2018