UNIT III - Chapter 3 - MIDP 2.0 Gaming

Mobile gaming has got very popular these days. It has in fact become the bread and butter for most mobile software companies. Also it is next big thing according to industry analysts. But in spite of all the hype the first generation MIDP platform was not having a direct support for mobile gaming. We as developers had to write our own game engines. This presented a very big barrier to the creativity of game developers. The look and feel of the game was highly correlated with the quality of game engine used. Since there were no standard game engines available every company developed its own game engine thereby increasing the development costs and time. Also since writing a game engine is not an easy task most software professionals that tried to enter this line without any support had to leave game development.

Keeping this in mind the developers of the MIDP 2.0 have given us a standard game engine. They have given us some standard classes that could make the development of 2D games a lot easier. In fact I can personally vouch that making a game in MIDP 2.0 is at least 70% simpler than making the same game in MIDP 1.0.

javax.microedition.lcdui.game

The game development classes in MIDP 2.0 are contained in this package. This package contains the following classes for simplifying the task of game creation:

·   GameCanvas

·   Layer

·   LayerManager

·   Sprite

·   TiledLayer

Each of these classes has been explained below in detail.

public abstract class GameCanvas

What is it?

The GameCanvas class is a very important new addition in MIDP 2.0. It provides the basis for a full fledged game user interface. The earlier Canvas class in MIDP 1.0 had certain shortcomings with respect to gaming. This class inherits from Canvas the functionality related to commands, input events etc. It also provides game-specific capabilities such as an off-screen graphics buffer and the ability to query key status.

o    Constructors

  • protected GameCanvas(boolean suppressKeyEvents)

This constructor creates a new instance of a GameCanvas. This constructor also creates a new buffer for the GameCanvas which is initially filled with white pixels.

o    Fields

  • public static final int DOWN_PRESSED

This bit represents the DOWN key.

Constant Value: 0x0040

  • public static final int FIRE_PRESSED

             This bit represents the FIRE key.

Constant Value: 0x0100

  • public static final int GAME_A_PRESSED

This bit represents the GAME_A key (This key may not be supported on all devices).

Constant Value: 0x0200

  • public static final int GAME_B_PRESSED

This bit represents the GAME_B key (This key may not be supported on all devices).

Constant Value: 0x0400

  • public static final int GAME_C_PRESSED

This bit represents the GAME_C key (This key may not be supported on all devices).

Constant Value: 0x0800

  • public static final int GAME_D_PRESSED

This bit represents the GAME_D key (This key may not be supported on all devices).

Constant Value: 0x1000

  • public static final int LEFT_PRESSED

             This bit represents the LEFT key.

Constant Value: 0x0004

  • public static final int RIGHT_PRESSED

             This bit represents the RIGHT key.

Constant Value: 0x0020

  • public static final int UP_PRESSED

This bit represents the UP key.

Constant Value: 0x0002

o    Methods

  • public void flushGraphics()

This method flushes the off-screen buffer to the display. Size of the flushed area is equal to the size of this GameCanvas. This method does not change the contents of the off-screen buffer.

  • public void flushGraphics(int x, int y, int width, int height)

This method flushes the specified region of the off-screen buffer to the display. This method does not change the contents of the off-screen buffer.

  • protected javax.microedition.lcdui.Graphics getGraphics()

This method returns the Graphics object that renders to this GameCanvas’ off-screen buffer.

  • public int getKeyStates()

This method returns an integer containing the key state information (one bit per key). This method return’s 0 if this GameCanvas is not currently shown.

  • public void paint(javax.microedition.lcdui.Graphics g)

This method paints this GameCanvas.

public abstract class Layer

What is it?

The Layer is an abstract class.  It represents a visual element of the game. Each Layer has certain properties like:

  • X coordinate
  • Y coordinate
  • Width
  • Height
  • Visibility status 

The subclasses of the Layer class must implement the paint method so that they can be rendered.

o    Methods

  • public int getHeight()

This method returns the height of the Layer object in pixels.

  • public int getWidth()

This method returns the width of the Layer object in pixels

  • public int getX()

This method returns the Layer object’s horizontal position.

  • public int getY()

This method returns the Layer object’s vertical position.

  • public boolean isVisible()

This method returns true if the Layer is visible. Otherwise it will return false when the Layer is invisible. 

  • public void move(int dx, int dy)

This method moves this Layer object by the specified horizontal and vertical distances.

  • public abstract void paint(javax.microedition.lcdui.Graphics g)

This method paints this Layer if it is visible. The Layer object is rendered at its current x and y position on the screen. For example if the position of the Layer object is at x: 100, y: 100 then this object will be rendered starting at this position.

  • public void setPosition(int x, int y)

This method sets this Layer’s position such that its top-left corner is located at (x,y) position in the painter’s coordinate system.

  • public void setVisible(boolean visible)

This method sets the visibility of this Layer. A visible Layer is rendered when its paint method is called while an invisible Layer is not rendered.

public class LayerManager

What is it?

The LayerManager class is used for managing a series of Layers. It is not that lLayers can not be used without this class but the LayerManager class simplifies the process of rendering the Layers which have been added to it by automatically rendering the correct regions of each Layer in the appropriate order. In fact LayerManager is one of the most powerful new features of MIDP 2.0. Keep in mind that the LayerManager paints the Layers in z – order i.e. the one appended last is painted first and the one appended first is painted the last.

 

o    Constructors

  • public LayerManager()

This constructor creates a new LayerManager.

o    Methods

  • public void append(javax.microedition.lcdui.game.Layer l)

This method appends a Layer to this LayerManager. The Layer is appended to the list of existing Layers such that it has the highest index i.e. will be painted the furthest away from the user.

  • public javax.microedition.lcdui.game.Layer getLayerAt(int index)

             This method returns the Layer that has the specified index.

  • public int getSize()

             This method returns the number of Layers

  • public void insert(javax.microedition.lcdui.game.Layer l, int index)

This method inserts a new Layer in this LayerManager at the specified index. The Layer is first removed from this LayerManager if it has already been added.

  • public void paint(javax.microedition.lcdui.Graphics g, int x, int y)

This method renders the LayerManager’s current view window at the specified location.

  • public void remove(javax.microedition.lcdui.game.Layer l)

This method removes the specified Layer from this LayerManager.

  • public void setViewWindow(int x, int y, int width, int height)

This method sets the view window on the LayerManager. The view window defines the region that the LayerManager draws when its paint method is called. This method allows the developer to control the size of the visible region. It also allows him to control the location as well of the view window relative to the LayerManager’s coordinate system. 

public class Sprite

What is it?

Games are generally composed of the characters and objects. Sprite is an element that can be rendered to show different characters and objects in the game. MIDP 2.0 provides a very versatile Sprite class. This class supports the following features:

·   Animations

·   Movement

·   Collision Detection

·   Image Transformations

·   Ability to Change The Visibility Status

The Sprite class is thus an important class while constructing any animated game. This class is particularly useful for arcade game and side scrolling games.

o    Constructors

  • public Sprite(javax.microedition.lcdui.Image image)

This constructor constructs a new non-animated Sprite using the Image in the parameter.

  • public Sprite(javax.microedition.lcdui.Image image, int frameWidth, int frameHeight)

This constructor creates new animated Sprite using frames contained in the provided Image. All the frames in the image provided should be of equal size. The frameWidth and frameHeight parameters define the size of the frames.

  • public Sprite(javax.microedition.lcdui.Image image, int frameWidth, int   frameHeight)

             This constructor creates a new Sprite from another Sprite.

o    Fields

  • public static int final TRANS_MIRROR

This field causes the Sprite to appear reflected about its vertical center.

Value: 2

  • public static int final TRANS_MIRROR_ROT

This field causes the Sprite to appear reflected about its vertical center and then rotated clockwise by 180 degrees.

Value: 1

  • public static int final TRANS_MIRROR_ROT

This field causes the Sprite to appear reflected about its vertical center and then rotated clockwise by 270 degrees.

Value: 4

  • public static int final TRANS_MIRROR_ROT

This field causes the Sprite to appear reflected about its vertical center and then rotated clockwise by 90 degrees.

Value: 7

  • public static int final TRANS_NONE

This field tells that no transformation should be applied to the Sprite.

Value: 0

  • public static int final TRANS_ROT

This field causes the Sprite to appear rotated clockwise by 180 degrees.

Value: 3

  • public static int final TRANS_ROT

This field causes the Sprite to appear rotated clockwise by 270 degrees.

Value: 6

  • public static int final TRANS_ROT

This field causes the Sprite to appear rotated clockwise by 90 degrees.

Value: 5

o    Methods

  • public boolean collidesWith(javax.microedition.lcdui.Image image, int x, int y, boolean pixelLevel)

This method returns true if this Sprite has collided with the Image else it will return false. If the pixelLevel parameter is true then the collision is checked for pixel by pixel. If the pixelLevel parameter is false then bounding box collision is used.

  • public boolean collidesWith(Sprite s, boolean pixelLevel)

This method returns true if the two Sprites have collided else it will return false. If the pixelLevel parameter is true then the collision is checked for pixel by pixel. If the pixelLevel parameter is false then bounding box collision is used.

  • public boolean collidesWith(TiledLayer t, boolean pixelLevel)

This method returns true if this Sprite has collided with the TiledLayer else it will return false. If the pixelLevel parameter is true then the collision is checked for pixel by pixel. If the pixelLevel parameter is false then bounding box collision is used. 

NOTE

In pixel by pixel collision the transparent pixels are not taken into account. Thus the collisions look very real. In bounding box collision the user may some times see collision even when the actual images have not collided because the colliding region may be having transparent pixels or have pixels that merge with the background color.

  • public void defineCollisionRectangle(int x, int y, int width, intheight)

This method defines the Sprite’s bounding rectangle that is used for collision detection purposes in bounding box collision. The x and y parameters in this method define the co-ordinate relative to the untransformed sprites top left edge.

  • public void defineReferencePixel(int x, int y)

This method defines the reference pixel for this Sprite. The x and y parameters in this method define the co-ordinate relative to the untransformed sprites top left edge. It may lie outside of the frame’s bounds.

  • public int getFrame()

             This method returns the current index of the current frame of the sprite.

  • public int getFrameSequenceLength()

             This method returns the number of elements in this Sprite’s frame sequence.

  • public int getRawFrameCount()

             This method returns the number of raw frames for this Sprite.

  • public int getRefPixelX()

             This method returns the horizontal location of the reference pixel

  • public int getRefPixelY()

             This method returns the vertical location of the reference pixel

  • public void nextFrame()

This method selects the next frame in the frame sequence.

  • public void paint(javax.microedition.lcdui.Graphics g)

             This method draws the Sprite.

  • public void prevFrame()

             This method selects the previous frame in the frame sequence.

  • public void setFrame(int sequenceIndex)

             This method selects the current frame in the frame sequence.

  • public void setFrameSequence(int[] sequence)

             This method is used for setting the frame sequence for this Sprite.

  • public void setImage(javax.microedition.lcdui.Image img, int frameWidth, int frameHeight)

             This method is used for changing the Image containing the Sprite’s frames.

  • public void setRefPixelPosition(int x, int y)

This method sets this Sprite’s position such that its reference pixel is located at x and y positions in the painter’s coordinate system.

  • public void setTransform(int transform)

This method sets the transform type for this Sprite. Transformations can be applied to a Sprite to change its rendered appearance.

public class TiledLayer

What is it?

Games with scrolling backgrounds are very popular. But in MIDP 1.0 creating such backgrounds with the use of repetitive tiles to save space was very difficult. Therefore in MIDP 2.0 the TiledLayer element has been added. It is a visual element composed of a grid of cells that can be filled with a set of tile images. This class allows large virtual layers to be created with just a few tiles. This technique is not a new technique. In fact I have known this technique from over a decade. What is new is its support in the mobile phone development environment. In fact in the mobile development environment this class is essential because of the limited memory and computational resources available to us for creating the games.

TiledLayer can be though of as a grid of equal sized cells much like MS Excel. Each cell can have a particular tile index which determines the rendering of that cell. A TiledLayer can be bigger in size than the mobile phone screen. Also MIDP 2.0 supports animated tiles.

o    Constructors

  • public TiledLayer(int columns, int rows, javax.microedition.lcdui.Image image, int tileWidth, int tileHeight)

This constructor constructs a new TiledLayer. The first two parameters determine the number of columns and rows in the TiledLayer. The image parameter determines the image containing tiles. The last parameters determine the size of the individual tiles in the TiledLayer.

o    Methods

  • public int createAnimatedTile(int staticTileIndex)

This method returns the index of newly created animated tile. The index for the animated tiles are always negative.

  • public void fillCells(int col, int row, int numCols, int numRows, int tileIndex)

This method fills a region cells with the specific tile. The tile index may refer to a static tile index, an animated tile or it may even be intentionally left blank. In fact in complex games with different superimposed TiledLayer objects leaving bank tiles is some time a necessity.

  • public int getAnimatedTile(int animatedTileIndex)

      This method gets the tile referenced by an animated tile.

  • public int getCell(int col, int row)

             This method returns the index of tile in cell.

  • public int getCellHeight()

             This method returns the height in pixels of a single cell in the TiledLayer grid.

  • public int getCellWidth()

             This method returns the width in pixels of a single cell in the TiledLayer grid.

  • public int getColumns()

             This method returns the width in columns of the TiledLayer grid.

  • public int getRows()

             This method returns the height in rows of the TiledLayer grid.

  • public void paint(javax.microedition.lcdui.Graphics g)

This method draws the TiledLayer. It is rendered subject to the clip region of the Graphics object.

  • public void setAnimatedTile(int animatedTileIndex, int staticTileIndex)

             This method associates an animated tile with the specified static tile.

  • public void setCell(int col, int row, int tileIndex)

            This method sets the contents of a particular cell.

  • public void setStaticTileSet(javax.microedition.lcdui.Image image, int tileWidth, int tileHeight)
This method change’s the static tile set. This replaces the current tile set with the new tile set as given in the parameters.
Comments