Creating a Game Plugin

From MaratisWiki
Jump to: navigation, search

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

The project must be set to create a DLL (or DYLIB on Mac); no console application or others. In your Visual C++ project, your XCode project or your Makefile, set up:

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.

The DLL

Compile, and be sure the compiled library is named Game.dll (on Windows) or Game.dylib (on Mac) and placed in the same directory as your game project file (*.mproj).

Simply run your game (from Maratis Player or within the Editor) and you're done. Maratis Player will automatically load your plugin.

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 onBeginScene function, you have to do it this way:

//on MyGame.h
class MyGame : public MGame
{
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, as well as adding new classes with the respective source files to have everything well organized and spread in different files.

Source code

GamePlugin.h

/////////////////////////////////////////////////////////////////////////////////////////////////////////
// GamePlugin
// GamePlugin.h
// 
// Code : Anael Seghezzi
/////////////////////////////////////////////////////////////////////////////////////////////////////////

#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
// 
// Code : Anael Seghezzi
/////////////////////////////////////////////////////////////////////////////////////////////////////////

#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
// 
// Code : Anael Seghezzi
/////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "MyGame.h"


// constructor
MyGame::MyGame(void):
MGame()
{}

// destructor
MyGame::~MyGame(void)
{}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox