1 ;;; guile-irrlicht --- FFI bindings for Irrlicht Engine
2 ;;; Copyright (C) 2019 Javier Sancho <jsf@jsancho.org>
4 ;;; This file is part of guile-irrlicht.
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.
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.
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/>.
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:)
54 add-animated-mesh-scene-node
56 add-camera-scene-node-fps!
57 add-octree-scene-node-am
61 set-material-texture-am!
66 (define* (create-device #:key
67 (device-type 'software)
68 (window-size '(640 480))
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
84 (if stencilbuffer 1 0)
86 (if (null-pointer? device) #f device))))
88 (define (get-cursor-control device)
89 (ffi:get-cursor-control device))
91 (define (get-file-system device)
92 (ffi:get-file-system device))
94 (define (get-video-driver device)
95 (ffi:get-video-driver device))
97 (define (get-gui-environment device)
98 (ffi:get-gui-environment device))
100 (define (get-scene-manager device)
101 (ffi:get-scene-manager device))
103 (define (is-window-active? device)
104 (if (> (ffi:is-window-active device) 0) #t #f))
106 (define (set-window-caption! device text)
107 (ffi:set-window-caption device (string->pointer text)))
109 (define (device-run? device)
110 (if (> (ffi:run device) 0) #t #f))
112 (define (device-drop! device)
113 (if (> (ffi:drop device) 0) #t #f))
116 ;; Video driver functions
117 (define* (begin-scene driver
122 (video-data %null-pointer)
124 (ffi-video:begin-scene driver
127 (make-c-struct ffi-video:scolor color)
129 (if (null? source-rect)
131 (make-c-struct ffi-core:rect source-rect))))
133 (define (end-scene driver)
134 (ffi-video:end-scene driver))
136 (define (get-fps driver)
137 (ffi-video:get-fps driver))
139 (define (get-texture driver filename)
140 (ffi-video:get-texture driver (string->pointer filename)))
142 (define (get-video-driver-name driver)
144 (ffi-video:get-video-driver-name driver)))
148 (define* (add-static-text! gui-env text rectangle
152 (parent %null-pointer)
154 (fill-background #f))
155 (ffi-gui:add-static-text gui-env
156 (string->pointer text)
157 (make-c-struct ffi-core:rect rectangle)
162 (if fill-background 1 0)))
164 (define (gui-draw-all gui-env)
165 (ffi-gui:draw-all gui-env))
167 (define (set-visible-cursor! cursor-control visible)
168 (ffi-gui:set-visible-cursor
174 (define* (add-file-archive! file-system filename
178 (archive-type 'unknown)
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)
193 (if ignore-paths 1 0)
195 (string->pointer password)
200 (define* (add-animated-mesh-scene-node scene-manager mesh
202 (parent %null-pointer)
207 (also-add-if-mesh-pointer-zero #f))
208 (let ((node (ffi-scene:add-animated-mesh-scene-node
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)))
219 (define* (add-camera-scene-node scene-manager
221 (parent %null-pointer)
226 (let ((camera (ffi-scene:add-camera-scene-node
229 (make-c-struct ffi-core:vector3df position)
230 (make-c-struct ffi-core:vector3df lookat)
232 (if make-active 1 0))))
233 (if (null-pointer? camera) #f camera)))
235 (define* (add-camera-scene-node-fps! scene-manager
237 (parent %null-pointer)
241 (key-map-array %null-pointer)
243 (no-vertical-movement #f)
247 (ffi-scene:add-camera-scene-node-fps
255 (if no-vertical-movement 1 0)
257 (if invert-mouse 1 0)
258 (if make-active 1 0)))
260 (define* (add-octree-scene-node-am scene-manager mesh
262 (parent %null-pointer)
264 (minimal-polys-per-node 512)
265 (also-add-if-mesh-pointer-zero #f))
266 (ffi-scene:add-octree-scene-node-am
271 minimal-polys-per-node
272 (if also-add-if-mesh-pointer-zero 1 0)))
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)))
278 (define (scene-draw-all scene-manager)
279 (ffi-scene:draw-all scene-manager))
281 (define (set-material-flag-am! node flag newvalue)
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
309 (define (set-material-texture-am! node texture-layer texture)
310 (ffi-scene:set-material-texture-am node texture-layer texture))
312 (define (set-md2-animation! node anim)
313 (let ((animation-type
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
341 (define (set-position! node newpos)
342 (ffi-scene:set-position
344 (make-c-struct ffi-core:vector3df newpos)))