]> git.jsancho.org Git - guile-irrlicht.git/blob - src/device.cpp
Use pointers directly from guile to C and viceversa
[guile-irrlicht.git] / src / device.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
25 #include "device.h"
26 #include "dimension2d.h"
27 #include "driver-types.h"
28 #include "event-receiver.h"
29 #include "gsubr.h"
30 #include "timer.h"
31 #include "wchar.h"
32 #include "wrapped.h"
33
34 using namespace irr;
35
36 extern "C" {
37
38   void
39   init_device (void)
40   {
41     init_device_type ();
42     DEFINE_GSUBR ("irr_createDevice", 7, 0, 0, irr_createDevice);
43     DEFINE_GSUBR ("irr_IrrlichtDevice_setWindowCaption", 2, 0, 0,
44                   irr_IrrlichtDevice_setWindowCaption);
45     DEFINE_GSUBR ("get-timer", 1, 0, 0, irr_getTimer);
46     DEFINE_GSUBR ("is-window-active?", 1, 0, 0, irr_isWindowActive);
47     DEFINE_GSUBR ("run", 1, 0, 0, irr_run);
48     DEFINE_GSUBR ("set-event-receiver!", 2, 0, 0, irr_setEventReceiver);
49     DEFINE_GSUBR ("set-resizable!", 2, 0, 0, irr_setResizable);
50     DEFINE_GSUBR ("yield", 1, 0, 0, irr_yield);
51   }
52
53   DEFINE_WRAPPED_TYPE (irr::IrrlichtDevice*, "device",
54                        init_device_type, device_p,
55                        wrap_device, unwrap_device);
56
57   SCM
58   irr_createDevice (SCM device_type,
59                     SCM window_size,
60                     SCM bits,
61                     SCM fullscreen,
62                     SCM stencilbuffer,
63                     SCM vsync,
64                     SCM receiver)
65   {
66     IrrlichtDevice* device =
67       createDevice (scm_to_driver_type (device_type),
68                     scm_to_dimension2d_u32 (window_size),
69                     scm_to_uint32 (bits),
70                     scm_to_bool (fullscreen),
71                     scm_to_bool (stencilbuffer),
72                     scm_to_bool (vsync),
73                     (IEventReceiver*)scm_to_pointer (receiver));
74     return scm_from_pointer ((void*)device, NULL);
75   }
76
77   SCM
78   irr_IrrlichtDevice_setWindowCaption (SCM device,
79                                        SCM text)
80   {
81     ((IrrlichtDevice*)scm_to_pointer (device))->
82       setWindowCaption (scm_to_wide_char_string (text));
83     return SCM_UNSPECIFIED;
84   }
85
86   SCM
87   irr_getTimer (SCM wrapped_device)
88   {
89     irr::IrrlichtDevice* device = unwrap_device (wrapped_device);
90     return wrap_timer (device->getTimer ());
91   }
92
93   SCM
94   irr_isWindowActive (SCM wrapped_device)
95   {
96     irr::IrrlichtDevice* device = unwrap_device (wrapped_device);
97     return scm_from_bool (device->isWindowActive ());
98   }
99
100   SCM
101   irr_run (SCM wrapped_device)
102   {
103     irr::IrrlichtDevice* device = unwrap_device (wrapped_device);
104     return scm_from_bool (device->run ());
105   }
106
107   SCM
108   irr_setEventReceiver (SCM wrapped_device,
109                         SCM receiver)
110   {
111     irr::IrrlichtDevice* device = unwrap_device (wrapped_device);
112     device->setEventReceiver (unwrap_event_receiver (receiver));
113     return SCM_UNSPECIFIED;
114   }
115
116   SCM
117   irr_setResizable (SCM wrapped_device,
118                     SCM resize)
119   {
120     irr::IrrlichtDevice* device = unwrap_device (wrapped_device);
121     device->setResizable (scm_to_bool (resize));
122     return SCM_UNSPECIFIED;
123   }
124
125   SCM
126   irr_yield (SCM wrapped_device)
127   {
128     irr::IrrlichtDevice* device = unwrap_device (wrapped_device);
129     device->yield ();
130     return SCM_UNSPECIFIED;
131   }
132
133 }