|
| 1 | +{ |
| 2 | + This file is part of: |
| 3 | +
|
| 4 | + SDL3 for Pascal |
| 5 | + (https://github.com/PascalGameDevelopment/SDL3-for-Pascal) |
| 6 | + SPDX-License-Identifier: Zlib |
| 7 | +} |
| 8 | + |
| 9 | +{ Example is based on examples/renderer/02-primitives/primitives.c } |
| 10 | + |
| 11 | +program primitives; |
| 12 | + |
| 13 | +uses |
| 14 | + SDL3, SysUtils; |
| 15 | + |
| 16 | +var |
| 17 | + Window: PSDL_Window = nil; |
| 18 | + Renderer: PSDL_Renderer = nil; |
| 19 | + Points: array[0..499] of TSDL_FPoint; |
| 20 | + Rect: TSDL_FRect; |
| 21 | + I: Integer; |
| 22 | + |
| 23 | +begin |
| 24 | + if not SDL_Init(SDL_INIT_VIDEO) then |
| 25 | + begin |
| 26 | + |
| 27 | + SDL_Log(PChar(Format('Couldn''t initialize SDL: %s', [SDL_GetError]))); |
| 28 | + Exit; |
| 29 | + end; |
| 30 | + |
| 31 | + if not SDL_CreateWindowAndRenderer('primitives', 640, 480, 0, @Window, @Renderer) then |
| 32 | + begin |
| 33 | + SDL_Log(PChar(Format('Couldn''t create window/renderer: %s', [SDL_GetError]))); |
| 34 | + Exit; |
| 35 | + end; |
| 36 | + |
| 37 | + { Set up some random points } |
| 38 | + Randomize; |
| 39 | + for i := 0 to High(Points)-1 do |
| 40 | + begin |
| 41 | + Points[I].x:=(Random(10000)/100)*4.4 + 100.0; |
| 42 | + Points[I].y:=(Random(10000)/100)*2.8 + 100.0; |
| 43 | + end; |
| 44 | + |
| 45 | + { as you can see from this, rendering draws over whatever was drawn before it. } |
| 46 | + SDL_SetRenderDrawColor(Renderer, 33, 33, 33, SDL_ALPHA_OPAQUE); { dark gray, full alpha } |
| 47 | + SDL_RenderClear(Renderer); { start with a blank canvas. } |
| 48 | + |
| 49 | + { draw a filled rectangle in the middle of the canvas. } |
| 50 | + SDL_SetRenderDrawColor(Renderer, 0, 0, 255, SDL_ALPHA_OPAQUE); { blue, full alpha } |
| 51 | + Rect.x := 100; |
| 52 | + Rect.y := 100; |
| 53 | + Rect.w := 440; |
| 54 | + Rect.h := 280; |
| 55 | + SDL_RenderFillRect(Renderer, @Rect); |
| 56 | + |
| 57 | + { draw some points across the canvas. } |
| 58 | + SDL_SetRenderDrawColor(Renderer, 255, 0, 0, SDL_ALPHA_OPAQUE); { red, full alpha } |
| 59 | + SDL_RenderPoints(Renderer, @Points[0], High(Points)-1); |
| 60 | + |
| 61 | + { draw a unfilled Rectangle in-set a little bit. } |
| 62 | + SDL_SetRenderDrawColor(Renderer, 0, 255, 0, SDL_ALPHA_OPAQUE); { green, full alpha } |
| 63 | + Rect.x := Rect.x+30; |
| 64 | + Rect.y := Rect.y+30; |
| 65 | + Rect.w := Rect.w-60; |
| 66 | + Rect.h := Rect.h-60; |
| 67 | + SDL_RenderRect(Renderer, @Rect); |
| 68 | + |
| 69 | + { draw two lines in an X across the whole canvas. } |
| 70 | + SDL_SetRenderDrawColor(Renderer, 255, 255, 0, SDL_ALPHA_OPAQUE); { yellow, full alpha } |
| 71 | + SDL_RenderLine(Renderer, 0, 0, 640, 480); |
| 72 | + SDL_RenderLine(Renderer, 0, 480, 640, 0); |
| 73 | + |
| 74 | + SDL_RenderPresent(Renderer); { put it all on the screen! } |
| 75 | + |
| 76 | + SDL_Delay(2000); |
| 77 | + |
| 78 | + SDL_Quit(); |
| 79 | +end. |
| 80 | + |
0 commit comments