1 /* guile-irrlicht --- GNU Guile bindings for Irrlicht Engine
3 Copyright (C) 2020 Javier Sancho <jsf@jsancho.org>
5 This file is part of guile-irrlicht.
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.
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.
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/>.
22 #include <irrlicht/irrlicht.h>
27 #include "gui-environment.h"
30 #include "primitive-types.h"
32 #include "scene-manager.h"
35 #include "video-driver.h"
41 init_video_driver (void)
43 init_video_driver_type ();
44 scm_c_define_gsubr ("begin-scene", 1, 0, 1, (scm_t_subr)irr_video_beginScene);
45 scm_c_define_gsubr ("draw-vertex-primitive-list", 3, 0, 1,
46 (scm_t_subr)irr_video_drawVertexPrimitiveList);
47 scm_c_define_gsubr ("end-scene", 1, 0, 0, (scm_t_subr)irr_video_endScene);
48 scm_c_define_gsubr ("get-fps", 1, 0, 0, (scm_t_subr)irr_video_getFPS);
49 scm_c_define_gsubr ("get-texture", 2, 0, 0, (scm_t_subr)irr_video_getTexture);
50 scm_c_define_gsubr ("get-video-driver", 1, 0, 0, (scm_t_subr)irr_getVideoDriver);
51 scm_c_define_gsubr ("set-transform!", 3, 0, 0, (scm_t_subr)irr_video_setTransform);
52 scm_c_export ("begin-scene", "draw-vertex-primitive-list", "end-scene", "get-fps",
53 "get-texture", "get-video-driver", "set-transform!", NULL);
56 DEFINE_WRAPPED_TYPE (irr::video::IVideoDriver*, "video-driver",
57 init_video_driver_type, video_driver_p,
58 wrap_video_driver, unwrap_video_driver);
61 irr_video_beginScene (SCM wrapped_video_driver,
64 SCM back_buffer = scm_from_bool(1);
65 SCM z_buffer = scm_from_bool(1);
66 SCM color = scm_list_4 (scm_from_uint32 (255),
70 SCM video_data = scm_from_bool(0);
71 SCM source_rect = scm_from_bool(0);
73 scm_c_bind_keyword_arguments ("begin-scene", rest, (scm_t_keyword_arguments_flags)0,
74 scm_from_utf8_keyword ("back-buffer"), &back_buffer,
75 scm_from_utf8_keyword ("z-buffer"), &z_buffer,
76 scm_from_utf8_keyword ("color"), &color,
77 scm_from_utf8_keyword ("video-data"), &video_data,
78 scm_from_utf8_keyword ("source-rect"), &source_rect,
81 irr::video::IVideoDriver* driver = unwrap_video_driver (wrapped_video_driver);
82 irr::core::rect<irr::s32>* sourceRectAddress = 0;
83 if (!scm_is_false (source_rect))
85 irr::core::rect<irr::s32> sourceRect = scm_to_rect_s32 (source_rect);
86 sourceRectAddress = &sourceRect;
88 return scm_from_bool (driver->beginScene (scm_to_bool (back_buffer),
89 scm_to_bool (z_buffer),
91 irr::video::SExposedVideoData (),
96 irr_video_drawVertexPrimitiveList (SCM wrapped_video_driver,
101 SCM v_type = scm_from_utf8_symbol ("standard");
102 SCM p_type = scm_from_utf8_symbol ("triangles");
104 scm_c_bind_keyword_arguments ("draw-vertex-primitive-list", rest, (scm_t_keyword_arguments_flags)0,
105 scm_from_utf8_keyword ("vertex-type"), &v_type,
106 scm_from_utf8_keyword ("primitive-type"), &p_type,
109 // Build vertex array
110 irr::u32 vertex_count = scm_to_uint32 (scm_length (vertices));
111 irr::video::S3DVertex s3d_vertices [vertex_count];
112 for (int i = 0; i < vertex_count; i++)
114 irr::video::S3DVertex* vertex = unwrap_vertex3d (scm_list_ref (vertices, scm_from_int (i)));
115 s3d_vertices[i] = irr::video::S3DVertex (vertex->Pos,
122 irr::u32 index_count = scm_to_uint32 (scm_length (indices));
123 SCM flat_indices = scm_apply_0 (scm_eval_string (scm_from_utf8_string ("append")),
125 int flat_length = scm_to_int (scm_length (flat_indices));
126 irr::u32 c_indices [flat_length];
127 for (int i = 0; i < flat_length; i++)
129 c_indices[i] = scm_to_uint32 (scm_list_ref (flat_indices, scm_from_int (i)));
133 irr::video::IVideoDriver* driver = unwrap_video_driver (wrapped_video_driver);
134 driver->drawVertexPrimitiveList (&s3d_vertices[0],
138 scm_to_vertex_type (v_type),
139 scm_to_primitive_type (p_type),
140 irr::video::EIT_32BIT);
141 return SCM_UNSPECIFIED;
145 irr_video_endScene (SCM wrapped_video_driver)
147 irr::video::IVideoDriver* driver = unwrap_video_driver (wrapped_video_driver);
148 return scm_from_bool (driver->endScene ());
152 irr_video_getFPS (SCM wrapped_video_driver)
154 irr::video::IVideoDriver* driver = unwrap_video_driver (wrapped_video_driver);
155 return scm_from_int32 (driver->getFPS ());
159 irr_video_getTexture (SCM wrapped_video_driver,
162 irr::video::IVideoDriver* driver = unwrap_video_driver (wrapped_video_driver);
163 irr::video::ITexture* texture = driver->getTexture (scm_to_utf8_stringn (filename, NULL));
164 return wrap_texture (texture);
168 irr_video_setMaterial (SCM wrapped_video_driver,
171 irr::video::IVideoDriver* driver = unwrap_video_driver (wrapped_video_driver);
172 driver->setMaterial (*(unwrap_material (material)));
173 return SCM_UNSPECIFIED;
177 irr_video_setTransform (SCM wrapped_video_driver,
181 irr::video::IVideoDriver* driver = unwrap_video_driver (wrapped_video_driver);
182 driver->setTransform (scm_to_transformation_state (state),
183 scm_to_matrix4 (mat));
184 return SCM_UNSPECIFIED;
188 irr_getVideoDriver (SCM wrapped_obj)
190 irr::video::IVideoDriver* driver;
191 if (device_p (wrapped_obj))
193 driver = unwrap_device (wrapped_obj)->getVideoDriver ();
197 scm_error (scm_arg_type_key, NULL, "Cannot get video driver from object: ~S",
198 scm_list_1 (wrapped_obj), scm_list_1 (wrapped_obj));
200 return wrap_video_driver (driver);
203 irr::video::E_TRANSFORMATION_STATE
204 scm_to_transformation_state (SCM transformation_state)
206 char* state = scm_to_utf8_stringn (scm_symbol_to_string (transformation_state), NULL);
207 if (!strcmp (state, "view"))
209 return irr::video::ETS_VIEW;
211 else if (!strcmp (state, "world"))
213 return irr::video::ETS_WORLD;
215 else if (!strcmp (state, "projection"))
217 return irr::video::ETS_PROJECTION;
219 else if (!strcmp (state, "texture0"))
221 return irr::video::ETS_TEXTURE_0;
223 else if (!strcmp (state, "texture1"))
225 return irr::video::ETS_TEXTURE_1;
227 else if (!strcmp (state, "texture2"))
229 return irr::video::ETS_TEXTURE_2;
231 else if (!strcmp (state, "texture3"))
233 return irr::video::ETS_TEXTURE_3;
237 scm_error (scm_arg_type_key, NULL, "Wrong transformation state: ~S",
238 scm_list_1 (transformation_state), scm_list_1 (transformation_state));