Creating a Game Plugin
Here we show how you can add your own C++ code in your Maratis game. You need to make a game plugin.
Contents |
Creating a project
In your visual cpp project, your xcode project or your makefile, add:
additional include
Maratis/SDK/MCore/Includes
Maratis/SDK/MEngine/Includes
additional library
Maratis/SDK/MCore/Libs
Maratis/SDK/MEngine/Libs
linking
MCore
MEngine
You can now add the source files of your plugin. See Source code
Where do I write my code?
On MyGame.h you will see various commented functions. These are functions that you can edit to add your C++ code. However when you do that you have to remember to call the respective base function, since this is a class derived from MGame. So, for example, if you want to personalize the onBegin function, you have to do it this way:
//on MyGame.h
//[...]
class MyGame : public MGame
{
public:
// constructors / destructors
//[...]
public:
//[...]
void onBeginScene(void);
//[...]
};
//on MyGame.cpp
void MyGame::onBeginScene(void)
{
MGame::onBeginScene(); // call the default MGame onBeginScene
//add your code here:
//[...]
}
Prototypes of the methods goes in MyGame.h and actual code in MyGame.cpp. You can of course add new methods and variables to the class.
Source code
GamePlugin.h
///////////////////////////////////////////////////////////////////////////////////////////////////////// // GamePlugin // GamePlugin.h ///////////////////////////////////////////////////////////////////////////////////////////////////////// #ifdef WIN32 #include <windows.h> extern "C" __declspec(dllexport) void StartPlugin(void); extern "C" __declspec(dllexport) void EndPlugin(void); #else extern "C" void StartPlugin(void); extern "C" void EndPlugin(void); #endif
GamePlugin.cpp
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// GamePlugin
// amePlugin.cpp
//
// Code : Anael Seghezzi
/////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "GamePlugin.h"
#include "MyGame.h"
MyGame * game = NULL;
void StartPlugin(void)
{
// get engine
MEngine * engine = MEngine::getInstance();
// set game
game = new MyGame();
engine->setGame(game);
}
void EndPlugin(void)
{
SAFE_DELETE(game);
}
MyGame.h
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// GamePlugin
// MyGame.h
/////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _MY_GAME_H
#define _MY_GAME_H
#include <MEngine.h>
class MyGame : public MGame
{
public:
// constructors / destructors
MyGame(void);
~MyGame(void);
public:
// events (virtuals from MGame class)
//void update(void);
//void draw(void);
//void onBegin(void){}
//void onEnd(void){}
//void onBeginLevel(void){}
//void onEndLevel(void){}
//void onBeginScene(void);
//void onEndScene(void);
};
#endif
MyGame.cpp
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// SimpleGamePlugin
// MyGame.cpp
/////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "MyGame.h"
// constructor
MyGame::MyGame(void):
MGame()
{}
// destructor
MyGame::~MyGame(void)
{}