The three main classes that comprise RoboTable Graphics are shown in Figure 5-5, namely RoboTableGraphic, RoboTableDrawingLayer and BaseRoboTableDrawing. An object of the RoboTableGraphic class is used as a graphics controller, an object of the RoboTableDrawingLayer class is used as a graphics layer and an object of the BaseRoboTableDrawing class is used as a graphics object.
Figure 5-5 RoboTable Graphics Components
The following sections explain the design of the RoboTable Graphics Components in detail.
5.3.1 RoboTable Graphics Controller
The RoboTableGraphic class is the graphics controller, which initialises a DirectX device object and the main 2D drawing object: Sprite. Sprite is a 2D drawing object provided by DirectX. The Sprite object uses a texture to draw images. A texture acts as an image holder. The render method is the main method for a RoboTable game to draw on the table. The object of RoboTableGraphic class will loop inside a collection of graphic layers, and execute the rendering method for all RoboTable drawing objects. A sample code for the Graphics controller to render an entire game scene is shown in Figure 5-6.
The sample code outlines how to draw the whole game scene using a back buffer20. A back buffer is a render target that the DirectX device uses to present the drawing objects to the scene (display). Objects need only be created once and loaded into memory once. When we need to redraw the whole scene, we only need to clear the buffer and repaint all drawing objects into the buffer.
n 1 RoboTableGraphic --- Sprite spr; Render() RoboTableDrawingLayer --- Render(Sprite spr) BaseRoboTableDrawing --- Render(Sprite spr); BitmapDrawing --- Render(Sprite spr); 1 n
Because all objects have been loaded into memory, the speed of redrawing the whole scene is much faster than creating a new instance of an object and drawing the screen pixel by pixel.
Figure 5-6 Sample Graphics Rendering Code
5.3.2 RoboTable Graphics Layer Class
The RoboTable Graphics Layer Class is used as a drawing object holder that can be placed on top of the other graphics layers, and provides graphics drawing functions that are easy to manage. Game developers can add, remove, hide or show the layer. Each graphics layer controls a collection of graphics drawing objects.
Currently the Game Manager provides six default graphics layers for game developers to interact with game scene rendering. These are shown in Figure 5-7. The lowest layer is for the initial game information, including the game name and the name of the game developer. The second layer is for the game map, and the third layer is for game utilities such as adding a new map that can be displayed with different land colours. The reason we need the game utility layer is that we do not want to alter the game map layer too often, because the game map normally acts as background,
publicvoid Render() {
…….//other codes
// Clear the back buffer to a blue color (ARGB = 000000ff)
device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0); // Begin the scene
device.BeginScene();
sprite.Begin(SpriteFlags.AlphaBlend); //draw the game on here
RoboTableDrawingLayer TempLayer;
foreach (KeyValuePair<string, RoboTableDrawingLayer> pair in _MultilayerRoboTableDrawing) { TempLayer = pair.Value.ShallowCopy(); if (TempLayer.LayerVisible) { TempLayer.Render(sprite); } } sprite.Flush(); sprite.End(); // End the scene. device.EndScene(); try
{
// Copy the back buffer to the display device.Present();
}
catch (DeviceLostException) {
// Indicate that the device has been lost deviceLost = true;
} }
and we do not need to redraw the game map repeatedly. The Game Info layer can be used to display state information. The Robot layer is the principle layer that the game engine needs in order to manage the movement of the robots. The Game Result Layer is used to display the final game result.
Figure 5-7 Default Graphics Layers
The default Graphics layers that are used for this project are created by the RoboTable graphics controller during game initialisation process. However, game developers can create their own layers easily using the abstract class of the Graphics layers.
5.3.3 RoboTable Graphics Drawing Class
A graphics object is an object that will be displayed in a RoboTable scene. A graphics object can be rotated, translated to different locations and scaled to different sizes. The graphics object content is usually an image, for example, a virtual robot is a graphics object that has a robot image. Similarly, a game map that displays on the game map layer is also a graphics object. A graphics object has a key name, and game developers can use this to control it. To control a graphics object in the RoboTable scene, game developers need to provide information specifying the layer the graphics object is in and the key name of the graphics object.
Currently the toolkits only provide one concrete graphics object class for a Bitmap image. However, game developers can implement their own graphics object class by inheriting the base graphics drawing class. Each graphics drawing class has a general interface Render (Sprite spr) for their parent layer to call. The BitmapDrawing class implements the base drawing class and provides the following code (Figure 5-8) to draw a bitmap.
Game Result Layer Robot Layer Game Info Layer Game Utility Layer
Game Map Layer
Figure 5-8 Sample Code to Draw Graphics Object
The Transform property of a Sprite object is a matrix object and we use the structure class Matrix provided by DirectX to process the transformation in 2D. The method we have used is
Matrix.Transformation2D, which is used to build a 2D transformation matrix in the XY plane.
The base graphics drawing class provides methods to control transformations of the drawing object, such as translation, rotation and scaling. The class modifies and calculates Matrix, and provides the following interfaces (Figure 5-9) to game developers.
Figure 5-9 Methods to Control Transformations of the Drawing Object
Every drawing object class inherits the same interfaces, so we can render the whole game scene easily. To move a drawing object from one position to another position, we can use the Translation method. To change the direction of an object, we can rotate it. We can also change the colour of the object though the properties provided by the base class.