]> git.jsancho.org Git - guile-irrlicht.git/blob - src/video-driver.cpp
video-driver
[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 "video-driver.h"
30
31
32 using namespace irr;
33
34
35 SCM
36 video_IVideoDriver_beginScene (SCM video_driver,
37                                SCM back_buffer,
38                                SCM z_buffer,
39                                SCM color,
40                                SCM video_data,
41                                SCM source_rect)
42 {
43   video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
44
45   // Source rect
46   core::rect<s32>* sourceRectAddress = 0;
47   if (!scm_is_false (source_rect))
48     {
49       core::rect<s32> sourceRect = scm_to_rect_s32 (source_rect);
50       sourceRectAddress = &sourceRect;
51     }
52
53   return scm_from_bool (driver->beginScene (scm_to_bool (back_buffer),
54                                             scm_to_bool (z_buffer),
55                                             scm_to_color (color),
56                                             video::SExposedVideoData (),
57                                             sourceRectAddress));
58 }
59
60
61 SCM
62 video_IVideoDriver_drawVertexPrimitiveList (SCM video_driver,
63                                             SCM vertices,
64                                             SCM indices,
65                                             SCM v_type,
66                                             SCM p_type)
67 {
68   // Build vertex array
69   u32 vertex_count = scm_to_uint32 (scm_length (vertices));
70   video::S3DVertex s3d_vertices [vertex_count];
71   for (int i = 0; i < vertex_count; i++)
72     {
73       video::S3DVertex* vertex =
74         (video::S3DVertex*) scm_to_pointer (scm_list_ref (vertices, scm_from_int (i)));
75       s3d_vertices[i] = video::S3DVertex (vertex->Pos,
76                                           vertex->Normal,
77                                           vertex->Color,
78                                           vertex->TCoords);
79     }
80
81   // Build index array
82   u32 index_count = scm_to_uint32 (scm_length (indices));
83   SCM flat_indices = scm_apply_0 (scm_eval_string (scm_from_utf8_string ("append")),
84                                   indices);
85   int flat_length = scm_to_int (scm_length (flat_indices));
86   u32 c_indices [flat_length];
87   for (int i = 0; i < flat_length; i++)
88     {
89       c_indices[i] = scm_to_uint32 (scm_list_ref (flat_indices, scm_from_int (i)));
90     }
91
92   // Draw vertices
93   video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
94   driver->drawVertexPrimitiveList (&s3d_vertices[0],
95                                    vertex_count,
96                                    &c_indices[0],
97                                    index_count,
98                                    scm_to_vertex_type (v_type),
99                                    scm_to_primitive_type (p_type),
100                                    video::EIT_32BIT);
101   return SCM_UNSPECIFIED;
102 }
103
104
105 SCM
106 video_IVideoDriver_endScene (SCM video_driver)
107 {
108   video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
109   return scm_from_bool (driver->endScene ());
110 }
111
112
113 SCM
114 video_IVideoDriver_getFPS (SCM video_driver)
115 {
116   video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
117   return scm_from_int32 (driver->getFPS ());
118 }
119
120
121 SCM
122 video_IVideoDriver_getTexture (SCM video_driver,
123                                SCM filename)
124 {
125   video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
126   video::ITexture* texture = driver->getTexture (scm_to_utf8_stringn (filename, NULL));
127   return scm_from_pointer ((void*) texture, NULL);
128 }
129
130
131 SCM
132 video_IVideoDriver_setMaterial (SCM video_driver,
133                                 SCM material)
134 {
135   video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
136   driver->setMaterial (*((video::SMaterial*) scm_to_pointer (material)));
137   return SCM_UNSPECIFIED;
138 }
139
140
141 SCM
142 video_IVideoDriver_setTransform (SCM video_driver,
143                                  SCM state,
144                                  SCM mat)
145 {
146   video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
147   driver->setTransform (scm_to_transformation_state (state),
148                         scm_to_matrix4 (mat));
149   return SCM_UNSPECIFIED;
150 }
151
152
153 extern "C" {
154
155   void
156   init_video_driver (void)
157   {
158     DEFINE_GSUBR ("video_IVideoDriver_beginScene", 6, 0, 0, video_IVideoDriver_beginScene);
159     DEFINE_GSUBR ("video_IVideoDriver_drawVertexPrimitiveList", 5, 0, 1,
160                   video_IVideoDriver_drawVertexPrimitiveList);
161     DEFINE_GSUBR ("video_IVideoDriver_endScene", 1, 0, 0, video_IVideoDriver_endScene);
162     DEFINE_GSUBR ("video_IVideoDriver_getFPS", 1, 0, 0, video_IVideoDriver_getFPS);
163     DEFINE_GSUBR ("video_IVideoDriver_getTexture", 2, 0, 0, video_IVideoDriver_getTexture);
164     DEFINE_GSUBR ("video_IVideoDriver_setMaterial", 2, 0, 0, video_IVideoDriver_setMaterial);
165     DEFINE_GSUBR ("video_IVideoDriver_setTransform", 3, 0, 0, video_IVideoDriver_setTransform);
166   }
167
168 }
169
170 video::E_TRANSFORMATION_STATE
171 scm_to_transformation_state (SCM transformation_state)
172 {
173   char* state = scm_to_utf8_stringn (scm_symbol_to_string (transformation_state), NULL);
174   if (!strcmp (state, "view"))
175     {
176       return video::ETS_VIEW;
177     }
178   else if (!strcmp (state, "world"))
179     {
180       return video::ETS_WORLD;
181     }
182   else if (!strcmp (state, "projection"))
183     {
184       return video::ETS_PROJECTION;
185     }
186   else if (!strcmp (state, "texture0"))
187     {
188       return video::ETS_TEXTURE_0;
189     }
190   else if (!strcmp (state, "texture1"))
191     {
192       return video::ETS_TEXTURE_1;
193     }
194   else if (!strcmp (state, "texture2"))
195     {
196       return video::ETS_TEXTURE_2;
197     }
198   else if (!strcmp (state, "texture3"))
199     {
200       return video::ETS_TEXTURE_3;
201     }
202   else
203     {
204       scm_error (scm_arg_type_key, NULL, "Wrong transformation state: ~S",
205                  scm_list_1 (transformation_state), scm_list_1 (transformation_state));
206     }
207 }