forked from netguy204/brianscheme
/
sdl.sch
287 lines (236 loc) · 10.9 KB
/
sdl.sch
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
(require 'ffi)
(define-struct sdl:event
(type
value))
(with-library (handle "libSDL")
(let ((init (ffi:dlsym handle "SDL_Init"))
(set-caption (ffi:dlsym handle "SDL_WM_SetCaption"))
(set-video-mode (ffi:dlsym handle "SDL_SetVideoMode"))
(rw-from-file (ffi:dlsym handle "SDL_RWFromFile"))
(load-bmp-rw (ffi:dlsym handle "SDL_LoadBMP_RW"))
(display-format (ffi:dlsym handle "SDL_DisplayFormat"))
(free-surface (ffi:dlsym handle "SDL_FreeSurface"))
(upper-blit (ffi:dlsym handle "SDL_UpperBlit"))
(update-rect (ffi:dlsym handle "SDL_UpdateRect"))
(quit (ffi:dlsym handle "SDL_Quit"))
(poll-event (ffi:dlsym handle "SDL_PollEvent"))
(wait-event (ffi:dlsym handle "SDL_WaitEvent"))
(create-rgb-surface (ffi:dlsym handle "SDL_CreateRGBSurface"))
(header "<SDL/SDL.h>"))
(define-constant-function sdl:INIT-VIDEO
(ffi:get-const header "%d" "SDL_INIT_VIDEO"))
(define-constant-function sdl:SW-SURFACE
(ffi:get-const header "%d" "SDL_SWSURFACE"))
(define-constant-function sdl:event:type-offset
(ffi:offset-of header "SDL_Event" "type"))
(define-constant-function sdl:size-of-event
(ffi:size-of header "SDL_Event"))
(ffi:define-enum "<SDL/SDL.h>" SDL_EventType sdl:event-type
(SDL_NOEVENT sdl:no-event) ; /**< Unused (do not remove) */
(SDL_ACTIVEEVENT sdl:active-event) ; /**< Application loses/gains visibility */
(SDL_KEYDOWN sdl:key-down) ; /**< Keys pressed */
(SDL_KEYUP sdl:key-up) ; /**< Keys released */
(SDL_MOUSEMOTION sdl:mouse-motion) ; /**< Mouse moved */
(SDL_MOUSEBUTTONDOWN sdl:mouse-button-down) ;/**< Mouse button pressed */
(SDL_MOUSEBUTTONUP sdl:mouse-button-up) ; /**< Mouse button released */
(SDL_JOYAXISMOTION sdl:joy-axis-motion) ; /**< Joystick axis motion */
(SDL_JOYBALLMOTION sdl:joy-ball-motion) ; /**< Joystick trackball motion */
(SDL_JOYHATMOTION sdl:joy-hat-motion) ; /**< Joystick hat position change */
(SDL_JOYBUTTONDOWN sdl:joy-button-down) ; /**< Joystick button pressed */
(SDL_JOYBUTTONUP sdl:joy-button-up) ; /**< Joystick button released */
(SDL_QUIT sdl:quit) ; /**< User-requested quit */
(SDL_SYSWMEVENT sdl:sys-wm-event) ; /**< System specific event */
(SDL_VIDEORESIZE sdl:video-resize) ; /**< User resized video mode */
(SDL_VIDEOEXPOSE sdl:video-expose) ; /**< Screen needs to be redrawn */
(SDL_USEREVENT sdl:user-event))
(define-constant-function sdl:rect:size-of
(ffi:size-of header "SDL_Rect"))
(define-constant-function sdl:rect:offset-of-x
(ffi:offset-of header "SDL_Rect" "x"))
(define-constant-function sdl:rect:offset-of-y
(ffi:offset-of header "SDL_Rect" "y"))
(define-constant-function sdl:rect:offset-of-w
(ffi:offset-of header "SDL_Rect" "w"))
(define-constant-function sdl:rect:offset-of-h
(ffi:offset-of header "SDL_Rect" "h"))
(define (sdl:make-rect x y width height)
(let ((rect (ffi:make-bytes (sdl:rect:size-of))))
(ffi:pack-bytes rect (sdl:rect:offset-of-x)
(ffi:bs->machine (ffi:integer->bytes x 2)))
(ffi:pack-bytes rect (sdl:rect:offset-of-y)
(ffi:bs->machine (ffi:integer->bytes y 2)))
(ffi:pack-bytes rect (sdl:rect:offset-of-w)
(ffi:bs->machine (ffi:integer->bytes width 2)))
(ffi:pack-bytes rect (sdl:rect:offset-of-h)
(ffi:bs->machine (ffi:integer->bytes height 2)))
rect))
(define (sdl:event:empty)
(let ((sz (sdl:size-of-event)))
(ffi:set-bytes (ffi:make-bytes sz) 0 sz 0)))
(ffi:define-header-struct "<SDL/SDL.h>" "SDL_Event"
sdl:event:unpack-mouse-motion-event
("motion.state" state (ffi:make-int-unpacker 1))
("motion.x" x (ffi:make-int-unpacker 2))
("motion.y" y (ffi:make-int-unpacker 2))
("motion.xrel" xrel (ffi:make-int-unpacker 2))
("motion.yrel" yrel (ffi:make-int-unpacker 2)))
(ffi:define-header-struct "<SDL/SDL.h>" "SDL_Event"
sdl:event:unpack-mouse-button-event
("button.button" button (ffi:make-int-unpacker 1))
("button.state" state (ffi:make-int-unpacker 1))
("button.x" x (ffi:make-int-unpacker 2))
("button.y" y (ffi:make-int-unpacker 2)))
(ffi:define-header-struct "<SDL/SDL.h>" "SDL_PixelFormat"
sdl:unpack-pixel-format
("BitsPerPixel" bits-per-pixel (ffi:make-int-unpacker 1))
("BytesPerPixel" bytes-per-pixel (ffi:make-int-unpacker 1))
("Rmask" rmask (ffi:make-int-unpacker 4))
("Gmask" gmask (ffi:make-int-unpacker 4))
("Bmask" bmask (ffi:make-int-unpacker 4))
("Amask" amask (ffi:make-int-unpacker 4))
("alpha" alpha (ffi:make-int-unpacker 1)))
(ffi:define-header-struct "<SDL/SDL.h>" "SDL_Surface"
sdl:unpack-surface
("w" w (ffi:make-int-unpacker 4))
("h" h (ffi:make-int-unpacker 4))
("pitch" pitch (ffi:make-int-unpacker 2))
("format" format (lambda (bytes offset)
(sdl:unpack-pixel-format (ffi:unpack-pointer bytes offset) 0))))
(ffi:define-header-struct "<SDL/SDL.h>" "SDL_Rect"
sdl:unpack-rect
("x" x (ffi:make-int-unpacker 2))
("y" y (ffi:make-int-unpacker 2))
("w" w (ffi:make-int-unpacker 2))
("h" h (ffi:make-int-unpacker 2)))
(define (sdl:event:unpack bytes offset)
(let ((type (unpack-sdl:event-type bytes (+ offset (sdl:event:type-offset)))))
(make-sdl:event 'type type
'value (case type
((sdl:mouse-button-down sdl:mouse-button-up)
(sdl:event:unpack-mouse-button-event bytes offset))
(sdl:mouse-motion
(sdl:event:unpack-mouse-motion-event bytes offset))
(else nil)))))
(define (sdl:poll/wait-event which)
(let* ((ev (sdl:event:empty))
(res (ffi:funcall which 'ffi-uint ev)))
(if (= res 1)
(sdl:event:unpack ev 0)
nil)))
(define (sdl:poll-event)
(sdl:poll/wait-event poll-event))
(define (sdl:wait-event)
(sdl:poll/wait-event wait-event))
(define (sdl:init flag)
(ffi:funcall init 'ffi-uint flag))
(define (sdl:wm-set-caption title iconified)
(ffi:funcall set-caption 'ffi-void title iconified))
(define (sdl:set-video-mode width height bpp flags)
(ffi:funcall set-video-mode 'ffi-pointer width height bpp flags))
(define (sdl:rw-from-file name mode)
(ffi:funcall rw-from-file 'ffi-pointer name mode))
(define (sdl:load-bmp-rw handle free?)
(ffi:funcall load-bmp-rw 'ffi-pointer handle (if free? 1 0)))
(define (sdl:load-bmp name)
(sdl:load-bmp-rw (sdl:rw-from-file name "rb") 1))
(define (sdl:display-format surface)
(ffi:funcall display-format 'ffi-pointer surface))
(define (sdl:create-rgb-surface flags width height depth rmask gmask bmask amask)
(ffi:funcall create-rgb-surface 'ffi-pointer
flags width height depth rmask gmask bmask amask))
(define (sdl:free-surface surface)
(ffi:funcall free-surface 'ffi-void surface))
(define (sdl:upper-blit src src-rect dst dst-rect)
(ffi:funcall upper-blit 'ffi-uint src src-rect dst dst-rect))
(define (sdl:blit-surface src src-rect dst dst-rect)
;; TODO: convenience rect syntax
(sdl:upper-blit src src-rect dst dst-rect))
(define (sdl:update-rect screen x y width height)
(ffi:funcall update-rect 'ffi-void screen x y width height))
(define (sdl:quit)
(ffi:funcall quit 'ffi-void))))
(with-library (handle "libSDL_gfx")
(let ((pixel-rgba (ffi:dlsym handle "pixelRGBA"))
(line-rgba (ffi:dlsym handle "lineRGBA"))
(aaline-rgba (ffi:dlsym handle "aalineRGBA"))
(rectangle-rgba (ffi:dlsym handle "rectangleRGBA"))
(box-rgba (ffi:dlsym handle "boxRGBA")))
(define (sdl:pixel-rgba surface x y r g b a)
(ffi:funcall pixel-rgba 'ffi-uint
surface
(ffi:alien-ushort x) (ffi:alien-ushort y)
(ffi:alien-uchar r) (ffi:alien-uchar g) (ffi:alien-uchar b)
(ffi:alien-uchar a)))
(define (sdl:line-rgba surface x1 y1 x2 y2 r g b a)
(ffi:funcall line-rgba 'ffi-uint
surface
(ffi:alien-ushort x1) (ffi:alien-ushort y1)
(ffi:alien-ushort x2) (ffi:alien-ushort y2)
(ffi:alien-uchar r) (ffi:alien-uchar g) (ffi:alien-uchar b)
(ffi:alien-uchar a)))
(define (sdl:aaline-rgba surface x1 y1 x2 y2 r g b a)
(ffi:funcall aaline-rgba 'ffi-uint
surface
(ffi:alien-ushort x1) (ffi:alien-ushort y1)
(ffi:alien-ushort x2) (ffi:alien-ushort y2)
(ffi:alien-uchar r) (ffi:alien-uchar g) (ffi:alien-uchar b)
(ffi:alien-uchar a)))
(define (sdl:rectangle-rgba surface x1 y1 x2 y2 r g b a)
(ffi:funcall rectangle-rgba 'ffi-uint
surface
(ffi:alien-ushort x1) (ffi:alien-ushort y1)
(ffi:alien-ushort x2) (ffi:alien-ushort y2)
(ffi:alien-uchar r) (ffi:alien-uchar g) (ffi:alien-uchar b)
(ffi:alien-uchar a)))
(define (sdl:box-rgba surface x1 y1 x2 y2 r g b a)
(ffi:funcall box-rgba 'ffi-uint
surface
(ffi:alien-ushort x1) (ffi:alien-ushort y1)
(ffi:alien-ushort x2) (ffi:alien-ushort y2)
(ffi:alien-uchar r) (ffi:alien-uchar g) (ffi:alien-uchar b)
(ffi:alien-uchar a)))))
(define (sdl:load-image-file file)
"load image from FILE and convert to display format"
(let* ((tmp (sdl:load-bmp file))
(img (sdl:display-format tmp)))
(sdl:free-surface tmp)
img))
(define (sdl:create-compatible-surface reference-surface width height)
(let* ((surface (sdl:unpack-surface reference-surface 0))
(format (second (assoc 'format surface)))
(rmask (second (assoc 'rmask format)))
(gmask (second (assoc 'gmask format)))
(bmask (second (assoc 'bmask format)))
(amask (second (assoc 'amask format)))
(depth (second (assoc 'bits-per-pixel format))))
(sdl:create-rgb-surface (sdl:SW-SURFACE) width height depth
rmask gmask bmask amask)))
(define (sdl:test)
(sdl:init (sdl:INIT-VIDEO))
(sdl:wm-set-caption "SDL Test" "SDL Test")
(let* ((screen (sdl:set-video-mode 640 480 0 0))
(src-rect (sdl:make-rect 0 0 128 128))
(last-x nil)
(last-y nil)
(mouse-active #f))
(set! *screen* screen)
(let loop ((ev (sdl:wait-event)))
(case (and ev (sdl:event-type-ref ev))
(sdl:mouse-button-down (set! mouse-active #t)
(let ((v (sdl:event-value-ref ev)))
(set! last-x (second (assoc 'x v)))
(set! last-y (second (assoc 'y v)))))
(sdl:mouse-button-up (set! mouse-active #f))
(sdl:mouse-motion (when mouse-active
(let* ((v (sdl:event-value-ref ev))
(x (second (assoc 'x v)))
(y (second (assoc 'y v))))
(sdl:aaline-rgba screen last-x last-y x y 255 255 255 255)
(set! last-x x)
(set! last-y y)))))
;(sdl:blit-surface bmp src-rect screen (sdl:make-rect bmp-x bmp-y 128 128))
;(sdl:pixel-rgba screen last-x last-y 255 255 255 255)
(sdl:update-rect screen 0 0 640 480)
(if (eq? (sdl:event-type-ref ev) 'sdl:quit)
(sdl:quit)
(loop (sdl:wait-event))))))