User Tools

Site Tools


Part 5 – Viewport and Camera

In order for anything to appear in your game, you will first need both a viewport and a camera.

A viewport is the rectangle that we can view the game world through. And a camera allows us to see all or part of that world at a time.

Take a look at your Project.ini and you'll see that a [Viewport] section has already been specified for you. There are some commented out options like Size and BackgroundColor:

Camera             = Camera
;RelativePosition  = bottom right
;Position          = (100, 100, 0); NB: Position is in pixels and will be ignored if RelativePosition is specified
;RelativeSize      = (0.5, 0.5, 0.0)
;Size              = (200, 150, 0) NB: Size is in pixels and will be ignored if RelativeSize is specified
;BackgroundColor   = (255, 180, 0)

Our game will have a blue sky as a background, so change the config to be:

Camera            = Camera
BackgroundColor   = (0, 180, 255)

Secondly, notice the Camera parameter is set to Camera? This means it is set to the [Camera] section, which has also already been defined for you:

FrustumWidth  = @Display.ScreenWidth
FrustumHeight = @Display.ScreenHeight
FrustumFar    = 1.0
FrustumNear   = 0.0
Position      = (0.0, 0.0, -1.0)

Change the camera position first:

FrustumWidth  = @Display.ScreenWidth
FrustumHeight = @Display.ScreenHeight
FrustumFar    = 1.0
FrustumNear   = 0.0
Position      = (400, 300, -1.0)

The camera is positioned into the center of the screen (400, 300) so that the whole screen can be viewed.

A frustum width and height does not have to be the same size as your screen, but for our game, we want it to be. So the width and height values are being inherited from the ScreenWidth and ScreenHeight parameters in the [Display] section.

The viewport needs to be created when our game runs, so let's take a look at the Project.cpp code:


Nice and simple. Our project already has this added. The Init() function runs once when your game first executes, and this is where the viewport is created.

The “Viewport” parameter specified in that function is the same name as the [Viewport] section in the config.

Because the camera was specified in the [Viewport] section, we do not need to create a camera in code.

Final thing for this step is to name our game. This name will show in the window title. Set this with the Title property in the [Display] section:

ScreenWidth   = 800
ScreenHeight  = 600
Title         = Platform Hero

Compile and run to see your game window with a blue background and a default object in the top left of the screen. This because of the position of the camera, and that the default object is at position (0,0) by default:

Next: Part 6 – Objects

en/guides/beginners/viewport_and_camera.txt · Last modified: 2018/02/14 04:47 (4 weeks ago) by iarwain