]> git.jsancho.org Git - guile-irrlicht.git/blob - src/video-driver.cpp
begin-scene
[guile-irrlicht.git] / src / video-driver.cpp
1 /* guile-irrlicht --- GNU Guile bindings for Irrlicht Engine
2
3    Copyright (C) 2020 Javier Sancho <jsf@jsancho.org>
4
5    This file is part of guile-irrlicht.
6
7    guile-irrlicht is free software; you can redistribute it and/or modify
8    it under the terms of the GNU Lesser General Public License as
9    published by the Free Software Foundation; either version 3 of the
10    License, or (at your option) any later version.
11
12    guile-irrlicht is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU Lesser General Public
18    License along with guile-irrlicht. If not, see
19    <http://www.gnu.org/licenses/>.
20 */
21
22 #include <irrlicht/irrlicht.h>
23 #include <libguile.h>
24 #include "color.h"
25 #include "gsubr.h"
26 #include "matrix4.h"
27 #include "primitive-types.h"
28 #include "rect.h"
29 #include "vertex3d.h"
30 #include "video-driver.h"
31
32
33 using namespace irr;
34
35
36 SCM
37 IVideoDriver_beginScene (SCM video_driver,
38                          SCM back_buffer,
39                          SCM z_buffer,
40                          SCM color,
41                          SCM video_data,
42                          SCM source_rect)
43 {
44   video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
45
46   // Source rect
47   core::rect<s32>* sourceRectAddress = 0;
48   if (!scm_is_false (source_rect))
49     {
50       core::rect<s32> sourceRect = scm_to_rect_s32 (source_rect);
51       sourceRectAddress = &sourceRect;
52     }
53
54   return scm_from_bool (driver->beginScene (scm_to_bool (back_buffer),
55                                             scm_to_bool (z_buffer),
56                                             scm_to_color (color),
57                                             video::SExposedVideoData (),
58                                             sourceRectAddress));
59 }
60
61
62 SCM
63 IVideoDriver_drawVertexPrimitiveList (SCM video_driver,
64                                       SCM vertices,
65                                       SCM indices,
66                                       SCM v_type,
67                                       SCM p_type)
68 {
69   // Build vertex array
70   u32 vertex_count = scm_to_uint32 (scm_length (vertices));
71   video::S3DVertex s3d_vertices [vertex_count];
72   for (int i = 0; i < vertex_count; i++)
73     {
74       video::S3DVertex* vertex =
75         (video::S3DVertex*) scm_to_pointer (scm_list_ref (vertices, scm_from_int (i)));
76       s3d_vertices[i] = video::S3DVertex (vertex->Pos,
77                                           vertex->Normal,
78                                           vertex->Color,
79                                           vertex->TCoords);
80     }
81
82   // Build index array
83   u32 index_count = scm_to_uint32 (scm_length (indices));
84   SCM flat_indices = scm_apply_0 (scm_eval_string (scm_from_utf8_string ("append")),
85                                   indices);
86   int flat_length = scm_to_int (scm_length (flat_indices));
87   u32 c_indices [flat_length];
88   for (int i = 0; i < flat_length; i++)
89     {
90       c_indices[i] = scm_to_uint32 (scm_list_ref (flat_indices, scm_from_int (i)));
91     }
92
93   // Draw vertices
94   video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
95   driver->drawVertexPrimitiveList (&s3d_vertices[0],
96                                    vertex_count,
97                                    &c_indices[0],
98                                    index_count,
99                                    scm_to_vertex_type (v_type),
100                                    scm_to_primitive_type (p_type),
101                                    video::EIT_32BIT);
102   return SCM_UNSPECIFIED;
103 }
104
105
106 SCM
107 IVideoDriver_endScene (SCM video_driver)
108 {
109   video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
110   return scm_from_bool (driver->endScene ());
111 }
112
113
114 SCM
115 IVideoDriver_getFPS (SCM video_driver)
116 {
117   video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
118   return scm_from_int32 (driver->getFPS ());
119 }
120
121
122 SCM
123 IVideoDriver_getTexture (SCM video_driver,
124                          SCM filename)
125 {
126   video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
127   video::ITexture* texture = driver->getTexture (scm_to_utf8_stringn (filename, NULL));
128   return scm_from_pointer ((void*) texture, NULL);
129 }
130
131
132 SCM
133 IVideoDriver_setMaterial (SCM video_driver,
134                           SCM material)
135 {
136   video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
137   driver->setMaterial (*((video::SMaterial*) scm_to_pointer (material)));
138   return SCM_UNSPECIFIED;
139 }
140
141
142 SCM
143 IVideoDriver_setTransform (SCM video_driver,
144                            SCM state,
145                            SCM mat)
146 {
147   video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
148   driver->setTransform (scm_to_transformation_state (state),
149                         scm_to_matrix4 (mat));
150   return SCM_UNSPECIFIED;
151 }
152
153
154 extern "C" {
155
156   void
157   init_video_driver (void)
158   {
159     DEFINE_GSUBR ("IVideoDriver_beginScene", 6, 0, 0, IVideoDriver_beginScene);
160     DEFINE_GSUBR ("IVideoDriver_drawVertexPrimitiveList", 5, 0, 1,
161                   IVideoDriver_drawVertexPrimitiveList);
162     DEFINE_GSUBR ("IVideoDriver_endScene", 1, 0, 0, IVideoDriver_endScene);
163     DEFINE_GSUBR ("IVideoDriver_getFPS", 1, 0, 0, IVideoDriver_getFPS);
164     DEFINE_GSUBR ("IVideoDriver_getTexture", 2, 0, 0, IVideoDriver_getTexture);
165     DEFINE_GSUBR ("IVideoDriver_setMaterial", 2, 0, 0, IVideoDriver_setMaterial);
166     DEFINE_GSUBR ("IVideoDriver_setTransform", 3, 0, 0, IVideoDriver_setTransform);
167   }
168
169 }
170
171 video::E_TRANSFORMATION_STATE
172 scm_to_transformation_state (SCM transformation_state)
173 {
174   char* state = scm_to_utf8_stringn (scm_symbol_to_string (transformation_state), NULL);
175   if (!strcmp (state, "view"))
176     {
177       return video::ETS_VIEW;
178     }
179   else if (!strcmp (state, "world"))
180     {
181       return video::ETS_WORLD;
182     }
183   else if (!strcmp (state, "projection"))
184     {
185       return video::ETS_PROJECTION;
186     }
187   else if (!strcmp (state, "texture0"))
188     {
189       return video::ETS_TEXTURE_0;
190     }
191   else if (!strcmp (state, "texture1"))
192     {
193       return video::ETS_TEXTURE_1;
194     }
195   else if (!strcmp (state, "texture2"))
196     {
197       return video::ETS_TEXTURE_2;
198     }
199   else if (!strcmp (state, "texture3"))
200     {
201       return video::ETS_TEXTURE_3;
202     }
203   else
204     {
205       scm_error (scm_arg_type_key, NULL, "Wrong transformation state: ~S",
206                  scm_list_1 (transformation_state), scm_list_1 (transformation_state));
207     }
208 }