Irrlicht learning memorandum 2Quake3Map

Source: Internet
Author: User
Tags add numbers
2Quake3Map official code ($ sdk) examples2.Quake3Map This example demonstrates loading a Quake3 map file to the irr engine and creating a user-controlled camera. It is as simple as HelloWorld, but it should be simpler. It is less GUI than the previous example, and the method for compressing files is added.

2Quake3Map official code ($ sdk) \ examples \ 02. Quake3Map This example demonstrates loading a Quake3 map file to the irr engine and creating a user-controlled camera. It is as simple as HelloWorld, but it should be simpler. It is less GUI than the previous example, and the method for compressing files is added.

2Quake3Map

Official code ($ sdk) \ examples \ 02. Quake3Map


This example shows how to load a Quake3 map file to the irr engine and create a user-controlled camera. It is as simple as HelloWorld, but it should be simpler. It is less GUI than the previous example, and the method for compressing files is added. The remaining content is existing in the previous example. As for the selection menu of multiple rendering modes, the frame rate statistics code should be completely unrelated to irr. This is the content that has been practiced in any programming language.

The irr namespace contains five sub-namespaces: core, scene, video, io, and gui. In this example, the engine author does not use usingnamespace xxx to declare the sub-namespace to familiarize the learner with the functions of each space.

The following code selects the irr driver in the example. On the command line interface, a simple menu selection interface is provided to determine the selected driver type through the swicht statement.

Video: E_DRIVER_TYPEdriverType;

Printf ("Pleaseselect the driver you want for this example: \ n "\

"(A) OpenGL 1.5 \ n (B) Direct3D 9.0c \ n (c) Direct3D 8.1 \ n "\

"(D) Burning's Software Renderer \ n (e) Software Renderer \ n "\

"(F) NullDevice \ n (otherKey) exit \ n ");

Chari;

Std: cin> I;

Switch (I)

{

Case 'A': driverType = video: EDT_OPENGL; break;

Case 'B': driverType = video: EDT_DIRECT3D9; break;

Case 'C': driverType = video: EDT_DIRECT3D8; break;

Case 'D': driverType = video: EDT_BURNINGSVIDEO; break;

Case 'E': driverType = video: EDT_SOFTWARE; break;

Case 'F': driverType = video: EDT_NULL; break;

Default: return 1;

}

This section of code often appears in irr examples. In other examples, it is encapsulated into the driverChoiceConsole function interface, with the header file driverChoice. h.

The following code uses the compressed file. The method is simple. You can obtain the irr file system by using the device pointer of irr.

The addFileArchive interface in the irr file system provides the ability to access zip compressed files. The detailed description of this interface is displayed in the header file.

Device-> getFileSystem ()-> addFileArchive ("../media/map-20kdm2.pk3 ");

After addFileArchive is called, The unzipped file is located in the same directory as the program file in the irr file system, but not in the operating system file system. The addFileArchive interface has parameters to specify whether the extracted file directory is valid. If the flag is set to invalid, the file directory in the compressed file will be ignored, and all the extracted files will be in the same directory.

Scene: IAnimatedMesh * mesh = smgr-> getMesh ("20kdm2. bsp ");

This line of code has already appeared in HelloWorld. The only difference is the file name in getMesh. The 20kdm2. bsp file is extracted from the map-20kdm2.pk3 file. Because the extracted file is located in the same directory as the program file, you only need to provide the file name to access the file using the relative path.

Scene: ISceneNode * node = 0;

If (mesh)

Node = smgr-> addOctreeSceneNode (mesh-> getMesh (0), 0,-1, 1024 );

The method for adding scene nodes here is similar to that for HelloWorld. The difference is that we used addAnimatedMeshSceneNode to add animation grid scene nodes. This time we used addOctreeSceneNode to add octotree scene nodes. You can see from the node name that the animated mesh node means that the node is moving. In this octree scenario, the node model is optimized using the octree, which is only valid for the static model. Generally, static models such as maps and terrain are very large in size, and the number of vertices used is extremely large. At the same time, all vertex data is transmitted to the video card for processing, which is a waste of Bandwidth Resources, at the same time, the video card also performs a large number of operations on the undisplayed part, which is a waste of hardware resources. After the optimization of the tree, you can crop the tree to obtain the vertex data of each display area. Only this part of data is transmitted to the video card, which can significantly improve the display speed. Irr has implemented all these optimization functions through the eight-Tree node.

If (node)

Node-> setPosition (core: vector3df (-1300,-144,-1249 ));

Set the location coordinates of the scene node. It also appears in HelloWorld.

Smgr-> addCameraSceneNodeFPS ();

This line looks familiar, that is, its tail is more FPS than it once saw. People familiar with games know what FPS is. Therefore, this line adds an FPS visual camera. How to control the camera movement when playing an FPS game, the same is true here. You only need to addCameraSceneNodeFPS to solve the FPS camera and the moving method.

Device-> getCursorControl ()-> setVisible (false );

In this sentence, you can use the word overlord to find out the meaning of each word. The irr device obtains the cursor control and is set to not display the cursor. In FPS games, you do not need to use the mouse cursor. Here, you can close the mouse cursor, which is nothing strange.

IntlastFPS =-1;

While (device-> run ())

{

If (device-> isWindowActive ())

{

Driver-> beginScene (true, true, video: SColor (255,200,200,200 ));

Smgr-> drawAll ();

Driver-> endScene ();

Intfps = driver-> getFPS ();

If (lastFPS! = Fps)

{

Core: stringwstr = L "Irrlicht Engine-Quake 3 Map example [";

Str + = driver-> getName ();

Str + = "] FPS :";

Str + = fps;

Device-> setWindowCaption (str. c_str ());

LastFPS = fps;

}

}

Else

Device-> yield ();

}

This code is still very similar to the one we saw, that is, there are several more lines in the middle. Taking a closer look at the extra rows, we have designed a counter to record the display frame rate. The getFPS () interface provided by the irr device can directly obtain the current display frame rate. Therefore, you do not need to use the timer and The accumulators to calculate the frame rate in a loop. This is very convenient. Core: stringw string type. If you see the namespace above, you should know that this is irr's own string type. Why not use STL strings? I also find it strange that many commonly used generic types are still not STL but irr. However, after reading their code, I feel that they are more concise and less commonly used than STL, and some functions not available in STL are also added. It may be that at the beginning of irr writing, STL was not yet part of the C ++ standard, or STL was not ideal for speed in game engines. However, I like the irr string type very much. It can directly add numbers and strings to the original string through the + = operator, and the string type does not distinguish width from width, it will automatically convert between the local character set and Unicode, which is quite convenient.

Pass the processed string to setWindowCaption and display it on the window title bar.

After the program is compiled, you can visit this Quake3Map. The operation is completely FPS, and you need to press Alt + F4. With just a few lines of code, the basic appearance of the FPS game is achieved, and it is indeed a bit exciting. It seems that it is not difficult to use it to make your own games.

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.