Irrlicht engine manual example (1): Hello, world!

Source: Internet
Author: User

This example describes how to set up your ide for the irrlicht engine and how to use it to write a simple helloworld program. The program code shows how to use the most basic things in the engine: videodriver, guienviroment, and scenemanager ).
The running result of the sample program is as follows:

 

  Configure ide
To use the engine, you must include the header file <irrlicht. h> (see the include directory in the engine SDK ). To let the compiler find our header file, you must specify the directory where the header file is located. In this regard, the first IDE and compiler are different. We use Microsoft Visual stadio C ++ 6.0 and. Net:
· If you are using version 6.0, open the menu (note ①) Tools-> options, select the Directory tab, and select the include option in the combo box, add the inlclude directory of the irrlicht engine to the directory list. Now the compiler will be able to find the header file irrlicht. h. We also need to add the location of irrlicht. lib to the list, so click the libraries tab and add the directory/lib/visualstudio.

· If your ide is Visual Studio. NET, open the menu tools> options. Open the projects folder on the left, select the VC ++ directory, and select 'show directories for include files' In the combo box ', add the/include directory of the engine to the List Directory so that the compiler can find the header file irrlicht. h. We also need the compiler to find irrlicht. Lib, so select 'show directories for library files' to add the/lib/visualstudio directory.
 

  Start now:
After the IDE is configured, the compiler can find the header file of the irrlicht engine, so we can include the header file in the code.

# Include <irrlicht. h>

Everything in the irrlicht engine is placed in the namespace IRR. If you want to use the class in the engine, add IRR: before the class name ::. For example, if you use irrlichtdevice, write IRR: irrlichtdevice. To avoid adding IRR: before each class name, tell the compiler The namespace we want to use.
[Code] Using namespace IRR; The [/Code] Engine has five sub-namespaces. You can click 'namespace List' in the top menu item to view the detailed descriptions of them in the manual. To keep the sample code concise, we don't want to specify a namespace every time, So we write this:

Using namespace core;
Using namespace scene;
Using namespace video;
Using namespace IO;
Using namespace GUI;

To use the irrlicht. dll file, we also need to use irrlicht. lib to connect. This can be set in the project, but on the other hand, we can simply use the paragma statement:

# Pragma comment (Lib, "irrlicht. lib ")

Now it's the turn of the main function: to make the example concise, we use int main () (note ②) because it can be used on any platform. However, on the Windows platform, we can also use the winmain function directly, if you want to get rid of the console window that pops up when the program starts with the main () function.

Int main ()
{

The most important function in the engine is createdevice (). You can use it to create an irrlicht device, which is the "root object" for the engine to do anything ". The createdevice () function has seven parameters:
· Devicetype: device type, which can be null ). Soft devices include direct3d8, Direct3D9, or OpenGL. In this example, we use edt_software, but for the purpose of trying, you may want to replace it with edt_null, edt_direct3d8, edt_direct3d9 or edt_opengl? Good luck!
· Windowsize: the size of the window to be created, or full screen mode. In this example, we use the 512x384 resolution.
· Bits: the number of bits per pixel in full screen mode. It should be 16 or 32. This parameter is ignored when running in window mode.
· Fullscreen: Specifies whether to enable the device to run in full screen mode.
· Stencilbuffer: Specifies whether to use the template cache in the shadow.
· Vsync: Specifies whether to allow field scan synchronization. It is only available in full screen mode.
· Eventreceiver: an object that accepts events. We don't need it here, so we set it to 0.

Irrlichtdevice * Device = createdevice (edt_software, dimension2d <s32> (512,384), 16, false, 0 );

Now let's add some content in the window title to make it look better. Note that there is a 'l' before the string: The irrlicht engine uses a wide character to display text.

Device-> setwindowcaption (L "Hello world! -Irrlicht engine Demo ");

Well, then we will save the pointer pointing to the video drive, scenario manager, and GUI environment so as not to always write such code: Device-> getvideodriver (), device-> getscenemanager (), and device-> getguienvironment ().

Videodriver * driver = device-> getvideodriver (); iscenemanager * smgr = device-> getscenemanager (); iguienvironment * guienv = device-> getguienvironment ();

We use a GUI environment to add a helloworld tag to the window. The text is placed in a rectangle from the upper left corner () to the lower right corner.

Guienv-> addstatictext (L "Hello world! This is the irrlicht software engine! ",
Rect <int> (, 22), true );

Let's make our example more interesting-load a quake2 model and display it, how? In fact, you only need to use getmesh () to get a grid from the scenario manager, and then use addanimatedmeshscenenode () to add a scene node for display on the screen. However, it is not limited to loading the quake2 file (. md2). Others, such as a Maya object file (. OBJ), a complete quake3 map or milshape file, are also acceptable!
By the way, the cool model called sysydney. md2 was developed by Brian Collins.

Ianimatedmesh * mesh = smgr-> getmesh (".../../Media/sysydney. md2"); ianimatedmeshscenenode * node = smgr-> addanimatedmeshscenenode (mesh );

In order to make the mesh model (note ③) more attractive, let's make a slight change to the material: no light is allowed, because there is no dynamic light source, and the model is completely black. Then, set the animation's cycle frame from 0 to 310. Finally, apply the texture to the mesh-otherwise the engine will use solid color for painting.

If (node)
{
Node-> setmaterialflag (emf_lighting, false );
Node-> setframeloop (0,310 );
Node-> setmaterialtexture (0, driver-> gettexture (".../../Media/sydney.bmp "));
}

In order to see the model, we place a camera in the position (0, 10,-40) of the 3D space, and the camera looks in the direction (, 0) from here.

Smgr-> addcamerascenenode (0, vector3df (0, 30,-40), vector3df (0, 5, 0 ));

Okay. Now that the scenario has been set up, start painting! We run the device in a while () loop until it stops, that is, when the user closes the window or presses Alt + F4 (Windows.

While (device-> Run ())
{

All plotting must be placed between the call of beginscene () and endscene. Beginscene () uses a certain color to clear the screen. If you want to use a deep cache, you can also. Then let the scenario manager and GUI environment draw their content. When endscene () is called, all the items just drawn are displayed on the screen.

Driver-> beginscene (true, true, scolor (255,100,101,140 ));
Smgr-> drawall ();
Guienv-> drawall ();
Driver-> endscene ();
}

After completing the above loop, we should delete the previously created irrlicht device with createdevice. In the irrlicht engine, you should delete all objects created using methods or functions with the 'create' prefix. You can use-> drop () to delete such objects. For more details, see the manual.

Device-> drop (); Return 0;
}

That's it! Compile and run.

  Possible errors or problems
  Visual Studio
When compiling examples in the manual, you may encounter the following errors:
Fatal error c1083: cannot open include file: 'irrlicht. H': no such file or directory
  Solution: The include directory may be inappropriate when you configure Visual Studio options. Refer to the section at the beginning of this example.
Link: lnk6004: helloworld.exe not found or not built by the last incremental link; wide Ming full Link
Link: Fatal error lnk1104: cannot open file "irrlicht. lib"
Error: executing link.exe
  Solution: The Library directory may be set incorrectly. Please refer to the section at the beginning of this example.

  Issues unrelated to the Compiler
This application has failed to start because irrlicht. dll was not found. Re-installing the application may fix this problem
  Solution: You may have forgotten to copy the irrlicht. dll file from the irrlicht/bin/visualstudio directory to the directory where your project file is located.
If the example in the manual is successfully compiled and run, but the following error is prompted on the console: cocould not load mesh, because file cocould not be opened ..: ../Media/sysydney. md2
Or:
Cocould not open file of texture: stones.jpg
Cocould not load texture: stones.jpg

  Solution: The files listed in error information cannot be found. Make sure that the specified directory in the main. cpp file exists and the missing file is also in this directory.

――――――――――
Note ①: the original version is extras. in the German version of vc6, the tool menu name is extras?
Note ②: the original article is void main (). It should be the author's mistake.
Note ③: original mesh (mesh). vertex and skeleton information of the model, indicating model resources. See [translation] irrlicht engine introduction.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.