Support for multiple devices in irrlicht (ghost engine)

Source: Internet
Author: User

To clarify an engine, We have to first clarify its hierarchy and then clarify the rendering process. This article provides the device abstraction layer in the ghost engine to help you quickly read the source code of the ghost engine.

 

Irrlichtdevice * Device =
Createdevice (drivertype, core: dimension2d <u32> (640,480 ),
16, false, shadows );
Everyone is familiar with this function. If you don't talk much about it, let's start from here...

Top layer interface of irrlichtdevice
Cirrdevicestub implements the device top-level interface class. All devices are derived from this class. The devices here refer to platform-related devices.
Class member:
Video: ivideodriver * videodriver; // graphic interface
Gui: iguienvironment * guienvironment; // GUI interface
Scene: iscenemanager * scenemanager; // scene node interface
Itimer * timer; // timer, which ensures that a device holds a timer
Gui: icursorcontrol * cursorcontrol; // mouse Controller
Ieventcycler * usercycler; // event Processor
Clogger * logger; // log
Iosoperator * operator; // related to system operations
IO: ifilesystem * filesystem; // File System
Scene: iscenemanager * inputreceivingscenemanager ;//
Video: cvideomodelist videomodelist; // graphical mode list
Sirrlichtcreationparameters creationparams; // parameters used to create a graphical Device
Smousemulticlicks mousemulticlicks; // multiple clicks (more than double-click)
The above class members can see what the device contains.

The following are implementation classes for different platforms
Cirrdeviceconsole
Cirrdevicelinux
Cirrdevicesdl
Cirrdevicewin32
From the class name, we can see which platform is targeted. Cirrdeviceconsole is the Console
The implementation of the above platforms is not only derived from cirrdevicestub, but also from iimagepresenter
Iimagepresenter has only one function.
//! Presents a surface in the client area
Virtual bool present (Video: iimage * surface, void * windowid = 0, core: rect <s32> * src = 0) = 0;
Output to target customer zone...

For the implementation of platform-related devices, take Win32 as an example.
The following are class members:
Core: position2d <s32> cursorpos;
Core: dimension2d <u32> windowsize;
Core: dimension2d <f32> invwindowsize;
Bool isvisible;
Hwnd;
S32 borderx, bordery;
Bool usereferencerect;
Core: rect <s32> referencerect;
We can see that it is to hold data related to the platform. The platform has nothing to do with cirrdevicestub.
Next, let's take a closer look at what the createdevice function has done.
Usage example:
Irrlichtdevice * Device =
Createdevice (drivertype, core: dimension2d <u32> (640,480 ),
16, false, shadows );

The parameter meaning of the createdevice function is not much said, and everyone who uses the API knows it.
The implementation is as follows:
Irrlicht_api irrlichtdevice * irrcallconv createdevice (Video: e_driver_type drivertype,
Const core: dimension2d <u32> & windowsize,
U32 bits, bool fullscreen,
Bool stencilbuffer, bool vsync, ieventreceiver * res)
{
Sirrlichtcreationparameters P;
P. drivertype = drivertype;
P. windowsize = windowsize;
P. Bits = (u8) bits;
P. fullscreen = fullscreen;
P. stencilbuffer = stencilbuffer;
P. vsync = vsync;
P. eventcycler = res;
Return createdeviceex (P );
}
It can be seen that it assigns the parameter to the sirrlichtcreationparameters structure, and then calls createdeviceex
The key is the createdeviceex, which enables the creation of the corresponding platform.
The main code is as follows:
Irrlichtdevice * Dev = 0;
# Ifdef _ irr_compile_with_windows_device _
If (Params. devicetype = eidt_win32 | (! Dev & Params. devicetype = eidt_best ))
Dev = new cirrdevicewin32 (Params );
# Endif
# Ifdef _ irr_compile_with_osx_device _
If (Params. devicetype = eidt_osx | (! Dev & Params. devicetype = eidt_best ))
Dev = new cirrdevicemacosx (Params );
# Endif
# Ifdef _ irr_compile_with_windows_ce_device _
If (Params. devicetype = eidt_wince | (! Dev & Params. devicetype = eidt_best ))
Dev = new cirrdevicewince (Params );
# Endif
# Ifdef _ irr_compile_with_x11_device _
If (Params. devicetype = eidt_x11 | (! Dev & Params. devicetype = eidt_best ))
Dev = new cirrdevicelinux (Params );
# Endif
# Ifdef _ irr_compile_with_sdl_device _
If (Params. devicetype = eidt_sdl | (! Dev & Params. devicetype = eidt_best ))
Dev = new cirrdevicesdl (Params );
# Endif
# Ifdef _ irr_compile_with_console_device _
If (Params. devicetype = eidt_console | (! Dev & Params. devicetype = eidt_best ))
Dev = new cirrdeviceconsole (Params );
# Endif
The relation between platforms is determined based on the macro definition to create the corresponding platform .....
Note that the Params parameter in the above Code is a sirrlichtcreationparameters struct. This parameter determines the information for creating a graphic device. Taking Win32 as an example, we will continue to explore how IRR achieves platform and graphic API independence.
Cirrdevicewin32 (const sirrlichtcreationparameters & Params );
This is the constructor of cirrdevicewin32. It can be seen that Params determines everything, And Params does not contain platform-related information except the API information specified by the user. Therefore, platform creation is irrelevant, while Params determines the choice of the graphic API. Of course, the choice of the graphic API can also be achieved through a similar solution, for example, when a user creates a device (calls createdevice) you can first test whether d3d is supported. If you do not select opegnl ....
Let's go further and explain how it was created ..
Next we will go to the implementation of his constructor.
The constructor passes Params to its parent class... and implements windows-related creation, such as window class registration and creation...
In the parent class, Params is saved by creationparams; // (if you forget it, see the place starting with the post )...
The constructor also calls another function (of course there are more than one, I just said, this is a function related to creationparams)
Well, it is: createdriver (); the Code for implementing this function is roughly as follows:
Void cirrdevicewin32: createdriver ()
{
Switch (creationparams. drivertype)
{
Case video: edt_direct3d8:
# Ifdef _ irr_compile_with_direct3d_8 _
Videodriver = video: createdirectx8driver
Break;
Case video: edt_direct3d9:
# Ifdef _ irr_compile_with_direct3d_9 _
Videodriver = video: createdirectx9driver
Break;
Case video: edt_opengl:
# Ifdef _ irr_compile_with_opengl _
Videodriver = video: createopengldriver (creationparams, filesystem, this );
Break;
Case video: edt_software:
# Ifdef _ irr_compile_with_software _
Videodriver = video: createsoftwaredriver (creationparams. windowsize,
Break;
Case video: edt_burningsvideo:
# Ifdef _ irr_compile_with_burningsvideo _
Videodriver = video: createsoftwaredriver2
Break;
Case video: edt_null:
// Create null driver
Videodriver = video: createnulldriver
Break;
}
}
Everything is clear ......

There is no detailed research here. It is just a simple introduction. It is a guide. You can read the source code based on this idea. I believe you will soon be able to understand it ~~~

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.