raylib
TOC »
Description
Bindings to the raylib game library!
The following types are represented as SRFI-4 vectors for performance and to keep the memory managed by CHICKEN:
| Vector2 | (make-f32vector 2) |
| Vector3 | (make-f32vector 3) |
| Vector4 | (make-f32vector 4) |
| Quaternion | (make-f32vector 4) |
| Matrix | (make-f32vector 16) |
| Rectangle | (make-f32vector 4) |
| Color | (make-u8vector 4) |
All other raylib structs are allocated on the unmanaged heap, and should be free'd by the user.
Complete bindings (except callbacks) for:
| raylib.h | (import raylib) |
| raymath.h | (import raymath) |
| rlgl.h | (import rlgl) |
| rcamera.h | (import rcamera) |
| raygui.h | (import raygui) |
Requirements
- raylib C library v6.2 (should be installed as a SHARED library on your system. See install-raylib.sh)
API
There's too many to document here. Refer to the header files for the official documentation. All function names are converted to kebab case, and all constants are left unchanged (in all caps).
What's missing
- Callback functions (e.g. SetLoadFileDataCallback())
Callbacks are tricky because we need to mark C functions that will trigger them using foreign-safe-lambda. See here for details. The TraceLog callback for example, could be triggered by virtually any raylib function, which would require everything to be a foreign-safe-lambda, possibly hindering performance. If you need these callbacks I highly suggest writing them in C with foreign-declare (which obviously can't be done in interpreter mode D:). If there's something I'm missing about this don't hesitate to contact me or make a pull request! Would love for callbacks to work!
Example 1
;; examples/basic-window.scm (import raylib) (init-window 800 450 "raylib [core] example - basic window") (let loop () (begin-drawing) (clear-background RAYWHITE) (draw-rectangle-rec #f32(10 10 780 430) BLUE) ; srfi-4 vector as a Rectangle! (draw-text "Congrats! You created your first window!" 190 200 20 LIGHTGRAY) (end-drawing) (unless (window-should-close) (loop))) (close-window)
Example 2
;; examples/scheme-stuff.scm (import raylib) (import scheme.base) (import chicken.memory) (import srfi-4) ;;;;; using functions with parameters of type (the srfi-4 types): ;;;;; Vector2 Vector3 Vector4 Quaternion Matrix Rectangle Color ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; in raylib.h: ;; bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec); (check-collision-circle-rec #f32(0 0) ;; Vector2 4 #f32(1 1 10 10)) ;; Rectangle ;; or for non-constants: (check-collision-circle-rec (f32vector (- 1 1) 0) ;; Vector2 4 (f32vector (+ 1 0) (+ 1 0) 10 10)) ;; Rectangle ;; in raylib.h: ;; Color ColorAlpha(Color color, float alpha); ;; Color is a u8vector: (color-lerp #u8(0 0 0 255) #u8(5 5 5 255) 0.7) ;; returns a u8vector (don't free it!) ;;;;; variadic functions trace-log and text-format: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (trace-log LOG_INFO "let me log this!") ;; not binded variadic, only 2 args (use chicken.format) (text-format "~S ~S!" "hi" "guys") ;; actually binded to chicken.format ;;;;; handling most structs ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; in raylib.h: ;; typedef struct Camera2D { ;; Vector2 offset; ;; Vector2 target; ;; float rotation; ;; float zoom; ;; } Camera2D; ;; allocates the struct and returns a foreign pointer (remember to free) (define camera (make-camera2d #f32(10 10) #f32(0 0) 0 1)) ;; field getters (camera2d-target camera) ;; returns f32vector ;; field setters (set! (camera2d-target camera) #f32(1 1)) ;; if you have the miscmacros egg you can do things like (srfi-17 setter power!): ;; (modify! (camera2d-rotation camera) add1) ;; free the foreign pointer (don't do this for the srfi-4 types!) (free camera) ;;;;; handling structs with array fields ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; in raylib.h: ;; typedef struct VrStereoConfig { ;; Matrix projection[2]; ;; Matrix viewOffset[2]; ;; float leftLensCenter[2]; ;; float rightLensCenter[2]; ;; float leftScreenCenter[2]; ;; float rightScreenCenter[2]; ;; float scale[2]; ;; float scaleIn[2]; ;; } VrStereoConfig; ;; allocates the struct and returns a foreign pointer (remember to free) ;; NOTE: array fields aren't included in the struct constructor because of their nature (define config (make-vr-stereo-config)) ;; get the leftLensCenter (define ptr (vr-stereo-config-left-lens-center config)) ;; use move-memory! to set the data there (copies 8 bytes of f32vector to struct) (move-memory! #f32(123 123) ptr) ;; copy 8 bytes from struct to f32vector (move-memory! requires size in this case idk why) (define x (make-f32vector 2)) (move-memory! ptr x 8) ;; projection field is a float array (move-memory! (list->f32vector (append (make-list 16 1) ;; projection[0] (4x4 matrix of all 1's) (make-list 16 2))) ;; projection[1] (4x4 matrix of all 2's) (vr-stereo-config-projection config)) ;; remember to free foreign memory (don't do this for the srfi-4 types tho like Vector2 and friends!) (free config)
Example 3
;; examples/gui.scm (import raylib) (import raygui) (import chicken.bitwise) (import srfi-4) (define (int->uint n) (bitwise-and n #xffffffff)) (init-window 400 200 "raygui - controls test suite") (set-target-fps 60) (define show-message-box #f) (let loop () (begin-drawing) ;; get-color expects a uint but gui-get-style returns an int (clear-background (get-color (int->uint (gui-get-style DEFAULT BACKGROUND_COLOR)))) (when (> (gui-button #f32(24 24 120 30) "#191#Show Message") 0) (set! show-message-box #t)) (when show-message-box ;; btn-active is an int that needs to be passed as a pointer, so wrap ;; in a s32vector (let ([btn-active (s32vector -1)]) (gui-message-box #f32(85 70 250 100) "#191#Message Box" "Hi! This is a message!" "Nice;Cool" btn-active) (when (>= (s32vector-ref btn-active 0) 0) (set! show-message-box #f)))) (end-drawing) (unless (window-should-close) (loop)))
Source repository
https://github.com/meowstr/chicken-raylib
Feel free to open issues and PR's!
Authors
Meowster ("Sasha Reinhard" on the mailing list)
License
MIT