Creating a splitscreen mode

Although online multiplayer games have become more and more mainstream and successful over the course of the last few years, there still are games being released that feature great local multiplayer modes. Games like Mario Kart are the proof that splitscreen multiplayer is far from dead and can be great fun. This is the reason why this recipe will show you how to create a splitscreen mode for your games!

Getting ready

Follow the steps found in Setting up the game structure in Chapter 1 before proceeding with this recipe to have the base for the following sample code.

How to do it...

Let's get to work by implementing a splitscreen mode:

  1. Open the file Application.py and add the following code:
    from direct.showbase.ShowBase import ShowBase
    from direct.actor.Actor import Actor
    from panda3d.core import Vec4
    class Application(ShowBase):
    def __init__(self):
    ShowBase.__init__(self)
    self.pandaActor = Actor("panda", {"walk": "panda-walk"})
    self.pandaActor.reparentTo(render)
    self.pandaActor.loop("walk")
    self.cam.node().getDisplayRegion(0).setActive(0)
    cameras = [self.makeCamera(self.win), self.makeCamera(self.win)]
    self.makeRegion(cameras[0], Vec4(0, 0.5, 0, 1), Vec4(0, 1, 0, 1))
    self.makeRegion(cameras[1], Vec4(0.5, 1, 0, 1), Vec4(1, 0, 0, 1))
    cameras[0].setPos(0, -30, 6)
    cameras[1].setPos(-30, 0, 6)
    cameras[1].lookAt(0, 0, 6)
    def makeRegion(self, cam, dimensions, color):
    region = cam.node().getDisplayRegion(0)
    region.setDimensions(dimensions.getX(), dimensions.getY(), dimensions.getZ(), dimensions.getW())
    region.setClearColor(color)
    region.setClearColorActive(True)
    aspect = float(region.getPixelWidth()) / float(region.getPixelHeight())
    cam.node().getLens().setAspectRatio(aspect)
    
  2. Press F6 to run the program. If your code is correct, you will see this:
How to do it...

How it works...

The first thing we do after adding the panda actor to the scene is disable the default camera. We drop it in favor of two new ones, neatly stored in the cameras list, which we will use for our splitscreen mode.

Splitting the screen into two halves is done using a feature of Panda3D called display regions. Display regions allow us to define an arbitrary area within the game window. We then are able to redirect the output of a camera to such a region. In our sample code, we use the makeRegion() method to create a new display region.

To create a new display region in makeRegion() we must resize the default display region of the camera given by the first parameter. The default display region has the same dimensions as the window, but can easily by resized using the setDimensions() method. The position and size of display regions is defined in a resolution independent way: The parameters passed to setDimensions() denote the positions of the left, right, bottom, and top sides of a rectangle within the window, where the origin of this coordinate system is at the bottom left of the window. Values may range from 0 to 1, so for example in our code we pass 0, 0.5, 0, and 1. This means that the left side of the display region is at the left edge of the window, the right side is at the middle of the window, the bottom edge of the rectangle is at the bottom of the window, and the top border is thus defined to be at the top of the window. We use a four-component vector to store these values, storing the coordinates of the left, right, bottom, and top edges of a display region in the x, y, z, and w components of the vector.

To create a new display region in makeRegion() we must resize the default display region of the camera given by the first parameter. The default display region has the same dimensions as the window, but can easily by resized using the setDimensions() method. The position and size of display regions is defined in a resolution independent way: The parameters passed to setDimensions() denote the positions of the left, right, bottom, and top sides of a rectangle within the window, where the origin of this coordinate system is at the bottom left of the window. Values may range from 0 to 1, so for example in our code we pass 0, 0.5, 0, and 1. This means that the left side of the display region is at the left edge of the window, the right side is at the middle of the window, the bottom edge of the rectangle is at the bottom of the window, and the top border is thus defined to be at the top of the window. We use a four-component vector to store these values, storing the coordinates of the left, right, bottom, and top edges of a display region in the x, y, z, and w components of the vector.

To help better distinguishing between the two display regions, we also set their background colors, before the last line, the makeRegion() method sets the aspect ratio of the camera according to the size of the new display region. This prevents the scene from being displayed, warped, and squeezed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset