AtlatestRepositorysigil-raylib

sigil-raylib / tree / src / raylibgraphics.sgl

1;;; (raylib graphics) — Drawing, shapes, collision detection, and text
2;;;
3;;; Provides drawing frame control (begin-drawing, end-drawing, clear-background),
4;;; shape drawing, collision detection, text rendering, and graphics-related
5;;; constants (BLEND_*, CAMERA_*, LOG_*, TEXTURE_FILTER_*, TEXTURE_WRAP_*,
6;;; PIXELFORMAT_*).
7
8(define-library (raylib graphics)
9 (import (sigil core)
10 (sigil ffi)
11 (raylib core))
13 (export
14 ;; Blend modes
15 BLEND_ALPHA BLEND_ADDITIVE BLEND_MULTIPLIED BLEND_ADD_COLORS
16 BLEND_SUBTRACT_COLORS BLEND_ALPHA_PREMULTIPLY BLEND_CUSTOM
17 BLEND_CUSTOM_SEPARATE
19 ;; Camera modes
20 CAMERA_CUSTOM CAMERA_FREE CAMERA_ORBITAL CAMERA_FIRST_PERSON
21 CAMERA_THIRD_PERSON
23 ;; Camera projections
24 CAMERA_PERSPECTIVE CAMERA_ORTHOGRAPHIC
26 ;; Trace log levels
27 LOG_ALL LOG_TRACE LOG_DEBUG LOG_INFO LOG_WARNING LOG_ERROR
28 LOG_FATAL LOG_NONE
30 ;; Texture filter modes
31 TEXTURE_FILTER_POINT TEXTURE_FILTER_BILINEAR
32 TEXTURE_FILTER_TRILINEAR TEXTURE_FILTER_ANISOTROPIC_4X
33 TEXTURE_FILTER_ANISOTROPIC_8X TEXTURE_FILTER_ANISOTROPIC_16X
35 ;; Texture wrap modes
36 TEXTURE_WRAP_REPEAT TEXTURE_WRAP_CLAMP TEXTURE_WRAP_MIRROR_REPEAT
37 TEXTURE_WRAP_MIRROR_CLAMP
39 ;; Pixel formats
40 PIXELFORMAT_UNCOMPRESSED_GRAYSCALE
41 PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA
42 PIXELFORMAT_UNCOMPRESSED_R5G6B5
43 PIXELFORMAT_UNCOMPRESSED_R8G8B8
44 PIXELFORMAT_UNCOMPRESSED_R5G5B5A1
45 PIXELFORMAT_UNCOMPRESSED_R4G4B4A4
46 PIXELFORMAT_UNCOMPRESSED_R8G8B8A8
47 PIXELFORMAT_UNCOMPRESSED_R32
48 PIXELFORMAT_UNCOMPRESSED_R32G32B32
49 PIXELFORMAT_UNCOMPRESSED_R32G32B32A32
50 PIXELFORMAT_UNCOMPRESSED_R16
51 PIXELFORMAT_UNCOMPRESSED_R16G16B16
52 PIXELFORMAT_UNCOMPRESSED_R16G16B16A16
54 ;; Drawing frame
55 begin-drawing end-drawing clear-background
56 begin-blend-mode end-blend-mode
57 begin-scissor-mode end-scissor-mode
59 ;; Shapes — Drawing
60 draw-pixel draw-line
61 draw-line-v draw-line-ex
62 draw-circle draw-circle-v
63 draw-circle-gradient draw-circle-lines draw-circle-lines-v
64 draw-ellipse draw-ellipse-lines
65 draw-ring draw-ring-lines
66 draw-rectangle draw-rectangle-v
67 draw-rectangle-rec draw-rectangle-pro
68 draw-rectangle-gradient-v draw-rectangle-gradient-h
69 draw-rectangle-gradient-ex
70 draw-rectangle-lines draw-rectangle-lines-ex
71 draw-rectangle-rounded draw-rectangle-rounded-lines
72 draw-triangle draw-triangle-lines
73 draw-poly draw-poly-lines draw-poly-lines-ex
75 ;; Shapes — Collision
76 check-collision-recs check-collision-circles
77 check-collision-circle-rec
78 check-collision-point-rec check-collision-point-circle
79 check-collision-point-triangle check-collision-point-line
81 ;; Text
82 draw-text draw-fps measure-text
84 ;; Convenience
85 with-drawing)
87 (begin
89 ;; ============================================================
90 ;; Blend Modes
91 ;; ============================================================
93 (define BLEND_ALPHA 0)
94 (define BLEND_ADDITIVE 1)
95 (define BLEND_MULTIPLIED 2)
96 (define BLEND_ADD_COLORS 3)
97 (define BLEND_SUBTRACT_COLORS 4)
98 (define BLEND_ALPHA_PREMULTIPLY 5)
99 (define BLEND_CUSTOM 6)
100 (define BLEND_CUSTOM_SEPARATE 7)
102 ;; ============================================================
103 ;; Camera Modes / Projections
104 ;; ============================================================
106 (define CAMERA_CUSTOM 0)
107 (define CAMERA_FREE 1)
108 (define CAMERA_ORBITAL 2)
109 (define CAMERA_FIRST_PERSON 3)
110 (define CAMERA_THIRD_PERSON 4)
112 (define CAMERA_PERSPECTIVE 0)
113 (define CAMERA_ORTHOGRAPHIC 1)
115 ;; ============================================================
116 ;; Trace Log Levels
117 ;; ============================================================
119 (define LOG_ALL 0)
120 (define LOG_TRACE 1)
121 (define LOG_DEBUG 2)
122 (define LOG_INFO 3)
123 (define LOG_WARNING 4)
124 (define LOG_ERROR 5)
125 (define LOG_FATAL 6)
126 (define LOG_NONE 7)
128 ;; ============================================================
129 ;; Texture Filter / Wrap Modes
130 ;; ============================================================
132 (define TEXTURE_FILTER_POINT 0)
133 (define TEXTURE_FILTER_BILINEAR 1)
134 (define TEXTURE_FILTER_TRILINEAR 2)
135 (define TEXTURE_FILTER_ANISOTROPIC_4X 3)
136 (define TEXTURE_FILTER_ANISOTROPIC_8X 4)
137 (define TEXTURE_FILTER_ANISOTROPIC_16X 5)
139 (define TEXTURE_WRAP_REPEAT 0)
140 (define TEXTURE_WRAP_CLAMP 1)
141 (define TEXTURE_WRAP_MIRROR_REPEAT 2)
142 (define TEXTURE_WRAP_MIRROR_CLAMP 3)
144 ;; ============================================================
145 ;; Pixel Formats
146 ;; ============================================================
148 (define PIXELFORMAT_UNCOMPRESSED_GRAYSCALE 1)
149 (define PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA 2)
150 (define PIXELFORMAT_UNCOMPRESSED_R5G6B5 3)
151 (define PIXELFORMAT_UNCOMPRESSED_R8G8B8 4)
152 (define PIXELFORMAT_UNCOMPRESSED_R5G5B5A1 5)
153 (define PIXELFORMAT_UNCOMPRESSED_R4G4B4A4 6)
154 (define PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 7)
155 (define PIXELFORMAT_UNCOMPRESSED_R32 8)
156 (define PIXELFORMAT_UNCOMPRESSED_R32G32B32 9)
157 (define PIXELFORMAT_UNCOMPRESSED_R32G32B32A32 10)
158 (define PIXELFORMAT_UNCOMPRESSED_R16 11)
159 (define PIXELFORMAT_UNCOMPRESSED_R16G16B16 12)
160 (define PIXELFORMAT_UNCOMPRESSED_R16G16B16A16 13)
162 ;; ============================================================
163 ;; Raw FFI Bindings (private)
164 ;; ============================================================
166 ;; --- Drawing ---
167 (define %clear-background (c-function rl-lib "ClearBackground" (list color-layout) ffi/void))
168 (define %begin-drawing (c-function rl-lib "BeginDrawing" '() ffi/void))
169 (define %end-drawing (c-function rl-lib "EndDrawing" '() ffi/void))
170 (define %begin-blend-mode (c-function rl-lib "BeginBlendMode" (list ffi/int) ffi/void))
171 (define %end-blend-mode (c-function rl-lib "EndBlendMode" '() ffi/void))
172 (define %begin-scissor-mode (c-function rl-lib "BeginScissorMode" (list ffi/int ffi/int ffi/int ffi/int) ffi/void))
173 (define %end-scissor-mode (c-function rl-lib "EndScissorMode" '() ffi/void))
175 ;; --- Shapes: Basic drawing ---
176 (define %draw-pixel (c-function rl-lib "DrawPixel" (list ffi/int ffi/int color-layout) ffi/void))
177 (define %draw-line (c-function rl-lib "DrawLine" (list ffi/int ffi/int ffi/int ffi/int color-layout) ffi/void))
178 (define %draw-line-v (c-function rl-lib "DrawLineV" (list vector2-layout vector2-layout color-layout) ffi/void))
179 (define %draw-line-ex (c-function rl-lib "DrawLineEx" (list vector2-layout vector2-layout ffi/float color-layout) ffi/void))
180 (define %draw-circle (c-function rl-lib "DrawCircle" (list ffi/int ffi/int ffi/float color-layout) ffi/void))
181 (define %draw-circle-v (c-function rl-lib "DrawCircleV" (list vector2-layout ffi/float color-layout) ffi/void))
182 (define %draw-circle-gradient (c-function rl-lib "DrawCircleGradient" (list ffi/int ffi/int ffi/float color-layout color-layout) ffi/void))
183 (define %draw-circle-lines (c-function rl-lib "DrawCircleLines" (list ffi/int ffi/int ffi/float color-layout) ffi/void))
184 (define %draw-circle-lines-v (c-function rl-lib "DrawCircleLinesV" (list vector2-layout ffi/float color-layout) ffi/void))
185 (define %draw-ellipse (c-function rl-lib "DrawEllipse" (list ffi/int ffi/int ffi/float ffi/float color-layout) ffi/void))
186 (define %draw-ellipse-lines (c-function rl-lib "DrawEllipseLines" (list ffi/int ffi/int ffi/float ffi/float color-layout) ffi/void))
187 (define %draw-ring (c-function rl-lib "DrawRing" (list vector2-layout ffi/float ffi/float ffi/float ffi/float ffi/int color-layout) ffi/void))
188 (define %draw-ring-lines (c-function rl-lib "DrawRingLines" (list vector2-layout ffi/float ffi/float ffi/float ffi/float ffi/int color-layout) ffi/void))
189 (define %draw-rectangle (c-function rl-lib "DrawRectangle" (list ffi/int ffi/int ffi/int ffi/int color-layout) ffi/void))
190 (define %draw-rectangle-v (c-function rl-lib "DrawRectangleV" (list vector2-layout vector2-layout color-layout) ffi/void))
191 (define %draw-rectangle-rec (c-function rl-lib "DrawRectangleRec" (list rectangle-layout color-layout) ffi/void))
192 (define %draw-rectangle-pro (c-function rl-lib "DrawRectanglePro" (list rectangle-layout vector2-layout ffi/float color-layout) ffi/void))
193 (define %draw-rectangle-gradient-v (c-function rl-lib "DrawRectangleGradientV" (list ffi/int ffi/int ffi/int ffi/int color-layout color-layout) ffi/void))
194 (define %draw-rectangle-gradient-h (c-function rl-lib "DrawRectangleGradientH" (list ffi/int ffi/int ffi/int ffi/int color-layout color-layout) ffi/void))
195 (define %draw-rectangle-gradient-ex (c-function rl-lib "DrawRectangleGradientEx" (list rectangle-layout color-layout color-layout color-layout color-layout) ffi/void))
196 (define %draw-rectangle-lines (c-function rl-lib "DrawRectangleLines" (list ffi/int ffi/int ffi/int ffi/int color-layout) ffi/void))
197 (define %draw-rectangle-lines-ex (c-function rl-lib "DrawRectangleLinesEx" (list rectangle-layout ffi/float color-layout) ffi/void))
198 (define %draw-rectangle-rounded (c-function rl-lib "DrawRectangleRounded" (list rectangle-layout ffi/float ffi/int color-layout) ffi/void))
199 (define %draw-rectangle-rounded-lines (c-function rl-lib "DrawRectangleRoundedLines" (list rectangle-layout ffi/float ffi/int color-layout) ffi/void))
200 (define %draw-triangle (c-function rl-lib "DrawTriangle" (list vector2-layout vector2-layout vector2-layout color-layout) ffi/void))
201 (define %draw-triangle-lines (c-function rl-lib "DrawTriangleLines" (list vector2-layout vector2-layout vector2-layout color-layout) ffi/void))
202 (define %draw-poly (c-function rl-lib "DrawPoly" (list vector2-layout ffi/int ffi/float ffi/float color-layout) ffi/void))
203 (define %draw-poly-lines (c-function rl-lib "DrawPolyLines" (list vector2-layout ffi/int ffi/float ffi/float color-layout) ffi/void))
204 (define %draw-poly-lines-ex (c-function rl-lib "DrawPolyLinesEx" (list vector2-layout ffi/int ffi/float ffi/float ffi/float color-layout) ffi/void))
206 ;; --- Collision detection ---
207 (define %check-collision-recs (c-function rl-lib "CheckCollisionRecs" (list rectangle-layout rectangle-layout) ffi/int))
208 (define %check-collision-circles (c-function rl-lib "CheckCollisionCircles" (list vector2-layout ffi/float vector2-layout ffi/float) ffi/int))
209 (define %check-collision-circle-rec (c-function rl-lib "CheckCollisionCircleRec" (list vector2-layout ffi/float rectangle-layout) ffi/int))
210 (define %check-collision-point-rec (c-function rl-lib "CheckCollisionPointRec" (list vector2-layout rectangle-layout) ffi/int))
211 (define %check-collision-point-circle (c-function rl-lib "CheckCollisionPointCircle" (list vector2-layout vector2-layout ffi/float) ffi/int))
212 (define %check-collision-point-triangle (c-function rl-lib "CheckCollisionPointTriangle" (list vector2-layout vector2-layout vector2-layout vector2-layout) ffi/int))
213 (define %check-collision-point-line (c-function rl-lib "CheckCollisionPointLine" (list vector2-layout vector2-layout vector2-layout ffi/int) ffi/int))
215 ;; --- Text ---
216 (define %draw-text (c-function rl-lib "DrawText" (list ffi/string ffi/int ffi/int ffi/int color-layout) ffi/void))
217 (define %draw-fps (c-function rl-lib "DrawFPS" (list ffi/int ffi/int) ffi/void))
218 (define %measure-text (c-function rl-lib "MeasureText" (list ffi/string ffi/int) ffi/int))
220 ;; ============================================================
221 ;; Public API
222 ;; ============================================================
224 ;; --- Drawing frame ---
226 ;;; Set up canvas (framebuffer) to start drawing.
227 (define (begin-drawing) (: -> void?) (%begin-drawing))
229 ;;; End canvas drawing and swap buffers.
230 (define (end-drawing) (: -> void?) (%end-drawing))
232 ;;; Set background color (framebuffer clear color).
233 (define (clear-background color) (: any? -> void?) (%clear-background color))
235 (define (begin-blend-mode mode) (: integer? -> void?) (%begin-blend-mode mode))
236 (define (end-blend-mode) (: -> void?) (%end-blend-mode))
237 (define (begin-scissor-mode x y w h) (: integer? integer? integer? integer? -> void?) (%begin-scissor-mode x y w h))
238 (define (end-scissor-mode) (: -> void?) (%end-scissor-mode))
240 ;; --- Shapes: Drawing ---
242 ;;; Draw a pixel.
243 (define (draw-pixel x y color)
244 (: integer? integer? any? -> void?)
245 (%draw-pixel x y color))
247 ;;; Draw a line.
248 (define (draw-line x1 y1 x2 y2 color)
249 (: integer? integer? integer? integer? any? -> void?)
250 (%draw-line x1 y1 x2 y2 color))
252 ;;; Draw a line (using Vector2).
253 (define (draw-line-v start end color)
254 (: any? any? any? -> void?)
255 (%draw-line-v start end color))
257 ;;; Draw a line (extended: Vector2, thickness).
258 (define (draw-line-ex start end thick color)
259 (: any? any? number? any? -> void?)
260 (%draw-line-ex start end thick color))
262 ;;; Draw a color-filled circle.
263 (define (draw-circle cx cy radius color)
264 (: integer? integer? number? any? -> void?)
265 (%draw-circle cx cy radius color))
267 ;;; Draw a color-filled circle (Vector2 center).
268 (define (draw-circle-v center radius color)
269 (: any? number? any? -> void?)
270 (%draw-circle-v center radius color))
272 ;;; Draw a gradient-filled circle.
273 (define (draw-circle-gradient cx cy radius inner outer)
274 (: integer? integer? number? any? any? -> void?)
275 (%draw-circle-gradient cx cy radius inner outer))
277 ;;; Draw circle outline.
278 (define (draw-circle-lines cx cy radius color)
279 (: integer? integer? number? any? -> void?)
280 (%draw-circle-lines cx cy radius color))
282 ;;; Draw circle outline (Vector2 center).
283 (define (draw-circle-lines-v center radius color)
284 (: any? number? any? -> void?)
285 (%draw-circle-lines-v center radius color))
287 ;;; Draw ellipse.
288 (define (draw-ellipse cx cy rh rv color)
289 (: integer? integer? number? number? any? -> void?)
290 (%draw-ellipse cx cy rh rv color))
292 ;;; Draw ellipse outline.
293 (define (draw-ellipse-lines cx cy rh rv color)
294 (: integer? integer? number? number? any? -> void?)
295 (%draw-ellipse-lines cx cy rh rv color))
297 ;;; Draw ring.
298 (define (draw-ring center inner-radius outer-radius start-angle end-angle segments color)
299 (: any? number? number? number? number? integer? any? -> void?)
300 (%draw-ring center inner-radius outer-radius start-angle end-angle segments color))
302 ;;; Draw ring outline.
303 (define (draw-ring-lines center inner-radius outer-radius start-angle end-angle segments color)
304 (: any? number? number? number? number? integer? any? -> void?)
305 (%draw-ring-lines center inner-radius outer-radius start-angle end-angle segments color))
307 ;;; Draw a color-filled rectangle.
308 (define (draw-rectangle x y w h color)
309 (: integer? integer? integer? integer? any? -> void?)
310 (%draw-rectangle x y w h color))
312 ;;; Draw a color-filled rectangle (Vector2 position and size).
313 (define (draw-rectangle-v position size color)
314 (: any? any? any? -> void?)
315 (%draw-rectangle-v position size color))
317 ;;; Draw a color-filled rectangle from Rectangle.
318 (define (draw-rectangle-rec rec color)
319 (: any? any? -> void?)
320 (%draw-rectangle-rec rec color))
322 ;;; Draw a color-filled rectangle with pro parameters.
323 (define (draw-rectangle-pro rec origin rotation color)
324 (: any? any? number? any? -> void?)
325 (%draw-rectangle-pro rec origin rotation color))
327 ;;; Draw a vertical-gradient-filled rectangle.
328 (define (draw-rectangle-gradient-v x y w h top bottom)
329 (: integer? integer? integer? integer? any? any? -> void?)
330 (%draw-rectangle-gradient-v x y w h top bottom))
332 ;;; Draw a horizontal-gradient-filled rectangle.
333 (define (draw-rectangle-gradient-h x y w h left right)
334 (: integer? integer? integer? integer? any? any? -> void?)
335 (%draw-rectangle-gradient-h x y w h left right))
337 ;;; Draw a gradient-filled rectangle with custom vertex colors.
338 (define (draw-rectangle-gradient-ex rec top-left bottom-left bottom-right top-right)
339 (: any? any? any? any? any? -> void?)
340 (%draw-rectangle-gradient-ex rec top-left bottom-left bottom-right top-right))
342 ;;; Draw rectangle outline.
343 (define (draw-rectangle-lines x y w h color)
344 (: integer? integer? integer? integer? any? -> void?)
345 (%draw-rectangle-lines x y w h color))
347 ;;; Draw rectangle outline with extended parameters.
348 (define (draw-rectangle-lines-ex rec thick color)
349 (: any? number? any? -> void?)
350 (%draw-rectangle-lines-ex rec thick color))
352 ;;; Draw rectangle with rounded corners.
353 (define (draw-rectangle-rounded rec roundness segments color)
354 (: any? number? integer? any? -> void?)
355 (%draw-rectangle-rounded rec roundness segments color))
357 ;;; Draw rectangle with rounded corners outline.
358 (define (draw-rectangle-rounded-lines rec roundness segments color)
359 (: any? number? integer? any? -> void?)
360 (%draw-rectangle-rounded-lines rec roundness segments color))
362 ;;; Draw a color-filled triangle.
363 (define (draw-triangle v1 v2 v3 color)
364 (: any? any? any? any? -> void?)
365 (%draw-triangle v1 v2 v3 color))
367 ;;; Draw triangle outline.
368 (define (draw-triangle-lines v1 v2 v3 color)
369 (: any? any? any? any? -> void?)
370 (%draw-triangle-lines v1 v2 v3 color))
372 ;;; Draw a regular polygon (Vector2 center).
373 (define (draw-poly center sides radius rotation color)
374 (: any? integer? number? number? any? -> void?)
375 (%draw-poly center sides radius rotation color))
377 ;;; Draw a polygon outline.
378 (define (draw-poly-lines center sides radius rotation color)
379 (: any? integer? number? number? any? -> void?)
380 (%draw-poly-lines center sides radius rotation color))
382 ;;; Draw a polygon outline with thickness.
383 (define (draw-poly-lines-ex center sides radius rotation thick color)
384 (: any? integer? number? number? number? any? -> void?)
385 (%draw-poly-lines-ex center sides radius rotation thick color))
387 ;; --- Collision detection ---
389 ;;; Check collision between two rectangles.
390 (define (check-collision-recs rec1 rec2)
391 (: any? any? -> boolean?)
392 (not (= 0 (%check-collision-recs rec1 rec2))))
394 ;;; Check collision between two circles.
395 (define (check-collision-circles center1 radius1 center2 radius2)
396 (: any? number? any? number? -> boolean?)
397 (not (= 0 (%check-collision-circles center1 radius1 center2 radius2))))
399 ;;; Check collision between circle and rectangle.
400 (define (check-collision-circle-rec center radius rec)
401 (: any? number? any? -> boolean?)
402 (not (= 0 (%check-collision-circle-rec center radius rec))))
404 ;;; Check if point is inside rectangle.
405 (define (check-collision-point-rec point rec)
406 (: any? any? -> boolean?)
407 (not (= 0 (%check-collision-point-rec point rec))))
409 ;;; Check if point is inside circle.
410 (define (check-collision-point-circle point center radius)
411 (: any? any? number? -> boolean?)
412 (not (= 0 (%check-collision-point-circle point center radius))))
414 ;;; Check if point is inside a triangle.
415 (define (check-collision-point-triangle point p1 p2 p3)
416 (: any? any? any? any? -> boolean?)
417 (not (= 0 (%check-collision-point-triangle point p1 p2 p3))))
419 ;;; Check if point belongs to line segment.
420 (define (check-collision-point-line point p1 p2 threshold)
421 (: any? any? any? integer? -> boolean?)
422 (not (= 0 (%check-collision-point-line point p1 p2 threshold))))
424 ;; --- Text ---
426 ;;; Draw text (using default font).
427 (define (draw-text text x y font-size color)
428 (: string? integer? integer? integer? any? -> void?)
429 (%draw-text text x y font-size color))
431 ;;; Draw current FPS.
432 (define (draw-fps x y)
433 (: integer? integer? -> void?)
434 (%draw-fps x y))
436 ;;; Measure string width for default font.
437 (define (measure-text text font-size)
438 (: string? integer? -> integer?)
439 (%measure-text text font-size))
441 ;; ============================================================
442 ;; Convenience Wrappers
443 ;; ============================================================
445 ;;; Execute body within a drawing frame.
446 (define (with-drawing proc)
447 (: procedure? -> void?)
448 (begin-drawing)
449 (proc)
450 (end-drawing))))