showcase_lines.mod 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. MODULE showcase_lines ;
  2. (*
  3. m2SDL3 showcase, ported from SDL upstream
  4. examples/renderer/03-lines/lines.c (public domain).
  5. Draws brown house-like line segments, a green polyline batch
  6. and a 360-line multicolor circle every frame.
  7. Porting notes
  8. - SDL_RenderLine/RenderLines take SHORTREAL coordinates;
  9. REAL math (cos/sin) converts via VAL(SHORTREAL, ...).
  10. - SDL_rand(256) becomes libc rand() MOD 256.
  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_RenderLine, SDL_RenderLines;
  23. FROM SDL3Rect IMPORT SDLFPoint, MakeFPoint;
  24. FROM SDL3Utils IMPORT DrainEvents;
  25. FROM MathLib0 IMPORT cos, sin;
  26. FROM libc IMPORT printf, rand, srand;
  27. CONST FrameMs = 16; MaxMs = 8000;
  28. CONST WinW = 640; WinH = 480;
  29. CONST Pi = 3.141592653589793;
  30. VAR
  31. win: WindowHandle;
  32. ren: RendererHandle;
  33. linePts: ARRAY [0..8] OF SDLFPoint;
  34. quit: BOOLEAN;
  35. start: LONGCARD;
  36. i: INTEGER;
  37. ang: REAL;
  38. cx, cy, ex, ey: SHORTREAL;
  39. PROCEDURE F (v: REAL) : SHORTREAL;
  40. BEGIN
  41. RETURN VAL(SHORTREAL, v)
  42. END F;
  43. PROCEDURE Col () : CARDINAL8;
  44. BEGIN
  45. RETURN VAL(CARDINAL8, rand() MOD 256)
  46. END Col;
  47. BEGIN
  48. IF NOT SDL_Init(SDLInitVideo) THEN
  49. printf("showcase_lines: SDL_Init failed\n");
  50. HALT(1)
  51. END;
  52. IF NOT SDL_CreateWindowAndRenderer("m2SDL3 lines",
  53. WinW, WinH,
  54. VAL(LONGCARD, WindowResizable),
  55. win, ren) THEN
  56. printf("showcase_lines: 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. linePts[0] := MakeFPoint(F(100.0), F(354.0));
  63. linePts[1] := MakeFPoint(F(220.0), F(230.0));
  64. linePts[2] := MakeFPoint(F(140.0), F(230.0));
  65. linePts[3] := MakeFPoint(F(320.0), F(100.0));
  66. linePts[4] := MakeFPoint(F(500.0), F(230.0));
  67. linePts[5] := MakeFPoint(F(420.0), F(230.0));
  68. linePts[6] := MakeFPoint(F(540.0), F(354.0));
  69. linePts[7] := MakeFPoint(F(400.0), F(354.0));
  70. linePts[8] := MakeFPoint(F(100.0), F(354.0));
  71. printf("showcase_lines: close window or wait 8s\n");
  72. quit := FALSE;
  73. start := SDL_GetTicks();
  74. WHILE NOT quit DO
  75. DrainEvents(quit);
  76. SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 100), VAL(CARDINAL8, 100),
  77. VAL(CARDINAL8, 100), VAL(CARDINAL8, 255));
  78. SDL_RenderClear(ren);
  79. (* brown house segments, one call per line. *)
  80. SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 127), VAL(CARDINAL8, 49),
  81. VAL(CARDINAL8, 32), VAL(CARDINAL8, 255));
  82. SDL_RenderLine(ren, F(240.0), F(450.0), F(400.0), F(450.0));
  83. SDL_RenderLine(ren, F(240.0), F(356.0), F(400.0), F(356.0));
  84. SDL_RenderLine(ren, F(240.0), F(356.0), F(240.0), F(450.0));
  85. SDL_RenderLine(ren, F(400.0), F(356.0), F(400.0), F(450.0));
  86. (* green polyline batch. *)
  87. SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 0), VAL(CARDINAL8, 255),
  88. VAL(CARDINAL8, 0), VAL(CARDINAL8, 255));
  89. SDL_RenderLines(ren, ADR(linePts), 9);
  90. (* multicolor circle around (320, 80), radius 30. *)
  91. cx := F(320.0); cy := F(80.0);
  92. FOR i := 0 TO 359 DO
  93. ang := VAL(REAL, i) * (Pi / 180.0);
  94. ex := F(320.0 + cos(ang) * 30.0);
  95. ey := F(80.0 + sin(ang) * 30.0);
  96. SDL_SetRenderDrawColor(ren, Col(), Col(), Col(),
  97. VAL(CARDINAL8, 255));
  98. SDL_RenderLine(ren, cx, cy, ex, ey)
  99. END;
  100. SDL_RenderPresent(ren);
  101. IF SDL_GetTicks() - start > MaxMs THEN quit := TRUE END;
  102. SDL_Delay(FrameMs)
  103. END;
  104. SDL_DestroyRenderer(ren);
  105. SDL_DestroyWindow(win);
  106. SDL_Quit;
  107. printf("showcase_lines: bye\n")
  108. END showcase_lines.