MobileDragon Basic Application Structure

In the MobileDragon game program flow is controlled by the MobileDragon core.

The very first call the MobileDragon core does to an application is call to MDGameSetPreferences() global function, which must be implemented by the user. The purpose is to determine some initial settings.

This function prototype is
void MDGameSetPreferences( mdragon::MDGamePreferences & )

MDGamePreferences structure members currently are:
string name – application name.
string title – application title. On Windows it is used as application window caption.
string pack_dir_name – game resource package name without any extension.
DWord system_pool_size – memory size in bytes that MobileDragon can use for it’s internal needs (such as file decompression, etc.)
string log_name – name for log file that will be created by MobileDragon core. string ‘_systemLog’ will be added to this name.
DWord symbian_uuid – application uuid for symbian platform.
DWord runs_per_second – how many times per second main game logic will be called (see MDGame::Update() method).
DWord palm_creator_id – application vendor id for palm platform.

Note that MDGameSetPreferences() function can be called more than once, so ensure it returns same data every call.

Next thing that MobileDragon does is it creates instance of main user’s game class object, which must be a derived from MDGame class and have it’s 3 pure virtual methods implemented:

virtual Bool Init()
called by MobileDragon core to perform main game class object initialization. Here you can perform basic tasks & initial resource loading. Return True to indicate that initialization completed successfully or False otherwise.

virtual Bool Update()
periodically called by MobileDragon core. All game logics must be implemented here.
MobileDragon core will try to call this method exactly such many times as it is set in preferences structure ( see MDGameSetPreferences function and MDGamePreferences structure ).

virtual void Draw()
periodically called by MobileDragon core. All idle time left from executing logic updates will be spent within Draw() cycle. Here & only here you visualize your game screen.

MobileDragon creates that instance by calling user-impemented global function MDGameCreate(). It must return pointer to created user’s main game class object instance, allocated on heap using new operator. Deletion of this object is MobileDragon core task.

This function prototype is
mdragon::MDGame* MDGameCreate( mdragon::System * system);

It takes pointer to MobileDragon Sytem object, and also it must allocate render object and let the core know about it assigning the system->render2d or system->render3d member variable to 2d or 3d render object instance.

Let’s provide source for example application that will: start, enter logic loop once and immediately exit without drawing anything on the screen.

// MDSampleApp.cpp

// define this to configure MobileDragon for Win32 target platform.
#define MD_OS_WIN32

// include MobileDragon main header.
#include "mdragon.h"

// prefere use of mdragon namespace.
using namespace mdragon;

/**
* This is MobileDraon sample application main game object class.
*/

class MDSampleApp : public MDGame
{
public:

MDSampleApp( System *md_system_ )
{
// remember pointer to MobileDragon System object.
md_system = md_system_;
}

~MDSampleApp()
{
// nothing to do here
}

/**
* Implement MDGame pure virtual metod.
*/

virtual Bool Init()
{
// Simply return True to tell MobileDragon core that
// initialization completed successfully.

return True;
}

/**
* Implement MDGame pure virtual metod.
*/

virtual Bool Update()
{
// Call to System::Exit() causes current logic update to
// be the last one, after it MobileDragon core will perform
// resource cleanup and return control to the host os.

md_system->Exit();

// Returning False from Update() shows MobileDragon that
// there is no need to draw the game screen, so Draw()
// method will not be called.

return False;
}

/**
* Implement MDGame pure virtual metod.
*/

virtual void Draw()
{
// This method will not be called at all in this sample.
}

private:
System *md_system;
};

/**
* Implement MDGameSetPreferences function.
*/

void MDGameSetPreferences( mdragon::MDGamePreferences & prefs )
{
// set up this game name
prefs.name = "MDSampleApp";

// set up this game title
prefs.title = "MDSampleApp";

// set up the name for PackDir file( not really used here )
prefs.pack_dir_name = "MDSampleApp";

// set the system memory pool size be 300 Kbytes.
prefs.system_pool_size = 300 * 1024;

// set preffered logic update method calls ( times per second ).
prefs.runs_per_second = 30;

// set MobileDragon system log name.
prefs.log_name = "MDSampleApp";
}

/**
* Implement MDGameCreate function.
*/

mdragon::MDGame * MDGameCreate( System * system )
{
// for example init MobileDragon system with 2d renderer object.
system->render2d = new mdragon::Render2D;

// create and return MDSampleApp object instance.
return new MDSampleApp( system );
}

 

 

Copyright 2005-2006 Herocraft Hitech Co. Ltd.