Building an Advanced Java Game on Symbian OS (part 1)

1/3/2012 4:01:47 PM
We outlined some of the constraints that devices and operating systems in the feature phone market impose on Java ME. Most of the limitations you encounter on other platforms do not apply when building MIDP games on Symbian OS. Within the bounds of available resources, Symbian OS does not place any limits on JAR size, the number of sockets you can open, or the number of threads you can have; the local storage space and the size of the heap can grow and shrink dynamically as required.

This means that you can do some things in a Symbian OS MIDlet that you would not even consider on other platforms. For example, you can load a large number of high-resolution images at once, play advanced sound formats instead of MIDI sequences, and distribute detailed models or level data as JAR file resources. That is not to say that you should be lax with your memory management but it need not be your first concern .

More importantly, as we see in this section, you can adopt a more abstract framework for your code base which translates directly into a high degree of re-usability. There is no need to use the extreme Java ME optimization techniques that are needed on low-end platforms (such as, avoiding abstract classes and interfaces to keep JAR sizes down).

We work through the main features of a sample game called Finding Higgs in honor of recent work done (well, planned anyway, before it shut down for repairs in its first week) at the Large Hadron Collider in CERN.[] The aim is to review a first-person-shooter game that demonstrates the use of various JSRs commonly used in game development and that are provided with Symbian OS. Figure 1 shows a screen shot of the game in action.

[] 'God Particle Discovery Likely', east/7608342.stm

Figure 1. Screenshot from Finding Higgs

1. Planning

Having clear answers to the following questions before development begins directly reduces risk and development time for the project. It also helps simplify your game design from the outset:

  1. What are the aims of the player?

    The player has one minute to destroy as many attackers as possible while using the least amount of ammunition. Bonus points are awarded if the player's hit rate exceeds 60%. In single-player mode, the aim is to beat the previous high score. In two-player, head-to-head mode the aim is to beat your opponent.

  2. What are the 'game over' conditions?

    The game completes after one minute has elapsed.

  3. What controls are available for player input?

    The player can use the five-way navigation joystick or the keys marked 2, 4, 5, 6 and 8 on the keypad for navigation. In this game, the FIRE action fires the gun and the up, down, left and right keys move the player in a polar orbit around the world origin in 3D space.

  4. How are scores and game status displayed?

    As you can see in Figure 8.3, the top left corner shows the current frame rate at all times. The top right shows the opponent's score during multi-player mode. This is updated via Bluetooth in real time. The bottom left shows the remaining game time; the bottom centre shows the number of shots fired; and the bottom right shows the player's current score.

  5. How is the pause–resume cycle handled?

    The MIDlet class monitors its own state, keeping itself in sync with notifications from the AMS at all times. The Controller class manages the application lifecycle and the GameController manages the game lifecycle. A separate pause menu is used to allow the player to quit the current game or change the settings at any time.

  6. How is the game world represented?

    The game world is 3D space where a series of attacks are launched along the z-axis towards the player. The Mobile 3D Graphics API (JSR-184) is used to represent this world.

  7. How are frame and simulation rates managed?

    The frame rate is capped at 25 FPS by granting up to 40 ms for each frame. This works by tracking the amount of time it takes to complete the rendering phase of the game loop. If this is found to be less than 40 ms, the thread sleeps for a short period in order to re-synchronize itself. This approach enables other active threads to use processor cycles for other useful tasks and allows CPU power management which can help increase battery life.

    Be careful with this – on Symbian OS, the native timer has a resolution of 1/64 seconds so what you request may not be what you actually get. Using a periodic timer in this manner is susceptible to drift because you actually only receive an event once about every 30 ms – so your actual frame rate is slightly less than expected.

    The simulation rate runs at a different rate than the frame rate by tracking the time since the last simulation step and only performing a new one if a sufficient time period has elapsed.

  8. Does the game respond to a change in display size?

    In this case, the game is not designed to respond to changes in screen size.

  9. What multimedia effects are employed?

    A WAV file is used for the sound effect of the weapon and a set of MIDI sequences are used to provide background music. At the start of each music sequence, a vibration event is triggered.

  10. How is navigation between screens in the application managed?

    In this case, the entire application navigation is abstracted over three packages with the main management being the responsibility of the Navigator class. The point is to provide both a custom-drawn menu system using only the low-level graphics routines in MIDP 2.0 and a more advanced menu system using the Scalable 2D Vector Graphics API (JSR-226) where supported.

2. Design

The game design consists of the logical subsystems shown in Figure 2.

Figure 2. High-level game design

The AMS and the MIDlet together represent the interface between the application and the outside world, i.e. the operating system. The application itself is designed on a Model-View-Controller (MVC) pattern, where the Controller class manages application view changes and navigation using the active menu system. Its primary role is to respond to lifecycle events from the operating system and to drive the application framework. To a large extent, the controller is not aware that is a 'game' so to speak. It owns a GameController instance that it notifies when events occur. Other than that, the controller remains independent of the particular application.

Figure 3 shows the application's main classes and their relationships. As you can see from the names, most of the classes are designed for re-use and have little to do with the particular game being built.

3. Menus and Navigation

This game uses a relatively heavy UI layer in the form of three packages. This is not necessarily recommended practice – it has been done this way to demonstrate how you can support two different menu system implementations and select the one to use at run time based on the target device.

Figure 3. Main application classes

public interface IMenuSupplier {
public void showMainMenu();
public void showPauseMenu();
public void showSettings();
public void showMultiPlay();
public IMessageScreen showMessageScreen(String message);

Application navigation is performed by the Navigator class which delegates menu requests to the appropriate implementation. In this way the rest of the application is completely isolated from the menu system in use. Figure 4 shows the main game menu with the SVG version on the left and the low-resolution graphics version on the right.

Figure 4. Dual menu systems: a) SVG and b) low-resolution graphics

Incidentally, you should be able to see that the menus overlay different background images. Every time a new game is started, the game randomly selects a new background from a set of 10 high-resolution images included in the application JAR file. That becomes the active background for the entire MIDlet until a new game is played.

There is no particular point to this other than that it varies the feel of the game increasing its interest level and the pictures look great (they're all from NASA). This also shows just one of the things you can do on Symbian OS when you do not have to operate under the stringent limitations imposed by other operating systems. You can use your imagination to come up with an almost endless variety of great effects and features.
- jQuery 1.3 : Simultaneous versus queued effects (part 2) - Working with multiple sets of elements & Callbacks
- jQuery 1.3 : Simultaneous versus queued effects (part 1) - Working with a single set of elements
- iPhone 3D Programming : Crisper Text with Distance Fields (part 3) - Implementing Outline, Glow, and Shadow Effects
- iPhone 3D Programming : Crisper Text with Distance Fields (part 2) - Smoothing and Derivatives
- iPhone 3D Programming : Crisper Text with Distance Fields (part 1) - Generating Distance Fields with Python
- Mapping Well-Known Patterns onto Symbian OS : Singleton
- Mapping Well-Known Patterns onto Symbian OS : Model–View–Controller
- The Anatomy of a Mobile Site : STYLING WITH CSS - CSS Considerations for Mobile & Optimizing CSS
- iPad Development : The Dual-Action Color Popover (part 3) - Serving Two Masters
- iPad Development : The Dual-Action Color Popover (part 2) - Hooking Up the Grid
- iPad Development : The Dual-Action Color Popover (part 1) - Creating a Simple Color Grid
- XNA Game Studio 3.0 : Creating Fake 3-D - Creating Shadows Using Transparent Colors
- Android Application Development : ViewGroups (part 2) - ListView, ListActivity, ScrollView & TabHost
- Android Application Development : ViewGroups (part 1) - Gallery and GridView
- Java ME on Symbian OS : MIDP 2.0 Game API Core Concepts
- Java ME on Symbian OS : Building a Simple MIDP Game
- jQuery 1.3 : Compound effects & Creating custom animations
- jQuery 1.3 : Effects and speed
- Mobile Web Apps : Quick Wins (part 2) - Form Field Attributes
Most View
- Sharepoint 2013 : Community portals and sites - Replying to a discussion
- Windows 8 : Creating a Windows Network - Installing Network Wiring (part 2) - Wiring with Patch Cables, Installing In-Wall Wiring
- SQL Server 2012 : Delivering Manageability and Performance (part 4) - POLICY-BASED MANAGEMENT - SQL Server — Max Degree of Parallelism
- Diagnosing SQL Server 2012 Using Extended Events (part 3) - Extended Events Terminology - Events, Event Fields , Actions
- Exchange Server 2013 : Deploying an Exchange 2013 server (part 3) - Recovering a failed server
- Windows Server 2008 R2 Remote Desktop Services : Installing and Configuring Remote Desktop Services (part 6)
- Windows 8 : Cloud Connections - Office 2013
- Windows 8 : Cloud Connections - SkyDrive (part 3) - To access SkyDrive from a browser
- Windows Small Business Server 2011 : Deploying Applications with Group Policy (part 1) - Publish or Assign Applications , Creating a Software Distribution Point
- Microsoft Visio 2010 : Distributing Templates and Stencils (part 1) - Creating a Template with a Document Stencil
Top 10
- Sharepoint 2013 : Developing Integrated Apps for Office and Sharepoint Solutions - The New App Model for Office
- Overview of Oauth in Sharepoint 2013 : Application Authorization - On-Premises App Authentication with S2S
- Overview of Oauth in Sharepoint 2013 : Application Authorization - Requesting Permissions Dynamically
- Microsoft Excel 2010 : Working with Graphics - Inserting a Diagram,Inserting an Object
- Microsoft Excel 2010 : Working with Graphics - Inserting WordArt, Using Smart Art in Excel
- Microsoft Excel 2010 : Working with Graphics - Using AutoShapes
- Overview of Oauth in Sharepoint 2013 : Application Authentication (part 2) - Managing Tokens in Your Application
- Overview of Oauth in Sharepoint 2013 : Application Authentication (part 1) - Using TokenHelper
- Overview of Oauth in Sharepoint 2013 : Creating and Managing Application Identities
- Overview of Oauth in Sharepoint 2013 : Introduction to OAuth