|
|
Overview The Mobile Dragon 2D API gives you the set of functions and classes for drawing 2D scenes. The 2D API is capable to draw points and lines primitives, 2d sprites, play 2d animation, display tile maps. The API is developed to work with DragonTool exported 2d sprite graphics data files.
Render2D Render2D class is the main class responsible for drawing
2d scenes. This class provides methods necessary for drawing points and
lines primitives and sprites. It also manages images loading. An instance
of the Rended2D class must present in every MobileDragon 2d application. GameProject::GameProject(System*
system_) Let’s take up with general Render2D features: Clearing the backbuffer: //
Define clear color. //
Clearing color backbuffer. Determine screen dimensions: Render2D provides a way to determine screen dimensions. //
Get screen width. //
Get screen height. Working with viewport and display origin: You can specify the rectangle “viewport” area on the
device screen. All drawings will be //
Set render viewport to top half of display area. or Also you can determine and specify viewport origin in global coordinates: //
Determine current vieport origin. //
Move origin 50 points down and 60 points left. or render->MoveOrigin( 50, -60 ); Scaling: You can specify scaling factor for viewport drawing
operations: //
Doble the scaling factor. Transparency: You can specify alpha transparency value for all subsequent
drawing operations: Gamma correction: Render2D provides methods to perform gamma correction.
Two methods are provided: ///
Get color gamma level. ///
Set new color gamma level. Drawing: Render2D provides methods for drawing points and lines primitives: //
Draw green point at position 5 , 5. All drawing operation are queued. To actually perform drawing call Render2D Flush() method. This draws all sprites and primitives to backbuffer. //
sort draw queue by z order and draw it. And to copy backbuffer to device display memory call Render2D Show() method: //
Copy backbuffer to display video memory.
Image Image class is used to hold .pcx images loaded from PackDir file. Only PCX 256 palette graphics format supported of square size with power of two – side length (8x8, 16x16, 32x32, 64x64, 128x128 and 256x256). Image can have transparent pixels. Pixels with value R=255, G=0, B=255 are always transparent and not drawn. Image alpha channel can be stored in file with the same name and added “+a” suffix. To store alpha values in PCX file format you need to copy pixel alpha channel value into it’s RGB components values then save new image as PCX 256 palette graphics format. Such a file will be automatically loaded into image alpha channel. ( for example if You have rgb bits of image stored in “test.pcx” file than alpha channel will be loaded from file "test+a.pcx" ). Load images using Render2D::LoadImage()
method. //
Find specified image by ASCII name in When this image is not needed any more – free occupied resources like this: img_test = NULL;
Sprite2D Sprite2D represents rectangular area of Image. Let’s
for example define Sprite2D object referencing area from point (10, 10)
with width 20 and height 16. To draw sprites use SpriteTransform class that holds Sprite2D rendering transformation.
SpriteTransform SpriteTransform class objects are used to hold Sprite2D
rendering transformations. Possible transformations include: position
translation, scaling, rotation, applying alpha transparency, visibility
flag. //
Let's draw created sprite at position (50, 100) with 50% transparency,
//
Create SpriteTransform object. //
Bind created sprite to it. //
Choose 50% alpha //
Rotated 45 degrees //
Scaled by 2 ( width and height )
Animation MobileDragon provides support for playing 2d sprite animation. Animation is represented as an array of SpriteTransform objects pointers. In general, you use animations loaded from GameData resource (see GameData class description for more details). SpriteTransform class has member functions GetTime(). GetTime() returns current frame animation delay. Actor2D class object can be used to control (“play”) animation. For example: //
Load the animation somewhere. //
Actor2d object instance. //
Advance animation time by 30 “points”.
Font2D Font2D class provides ability to display text messages.
Font2D character glyphs loaded from binary file <Name>.bin and <Name>.pcx.
These files can be prepared with TextureFont utility. //
Font2D object instance. //
Load game font. And to display sample message: //
Display message.
GameData MobileDragon comes with tool named DragonTool – program to edit 2d games graphics resources. These resources include sprite collections, tile maps and animations. DragonTool exports projects to one ore more *.pcx files and one *.dt binary file. These files must be kept together when composing game resource PackDir image. GameData class is used to load and manage DragonTool prepared resource. //
Instance of GameData object. //
SVector of SpriteTransform objects to hold actual SpriteTransform //
Vectors to hold node CustomData attributes. //
TileMap object to load. //
Load GameData resource named “set1_1”. GameData structure is the same tree structure seen in
DragonTool outline. After loading the resource we can walk the tree and
load variuos node items. At the beginning current node is the root node.
//
Enter the root node. Then go to sprite collection node named “palette”. This is dummy node with Sprites linked as child nodes to it. It holds sprite palette for our indexed tile map. //
Step to “palette” node. //
Export palette node. Then let’s load the tile map itself. That tile map resides in node “level10/floor” //
Go to top node in this level. //
Goto node “level10” and enter it. //
Go to tile map node “floor”. //
And export it to TileMap object. //
Set palette for this tile map. And set tile map cell size. Let’s for example load animations from node “/animation”: //
Enter root node. //
Go to “animation” node. //
Export node animations to anims object. //
Get individual animations from node. //
Get animation “walk-face”. //
Get animation “walk-right”.
Copyright 2005-2006 Herocraft Hitech Co. Ltd. |