showcase_rectangles.mod 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. MODULE showcase_rectangles ;
  2. (*
  3. m2SDL showcase, ported from SDL-main
  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 -> SDL2 adaptations
  8. - SDL_RenderRect/Rects/FillRect/FillRects become the SDL2
  9. SDL_RenderDrawRect(s) / SDL_RenderFillRect(s) spellings.
  10. - SDL_FRect floats become integer SDLRect; the -1.0..1.0 scale
  11. is kept as permille (-500..500) with integer arithmetic.
  12. - Rect arrays pass as ADR(rects) + count (see SDLRender docs).
  13. - Callback structure flattened into a main loop; auto-quit
  14. after 8 s so the showcase also terminates unattended.
  15. *)
  16. FROM SYSTEM IMPORT ADDRESS, ADR, CARDINAL8;
  17. FROM SDL2 IMPORT SDL_Init, SDL_Quit, SDL_Delay,
  18. SDL_GetTicks, SDL_GetTicks64, SDLInitVideo;
  19. FROM SDLVideo IMPORT WindowHandle, SDL_DestroyWindow, WindowResizable;
  20. FROM SDLRender IMPORT RendererHandle,
  21. SDL_CreateWindowAndRenderer, SDL_DestroyRenderer,
  22. SDL_SetRenderDrawColor, SDL_RenderClear, SDL_RenderPresent,
  23. SDL_RenderSetLogicalSize,
  24. SDL_RenderDrawRect, SDL_RenderDrawRects,
  25. SDL_RenderFillRect, SDL_RenderFillRects;
  26. FROM SDLRect IMPORT SDLRect, MakeRect;
  27. FROM SDLUtils IMPORT DrainEvents;
  28. FROM libc IMPORT printf;
  29. CONST FrameMs = 16; MaxMs = 8000;
  30. CONST WinW = 640; WinH = 480;
  31. VAR
  32. win: WindowHandle;
  33. ren: RendererHandle;
  34. rects: ARRAY [0..15] OF SDLRect;
  35. quit: BOOLEAN;
  36. start: LONGCARD;
  37. now: CARDINAL;
  38. dir, sm, i, size, w, h: INTEGER;
  39. BEGIN
  40. IF SDL_Init(SDLInitVideo) # 0 THEN
  41. printf("showcase_rectangles: SDL_Init failed\n");
  42. HALT(1)
  43. END;
  44. IF SDL_CreateWindowAndRenderer(WinW, WinH, WindowResizable,
  45. win, ren) # 0 THEN
  46. printf("showcase_rectangles: CreateWindowAndRenderer failed\n");
  47. SDL_Quit;
  48. HALT(1)
  49. END;
  50. SDL_RenderSetLogicalSize(ren, WinW, WinH);
  51. printf("showcase_rectangles: close window or wait 8s\n");
  52. quit := FALSE;
  53. start := SDL_GetTicks64();
  54. WHILE NOT quit DO
  55. DrainEvents(quit);
  56. now := SDL_GetTicks();
  57. (* breathe: +1 for one second, -1 for the next. *)
  58. IF VAL(INTEGER, now MOD 2000) >= 1000 THEN dir := 1 ELSE dir := -1 END;
  59. (* scale in permille, -500..500. *)
  60. sm := (VAL(INTEGER, now MOD 1000) - 500) * dir;
  61. SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 0), VAL(CARDINAL8, 0),
  62. VAL(CARDINAL8, 0), VAL(CARDINAL8, 255));
  63. SDL_RenderClear(ren);
  64. (* single outlined square, top left. *)
  65. rects[0] := MakeRect(100, 100, 100 + 100 * sm DIV 500,
  66. 100 + 100 * sm DIV 500);
  67. SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 255), VAL(CARDINAL8, 0),
  68. VAL(CARDINAL8, 0), VAL(CARDINAL8, 255));
  69. SDL_RenderDrawRect(ren, rects[0]);
  70. (* three centered outlines at once. *)
  71. FOR i := 0 TO 2 DO
  72. size := (i + 1) * 50;
  73. w := size + size * sm DIV 500;
  74. rects[i] := MakeRect((WinW - w) DIV 2, (WinH - w) DIV 2, w, w)
  75. END;
  76. SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 0), VAL(CARDINAL8, 255),
  77. VAL(CARDINAL8, 0), VAL(CARDINAL8, 255));
  78. SDL_RenderDrawRects(ren, ADR(rects), 3);
  79. (* one filled rectangle. *)
  80. rects[0] := MakeRect(400, 50, 100 + 100 * sm DIV 500,
  81. 50 + 50 * sm DIV 500);
  82. SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 0), VAL(CARDINAL8, 0),
  83. VAL(CARDINAL8, 255), VAL(CARDINAL8, 255));
  84. SDL_RenderFillRect(ren, rects[0]);
  85. (* white bar chart along the bottom. *)
  86. FOR i := 0 TO 15 DO
  87. w := WinW DIV 16;
  88. h := i * 8;
  89. rects[i] := MakeRect(i * w, WinH - h, w, h)
  90. END;
  91. SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 255), VAL(CARDINAL8, 255),
  92. VAL(CARDINAL8, 255), VAL(CARDINAL8, 255));
  93. SDL_RenderFillRects(ren, ADR(rects), 16);
  94. SDL_RenderPresent(ren);
  95. IF SDL_GetTicks64() - start > MaxMs THEN quit := TRUE END;
  96. SDL_Delay(FrameMs)
  97. END;
  98. SDL_DestroyRenderer(ren);
  99. SDL_DestroyWindow(win);
  100. SDL_Quit;
  101. printf("showcase_rectangles: bye\n")
  102. END showcase_rectangles.