My Books

My Software

Introduction

The idea with this tutorial is to go through how to use viewports in XNA. I thought that the best way to do that would be to demonstrate one of the better uses for viewports, which is in making a split screen game. That's when you have multiple players that each see the game from their own viewpoint, and the screen is divided up and each player gets a part of the screen for themselves. In this tutorial, we will start with a little bit of ground work, before going on to how viewports work, and then discuss how to make a game that uses a split screen using viewports.

Preparation

Before we get started, there is a little bit of work that needs to be done. First of all, before you really get going on this tutorial, it would be good to have an understanding of how 3D games work. If you haven't already done that, I would suggest you at least go back and take a look at the tutorial on using 3D models in a game. In this tutorial, I am going to start with the code below, which includes elements from the Simple 3D Animation tutorial, as well as the BasicEffect lighting tutorial. You are free to work this stuff into your own program or to start from the code below.

This code requires the SimpleShip model from the 3D Model Library, which you can download and add to your project. When you run it, you should get something like the image below, with the ship being viewed from the top, with the model spinning in circles.

Viewports

XNA uses a class called the Viewport class. This class can be thought of as a window into the game. It is basically made up of just a rectangle on the screen, with a position and size, and also has information about depth, which XNA will use to determine the order of drawing triangles. Viewports are used frequently in XNA. When you tell your game to use a particular viewport, nothing outside of the viewport will get drawn.

In this tutorial, we are going to create four viewports to view our game from four different viewpoints, which would be typical in a four player game. We will draw the game four times, once in each corner. For this we will need to create four different view matrices, one for each viewpoint. In the code below, we create these four different view matrices, one from above the model (topView), one from the front of the model (frontView), one from the side of the model (sideView) and one from a random perspective view (perspectiveView). Additionally, we will create four Viewport objects, which will eventually be used for drawing each of the four viewpoints. Add the following four variables as instance variables to your game class:

So now we should have a model matrix, a projection matrix, and four view matrices, as well as our new viewports. The next real step is to set up the viewports. You will want to add the following code somewhere to your game where it won't be called at every update. I put mine in the Initialize() method.

Notice that with this code, we've divided the default screen up into four equal sized areas, with the topViewport in the upper left corner of the window, the sideViewport in the upper right corner of the window, the frontViewport in the lower left corner, and the perspectiveViewport in the lower right corner.

Drawing with Split Screens

With our four view matrices prepared, and our viewports set up, all we need to do is handle the drawing of the game with the viewports to make a split screen game. This is fairly simple to do. By default, an XNA game is already using a viewport that covers the entire screen. The first thing we will need to do is store the default viewport to a temporary location, so that when we are done, we can restore it. Then, for each of the views, we will set the correct viewport, clear the viewport, and then draw the scene from the correct viewpoint. This is done with the change below.

What's Next?

This tutorial covers the basics of viewports, by using them to make a split screen game for four players. There isn't really another tutorial that is an ideal next step after this one, but you might want to go back and take a look at some of the other advanced XNA tutorials available here.