A tutorial by Max Winkel.
In this tutorial we'll learn how to use splitscreen (e.g. for racing-games) with Irrlicht. We'll create a viewport divided into 4 parts, with 3 fixed cameras and one user-controlled.
Ok, let's start with the headers (I think there's nothing to say about it)
#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif
using namespace core;
using namespace video;
using namespace scene;
Main header file of the irrlicht, the only file needed to include.
Everything in the Irrlicht Engine can be found in this namespace.
Now we'll define the resolution in a constant for use in initializing the device and setting up the viewport. In addition we set up a global variable saying splitscreen is active or not.
const int ResX=800;
const int ResY=600;
const bool fullScreen=false;
bool SplitScreen=true;
Now we need four pointers to our cameras which are created later:
ICameraSceneNode *camera[4]={0,0,0,0};
In our event-receiver we switch the SplitScreen-variable, whenever the user press the S-key. All other events are sent to the FPS camera.
{
public:
virtual bool OnEvent(
const SEvent& event)
{
{
SplitScreen = !SplitScreen;
return true;
}
if (camera[3])
return false;
}
};
Interface of an object which can receive events.
virtual bool OnEvent(const SEvent &event)=0
Called if an event happened.
@ EET_KEY_INPUT_EVENT
A key input event.
SEvents hold information about an event. See irr::IEventReceiver for details on event handling.
struct SKeyInput KeyInput
Ok, now the main-function: First, we initialize the device, get the SourceManager and VideoDriver, load an animated mesh from .md2 and a map from .pk3. Because that's old stuff, I won't explain every step. Just take care of the maps position.
int main()
{
if (driverType==video::EDT_COUNT)
return 1;
MyEventReceiver receiver;
dimension2du(ResX,ResY), 32, fullScreen,
false, false, &receiver);
if (!device)
return 1;
IAnimatedMesh *model = smgr->getMesh("../../media/sydney.md2");
if (!model)
return 1;
IAnimatedMeshSceneNode *model_node = smgr->addAnimatedMeshSceneNode(model);
if (model_node)
{
ITexture *texture = driver->
getTexture(
"../../media/sydney.bmp");
model_node->setMaterialTexture(0,texture);
model_node->setMD2Animation(scene::EMAT_RUN);
model_node->setMaterialFlag(EMF_LIGHTING,false);
}
IAnimatedMesh *map = smgr->getMesh("20kdm2.bsp");
if (map)
{
ISceneNode *map_node = smgr->addOctreeSceneNode(map->getMesh(0));
map_node->setPosition(vector3df(-850,-220,-850));
}
The Irrlicht device. You can create it with createDevice() or createDeviceEx().
virtual scene::ISceneManager * getSceneManager()=0
Provides access to the scene manager.
virtual video::IVideoDriver * getVideoDriver()=0
Provides access to the video driver for drawing 3d and 2d geometry.
virtual io::IFileSystem * getFileSystem()=0
Provides access to the virtual file system.
virtual bool addFileArchive(const path &filename, bool ignoreCase=true, bool ignorePaths=true, E_FILE_ARCHIVE_TYPE archiveType=EFAT_UNKNOWN, const core::stringc &password="", IFileArchive **retArchive=0)=0
Adds an archive to the file system.
virtual ITexture * getTexture(const io::path &filename)=0
Get access to a named texture.
E_DRIVER_TYPE
An enum for all types of drivers the Irrlicht Engine supports.
Now we create our four cameras. One is looking at the model from the front, one from the top and one from the side. In addition there's a FPS-camera which can be controlled by the user.
camera[0] = smgr->addCameraSceneNode(0, vector3df(50,0,0), vector3df(0,0,0));
camera[1] = smgr->addCameraSceneNode(0, vector3df(0,50,0), vector3df(0,0,0));
camera[2] = smgr->addCameraSceneNode(0, vector3df(0,0,50), vector3df(0,0,0));
camera[3] = smgr->addCameraSceneNodeFPS();
if (camera[3])
Create a variable for counting the fps and hide the mouse:
int lastFPS = -1;
virtual gui::ICursorControl * getCursorControl()=0
Provides access to the cursor control.
virtual void setVisible(bool visible)=0
Changes the visible state of the mouse cursor.
There wasn't much new stuff - till now! Only by defining four cameras, the game won't be splitscreen. To do this you need several steps:
- Set the viewport to the whole screen
- Begin a new scene (Clear screen)
- The following 3 steps are repeated for every viewport in the splitscreen
- Set the viewport to the area you wish
- Activate the camera which should be "linked" with the viewport
- Render all objects
- If you have a GUI:
- Set the viewport the whole screen
- Display the GUI
- End scene
Sounds a little complicated, but you'll see it isn't:
{
driver->setViewPort(rect<s32>(0,0,ResX,ResY));
driver->beginScene(true,true,SColor(255,100,100,100));
if (SplitScreen)
{
smgr->setActiveCamera(camera[0]);
driver->setViewPort(rect<s32>(0,0,ResX/2,ResY/2));
smgr->drawAll();
smgr->setActiveCamera(camera[1]);
driver->setViewPort(rect<s32>(ResX/2,0,ResX,ResY/2));
smgr->drawAll();
smgr->setActiveCamera(camera[2]);
driver->setViewPort(rect<s32>(0,ResY/2,ResX/2,ResY));
smgr->drawAll();
driver->setViewPort(rect<s32>(ResX/2,ResY/2,ResX,ResY));
}
smgr->setActiveCamera(camera[3]);
smgr->drawAll();
driver->endScene();
virtual bool run()=0
Runs the device.
As you can probably see, the image is rendered for every viewport separately. That means, that you'll loose much performance. Ok, if you're asking "How do I have to set the viewport
to get this or that screen?", don't panic. It's really easy: In the rect-function you define 4 coordinates:
- X-coordinate of the corner left top
- Y-coordinate of the corner left top
- X-coordinate of the corner right bottom
- Y-coordinate of the corner right bottom
That means, if you want to split the screen into 2 viewports you would give the following coordinates:
- 1st viewport: 0,0,ResX/2,ResY
- 2nd viewport: ResX/2,0,ResX,ResY
If you didn't fully understand, just play around with the example to check out what happens.
Now we just view the current fps and shut down the engine, when the user wants to:
if (driver->getFPS() != lastFPS)
{
lastFPS = driver->getFPS();
tmp += lastFPS;
tmp += ")";
}
}
return 0;
}
bool drop() const
Drops the object. Decrements the reference counter by one.
virtual void setWindowCaption(const wchar_t *text)=0
Sets the caption of the window.
Axis aligned bounding box in 3d dimensional space.
That's it! Just compile and play around with the program. Note: With the S-Key you can switch between using splitscreen and not.