Including External Libraries as Plugins using Microsoft VC++ 2008

From C4 Engine Wiki

Jump to: navigation, search
Image:warning.jpg Under Construction

This article is still under construction and may contain sections that are incomplete.

Contents

Overview

Including External Libraries as Plugins

This article shows how to compile and link a plugin for use with C4, using Microsoft VC++ 2008. The TinyXML library will be used as an example to link with.

Important User Information

Tools Required:

  • Microsoft VC++ 2008
  • TinyXML [1]
  • Build 2.6 or higher and License of C4. Previous versions not tested, although may work.

Creating Plugin Project File

  • Go to directory C4-2.6\VisualStudio2008\C4.
  • Copy the SoundPlayer folder and paste it in the C4 folder, so there is a new directory called Copy of SoundPlayer.
  • Rename "Copy of SoundPlayer" to XML
  • Go into the directory and rename SoundPlayer.vcproj to XML.vcproj
  • Delete all other files in that directory besides XML.vcproj
  • Open up XML.vcproj in notepad and:

in notepad, find "Tool_SoundPlayer" and replace with "Tool_XML" in notepad, find "SoundPlayer.dll" and replace with "XML.dll" (three instances)

  • Open up C4 solution in visual studio and:

in the solution explorer, right click on Solution 'C4' -> Add -> Existing Project find and select XML.vcproj

Copying Library Files

  • Create the directory:

C4-2.6\SDK\Windows\XMLSDK\Lib

  • Copy tinyxml.lib and tinyxmld.lib into that directory

Linking The Plugin

In Visual Studio:

  • Right click on Engine project -> C\C++ -> Additional Library directories

add

..\..\..\SDK\Windows\XMLSDK\Lib


  • Right click on Game project -> Linker -> Input

add

../XML/$(OutDir)/XML.lib


  • Right click on Plug_XML project -> C\C++ -> Preprocessor -> Preprocessor Definitions

add

C4XML


  • Right click on Plug_XML project -> Linker -> Input -> Additional Dependencies

in debug mode add

tinyxmld.lib

in optimized add

tinyxml.lib


  • Right click on Plug_XML project -> Linker -> General -> Additional Library directories

add

..\..\..\SDK\Windows\XMLSDK\Lib

Source Code


#ifndef C4XMLPlugin_h
#define C4XMLPlugin_h


//# \component	XML Plugin
//# \prefix		XMLPlugin/


#include "C4Plugins.h"
#include "C4Panels.h"
#include "C4Image.h"
#include "C4Files.h"

#include "C4Vector3D.h"
#include "C4String.h"
#include "C4ColorRGBA.h"


#ifdef C4XML

	#define C4XMLAPI C4MODULEEXPORT
	
	extern "C" 
	{
		C4MODULEEXPORT C4::Plugin *ConstructPlugin(void);
	}

#else

	#define C4XMLAPI C4MODULEIMPORT
		
#endif


namespace C4
{
	class XMLPlugin : public Plugin, public Singleton<XMLPlugin>
	{
	public:

		XMLPlugin();
		~XMLPlugin();

		// This is the quivelent of update for a plugin
		//void PluginTask(void);

		C4XMLAPI void OpenFilePack(const String<>& fileName) const;
		C4XMLAPI void CloseFilePack() const;

		C4XMLAPI void OpenFileUnpack(const String<>& fileNam);
		C4XMLAPI void CloseFileUnpack();

		C4XMLAPI void PackString(const String<>& varName, const String<> &val) const;
		C4XMLAPI void PackLong(const String<>& varName, const long val) const;
		C4XMLAPI void PackFloat(const String<>& varName, const float val) const;
		C4XMLAPI void PackBool(const String<>& varName, const bool val) const;
		C4XMLAPI void PackVector3D(const String<>& varName, const Vector3D &val) const;
		C4XMLAPI void PackColorRGBA(const String<>& varName, const ColorRGBA &val) const;

		C4XMLAPI void UnpackString(const String<>& varName, String<> *retVal);
		C4XMLAPI void UnpackLong(const String<>& varName, long *retVal);
		C4XMLAPI void UnpackFloat(const String<>& varName, float *retVal);
		C4XMLAPI void UnpackBool(const String<>& varName, bool *retVal);
		C4XMLAPI void UnpackVector3D(const String<>& varName, Vector3D *retVal);
		C4XMLAPI void UnpackColorRGBA(const String<>& varName, ColorRGBA *retVal);

	protected:
	
	};


	C4XMLAPI extern XMLPlugin *TheXMLPlugin;
}



#endif

Add your TinyXML code into the functions below, or create your own functions.



#include <tinyxml.h>

#include "C4XMLPlugin.h"

#include "C4Configuration.h"


using namespace C4;


XMLPlugin *C4::TheXMLPlugin = nullptr;


C4::Plugin *ConstructPlugin(void)
{
	return (new XMLPlugin);
}


XMLPlugin::XMLPlugin() : Singleton<TinyXMLPlugin>(TheXMLPlugin)
{
}

XMLPlugin::~XMLPlugin()
{
}

void XMLPlugin::OpenFilePack(const String<>& fileName) const
{
}

void XMLPlugin::CloseFilePack() const
{
}

void XMLPlugin::OpenFileUnpack(const String<>& fileNam)
{
}

void XMLPlugin::CloseFileUnpack()
{
}


void XMLPlugin::PackString(const String<>& varName, const String<> &val) const
{
}

void XMLPlugin::PackLong(const String<>& varName, const long val) const
{
}

void XMLPlugin::PackFloat(const String<>& varName, const float val) const
{
}

void XMLPlugin::PackBool(const String<>& varName, const bool val) const
{
}

void XMLPlugin::PackVector3D(const String<>& varName, const Vector3D &val) const
{
}

void XMLPlugin::PackColorRGBA(const String<>& varName, const ColorRGBA &val) const
{
}

// **********************************************************************

void XMLPlugin::UnpackString(const String<>& varName, String<> *retVal)
{
}

void XMLPlugin::UnpackLong(const String<>& varName, long *retVal)
{
}

void XMLPlugin::UnpackFloat(const String<>& varName, float *retVal)
{
}

void XMLPlugin::UnpackBool(const String<>& varName, bool *retVal)
{
}

void XMLPlugin::UnpackVector3D(const String<>& varName, Vector3D *retVal)
{
}

void XMLPlugin::UnpackColorRGBA(const String<>& varName, ColorRGBA *retVal)
{
}


General Advice From C4 Forums

It depends on what kind of plugin you are making. If it's just a general plugin that contains some functionality that you want to make available to multiple games, then the Extras plugin that ships with C4 is a perfect example. The files C4ExtrasPlugin.h and .cpp show pretty much the minimum amount of code needed for a plugin DLL.


When incorporating a third-party technology, we like to create a plugin module for C4 inside which all communication with the external library is isolated. Two examples ship with the engine, one for playing QuickTime movies, and another for rendering web pages into texture maps using Awesomium.


Eric: If you use any standard headers, then you need to disable the C4FASTBUILD option.

I'm using stdio.h, but have not set C4FASTBUILD to 0. I'm using the include in source rather than headers. This seems to work for me.

Eric: The C headers probably won't cause any problems, but any C++ or Windows headers will.


new/delete are interfering with compilation: The only way that this would happen is if that external library is using the new operator in its header files. That being the case, you need to include any headers associated with that library before any C4 headers for each translation unit.


Give the plugin a different name to the external library you are using.

Personal tools