showcase_clear.mod 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. MODULE showcase_clear ;
  2. (*
  3. m2SDL3 showcase, ported from SDL upstream
  4. examples/renderer/01-clear/clear.c (public domain).
  5. Clears the window to a smoothly fading color every frame; the
  6. sine-wave trick picks the color from the elapsed time.
  7. SDL3 native form (no SDL2 adaptation needed here):
  8. - SDL_CreateWindowAndRenderer(title, w, h, flags, win^, ren^).
  9. - SDL_SetRenderDrawColorFloat takes 0.0..1.0 SHORTREAL values.
  10. - Callback structure flattened into a plain main loop with
  11. DrainEvents; auto-quit after 8 s so it terminates unattended.
  12. *)
  13. FROM SYSTEM IMPORT CARDINAL8;
  14. FROM SDL3 IMPORT SDL_Init, SDL_Quit, SDL_Delay,
  15. SDL_GetTicks, SDLInitVideo;
  16. FROM SDL3Video IMPORT WindowHandle, SDL_DestroyWindow, WindowResizable;
  17. FROM SDL3Render IMPORT RendererHandle,
  18. SDL_CreateWindowAndRenderer, SDL_DestroyRenderer,
  19. SDL_SetRenderDrawColorFloat, SDL_RenderClear, SDL_RenderPresent,
  20. SDL_SetRenderLogicalPresentation, LogicalLetterbox;
  21. FROM SDL3Utils IMPORT DrainEvents;
  22. FROM MathLib0 IMPORT sin;
  23. FROM libc IMPORT printf;
  24. CONST FrameMs = 16; MaxMs = 8000;
  25. VAR
  26. win: WindowHandle;
  27. ren: RendererHandle;
  28. quit: BOOLEAN;
  29. start: LONGCARD;
  30. t: REAL;
  31. r, g, b: SHORTREAL;
  32. BEGIN
  33. IF NOT SDL_Init(SDLInitVideo) THEN
  34. printf("showcase_clear: SDL_Init failed\n");
  35. HALT(1)
  36. END;
  37. IF NOT SDL_CreateWindowAndRenderer("m2SDL3 clear", 640, 480,
  38. VAL(LONGCARD, WindowResizable),
  39. win, ren) THEN
  40. printf("showcase_clear: CreateWindowAndRenderer failed\n");
  41. SDL_Quit;
  42. HALT(1)
  43. END;
  44. SDL_SetRenderLogicalPresentation(ren, 640, 480, LogicalLetterbox);
  45. printf("showcase_clear: fading, close window or wait 8s\n");
  46. quit := FALSE;
  47. start := SDL_GetTicks();
  48. WHILE NOT quit DO
  49. DrainEvents(quit);
  50. (* seconds since start, as REAL (double). *)
  51. t := LFLOAT(SDL_GetTicks()) / 1000.0;
  52. r := VAL(SHORTREAL, 0.5 + 0.5 * sin(t));
  53. g := VAL(SHORTREAL, 0.5 + 0.5 * sin(t + 2.0944));
  54. b := VAL(SHORTREAL, 0.5 + 0.5 * sin(t + 4.1888));
  55. SDL_SetRenderDrawColorFloat(ren, r, g, b, VAL(SHORTREAL, 1.0));
  56. SDL_RenderClear(ren);
  57. SDL_RenderPresent(ren);
  58. IF SDL_GetTicks() - start > MaxMs THEN quit := TRUE END;
  59. SDL_Delay(FrameMs)
  60. END;
  61. SDL_DestroyRenderer(ren);
  62. SDL_DestroyWindow(win);
  63. SDL_Quit;
  64. printf("showcase_clear: bye\n")
  65. END showcase_clear.