AtlatestRepositorysigil-raylib

sigil-raylib / tree / src / raylibcore.sgl

1;;; (raylib core) — Library handle, struct layouts, colors, timing, misc, file system
2;;;
3;;; Core module providing the shared library handle, struct layout definitions,
4;;; color/vector/rectangle helpers, predefined colors, config flags, timing,
5;;; random values, misc utilities, file system functions, and convenience wrappers.
6
7(define-library (raylib core)
8 (import (sigil core)
9 (sigil ffi))
11 (export
12 ;; Library handle
13 rl-lib
15 ;; Struct layouts
16 color-layout
17 vector2-layout
18 vector3-layout
19 vector4-layout
20 rectangle-layout
21 camera-2d-layout
22 camera-3d-layout
24 ;; Color helpers
25 make-color
26 color-r color-g color-b color-a
28 ;; Vector/Rectangle helpers
29 make-vec2 vec2-x vec2-y
30 make-vec3 vec3-x vec3-y vec3-z
31 make-vec4
32 make-rect rect-x rect-y rect-width rect-height
34 ;; Predefined colors
35 LIGHTGRAY GRAY DARKGRAY YELLOW GOLD ORANGE PINK RED MAROON
36 GREEN LIME DARKGREEN SKYBLUE BLUE DARKBLUE PURPLE VIOLET
37 DARKPURPLE BEIGE BROWN DARKBROWN WHITE BLACK BLANK MAGENTA RAYWHITE
39 ;; Config flags
40 FLAG_VSYNC_HINT FLAG_FULLSCREEN_MODE FLAG_WINDOW_RESIZABLE
41 FLAG_WINDOW_UNDECORATED FLAG_WINDOW_HIDDEN FLAG_WINDOW_MINIMIZED
42 FLAG_WINDOW_MAXIMIZED FLAG_WINDOW_UNFOCUSED FLAG_WINDOW_TOPMOST
43 FLAG_WINDOW_ALWAYS_RUN FLAG_WINDOW_TRANSPARENT FLAG_WINDOW_HIGHDPI
44 FLAG_WINDOW_MOUSE_PASSTHROUGH FLAG_BORDERLESS_WINDOWED_MODE
45 FLAG_MSAA_4X_HINT FLAG_INTERLACED_HINT
47 ;; Timing
48 set-target-fps get-frame-time get-time get-fps
50 ;; Custom frame control
51 swap-screen-buffer poll-input-events wait-time
53 ;; Random values
54 set-random-seed get-random-value
56 ;; Misc
57 take-screenshot set-config-flags open-url
58 set-trace-log-level
60 ;; File system
61 file-exists? directory-exists?
62 is-file-extension? get-file-length
63 get-file-extension get-file-name get-file-name-without-ext
64 get-directory-path get-prev-directory-path
65 get-working-directory get-application-directory
66 make-directory change-directory
67 is-path-file? is-file-name-valid?
68 is-file-dropped?
70 )
72 (begin
74 ;; ============================================================
75 ;; Library Loading
76 ;; ============================================================
78 (define rl-lib (c-library "libraylib"))
80 ;; ============================================================
81 ;; Struct Layouts
82 ;; ============================================================
84 (define color-layout
85 (c-struct-layout r: ffi/uint8 g: ffi/uint8 b: ffi/uint8 a: ffi/uint8))
87 (define vector2-layout
88 (c-struct-layout x: ffi/float y: ffi/float))
90 (define vector3-layout
91 (c-struct-layout x: ffi/float y: ffi/float z: ffi/float))
93 (define vector4-layout
94 (c-struct-layout x: ffi/float y: ffi/float z: ffi/float w: ffi/float))
96 (define rectangle-layout
97 (c-struct-layout x: ffi/float y: ffi/float width: ffi/float height: ffi/float))
99 (define camera-2d-layout
100 (c-struct-layout
101 offset-x: ffi/float offset-y: ffi/float
102 target-x: ffi/float target-y: ffi/float
103 rotation: ffi/float
104 zoom: ffi/float))
106 (define camera-3d-layout
107 (c-struct-layout
108 pos-x: ffi/float pos-y: ffi/float pos-z: ffi/float
109 target-x: ffi/float target-y: ffi/float target-z: ffi/float
110 up-x: ffi/float up-y: ffi/float up-z: ffi/float
111 fovy: ffi/float
112 projection: ffi/int))
114 ;; ============================================================
115 ;; Raw FFI Bindings (private)
116 ;; ============================================================
118 ;; --- Timing ---
119 (define %set-target-fps (c-function rl-lib "SetTargetFPS" (list ffi/int) ffi/void))
120 (define %get-frame-time (c-function rl-lib "GetFrameTime" '() ffi/float))
121 (define %get-time (c-function rl-lib "GetTime" '() ffi/double))
122 (define %get-fps (c-function rl-lib "GetFPS" '() ffi/int))
124 ;; --- Custom frame control ---
125 (define %swap-screen-buffer (c-function rl-lib "SwapScreenBuffer" '() ffi/void))
126 (define %poll-input-events (c-function rl-lib "PollInputEvents" '() ffi/void))
127 (define %wait-time (c-function rl-lib "WaitTime" (list ffi/double) ffi/void))
129 ;; --- Random ---
130 (define %set-random-seed (c-function rl-lib "SetRandomSeed" (list ffi/uint) ffi/void))
131 (define %get-random-value (c-function rl-lib "GetRandomValue" (list ffi/int ffi/int) ffi/int))
133 ;; --- Misc ---
134 (define %take-screenshot (c-function rl-lib "TakeScreenshot" (list ffi/string) ffi/void))
135 (define %set-config-flags (c-function rl-lib "SetConfigFlags" (list ffi/uint) ffi/void))
136 (define %open-url (c-function rl-lib "OpenURL" (list ffi/string) ffi/void))
137 (define %set-trace-log-level (c-function rl-lib "SetTraceLogLevel" (list ffi/int) ffi/void))
139 ;; --- File system ---
140 (define %file-exists (c-function rl-lib "FileExists" (list ffi/string) ffi/int))
141 (define %directory-exists (c-function rl-lib "DirectoryExists" (list ffi/string) ffi/int))
142 (define %is-file-extension (c-function rl-lib "IsFileExtension" (list ffi/string ffi/string) ffi/int))
143 (define %get-file-length (c-function rl-lib "GetFileLength" (list ffi/string) ffi/int))
144 (define %get-file-extension (c-function rl-lib "GetFileExtension" (list ffi/string) ffi/string))
145 (define %get-file-name (c-function rl-lib "GetFileName" (list ffi/string) ffi/string))
146 (define %get-file-name-without-ext (c-function rl-lib "GetFileNameWithoutExt" (list ffi/string) ffi/string))
147 (define %get-directory-path (c-function rl-lib "GetDirectoryPath" (list ffi/string) ffi/string))
148 (define %get-prev-directory-path (c-function rl-lib "GetPrevDirectoryPath" (list ffi/string) ffi/string))
149 (define %get-working-directory (c-function rl-lib "GetWorkingDirectory" '() ffi/string))
150 (define %get-application-directory (c-function rl-lib "GetApplicationDirectory" '() ffi/string))
151 (define %make-directory (c-function rl-lib "MakeDirectory" (list ffi/string) ffi/int))
152 (define %change-directory (c-function rl-lib "ChangeDirectory" (list ffi/string) ffi/int))
153 (define %is-path-file (c-function rl-lib "IsPathFile" (list ffi/string) ffi/int))
154 (define %is-file-name-valid (c-function rl-lib "IsFileNameValid" (list ffi/string) ffi/int))
155 (define %is-file-dropped (c-function rl-lib "IsFileDropped" '() ffi/int))
157 ;; ============================================================
158 ;; Color Helpers
159 ;; ============================================================
161 ;;; Create a Color dict for passing to raylib functions.
162 (define (make-color r g b a)
163 (: integer? integer? integer? integer? -> any?)
164 (dict r: r g: g b: b a: a))
166 ;;; Extract red component from a Color dict.
167 (define (color-r c) (: any? -> integer?) (dict-ref c r:))
168 ;;; Extract green component from a Color dict.
169 (define (color-g c) (: any? -> integer?) (dict-ref c g:))
170 ;;; Extract blue component from a Color dict.
171 (define (color-b c) (: any? -> integer?) (dict-ref c b:))
172 ;;; Extract alpha component from a Color dict.
173 (define (color-a c) (: any? -> integer?) (dict-ref c a:))
175 ;; ============================================================
176 ;; Vector/Rectangle Helpers
177 ;; ============================================================
179 ;;; Create a Vector2 dict.
180 (define (make-vec2 x y)
181 (: number? number? -> any?)
182 (dict x: x y: y))
184 (define (vec2-x v) (: any? -> number?) (dict-ref v x:))
185 (define (vec2-y v) (: any? -> number?) (dict-ref v y:))
187 ;;; Create a Vector3 dict.
188 (define (make-vec3 x y z)
189 (: number? number? number? -> any?)
190 (dict x: x y: y z: z))
192 (define (vec3-x v) (: any? -> number?) (dict-ref v x:))
193 (define (vec3-y v) (: any? -> number?) (dict-ref v y:))
194 (define (vec3-z v) (: any? -> number?) (dict-ref v z:))
196 ;;; Create a Vector4 dict.
197 (define (make-vec4 x y z w)
198 (: number? number? number? number? -> any?)
199 (dict x: x y: y z: z w: w))
201 ;;; Create a Rectangle dict.
202 (define (make-rect x y width height)
203 (: number? number? number? number? -> any?)
204 (dict x: x y: y width: width height: height))
206 (define (rect-x r) (: any? -> number?) (dict-ref r x:))
207 (define (rect-y r) (: any? -> number?) (dict-ref r y:))
208 (define (rect-width r) (: any? -> number?) (dict-ref r width:))
209 (define (rect-height r) (: any? -> number?) (dict-ref r height:))
211 ;; ============================================================
212 ;; Predefined Colors
213 ;; ============================================================
215 (define LIGHTGRAY (make-color 200 200 200 255))
216 (define GRAY (make-color 130 130 130 255))
217 (define DARKGRAY (make-color 80 80 80 255))
218 (define YELLOW (make-color 253 249 0 255))
219 (define GOLD (make-color 255 203 0 255))
220 (define ORANGE (make-color 255 161 0 255))
221 (define PINK (make-color 255 109 194 255))
222 (define RED (make-color 230 41 55 255))
223 (define MAROON (make-color 190 33 55 255))
224 (define GREEN (make-color 0 228 48 255))
225 (define LIME (make-color 0 158 47 255))
226 (define DARKGREEN (make-color 0 117 44 255))
227 (define SKYBLUE (make-color 102 191 255 255))
228 (define BLUE (make-color 0 121 241 255))
229 (define DARKBLUE (make-color 0 82 172 255))
230 (define PURPLE (make-color 200 122 255 255))
231 (define VIOLET (make-color 135 60 190 255))
232 (define DARKPURPLE (make-color 112 31 126 255))
233 (define BEIGE (make-color 211 176 131 255))
234 (define BROWN (make-color 127 106 79 255))
235 (define DARKBROWN (make-color 76 63 47 255))
236 (define WHITE (make-color 255 255 255 255))
237 (define BLACK (make-color 0 0 0 255))
238 (define BLANK (make-color 0 0 0 0))
239 (define MAGENTA (make-color 255 0 255 255))
240 (define RAYWHITE (make-color 245 245 245 255))
242 ;; ============================================================
243 ;; Config Flags
244 ;; ============================================================
246 (define FLAG_VSYNC_HINT #x00000040)
247 (define FLAG_FULLSCREEN_MODE #x00000002)
248 (define FLAG_WINDOW_RESIZABLE #x00000004)
249 (define FLAG_WINDOW_UNDECORATED #x00000008)
250 (define FLAG_WINDOW_HIDDEN #x00000080)
251 (define FLAG_WINDOW_MINIMIZED #x00000200)
252 (define FLAG_WINDOW_MAXIMIZED #x00000400)
253 (define FLAG_WINDOW_UNFOCUSED #x00000800)
254 (define FLAG_WINDOW_TOPMOST #x00001000)
255 (define FLAG_WINDOW_ALWAYS_RUN #x00000100)
256 (define FLAG_WINDOW_TRANSPARENT #x00000010)
257 (define FLAG_WINDOW_HIGHDPI #x00002000)
258 (define FLAG_WINDOW_MOUSE_PASSTHROUGH #x00004000)
259 (define FLAG_BORDERLESS_WINDOWED_MODE #x00008000)
260 (define FLAG_MSAA_4X_HINT #x00000020)
261 (define FLAG_INTERLACED_HINT #x00010000)
263 ;; ============================================================
264 ;; Public API
265 ;; ============================================================
267 ;; --- Timing ---
269 ;;; Set target FPS (maximum).
270 (define (set-target-fps fps) (: integer? -> void?) (%set-target-fps fps))
271 ;;; Get time in seconds for last frame drawn (delta time).
272 (define (get-frame-time) (: -> number?) (%get-frame-time))
273 ;;; Get elapsed time in seconds since InitWindow.
274 (define (get-time) (: -> number?) (%get-time))
275 ;;; Get current FPS.
276 (define (get-fps) (: -> integer?) (%get-fps))
278 ;; --- Custom frame control ---
280 (define (swap-screen-buffer) (: -> void?) (%swap-screen-buffer))
281 (define (poll-input-events) (: -> void?) (%poll-input-events))
282 (define (wait-time seconds) (: number? -> void?) (%wait-time seconds))
284 ;; --- Random ---
286 (define (set-random-seed seed) (: integer? -> void?) (%set-random-seed seed))
287 (define (get-random-value min max) (: integer? integer? -> integer?) (%get-random-value min max))
289 ;; --- Misc ---
291 (define (take-screenshot file-name) (: string? -> void?) (%take-screenshot file-name))
292 (define (set-config-flags flags) (: integer? -> void?) (%set-config-flags flags))
293 (define (open-url url) (: string? -> void?) (%open-url url))
294 (define (set-trace-log-level level) (: integer? -> void?) (%set-trace-log-level level))
296 ;; --- File system ---
298 (define (file-exists? file-name) (: string? -> boolean?) (not (= 0 (%file-exists file-name))))
299 (define (directory-exists? dir-path) (: string? -> boolean?) (not (= 0 (%directory-exists dir-path))))
300 (define (is-file-extension? file-name ext) (: string? string? -> boolean?) (not (= 0 (%is-file-extension file-name ext))))
301 (define (get-file-length file-name) (: string? -> integer?) (%get-file-length file-name))
302 (define (get-file-extension file-name) (: string? -> string?) (%get-file-extension file-name))
303 (define (get-file-name file-path) (: string? -> string?) (%get-file-name file-path))
304 (define (get-file-name-without-ext file-path) (: string? -> string?) (%get-file-name-without-ext file-path))
305 (define (get-directory-path file-path) (: string? -> string?) (%get-directory-path file-path))
306 (define (get-prev-directory-path dir-path) (: string? -> string?) (%get-prev-directory-path dir-path))
307 (define (get-working-directory) (: -> string?) (%get-working-directory))
308 (define (get-application-directory) (: -> string?) (%get-application-directory))
309 (define (make-directory dir-path) (: string? -> integer?) (%make-directory dir-path))
310 (define (change-directory dir-path) (: string? -> boolean?) (not (= 0 (%change-directory dir-path))))
311 (define (is-path-file? path) (: string? -> boolean?) (not (= 0 (%is-path-file path))))
312 (define (is-file-name-valid? file-name) (: string? -> boolean?) (not (= 0 (%is-file-name-valid file-name))))
313 (define (is-file-dropped?) (: -> boolean?) (not (= 0 (%is-file-dropped))))
315 ))