Irrlicht Engine logo

Tutorial: Splitscreen by Max Winkel
In this tutorial you'll lern to use splitscreen (e.g. for racing-games) with Irrlicht. In this example you'll create a viewport divided into 4 quarters. There'll be 3 fixed viewports and one user-controlled.


Lets start!

Ok, let's start with the headers (I think there's nothing to say about it):

#include <irrlicht.h>
#include <stdio.h>
#include <wchar.h>


//To compile with DevCPP, delete the following line and add -lirrlicht to the linker-options instead
#pragma comment(lib, "Irrlicht.lib")

//Namespaces for the engine
using namespace irr;
using namespace video;
using namespace core;
using namespace scene;
using namespace io;
using namespace gui;

Now we'll define the resolution in a constant for using in initializing the device and setting up the viewport. In addition we set up a global variable saying splitscreen is wished, or not.

//Resolution
#define ResX 800
#define ResY 600
#define fullScreen false


//Use SplitScreen?
bool SplitScreen = true;

Now we need four pointers to our cameras which are created later:

//cameras
ICameraSceneNode *camera[4] = {0,0,0,0};

In our event-receiver we switch the SplitScreen-variable, when ever the user press the S-key. All other events are sent to the FPS camera:

//Eventreceiver
class MyEventReceiver : public IEventReceiver {
public:
  virtual bool OnEvent(SEvent event) {

    //Key S enables/disables SplitScreen
    if (event.KeyInput.Key == KEY_KEY_S && event.KeyInput.PressedDown) {
      SplitScreen = !SplitScreen;
      return true;
    }

    //Send all other events to camera4
    if (camera[3])
      camera[3]->OnEvent(event);

    return false;
  }
};

Ok, now the main-function:
First, we initialize the device, get the SourceManger and VideoDriver, load an animated mesh from .md2 and a map from .pk3. Because that's old stuff, I won't explaine everey step. Just take care of the maps position:

int main() {
  //Instance of the EventReceiver
  MyEventReceiver receiver;

  //Initialise the engine
  IrrlichtDevice *device = createDevice(
      DT_DIRECT3D8,
      dimension2d<s32>(ResX,ResY),
      32,
      fullScreen,
      false,
      &receiver);

  ISceneManager *smgr = device->getSceneManager();
  IVideoDriver *driver = device->getVideoDriver();

  //Load model
  IAnimatedMesh *model = smgr->getMesh("sydney.md2");
  IAnimatedMeshSceneNode *model_node = smgr->addAnimatedMeshSceneNode(model);
  //Load texture
  ITexture *texture = driver->getTexture("sydney.bmp");
  model_node->setMaterialTexture(0,texture);
  //Disable lighting (we've got no light)
  model_node->setMaterialFlag(EMF_LIGHTING,false);

  //Load map
  device->getFileSystem()->addZipFileArchive("map-20kdm2.pk3");
  IAnimatedMesh *map = smgr->getMesh("20kdm2.bsp");
  ISceneNode *map_node = smgr->addOctTreeSceneNode(map->getMesh(0));
  //Set position
  map_node->setPosition(vector3df(-850,-220,-850));

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:

//-=Create 3 fixed and one user-controlled cameras=-
  //Front
  camera[0] = smgr->addCameraSceneNode(
      0,
      vector3df(50,0,0),
      vector3df(0,0,0));
  //Top
  camera[1] = smgr->addCameraSceneNode(
      0,
      vector3df(0,50,0),
      vector3df(0,0,0));
  //Left
  camera[2] = smgr->addCameraSceneNode(
      0,
      vector3df(0,0,50),
      vector3df(0,0,0));
  //User-controlled
  camera[3] = smgr->addCameraSceneNodeFPS();

Create a variable for counting the fps and hide the mouse:
//Hide mouse
  device->getCursorControl()->setVisible(false);

  //We want to count the fps
  int lastFPS = -1;

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:

  1. Set the viewport to the whole screen
  2. Begin a new scene (Clear screen)

  3. The following 3 steps are repeated for every vieport in the splitted screen
  4. Set the viewport to the area you wish
  5. Activate the camera which should be "linked" with the viewport
  6. Render all objects

  7. If you have a GUI:
  8. Set the viewport the whole screen
  9. Display the GUI
  10. End scene
Sounds a little complicated, but you'll see it isn't:



 while(device->run()) {
    //Set the viewpoint the the whole screen and begin scene
    driver->setViewPort(rect<s32>(0,0,ResX,ResY));
    driver->beginScene(true,true,SColor(0,100,100,100));

      //If SplitScreen is used
      if (SplitScreen) {
        //Activate camera1
        smgr->setActiveCamera(camera[0]);
        //Set viewpoint to the first quarter (left top)
        driver->setViewPort(rect<s32>(0,0,ResX/2,ResY/2));
        //Draw scene
        smgr->drawAll();

        //Activate camera2
        smgr->setActiveCamera(camera[1]);
        //Set viewpoint to the second quarter (right top)
        driver->setViewPort(rect<s32>(ResX/2,0,ResX,ResY/2));
        //Draw scene
        smgr->drawAll();

        //Activate camera3
        smgr->setActiveCamera(camera[2]);
        //Set viewpoint to the third quarter (left bottom)
        driver->setViewPort(rect<s32>(0,ResY/2,ResX/2,ResY));
        //Draw scene
        smgr->drawAll();

        //Set viewport the last quarter (right bottom)
        driver->setViewPort(rect<s32>(ResX/2,ResY/2,ResX,ResY));
      }

      //Activate camera4
      smgr->setActiveCamera(camera[3]);
      //Draw scene
      smgr->drawAll();

    driver->endScene();

As you can probably see, the image is rendered for everey viewport seperatly. That means, that you'll loose much performance.
Ok, if you're aksing "e;How I have to set the vieport, to get this or that screen?"e;, don't panic. It's really easy: In the rect-function you define 4 coordinates:

  1. X-coordinate of the corner left top
  2. Y-coordinate of the corner left top
  3. X-coordinate of the corner right bottom
  4. 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 arround 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:

    //Get and show fps
    if (driver->getFPS() != lastFPS) {
      lastFPS = driver->getFPS();
      wchar_t tmp[1024];
      swprintf(
          tmp,
          1024,
          L"Irrlicht SplitScreen-Example (FPS: %d)",
          lastFPS);
      device->setWindowCaption(tmp);
    }
  }

  //Delete device
  device->drop();

  return 0;
}

That's it! Just compile and play arround with the programm.
Note: With the S-Key you can switch between using splitscreen and not.

 

 

 


Valid XHTML 1.0! Valid CSS!


Irrlicht Engine and Irrlicht Engine webpage © 2003-2005 by Nikolaus Gebhardt