showcase_points.mod 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. MODULE showcase_points ;
  2. (*
  3. m2SDL3 showcase, ported from SDL upstream
  4. examples/renderer/04-points/points.c (public domain).
  5. 500 white points drift down-right at 30..60 px/s; points that
  6. leave the screen respawn at the top or left edge.
  7. Porting notes
  8. - SDL_randf() becomes (rand() MOD 10000) / 10000.0 with libc rand.
  9. - Positions/speeds live in parallel arrays so the whole set goes
  10. to SDL_RenderPoints in one call, as in the C version.
  11. - Callback structure flattened into a main loop; auto-quit
  12. after 8 s so the showcase also terminates unattended.
  13. *)
  14. FROM SYSTEM IMPORT ADDRESS, ADR, CARDINAL8;
  15. FROM SDL3 IMPORT SDL_Init, SDL_Quit, SDL_Delay,
  16. SDL_GetTicks, SDLInitVideo;
  17. FROM SDL3Video IMPORT WindowHandle, SDL_DestroyWindow, WindowResizable;
  18. FROM SDL3Render IMPORT RendererHandle,
  19. SDL_CreateWindowAndRenderer, SDL_DestroyRenderer,
  20. SDL_SetRenderDrawColor, SDL_RenderClear, SDL_RenderPresent,
  21. SDL_SetRenderLogicalPresentation, LogicalLetterbox,
  22. SDL_RenderPoints;
  23. FROM SDL3Rect IMPORT SDLFPoint, MakeFPoint;
  24. FROM SDL3Utils IMPORT DrainEvents;
  25. FROM libc IMPORT printf, rand, srand;
  26. CONST FrameMs = 16; MaxMs = 8000;
  27. CONST WinW = 640; WinH = 480; NPoints = 500;
  28. CONST MinSpeed = 30.0; MaxSpeed = 60.0;
  29. VAR
  30. win: WindowHandle;
  31. ren: RendererHandle;
  32. points: ARRAY [0..NPoints-1] OF SDLFPoint;
  33. speeds: ARRAY [0..NPoints-1] OF REAL;
  34. quit: BOOLEAN;
  35. start, last, now: LONGCARD;
  36. elapsed: REAL;
  37. i: INTEGER;
  38. (* Random REAL in [0.0, 1.0). *)
  39. PROCEDURE RandF () : REAL;
  40. BEGIN
  41. RETURN VAL(REAL, rand() MOD 10000) / 10000.0
  42. END RandF;
  43. PROCEDURE RandSpeed () : REAL;
  44. BEGIN
  45. RETURN MinSpeed + RandF() * (MaxSpeed - MinSpeed)
  46. END RandSpeed;
  47. BEGIN
  48. IF NOT SDL_Init(SDLInitVideo) THEN
  49. printf("showcase_points: SDL_Init failed\n");
  50. HALT(1)
  51. END;
  52. IF NOT SDL_CreateWindowAndRenderer("m2SDL3 points",
  53. WinW, WinH,
  54. VAL(LONGCARD, WindowResizable),
  55. win, ren) THEN
  56. printf("showcase_points: CreateWindowAndRenderer failed\n");
  57. SDL_Quit;
  58. HALT(1)
  59. END;
  60. SDL_SetRenderLogicalPresentation(ren, WinW, WinH, LogicalLetterbox);
  61. srand(VAL(INTEGER, SDL_GetTicks()));
  62. FOR i := 0 TO NPoints - 1 DO
  63. points[i] := MakeFPoint(VAL(SHORTREAL, RandF() * VAL(REAL, WinW)),
  64. VAL(SHORTREAL, RandF() * VAL(REAL, WinH)));
  65. speeds[i] := RandSpeed()
  66. END;
  67. printf("showcase_points: close window or wait 8s\n");
  68. quit := FALSE;
  69. start := SDL_GetTicks();
  70. last := start;
  71. WHILE NOT quit DO
  72. DrainEvents(quit);
  73. now := SDL_GetTicks();
  74. elapsed := VAL(REAL, now - last) / 1000.0;
  75. last := now;
  76. FOR i := 0 TO NPoints - 1 DO
  77. points[i].x := VAL(SHORTREAL,
  78. VAL(REAL, points[i].x)
  79. + elapsed * speeds[i]);
  80. points[i].y := VAL(SHORTREAL,
  81. VAL(REAL, points[i].y)
  82. + elapsed * speeds[i]);
  83. IF (VAL(REAL, points[i].x) >= VAL(REAL, WinW))
  84. OR (VAL(REAL, points[i].y) >= VAL(REAL, WinH)) THEN
  85. IF rand() MOD 2 = 0 THEN
  86. points[i].x := VAL(SHORTREAL, RandF() * VAL(REAL, WinW));
  87. points[i].y := VAL(SHORTREAL, 0.0)
  88. ELSE
  89. points[i].x := VAL(SHORTREAL, 0.0);
  90. points[i].y := VAL(SHORTREAL, RandF() * VAL(REAL, WinH))
  91. END;
  92. speeds[i] := RandSpeed()
  93. END
  94. END;
  95. SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 0), VAL(CARDINAL8, 0),
  96. VAL(CARDINAL8, 0), VAL(CARDINAL8, 255));
  97. SDL_RenderClear(ren);
  98. SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 255), VAL(CARDINAL8, 255),
  99. VAL(CARDINAL8, 255), VAL(CARDINAL8, 255));
  100. SDL_RenderPoints(ren, ADR(points), NPoints);
  101. SDL_RenderPresent(ren);
  102. IF now - start > MaxMs THEN quit := TRUE END;
  103. SDL_Delay(FrameMs)
  104. END;
  105. SDL_DestroyRenderer(ren);
  106. SDL_DestroyWindow(win);
  107. SDL_Quit;
  108. printf("showcase_points: bye\n")
  109. END showcase_points.