Graphics Reference
In-Depth Information
Figure 16-17 The viewports.py script controller
The content of viewports.py is of course where the real action is in this example. The first thing you
need to do is to import the module that handles putting the game imagery on the screen. That module is called
render. Import the module just as you would with any other module, along with importing logic, as shown here:
from bge import logic as GL
from bge import render
To divide the game view area between viewports, you first need to retrieve the height and width of the total
view area. The render module enables you to access this information as follows:
height = render.getWindowHeight()
width = render.getWindowWidth()
Thenextfewlinesofcodeshouldbefamiliar frompreviousexamples inthischapter,inwhichthesceneand
the objects in the scene are retrieved and assigned to variables:
scene = GL.getCurrentScene()
obList = scene.objects
redcam = obList["OBRedCamera"]
bluecam = obList["OBBlueCamera"]
overview = obList["OBOverviewCamera"]
To set viewports dimensions, use the setViewport() method on the appropriate Camera object. The
method takes four arguments. They represent, in order, the following parameters: the position of the left side of
the viewport, counting left to right from zero to the width of the full view area; the position of the bottom edge
of the viewport, counting bottom to top from zero to the height of the full view area; the right side of the view-
port, counting left to right from zero to the width of the viewing area (if this number is smaller than the first ar-
gument, the viewport will not be visible); and finally, the top edge of the viewport, counting bottom to top from
zerototheheightofthefullviewarea(ifthisnumberissmallerthanthesecondargument,theviewportwillnot
be visible). The overview camera is set to be visible over the other two viewports by using the setOnTop()
method. Finally, you need to enable each of the viewports by setting the property useViewport to True for
each Camera object individually:
bluecam.setViewport(0,0,width/2,height)
redcam.setViewport(width/2,0,width,height)
overview.setViewport(width/4,0,width*3/4,height*1/3)
overview.setOnTop()
bluecam.useViewport = True
redcam.useViewport = True
overview.useViewport = True
Whenyourunthegame-playmodenow,youwillseeasplit-screenviewwithanoverlaidviewport,asshown
in Figure 16-18 . This figure is reproduced in color in the color insert of the topic.
 
 
Search WWH ::




Custom Search