| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- MODULE showcase_primitives ;
- (*
- m2SDL showcase, ported from SDL-main
- examples/renderer/02-primitives/primitives.c (public domain).
- Draws a filled rectangle, 500 random points, an inset outline
- and a yellow X across the canvas every frame.
- SDL3 -> SDL2 adaptations
- - SDL_RenderRect/RenderPoints become SDL2
- SDL_RenderDrawRect / SDL_RenderDrawPoints.
- - SDL_FPoint floats become integer SDLPoint; SDL_randf is
- replaced by libc rand seeded from SDL_GetTicks.
- - Point array passes as ADR(points) + count (see SDLRender).
- - Callback structure flattened into a main loop; auto-quit
- after 8 s so the showcase also terminates unattended.
- *)
- FROM SYSTEM IMPORT ADDRESS, ADR, CARDINAL8;
- FROM SDL2 IMPORT SDL_Init, SDL_Quit, SDL_Delay,
- SDL_GetTicks, SDL_GetTicks64, SDLInitVideo;
- FROM SDLVideo IMPORT WindowHandle, SDL_DestroyWindow, WindowResizable;
- FROM SDLRender IMPORT RendererHandle,
- SDL_CreateWindowAndRenderer, SDL_DestroyRenderer,
- SDL_SetRenderDrawColor, SDL_RenderClear, SDL_RenderPresent,
- SDL_RenderSetLogicalSize,
- SDL_RenderDrawLine, SDL_RenderDrawRect, SDL_RenderDrawPoints,
- SDL_RenderFillRect;
- FROM SDLRect IMPORT SDLRect, SDLPoint, MakeRect, MakePoint;
- FROM SDLUtils IMPORT DrainEvents;
- FROM libc IMPORT printf, rand, srand;
- CONST FrameMs = 16; MaxMs = 8000;
- CONST WinW = 640; WinH = 480; NPoints = 500;
- VAR
- win: WindowHandle;
- ren: RendererHandle;
- points: ARRAY [0..NPoints-1] OF SDLPoint;
- rect: SDLRect;
- quit: BOOLEAN;
- start: LONGCARD;
- i: INTEGER;
- BEGIN
- IF SDL_Init(SDLInitVideo) # 0 THEN
- printf("showcase_primitives: SDL_Init failed\n");
- HALT(1)
- END;
- IF SDL_CreateWindowAndRenderer(WinW, WinH, WindowResizable,
- win, ren) # 0 THEN
- printf("showcase_primitives: CreateWindowAndRenderer failed\n");
- SDL_Quit;
- HALT(1)
- END;
- SDL_RenderSetLogicalSize(ren, WinW, WinH);
- (* scatter the points inside the blue rectangle, as in the C code. *)
- srand(VAL(INTEGER, SDL_GetTicks()));
- FOR i := 0 TO NPoints - 1 DO
- points[i] := MakePoint(rand() MOD 440 + 100, rand() MOD 280 + 100)
- END;
- printf("showcase_primitives: close window or wait 8s\n");
- quit := FALSE;
- start := SDL_GetTicks64();
- WHILE NOT quit DO
- DrainEvents(quit);
- SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 33), VAL(CARDINAL8, 33),
- VAL(CARDINAL8, 33), VAL(CARDINAL8, 255));
- SDL_RenderClear(ren);
- rect := MakeRect(100, 100, 440, 280);
- SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 0), VAL(CARDINAL8, 0),
- VAL(CARDINAL8, 255), VAL(CARDINAL8, 255));
- SDL_RenderFillRect(ren, rect);
- SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 255), VAL(CARDINAL8, 0),
- VAL(CARDINAL8, 0), VAL(CARDINAL8, 255));
- SDL_RenderDrawPoints(ren, ADR(points), NPoints);
- rect := MakeRect(130, 130, 380, 220);
- SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 0), VAL(CARDINAL8, 255),
- VAL(CARDINAL8, 0), VAL(CARDINAL8, 255));
- SDL_RenderDrawRect(ren, rect);
- SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 255), VAL(CARDINAL8, 255),
- VAL(CARDINAL8, 0), VAL(CARDINAL8, 255));
- SDL_RenderDrawLine(ren, 0, 0, WinW, WinH);
- SDL_RenderDrawLine(ren, 0, WinH, WinW, 0);
- SDL_RenderPresent(ren);
- IF SDL_GetTicks64() - start > MaxMs THEN quit := TRUE END;
- SDL_Delay(FrameMs)
- END;
- SDL_DestroyRenderer(ren);
- SDL_DestroyWindow(win);
- SDL_Quit;
- printf("showcase_primitives: bye\n")
- END showcase_primitives.
|