]> git.jsancho.org Git - c-irrlicht.git/blob - examples/03.CustomSceneNode.c
Use structs with casting, without classes replication
[c-irrlicht.git] / examples / 03.CustomSceneNode.c
1 /* c-irrlicht --- C bindings for Irrlicht Engine
2
3    Copyright (C) 2019 Javier Sancho <jsf@jsancho.org>
4
5    This file is part of c-irrlicht.
6
7    c-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    c-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 <cirrlicht/cirrlicht.h>
23 #include <stddef.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 int main()
28 {
29   // ask user for driver
30   irr_video_E_DRIVER_TYPE driverType;
31
32   printf("Please select the driver you want for this example:\n"        \
33          " (a) OpenGL 1.5\n (b) Direct3D 9.0c\n (c) Direct3D 8.1\n"     \
34          " (d) Burning's Software Renderer\n (e) Software Renderer\n"   \
35          " (f) NullDevice\n (otherKey) exit\n\n");
36
37   char i = getchar();
38
39   switch(i)
40     {
41     case 'a': driverType = irr_video_EDT_OPENGL; break;
42     case 'b': driverType = irr_video_EDT_DIRECT3D9; break;
43     case 'c': driverType = irr_video_EDT_DIRECT3D8; break;
44     case 'd': driverType = irr_video_EDT_BURNINGSVIDEO; break;
45     case 'e': driverType = irr_video_EDT_SOFTWARE; break;
46     case 'f': driverType = irr_video_EDT_NULL; break;
47     default: return 1;
48     }
49
50   // create device and exit if creation failed
51   irr_core_dimension2d_u32 windowSize = {640, 480};
52   irr_IrrlichtDevice *device =
53     irr_createDevice(driverType, &windowSize, 16, false, false, false);
54
55   if (!device)
56     {
57       return 1;  // could not create selected driver
58     }
59
60   // create engine and camera
61   irr_setWindowCaption(device, "Custom Scene Node - Irrlicht Engine Demo");
62
63   irr_video_IVideoDriver* driver = irr_getVideoDriver(device);
64   irr_scene_ISceneManager* smgr = irr_getSceneManager(device);
65
66   irr_core_vector3df position = {0, -40, 0};
67   irr_core_vector3df lookat = {0, 0, 0};
68   irr_scene_addCameraSceneNode(smgr, NULL, &position, &lookat, -1, true);
69
70   // create our custom scene node
71   irr_scene_ISceneNode* myNode;
72   irr_core_aabbox3d_f32 box;
73   irr_video_S3DVertex vertices[] =
74     {
75      // pos, normal, color, tcoords
76      { {0, 0, 10}, {1, 1, 0}, MAKE_COLOR(255, 0, 255, 255), {0, 1} },
77      { {10, 0, -10}, {1, 0, 0}, MAKE_COLOR(255, 255, 0, 255), {1, 1} },
78      { {0, 20, 0}, {0, 1, 1}, MAKE_COLOR(255, 255, 255, 0), {1, 0} },
79      { {-10, 0, -10}, {0, 0, 1}, MAKE_COLOR(255, 0, 255, 0), {0, 0} }
80     };
81   irr_video_SMaterial material = S_MATERIAL_DEFAULT;
82   material.wireframe = false;
83   material.lighting = false;
84
85   irr_core_aabbox3d_reset(&box, &vertices[0].pos);
86   for (int i=1; i<4; i++)
87     {
88       irr_core_aabbox3d_addInternalPoint(&box, &vertices[i].pos);
89     }
90
91   void customRender()
92   {
93     uint16_t indices[] = {0, 2, 3,    2, 1, 3,    1, 0, 3,    2, 0, 1};
94     irr_video_setMaterial(driver, &material);
95     irr_video_setTransform(driver,
96                            irr_video_ETS_WORLD,
97                            irr_scene_getAbsoluteTransformation(myNode));
98     irr_video_drawVertexPrimitiveList(driver,
99                                       &vertices[0], 4,
100                                       &indices[0], 4,
101                                       irr_video_EVT_STANDARD,
102                                       irr_scene_EPT_TRIANGLES,
103                                       irr_video_EIT_16BIT);
104   }
105
106   irr_core_aabbox3d_f32* customGetBoundingBox()
107   {
108     return &box;
109   }
110
111   uint32_t customGetMaterialCount()
112   {
113     return 1;
114   }
115
116   irr_video_SMaterial* customGetMaterial(unsigned int i)
117   {
118     return &material;
119   }
120
121   myNode = irr_scene_addCustomSceneNode(smgr, NULL, 666, NULL, NULL, NULL,
122                                         customRender, customGetBoundingBox,
123                                         customGetMaterialCount, customGetMaterial);
124
125   // add rotation
126   irr_core_vector3df rotation = {0.8, 0, 0.8};
127   irr_scene_ISceneNodeAnimator* anim =
128     irr_scene_createRotationAnimator(smgr, &rotation);
129
130   if (anim)
131     {
132       irr_scene_addAnimator(myNode, anim);
133       irr_drop(anim);
134       anim = NULL;
135     }
136
137   // loop
138   int frames = 0;
139   irr_video_SColor bgcolor = MAKE_COLOR(0, 100, 100, 100);
140   while (irr_run(device))
141     {
142       irr_video_beginScene(driver, true, true, &bgcolor, NULL, NULL);
143       irr_scene_drawAll(smgr);
144       irr_video_endScene(driver);
145
146       frames++;
147       if (frames == 100)
148         {
149           int fps = irr_video_getFPS(driver);
150           const char *driverName = irr_video_getName(driver);
151           size_t capsize =
152             snprintf(NULL, 0, "Irrlicht Engine [%s] FPS:%d",
153                      driverName, fps);
154           char *caption = (char*)malloc(capsize + 1);
155           snprintf(caption, capsize + 1,
156                    "Irrlicht Engine [%s] FPS:%d",
157                    driverName, fps);
158
159           irr_setWindowCaption(device, caption);
160           frames = 0;
161         }
162     }
163
164   irr_drop(device);
165   return 0;
166 }