]> git.jsancho.org Git - guile-irrlicht.git/blob - irrlicht.scm
Quake3Map example with all the functions needed
[guile-irrlicht.git] / irrlicht.scm
1 ;;; guile-irrlicht --- FFI bindings for Irrlicht Engine
2 ;;; Copyright (C) 2019 Javier Sancho <jsf@jsancho.org>
3 ;;;
4 ;;; This file is part of guile-irrlicht.
5 ;;;
6 ;;; Guile-irrlicht is free software; you can redistribute it and/or modify
7 ;;; it under the terms of the GNU Lesser General Public License as
8 ;;; published by the Free Software Foundation; either version 3 of the
9 ;;; License, or (at your option) any later version.
10 ;;;
11 ;;; Guile-irrlicht is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ;;; General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU Lesser General Public
17 ;;; License along with guile-irrlicht.  If not, see
18 ;;; <http://www.gnu.org/licenses/>.
19
20
21 (define-module (irrlicht)
22   #:use-module (ice-9 match)
23   #:use-module (system foreign)
24   #:use-module ((irrlicht bindings) #:prefix ffi:)
25   #:use-module ((irrlicht bindings core) #:prefix ffi-core:)
26   #:use-module ((irrlicht bindings gui) #:prefix ffi-gui:)
27   #:use-module ((irrlicht bindings io) #:prefix ffi-io:)
28   #:use-module ((irrlicht bindings scene) #:prefix ffi-scene:)
29   #:use-module ((irrlicht bindings video) #:prefix ffi-video:)
30   #:export (;; device
31             create-device
32             get-cursor-control
33             get-file-system
34             get-video-driver
35             get-gui-environment
36             get-scene-manager
37             is-window-active?
38             set-window-caption!
39             device-run?
40             device-drop!
41             ;; video driver
42             begin-scene
43             end-scene
44             get-fps
45             get-texture
46             get-video-driver-name
47             ;; gui
48             add-static-text!
49             gui-draw-all
50             set-visible-cursor!
51             ;; io
52             add-file-archive!
53             ;; scene
54             add-animated-mesh-scene-node
55             add-camera-scene-node
56             add-camera-scene-node-fps!
57             add-octree-scene-node-am
58             get-mesh
59             scene-draw-all
60             set-material-flag-am!
61             set-material-texture-am!
62             set-md2-animation!
63             set-position!))
64
65 ;; Device functions
66 (define* (create-device #:key
67                         (device-type 'software)
68                         (window-size '(640 480))
69                         (bits 16)
70                         (fullscreen #f)
71                         (stencilbuffer #f)
72                         (vsync #f))
73   (let ((driver (match device-type
74                        ('null ffi-video:EDT_NULL)
75                        ('software ffi-video:EDT_SOFTWARE)
76                        ('burnings ffi-video:EDT_BURNINGSVIDEO)
77                        ('direct3d8 ffi-video:EDT_DIRECT3D8)
78                        ('direct3d9 ffi-video:EDT_DIRECT3D9)
79                        ('opengl ffi-video:EDT_OPENGL)
80                        ('count ffi-video:EDT_COUNT)))
81         (wsize (make-c-struct ffi-core:dimension2d window-size)))
82     (let ((device (ffi:create-device driver wsize bits
83                                      (if fullscreen 1 0)
84                                      (if stencilbuffer 1 0)
85                                      (if vsync 1 0))))
86       (if (null-pointer? device) #f device))))
87
88 (define (get-cursor-control device)
89   (ffi:get-cursor-control device))
90
91 (define (get-file-system device)
92   (ffi:get-file-system device))
93
94 (define (get-video-driver device)
95   (ffi:get-video-driver device))
96
97 (define (get-gui-environment device)
98   (ffi:get-gui-environment device))
99
100 (define (get-scene-manager device)
101   (ffi:get-scene-manager device))
102
103 (define (is-window-active? device)
104   (if (> (ffi:is-window-active device) 0) #t #f))
105
106 (define (set-window-caption! device text)
107   (ffi:set-window-caption device (string->pointer text)))
108
109 (define (device-run? device)
110   (if (> (ffi:run device) 0) #t #f))
111
112 (define (device-drop! device)
113   (if (> (ffi:drop device) 0) #t #f))
114
115
116 ;; Video driver functions
117 (define* (begin-scene driver
118                       #:key
119                       (back-buffer #t)
120                       (z-buffer #t)
121                       (color '(255 0 0 0))
122                       (video-data %null-pointer)
123                       (source-rect '()))
124   (ffi-video:begin-scene driver
125                          (if back-buffer 1 0)
126                          (if z-buffer 1 0)
127                          (make-c-struct ffi-video:scolor color)
128                          video-data
129                          (if (null? source-rect)
130                              %null-pointer
131                              (make-c-struct ffi-core:rect source-rect))))
132
133 (define (end-scene driver)
134   (ffi-video:end-scene driver))
135
136 (define (get-fps driver)
137   (ffi-video:get-fps driver))
138
139 (define (get-texture driver filename)
140   (ffi-video:get-texture driver (string->pointer filename)))
141
142 (define (get-video-driver-name driver)
143   (pointer->string
144    (ffi-video:get-video-driver-name driver)))
145
146
147 ;; GUI functions
148 (define* (add-static-text! gui-env text rectangle
149                            #:key
150                            (border #f)
151                            (word-wrap #t)
152                            (parent %null-pointer)
153                            (id -1)
154                            (fill-background #f))
155   (ffi-gui:add-static-text gui-env
156                            (string->pointer text)
157                            (make-c-struct ffi-core:rect rectangle)
158                            (if border 1 0)
159                            (if word-wrap 1 0)
160                            parent
161                            id
162                            (if fill-background 1 0)))
163
164 (define (gui-draw-all gui-env)
165   (ffi-gui:draw-all gui-env))
166
167 (define (set-visible-cursor! cursor-control visible)
168   (ffi-gui:set-visible-cursor
169    cursor-control
170    (if visible 1 0)))
171
172
173 ;; IO functions
174 (define* (add-file-archive! file-system filename
175                             #:key
176                             (ignore-case #t)
177                             (ignore-paths #t)
178                             (archive-type 'unknown)
179                             (password "")
180                             (ret-archive %null-pointer))
181   (let ((type (match archive-type
182                      ('zip ffi-io:EFAT_ZIP)
183                      ('gzip ffi-io:EFAT_GZIP)
184                      ('folder ffi-io:EFAT_FOLDER)
185                      ('pak ffi-io:EFAT_PAK)
186                      ('npk ffi-io:EFAT_NPK)
187                      ('tar ffi-io:EFAT_TAR)
188                      ('wad ffi-io:EFAT_WAD)
189                      ('unknown ffi-io:EFAT_UNKNOWN))))
190     (ffi-io:add-file-archive file-system
191                              (string->pointer filename)
192                              (if ignore-case 1 0)
193                              (if ignore-paths 1 0)
194                              type
195                              (string->pointer password)
196                              ret-archive)))
197
198
199 ;; Scene functions
200 (define* (add-animated-mesh-scene-node scene-manager mesh
201                                        #:key
202                                        (parent %null-pointer)
203                                        (id -1)
204                                        (position '(0 0 0))
205                                        (rotation '(0 0 0))
206                                        (scale '(1 1 1))
207                                        (also-add-if-mesh-pointer-zero #f))
208   (let ((node (ffi-scene:add-animated-mesh-scene-node
209                scene-manager
210                mesh
211                parent
212                id
213                (make-c-struct ffi-core:vector3df position)
214                (make-c-struct ffi-core:vector3df rotation)
215                (make-c-struct ffi-core:vector3df scale)
216                (if also-add-if-mesh-pointer-zero 1 0))))
217     (if (null-pointer? node) #f node)))
218
219 (define* (add-camera-scene-node scene-manager
220                                 #:key
221                                 (parent %null-pointer)
222                                 (position '(0 0 0))
223                                 (lookat '(0 0 100))
224                                 (id -1)
225                                 (make-active #t))
226   (let ((camera (ffi-scene:add-camera-scene-node
227                  scene-manager
228                  parent
229                  (make-c-struct ffi-core:vector3df position)
230                  (make-c-struct ffi-core:vector3df lookat)
231                  id
232                  (if make-active 1 0))))
233     (if (null-pointer? camera) #f camera)))
234
235 (define* (add-camera-scene-node-fps! scene-manager
236                                      #:key
237                                      (parent %null-pointer)
238                                      (rotate-speed 100.0)
239                                      (move-speed 0.5)
240                                      (id -1)
241                                      (key-map-array %null-pointer)
242                                      (key-map-size 0)
243                                      (no-vertical-movement #f)
244                                      (jump-speed 0.0)
245                                      (invert-mouse #f)
246                                      (make-active #t))
247   (ffi-scene:add-camera-scene-node-fps
248    scene-manager
249    parent
250    rotate-speed
251    move-speed
252    id
253    key-map-array
254    key-map-size
255    (if no-vertical-movement 1 0)
256    jump-speed
257    (if invert-mouse 1 0)
258    (if make-active 1 0)))
259
260 (define* (add-octree-scene-node-am scene-manager mesh
261                                    #:key
262                                    (parent %null-pointer)
263                                    (id -1)
264                                    (minimal-polys-per-node 512)
265                                    (also-add-if-mesh-pointer-zero #f))
266   (ffi-scene:add-octree-scene-node-am
267    scene-manager
268    mesh
269    parent
270    id
271    minimal-polys-per-node
272    (if also-add-if-mesh-pointer-zero 1 0)))
273
274 (define (get-mesh scene-manager filename)
275   (let ((mesh (ffi-scene:get-mesh scene-manager (string->pointer filename))))
276     (if (null-pointer? mesh) #f mesh)))
277
278 (define (scene-draw-all scene-manager)
279   (ffi-scene:draw-all scene-manager))
280
281 (define (set-material-flag-am! node flag newvalue)
282   (let ((material-flag
283          (match flag
284                 ('wireframe ffi-video:EMF_WIREFRAME)
285                 ('pointcloud ffi-video:EMF_POINTCLOUD)
286                 ('gouraud-shading ffi-video:EMF_GOURAUD_SHADING)
287                 ('lighting ffi-video:EMF_LIGHTING)
288                 ('zbuffer ffi-video:EMF_ZBUFFER)
289                 ('zwrite-enable ffi-video:EMF_ZWRITE_ENABLE)
290                 ('back-face-culling ffi-video:EMF_BACK_FACE_CULLING)
291                 ('front-face-culling ffi-video:EMF_FRONT_FACE_CULLING)
292                 ('bilinear-filter ffi-video:EMF_BILINEAR_FILTER)
293                 ('trilinear-filter ffi-video:EMF_TRILINEAR_FILTER)
294                 ('anisotropic-filter ffi-video:EMF_ANISOTROPIC_FILTER)
295                 ('fog-enable ffi-video:EMF_FOG_ENABLE)
296                 ('normalize-normals ffi-video:EMF_NORMALIZE_NORMALS)
297                 ('texture-wrap ffi-video:EMF_TEXTURE_WRAP)
298                 ('anti-aliasing ffi-video:EMF_ANTI_ALIASING)
299                 ('color-mask ffi-video:EMF_COLOR_MASK)
300                 ('color-material ffi-video:EMF_COLOR_MATERIAL)
301                 ('use-mip-maps ffi-video:EMF_USE_MIP_MAPS)
302                 ('blend-operation ffi-video:EMF_BLEND_OPERATION)
303                 ('polygon-offset ffi-video:EMF_POLYGON_OFFSET))))
304     (ffi-scene:set-material-flag-am
305      node
306      material-flag
307      (if newvalue 1 0))))
308
309 (define (set-material-texture-am! node texture-layer texture)
310   (ffi-scene:set-material-texture-am node texture-layer texture))
311
312 (define (set-md2-animation! node anim)
313   (let ((animation-type
314          (match anim
315                 ('stand ffi-scene:EMAT_STAND)
316                 ('run ffi-scene:EMAT_RUN)
317                 ('attack ffi-scene:EMAT_ATTACK)
318                 ('pain-a ffi-scene:EMAT_PAIN_A)
319                 ('pain-b ffi-scene:EMAT_PAIN_B)
320                 ('pain-c ffi-scene:EMAT_PAIN_C)
321                 ('jump ffi-scene:EMAT_JUMP)
322                 ('flip ffi-scene:EMAT_FLIP)
323                 ('salute ffi-scene:EMAT_SALUTE)
324                 ('fallback ffi-scene:EMAT_FALLBACK)
325                 ('wave ffi-scene:EMAT_WAVE)
326                 ('point ffi-scene:EMAT_POINT)
327                 ('crouch-stand ffi-scene:EMAT_CROUCH_STAND)
328                 ('crouch-walk ffi-scene:EMAT_CROUCH_WALK)
329                 ('crouch-attack ffi-scene:EMAT_CROUCH_ATTACK)
330                 ('crouch-pain ffi-scene:EMAT_CROUCH_PAIN)
331                 ('crouch-death ffi-scene:EMAT_CROUCH_DEATH)
332                 ('death-fallback ffi-scene:EMAT_DEATH_FALLBACK)
333                 ('death-fallforward ffi-scene:EMAT_DEATH_FALLFORWARD)
334                 ('death-fallbackslow ffi-scene:EMAT_DEATH_FALLBACKSLOW)
335                 ('boom ffi-scene:EMAT_BOOM)
336                 ('count ffi-scene:EMAT_COUNT))))
337     (ffi-scene:set-md2-animation
338      node
339      animation-type)))
340
341 (define (set-position! node newpos)
342   (ffi-scene:set-position
343    node
344    (make-c-struct ffi-core:vector3df newpos)))