-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpackage.lisp
More file actions
164 lines (153 loc) · 5.83 KB
/
Copy pathpackage.lisp
File metadata and controls
164 lines (153 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
;; NOTE: 2025-07-05 Happy Birthday!
;;
;; Remember, if you're having issues loading this under ECL, load it via the
;; form found in `repl.lisp', not via automated system loading through Sly! The
;; linker flags need to be set properly before this can be loaded, even when
;; doing REPL developement.
(defpackage raylib
(:use :cl #+sbcl :sb-alien)
(:local-nicknames (#:tg #:trivial-garbage))
;; --- Types --- ;;
(:export #:vector2 #:make-vector2 #:vector2-x #:vector2-y
#:rectangle #:make-rectangle #:rectangle-x #:rectangle-y #:rectangle-width #:rectangle-height
#:color #:make-color #:color-alpha
#:texture #:texture-width #:texture-height
#:font
#:audio-stream #:sound
#:music #:music-looping
#:camera-2d #:make-camera-2d #:camera-2d-offset #:camera-2d-target #:get-world-to-screen-2d
#:keyboard-key #:gamepad-button)
;; --- Functions --- ;;
(:export #:init-window #:close-window
#:init-audio-device #:close-audio-device
#:set-target-fps #:window-should-close
#:begin-drawing #:end-drawing
#:begin-mode-2d #:end-mode-2d
#:clear-background #:draw-fps #:draw-text #:draw-text-ex #:draw-rectangle #:draw-line #:draw-pixel
#:load-texture #:unload-texture #:is-texture-valid #:draw-texture #:draw-texture-v #:draw-texture-rec
#:load-sound #:unload-sound #:play-sound
#:load-music-stream #:unload-music-stream #:is-music-stream-playing #:play-music-stream #:update-music-stream
#:load-font #:load-font-ex #:unload-font #:is-font-valid #:get-font-default
#:is-key-pressed #:is-key-down
#:is-gamepad-button-pressed #:is-gamepad-button-down #:get-gamepad-name #:is-gamepad-available #:get-gamepad-button-pressed
#:get-gamepad-axis-count #:get-gamepad-axis-movement
#:check-collision-recs #:check-collision-point-rec
#:get-frame-time)
;; --- Library Loading --- ;;
#+sbcl
(:export #:load-shared-objects)
(:documentation "A light wrapping of necessary Raylib types and functions."))
(in-package :raylib)
#+sbcl
(defun load-shared-objects (&key (target nil))
"Dynamically load the necessary `.so' files. This is wrapped as a function so that
downstream callers can call it again as necessary when the Lisp Image is being
restarted. Note the use of `:dont-save' below. This is to allow the package to
be compiled with `.so' files found in one location, but run with ones from another."
(let ((dir (case target
(:linux "/usr/lib/")
(:darwin #+arm64 "/opt/homebrew/lib/"
#-arm64 "/usr/local/lib/")
(t "lib/"))))
#+linux
(progn
(load-shared-object (merge-pathnames "liblisp-raylib.so" dir) :dont-save t)
(load-shared-object (merge-pathnames "liblisp-raylib-shim.so" dir) :dont-save t))
#+win32
(progn
(load-shared-object (merge-pathnames "lisp-raylib.dll" dir) :dont-save t)
(load-shared-object (merge-pathnames "lisp-raylib-shim.dll" dir) :dont-save t))
#+darwin
(progn
(load-shared-object (merge-pathnames "liblisp-raylib.dylib" dir) :dont-save t)
(load-shared-object (merge-pathnames "liblisp-raylib-shim.dylib" dir) :dont-save t))))
#+sbcl
(load-shared-objects)
;; NOTE: 2025-01-03 We preload the shared libraries here to ensure that all
;; functions are already visible when we start to reference them in other files.
#+(and ecl linux)
(progn
(ffi:load-foreign-library #p"lib/liblisp-raylib.so")
(ffi:load-foreign-library #p"lib/liblisp-raylib-shim.so"))
#+(and ecl darwin)
(progn
(ffi:load-foreign-library #p"lib/liblisp-raylib.dylib")
(ffi:load-foreign-library #p"lib/liblisp-raylib-shim.dylib"))
;; --- Keyboard and Gamepad --- ;;
;; HACK: 2024-12-29 Hacked manually as I couldn't figure out a first-class way
;; to reference enum values.
(defun keyboard-key (kw)
(case kw
;; --- Directions --- ;;
(:right 262)
(:left 263)
(:down 264)
(:up 265)
;; --- Control --- ;;
(:space 32)
(:tab 258)
(:enter 257)
(:shift 340)
;; --- Numbers --- ;;
(:zero 48)
(:one 49)
(:two 50)
(:three 51)
(:four 52)
(:five 53)
(:six 54)
(:seven 55)
(:eight 56)
(:nine 57)
;; --- Letters --- ;;
(:w 87)
(:a 65)
(:s 83)
(:d 68)
(:r 82)
(t (error "Unknown keyboard key: ~a" kw))))
#++
(keyboard-key :kim)
(defun gamepad-button (kw)
(case kw
(:unknown 0)
(:left-face-up 1)
(:left-face-right 2)
(:left-face-down 3)
(:left-face-left 4)
(:right-face-up 5)
(:right-face-right 6)
(:right-face-down 7)
(:right-face-left 8)
(:left-trigger-1 9)
(:left-trigger-2 10)
(:right-trigger-1 11)
(:right-trigger-2 12)
(:middle-left 13)
(:middle 14)
(:middle-right 15)
(:left-thumb 16)
(:right-thumb 17)
(t (error "Unknown gamepad button: ~a" kw))))
;; Sanity test: This should work as-is under either compiler.
#+nil
(progn
(init-window 300 300 "hello!")
(let* ((white (make-color :r 255 :g 255 :b 255 :a 255))
(black (make-color :r 0 :g 0 :b 0 :a 255))
(text "Café 日本語!")
(points (concatenate 'vector (mapcar #'char-code (concatenate 'list text))))
(font (load-font-ex #p"/usr/share/fonts/OTF/ipag.ttf" 20 points))
(pos (make-vector2 :x 100 :y 100)))
(unless (is-font-valid font)
(error "Invalid font!"))
(set-target-fps 100)
(loop :while (not (window-should-close))
:do (progn (begin-drawing)
(clear-background white)
(draw-text "Café" 50 50 20 black)
(draw-text-ex font "日本語 Café" pos 20 5 black)
(draw-fps 0 0)
(end-drawing)))
(unload-font font)
(close-window)))