diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/irrlicht-1.8.1/examples/01.HelloWorld/main.cpp | 236 |
1 files changed, 236 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8.1/examples/01.HelloWorld/main.cpp b/libraries/irrlicht-1.8.1/examples/01.HelloWorld/main.cpp new file mode 100644 index 0000000..a53483c --- /dev/null +++ b/libraries/irrlicht-1.8.1/examples/01.HelloWorld/main.cpp | |||
@@ -0,0 +1,236 @@ | |||
1 | /** Example 001 HelloWorld | ||
2 | |||
3 | This Tutorial shows how to set up the IDE for using the Irrlicht Engine and how | ||
4 | to write a simple HelloWorld program with it. The program will show how to use | ||
5 | the basics of the VideoDriver, the GUIEnvironment, and the SceneManager. | ||
6 | Microsoft Visual Studio is used as an IDE, but you will also be able to | ||
7 | understand everything if you are using a different one or even another | ||
8 | operating system than windows. | ||
9 | |||
10 | You have to include the header file <irrlicht.h> in order to use the engine. The | ||
11 | header file can be found in the Irrlicht Engine SDK directory \c include. To let | ||
12 | the compiler find this header file, the directory where it is located has to be | ||
13 | specified. This is different for every IDE and compiler you use. Let's explain | ||
14 | shortly how to do this in Microsoft Visual Studio: | ||
15 | |||
16 | - If you use Version 6.0, select the Menu Extras -> Options. | ||
17 | Select the directories tab, and select the 'Include' Item in the combo box. | ||
18 | Add the \c include directory of the irrlicht engine folder to the list of | ||
19 | directories. Now the compiler will find the Irrlicht.h header file. We also | ||
20 | need the irrlicht.lib to be found, so stay in that dialog, select 'Libraries' | ||
21 | in the combo box and add the \c lib/VisualStudio directory. | ||
22 | \image html "vc6optionsdir.jpg" | ||
23 | \image latex "vc6optionsdir.jpg" | ||
24 | \image html "vc6include.jpg" | ||
25 | \image latex "vc6include.jpg" | ||
26 | |||
27 | - If your IDE is Visual Studio .NET, select Tools -> Options. | ||
28 | Select the projects entry and then select VC++ directories. Select 'show | ||
29 | directories for include files' in the combo box, and add the \c include | ||
30 | directory of the irrlicht engine folder to the list of directories. Now the | ||
31 | compiler will find the Irrlicht.h header file. We also need the irrlicht.lib | ||
32 | to be found, so stay in that dialog, select 'show directories for Library | ||
33 | files' and add the \c lib/VisualStudio directory. | ||
34 | \image html "vcnetinclude.jpg" | ||
35 | \image latex "vcnetinclude.jpg" | ||
36 | |||
37 | That's it. With your IDE set up like this, you will now be able to develop | ||
38 | applications with the Irrlicht Engine. | ||
39 | |||
40 | Lets start! | ||
41 | |||
42 | After we have set up the IDE, the compiler will know where to find the Irrlicht | ||
43 | Engine header files so we can include it now in our code. | ||
44 | */ | ||
45 | #include <irrlicht.h> | ||
46 | |||
47 | /* | ||
48 | In the Irrlicht Engine, everything can be found in the namespace 'irr'. So if | ||
49 | you want to use a class of the engine, you have to write irr:: before the name | ||
50 | of the class. For example to use the IrrlichtDevice write: irr::IrrlichtDevice. | ||
51 | To get rid of the irr:: in front of the name of every class, we tell the | ||
52 | compiler that we use that namespace from now on, and we will not have to write | ||
53 | irr:: anymore. | ||
54 | */ | ||
55 | using namespace irr; | ||
56 | |||
57 | /* | ||
58 | There are 5 sub namespaces in the Irrlicht Engine. Take a look at them, you can | ||
59 | read a detailed description of them in the documentation by clicking on the top | ||
60 | menu item 'Namespace List' or by using this link: | ||
61 | http://irrlicht.sourceforge.net/docu/namespaces.html | ||
62 | Like the irr namespace, we do not want these 5 sub namespaces now, to keep this | ||
63 | example simple. Hence, we tell the compiler again that we do not want always to | ||
64 | write their names. | ||
65 | */ | ||
66 | using namespace core; | ||
67 | using namespace scene; | ||
68 | using namespace video; | ||
69 | using namespace io; | ||
70 | using namespace gui; | ||
71 | |||
72 | /* | ||
73 | To be able to use the Irrlicht.DLL file, we need to link with the Irrlicht.lib. | ||
74 | We could set this option in the project settings, but to make it easy, we use a | ||
75 | pragma comment lib for VisualStudio. On Windows platforms, we have to get rid | ||
76 | of the console window, which pops up when starting a program with main(). This | ||
77 | is done by the second pragma. We could also use the WinMain method, though | ||
78 | losing platform independence then. | ||
79 | */ | ||
80 | #ifdef _IRR_WINDOWS_ | ||
81 | #pragma comment(lib, "Irrlicht.lib") | ||
82 | #pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup") | ||
83 | #endif | ||
84 | |||
85 | |||
86 | /* | ||
87 | This is the main method. We can now use main() on every platform. | ||
88 | */ | ||
89 | int main() | ||
90 | { | ||
91 | /* | ||
92 | The most important function of the engine is the createDevice() | ||
93 | function. The IrrlichtDevice is created by it, which is the root | ||
94 | object for doing anything with the engine. createDevice() has 7 | ||
95 | parameters: | ||
96 | |||
97 | - deviceType: Type of the device. This can currently be the Null-device, | ||
98 | one of the two software renderers, D3D8, D3D9, or OpenGL. In this | ||
99 | example we use EDT_SOFTWARE, but to try out, you might want to | ||
100 | change it to EDT_BURNINGSVIDEO, EDT_NULL, EDT_DIRECT3D8, | ||
101 | EDT_DIRECT3D9, or EDT_OPENGL. | ||
102 | |||
103 | - windowSize: Size of the Window or screen in FullScreenMode to be | ||
104 | created. In this example we use 640x480. | ||
105 | |||
106 | - bits: Amount of color bits per pixel. This should be 16 or 32. The | ||
107 | parameter is often ignored when running in windowed mode. | ||
108 | |||
109 | - fullscreen: Specifies if we want the device to run in fullscreen mode | ||
110 | or not. | ||
111 | |||
112 | - stencilbuffer: Specifies if we want to use the stencil buffer (for | ||
113 | drawing shadows). | ||
114 | |||
115 | - vsync: Specifies if we want to have vsync enabled, this is only useful | ||
116 | in fullscreen mode. | ||
117 | |||
118 | - eventReceiver: An object to receive events. We do not want to use this | ||
119 | parameter here, and set it to 0. | ||
120 | |||
121 | Always check the return value to cope with unsupported drivers, | ||
122 | dimensions, etc. | ||
123 | */ | ||
124 | IrrlichtDevice *device = | ||
125 | createDevice( video::EDT_SOFTWARE, dimension2d<u32>(640, 480), 16, | ||
126 | false, false, false, 0); | ||
127 | |||
128 | if (!device) | ||
129 | return 1; | ||
130 | |||
131 | /* | ||
132 | Set the caption of the window to some nice text. Note that there is an | ||
133 | 'L' in front of the string. The Irrlicht Engine uses wide character | ||
134 | strings when displaying text. | ||
135 | */ | ||
136 | device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo"); | ||
137 | |||
138 | /* | ||
139 | Get a pointer to the VideoDriver, the SceneManager and the graphical | ||
140 | user interface environment, so that we do not always have to write | ||
141 | device->getVideoDriver(), device->getSceneManager(), or | ||
142 | device->getGUIEnvironment(). | ||
143 | */ | ||
144 | IVideoDriver* driver = device->getVideoDriver(); | ||
145 | ISceneManager* smgr = device->getSceneManager(); | ||
146 | IGUIEnvironment* guienv = device->getGUIEnvironment(); | ||
147 | |||
148 | /* | ||
149 | We add a hello world label to the window, using the GUI environment. | ||
150 | The text is placed at the position (10,10) as top left corner and | ||
151 | (260,22) as lower right corner. | ||
152 | */ | ||
153 | guienv->addStaticText(L"Hello World! This is the Irrlicht Software renderer!", | ||
154 | rect<s32>(10,10,260,22), true); | ||
155 | |||
156 | /* | ||
157 | To show something interesting, we load a Quake 2 model and display it. | ||
158 | We only have to get the Mesh from the Scene Manager with getMesh() and add | ||
159 | a SceneNode to display the mesh with addAnimatedMeshSceneNode(). We | ||
160 | check the return value of getMesh() to become aware of loading problems | ||
161 | and other errors. | ||
162 | |||
163 | Instead of writing the filename sydney.md2, it would also be possible | ||
164 | to load a Maya object file (.obj), a complete Quake3 map (.bsp) or any | ||
165 | other supported file format. By the way, that cool Quake 2 model | ||
166 | called sydney was modelled by Brian Collins. | ||
167 | */ | ||
168 | IAnimatedMesh* mesh = smgr->getMesh("../../media/sydney.md2"); | ||
169 | if (!mesh) | ||
170 | { | ||
171 | device->drop(); | ||
172 | return 1; | ||
173 | } | ||
174 | IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh ); | ||
175 | |||
176 | /* | ||
177 | To let the mesh look a little bit nicer, we change its material. We | ||
178 | disable lighting because we do not have a dynamic light in here, and | ||
179 | the mesh would be totally black otherwise. Then we set the frame loop, | ||
180 | such that the predefined STAND animation is used. And last, we apply a | ||
181 | texture to the mesh. Without it the mesh would be drawn using only a | ||
182 | color. | ||
183 | */ | ||
184 | if (node) | ||
185 | { | ||
186 | node->setMaterialFlag(EMF_LIGHTING, false); | ||
187 | node->setMD2Animation(scene::EMAT_STAND); | ||
188 | node->setMaterialTexture( 0, driver->getTexture("../../media/sydney.bmp") ); | ||
189 | } | ||
190 | |||
191 | /* | ||
192 | To look at the mesh, we place a camera into 3d space at the position | ||
193 | (0, 30, -40). The camera looks from there to (0,5,0), which is | ||
194 | approximately the place where our md2 model is. | ||
195 | */ | ||
196 | smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0)); | ||
197 | |||
198 | /* | ||
199 | Ok, now we have set up the scene, lets draw everything: We run the | ||
200 | device in a while() loop, until the device does not want to run any | ||
201 | more. This would be when the user closes the window or presses ALT+F4 | ||
202 | (or whatever keycode closes a window). | ||
203 | */ | ||
204 | while(device->run()) | ||
205 | { | ||
206 | /* | ||
207 | Anything can be drawn between a beginScene() and an endScene() | ||
208 | call. The beginScene() call clears the screen with a color and | ||
209 | the depth buffer, if desired. Then we let the Scene Manager and | ||
210 | the GUI Environment draw their content. With the endScene() | ||
211 | call everything is presented on the screen. | ||
212 | */ | ||
213 | driver->beginScene(true, true, SColor(255,100,101,140)); | ||
214 | |||
215 | smgr->drawAll(); | ||
216 | guienv->drawAll(); | ||
217 | |||
218 | driver->endScene(); | ||
219 | } | ||
220 | |||
221 | /* | ||
222 | After we are done with the render loop, we have to delete the Irrlicht | ||
223 | Device created before with createDevice(). In the Irrlicht Engine, you | ||
224 | have to delete all objects you created with a method or function which | ||
225 | starts with 'create'. The object is simply deleted by calling ->drop(). | ||
226 | See the documentation at irr::IReferenceCounted::drop() for more | ||
227 | information. | ||
228 | */ | ||
229 | device->drop(); | ||
230 | |||
231 | return 0; | ||
232 | } | ||
233 | |||
234 | /* | ||
235 | That's it. Compile and run. | ||
236 | **/ | ||