showcase_rectangles.mod 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. MODULE showcase_rectangles ;
  2. (*
  3. m2SDL3 showcase, ported from SDL upstream
  4. examples/renderer/05-rectangles/rectangles.c (public domain).
  5. Draws animated rectangle outlines, a filled rectangle and a
  6. bar chart every frame; sizes breathe in and out over 2 seconds.
  7. SDL3 native form:
  8. - SDL_RenderRect/Rects/FillRect/FillRects take SDL_FRect
  9. (SHORTREAL fields); integer sizes are converted with
  10. VAL(SHORTREAL, VAL(REAL, ...)).
  11. - Rect arrays pass as ADR(rects) + count (see SDL3Render).
  12. - Callback structure flattened into a main loop; auto-quit
  13. after 8 s so the showcase also terminates unattended.
  14. *)
  15. FROM SYSTEM IMPORT ADDRESS, ADR, CARDINAL8;
  16. FROM SDL3 IMPORT SDL_Init, SDL_Quit, SDL_Delay,
  17. SDL_GetTicks, SDLInitVideo;
  18. FROM SDL3Video IMPORT WindowHandle, SDL_DestroyWindow, WindowResizable;
  19. FROM SDL3Render IMPORT RendererHandle,
  20. SDL_CreateWindowAndRenderer, SDL_DestroyRenderer,
  21. SDL_SetRenderDrawColor, SDL_RenderClear, SDL_RenderPresent,
  22. SDL_SetRenderLogicalPresentation, LogicalLetterbox,
  23. SDL_RenderRect, SDL_RenderRects,
  24. SDL_RenderFillRect, SDL_RenderFillRects;
  25. FROM SDL3Rect IMPORT SDLFRect, MakeFRect;
  26. FROM SDL3Utils IMPORT DrainEvents;
  27. FROM libc IMPORT printf;
  28. CONST FrameMs = 16; MaxMs = 8000;
  29. CONST WinW = 640; WinH = 480;
  30. VAR
  31. win: WindowHandle;
  32. ren: RendererHandle;
  33. rects: ARRAY [0..15] OF SDLFRect;
  34. quit: BOOLEAN;
  35. start: LONGCARD;
  36. now: LONGCARD;
  37. dir, sm, i, size, w, h: INTEGER;
  38. PROCEDURE F (v: INTEGER) : SHORTREAL;
  39. BEGIN
  40. RETURN VAL(SHORTREAL, VAL(REAL, v))
  41. END F;
  42. BEGIN
  43. IF NOT SDL_Init(SDLInitVideo) THEN
  44. printf("showcase_rectangles: SDL_Init failed\n");
  45. HALT(1)
  46. END;
  47. IF NOT SDL_CreateWindowAndRenderer("m2SDL3 rectangles",
  48. WinW, WinH,
  49. VAL(LONGCARD, WindowResizable),
  50. win, ren) THEN
  51. printf("showcase_rectangles: CreateWindowAndRenderer failed\n");
  52. SDL_Quit;
  53. HALT(1)
  54. END;
  55. SDL_SetRenderLogicalPresentation(ren, WinW, WinH, LogicalLetterbox);
  56. printf("showcase_rectangles: close window or wait 8s\n");
  57. quit := FALSE;
  58. start := SDL_GetTicks();
  59. WHILE NOT quit DO
  60. DrainEvents(quit);
  61. now := SDL_GetTicks();
  62. (* breathe: +1 for one second, -1 for the next. *)
  63. IF VAL(INTEGER, now MOD 2000) >= 1000 THEN dir := 1 ELSE dir := -1 END;
  64. (* scale in permille, -500..500. *)
  65. sm := (VAL(INTEGER, now MOD 1000) - 500) * dir;
  66. SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 0), VAL(CARDINAL8, 0),
  67. VAL(CARDINAL8, 0), VAL(CARDINAL8, 255));
  68. SDL_RenderClear(ren);
  69. (* single outlined square, top left. *)
  70. rects[0] := MakeFRect(F(100), F(100),
  71. F(100 + 100 * sm DIV 500),
  72. F(100 + 100 * sm DIV 500));
  73. SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 255), VAL(CARDINAL8, 0),
  74. VAL(CARDINAL8, 0), VAL(CARDINAL8, 255));
  75. SDL_RenderRect(ren, rects[0]);
  76. (* three centered outlines at once. *)
  77. FOR i := 0 TO 2 DO
  78. size := (i + 1) * 50;
  79. w := size + size * sm DIV 500;
  80. rects[i] := MakeFRect(F((WinW - w) DIV 2), F((WinH - w) DIV 2),
  81. F(w), F(w))
  82. END;
  83. SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 0), VAL(CARDINAL8, 255),
  84. VAL(CARDINAL8, 0), VAL(CARDINAL8, 255));
  85. SDL_RenderRects(ren, ADR(rects), 3);
  86. (* one filled rectangle. *)
  87. rects[0] := MakeFRect(F(400), F(50),
  88. F(100 + 100 * sm DIV 500),
  89. F(50 + 50 * sm DIV 500));
  90. SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 0), VAL(CARDINAL8, 0),
  91. VAL(CARDINAL8, 255), VAL(CARDINAL8, 255));
  92. SDL_RenderFillRect(ren, rects[0]);
  93. (* white bar chart along the bottom. *)
  94. FOR i := 0 TO 15 DO
  95. w := WinW DIV 16;
  96. h := i * 8;
  97. rects[i] := MakeFRect(F(i * w), F(WinH - h), F(w), F(h))
  98. END;
  99. SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 255), VAL(CARDINAL8, 255),
  100. VAL(CARDINAL8, 255), VAL(CARDINAL8, 255));
  101. SDL_RenderFillRects(ren, ADR(rects), 16);
  102. SDL_RenderPresent(ren);
  103. IF SDL_GetTicks() - start > MaxMs THEN quit := TRUE END;
  104. SDL_Delay(FrameMs)
  105. END;
  106. SDL_DestroyRenderer(ren);
  107. SDL_DestroyWindow(win);
  108. SDL_Quit;
  109. printf("showcase_rectangles: bye\n")
  110. END showcase_rectangles.