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 scene) #:prefix ffi-scene:)
28 #:use-module ((irrlicht bindings video) #:prefix ffi-video:)
45 add-animated-mesh-scene-node
50 set-material-texture-am!
54 (define* (create-device #:key
55 (device-type 'software)
56 (window-size '(640 480))
61 (let ((driver (match device-type
62 ('null ffi-video:EDT_NULL)
63 ('software ffi-video:EDT_SOFTWARE)
64 ('burnings ffi-video:EDT_BURNINGSVIDEO)
65 ('direct3d8 ffi-video:EDT_DIRECT3D8)
66 ('direct3d9 ffi-video:EDT_DIRECT3D9)
67 ('opengl ffi-video:EDT_OPENGL)
68 ('count ffi-video:EDT_COUNT)))
69 (wsize (make-c-struct ffi-core:dimension2d window-size)))
70 (let ((device (ffi:create-device driver wsize bits
72 (if stencilbuffer 1 0)
74 (if (null-pointer? device) #f device))))
76 (define (get-video-driver device)
77 (ffi:get-video-driver device))
79 (define (get-gui-environment device)
80 (ffi:get-gui-environment device))
82 (define (get-scene-manager device)
83 (ffi:get-scene-manager device))
85 (define (set-window-caption! device text)
86 (ffi:set-window-caption device (string->pointer text)))
88 (define (device-run? device)
89 (if (> (ffi:run device) 0) #t #f))
91 (define (device-drop! device)
92 (if (> (ffi:drop device) 0) #t #f))
96 (define* (begin-scene driver
101 (video-data %null-pointer)
103 (ffi-video:begin-scene driver
106 (make-c-struct ffi-video:scolor color)
108 (if (null? source-rect)
110 (make-c-struct ffi-core:rect source-rect))))
112 (define (end-scene driver)
113 (ffi-video:end-scene driver))
115 (define (get-texture driver filename)
116 (ffi-video:get-texture driver (string->pointer filename)))
120 (define* (add-static-text! gui-env text rectangle
124 (parent %null-pointer)
126 (fill-background #f))
127 (ffi-gui:add-static-text gui-env
128 (string->pointer text)
129 (make-c-struct ffi-core:rect rectangle)
134 (if fill-background 1 0)))
136 (define (gui-draw-all gui-env)
137 (ffi-gui:draw-all gui-env))
141 (define* (add-animated-mesh-scene-node scene-manager mesh
143 (parent %null-pointer)
148 (also-add-if-mesh-pointer-zero #f))
149 (let ((node (ffi-scene:add-animated-mesh-scene-node
154 (make-c-struct ffi-core:vector3df position)
155 (make-c-struct ffi-core:vector3df rotation)
156 (make-c-struct ffi-core:vector3df scale)
157 (if also-add-if-mesh-pointer-zero 1 0))))
158 (if (null-pointer? node) #f node)))
160 (define* (add-camera-scene-node scene-manager
162 (parent %null-pointer)
167 (let ((camera (ffi-scene:add-camera-scene-node
170 (make-c-struct ffi-core:vector3df position)
171 (make-c-struct ffi-core:vector3df lookat)
173 (if make-active 1 0))))
174 (if (null-pointer? camera) #f camera)))
176 (define (get-mesh scene-manager filename)
177 (let ((mesh (ffi-scene:get-mesh scene-manager (string->pointer filename))))
178 (if (null-pointer? mesh) #f mesh)))
180 (define (scene-draw-all scene-manager)
181 (ffi-scene:draw-all scene-manager))
183 (define (set-material-flag-am! node flag newvalue)
186 ('wireframe ffi-video:EMF_WIREFRAME)
187 ('pointcloud ffi-video:EMF_POINTCLOUD)
188 ('gouraud-shading ffi-video:EMF_GOURAUD_SHADING)
189 ('lighting ffi-video:EMF_LIGHTING)
190 ('zbuffer ffi-video:EMF_ZBUFFER)
191 ('zwrite-enable ffi-video:EMF_ZWRITE_ENABLE)
192 ('back-face-culling ffi-video:EMF_BACK_FACE_CULLING)
193 ('front-face-culling ffi-video:EMF_FRONT_FACE_CULLING)
194 ('bilinear-filter ffi-video:EMF_BILINEAR_FILTER)
195 ('trilinear-filter ffi-video:EMF_TRILINEAR_FILTER)
196 ('anisotropic-filter ffi-video:EMF_ANISOTROPIC_FILTER)
197 ('fog-enable ffi-video:EMF_FOG_ENABLE)
198 ('normalize-normals ffi-video:EMF_NORMALIZE_NORMALS)
199 ('texture-wrap ffi-video:EMF_TEXTURE_WRAP)
200 ('anti-aliasing ffi-video:EMF_ANTI_ALIASING)
201 ('color-mask ffi-video:EMF_COLOR_MASK)
202 ('color-material ffi-video:EMF_COLOR_MATERIAL)
203 ('use-mip-maps ffi-video:EMF_USE_MIP_MAPS)
204 ('blend-operation ffi-video:EMF_BLEND_OPERATION)
205 ('polygon-offset ffi-video:EMF_POLYGON_OFFSET))))
206 (ffi-scene:set-material-flag-am
211 (define (set-material-texture-am! node texture-layer texture)
212 (ffi-scene:set-material-texture-am node texture-layer texture))
214 (define (set-md2-animation! node anim)
215 (let ((animation-type
217 ('stand ffi-scene:EMAT_STAND)
218 ('run ffi-scene:EMAT_RUN)
219 ('attack ffi-scene:EMAT_ATTACK)
220 ('pain-a ffi-scene:EMAT_PAIN_A)
221 ('pain-b ffi-scene:EMAT_PAIN_B)
222 ('pain-c ffi-scene:EMAT_PAIN_C)
223 ('jump ffi-scene:EMAT_JUMP)
224 ('flip ffi-scene:EMAT_FLIP)
225 ('salute ffi-scene:EMAT_SALUTE)
226 ('fallback ffi-scene:EMAT_FALLBACK)
227 ('wave ffi-scene:EMAT_WAVE)
228 ('point ffi-scene:EMAT_POINT)
229 ('crouch-stand ffi-scene:EMAT_CROUCH_STAND)
230 ('crouch-walk ffi-scene:EMAT_CROUCH_WALK)
231 ('crouch-attack ffi-scene:EMAT_CROUCH_ATTACK)
232 ('crouch-pain ffi-scene:EMAT_CROUCH_PAIN)
233 ('crouch-death ffi-scene:EMAT_CROUCH_DEATH)
234 ('death-fallback ffi-scene:EMAT_DEATH_FALLBACK)
235 ('death-fallforward ffi-scene:EMAT_DEATH_FALLFORWARD)
236 ('death-fallbackslow ffi-scene:EMAT_DEATH_FALLBACKSLOW)
237 ('boom ffi-scene:EMAT_BOOM)
238 ('count ffi-scene:EMAT_COUNT))))
239 (ffi-scene:set-md2-animation