| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- MODULE showcase_rectangles ;
- (*
- m2SDL showcase, ported from SDL-main
- examples/renderer/05-rectangles/rectangles.c (public domain).
- Draws animated rectangle outlines, a filled rectangle and a
- bar chart every frame; sizes breathe in and out over 2 seconds.
- SDL3 -> SDL2 adaptations
- - SDL_RenderRect/Rects/FillRect/FillRects become the SDL2
- SDL_RenderDrawRect(s) / SDL_RenderFillRect(s) spellings.
- - SDL_FRect floats become integer SDLRect; the -1.0..1.0 scale
- is kept as permille (-500..500) with integer arithmetic.
- - Rect arrays pass as ADR(rects) + count (see SDLRender docs).
- - 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_RenderDrawRect, SDL_RenderDrawRects,
- SDL_RenderFillRect, SDL_RenderFillRects;
- FROM SDLRect IMPORT SDLRect, MakeRect;
- FROM SDLUtils IMPORT DrainEvents;
- FROM libc IMPORT printf;
- CONST FrameMs = 16; MaxMs = 8000;
- CONST WinW = 640; WinH = 480;
- VAR
- win: WindowHandle;
- ren: RendererHandle;
- rects: ARRAY [0..15] OF SDLRect;
- quit: BOOLEAN;
- start: LONGCARD;
- now: CARDINAL;
- dir, sm, i, size, w, h: INTEGER;
- BEGIN
- IF SDL_Init(SDLInitVideo) # 0 THEN
- printf("showcase_rectangles: SDL_Init failed\n");
- HALT(1)
- END;
- IF SDL_CreateWindowAndRenderer(WinW, WinH, WindowResizable,
- win, ren) # 0 THEN
- printf("showcase_rectangles: CreateWindowAndRenderer failed\n");
- SDL_Quit;
- HALT(1)
- END;
- SDL_RenderSetLogicalSize(ren, WinW, WinH);
- printf("showcase_rectangles: close window or wait 8s\n");
- quit := FALSE;
- start := SDL_GetTicks64();
- WHILE NOT quit DO
- DrainEvents(quit);
- now := SDL_GetTicks();
- (* breathe: +1 for one second, -1 for the next. *)
- IF VAL(INTEGER, now MOD 2000) >= 1000 THEN dir := 1 ELSE dir := -1 END;
- (* scale in permille, -500..500. *)
- sm := (VAL(INTEGER, now MOD 1000) - 500) * dir;
- SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 0), VAL(CARDINAL8, 0),
- VAL(CARDINAL8, 0), VAL(CARDINAL8, 255));
- SDL_RenderClear(ren);
- (* single outlined square, top left. *)
- rects[0] := MakeRect(100, 100, 100 + 100 * sm DIV 500,
- 100 + 100 * sm DIV 500);
- SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 255), VAL(CARDINAL8, 0),
- VAL(CARDINAL8, 0), VAL(CARDINAL8, 255));
- SDL_RenderDrawRect(ren, rects[0]);
- (* three centered outlines at once. *)
- FOR i := 0 TO 2 DO
- size := (i + 1) * 50;
- w := size + size * sm DIV 500;
- rects[i] := MakeRect((WinW - w) DIV 2, (WinH - w) DIV 2, w, w)
- END;
- SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 0), VAL(CARDINAL8, 255),
- VAL(CARDINAL8, 0), VAL(CARDINAL8, 255));
- SDL_RenderDrawRects(ren, ADR(rects), 3);
- (* one filled rectangle. *)
- rects[0] := MakeRect(400, 50, 100 + 100 * sm DIV 500,
- 50 + 50 * sm DIV 500);
- SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 0), VAL(CARDINAL8, 0),
- VAL(CARDINAL8, 255), VAL(CARDINAL8, 255));
- SDL_RenderFillRect(ren, rects[0]);
- (* white bar chart along the bottom. *)
- FOR i := 0 TO 15 DO
- w := WinW DIV 16;
- h := i * 8;
- rects[i] := MakeRect(i * w, WinH - h, w, h)
- END;
- SDL_SetRenderDrawColor(ren, VAL(CARDINAL8, 255), VAL(CARDINAL8, 255),
- VAL(CARDINAL8, 255), VAL(CARDINAL8, 255));
- SDL_RenderFillRects(ren, ADR(rects), 16);
- 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_rectangles: bye\n")
- END showcase_rectangles.
|