]> git.jsancho.org Git - guile-irrlicht.git/blob - src/IrrlichtDevice.cpp
get-video-driver
[guile-irrlicht.git] / src / IrrlichtDevice.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 <wchar.h>
25
26 #include "dimension2d.h"
27 #include "EDriverTypes.h"
28 #include "IrrlichtDevice.h"
29 #include "IVideoDriver.h"
30
31 extern "C" {
32
33   void
34   init_device (void)
35   {
36     init_device_type ();
37     scm_c_define_gsubr ("create-device", 7, 0, 0, (scm_t_subr)irr_createDevice);
38     scm_c_define_gsubr ("get-video-driver", 1, 0, 0, (scm_t_subr)irr_getVideoDriver);
39     scm_c_define_gsubr ("set-window-caption!", 2, 0, 0, (scm_t_subr)irr_setWindowCaption);
40   }
41
42   static SCM device_type;
43
44   void
45   init_device_type (void)
46   {
47     SCM name, slots;
48     scm_t_struct_finalize finalizer;
49
50     name = scm_from_utf8_symbol ("device");
51     slots = scm_list_1 (scm_from_utf8_symbol ("data"));
52     finalizer = NULL;
53
54     device_type =
55       scm_make_foreign_object_type (name, slots, finalizer);
56   }
57
58   SCM
59   wrap_device (irr::IrrlichtDevice* device)
60   {
61     return scm_make_foreign_object_1 (device_type, device);
62   }
63
64   irr::IrrlichtDevice*
65   unwrap_device (SCM device_obj)
66   {
67     scm_assert_foreign_object_type (device_type, device_obj);
68     return (irr::IrrlichtDevice*)scm_foreign_object_ref (device_obj, 0);
69   }
70
71   SCM
72   irr_createDevice (SCM deviceType,
73                     SCM windowSize,
74                     SCM bits,
75                     SCM fullscreen,
76                     SCM stencilbuffer,
77                     SCM vsync,
78                     SCM receiver)
79   {
80     irr::IrrlichtDevice* device =
81       irr::createDevice (scm_to_driver_type (deviceType),
82                          scm_to_dimension2d_u32 (windowSize),
83                          scm_to_uint32 (bits),
84                          scm_to_bool (fullscreen),
85                          scm_to_bool (stencilbuffer),
86                          scm_to_bool (vsync));
87     return wrap_device (device);
88   }
89
90   SCM
91   irr_getVideoDriver (SCM device_obj)
92   {
93     irr::IrrlichtDevice* device = unwrap_device (device_obj);
94     irr::video::IVideoDriver* driver = device->getVideoDriver();
95     return wrap_video_driver (driver);
96   }
97
98   SCM
99   irr_setWindowCaption (SCM device_obj,
100                         SCM text)
101   {
102     irr::IrrlichtDevice* device = unwrap_device (device_obj);
103     char* ctext;
104     wchar_t* wtext;
105
106     ctext = scm_to_utf8_stringn (text, NULL);
107     wtext = (wchar_t*)malloc ((strlen (ctext) + 1) * sizeof (wchar_t));
108     mbstowcs (wtext, ctext, strlen (ctext) + 1);
109
110     device->setWindowCaption (wtext);
111     return SCM_UNSPECIFIED;
112   }
113
114 }