Cpp:Building A Simple Tile Engine:Part 1
From GPWiki[edit] Part 1: Defining the interfacesIn this first part of the tutorial we are going to define the general interfaces that will be used by the classes we create later on. We need to define a base interface that exposes functions to the engine, and everything we stuff into the engine needs to implement this. The following diagram shows the processes that happen in our game engine from start till end.
The engine makes heavy use of virtual functions. The way it works, is that you make an engine class that inherit the CEngine class and overload some or all of its virtual functions. Thus, when the engine calls its virtual functions (e.g. Render()), the overloaded function is called instead. We are going to make our custom implementation of the engine called CTileEngine, that will inherit the following virtual functions from CEngine:
In any game there will be a number of tiles and units, and it would be convenient to have a class to wrap those classes. We'll create a "manager" class for each type; CTileManager and CUnitManger. Both classes will also need the same functions as the classes they contain - that is VLoad(), VGetSaveData(), VThink() and VRender(). To wrap up the classes even more we're going to create yet another class. This one will be called CGame and will contain CTileManager and CUnitManager, and will also use the same interface. This is what our interface looks like: [edit] File: Interfaces.h#include "SDL.h" #include "../Libs/TinyXML/tinyxml.h" #include <string> using std::string; class IBaseEntity { public: virtual void VThink ( const int& iElapsedTime ) = 0; virtual void VRender( SDL_Surface* pDestSurface ) = 0; virtual bool VLoad ( TiXmlElement* pXMLData ) = 0; virtual TiXmlElement* VGetSaveData () = 0; }; Don't be scared about TiXmlElement class - we'll get to that in due time! Other than that, it's not that bad, is it? We include the SDL, TinyXML and C++ string header and just define the interface like we've discussed. [edit] Complete engine structureWe've now added a lot of functionality that extends the functionality of the original engine. We're still yet to define and implement the classes we've discussed in this article, but we've already defined the interface that we will use for communication. So, using that information, here is a complete diagram of what will happen inside the engine we're building:
Proceed to Part 2: Creating the base entity
|



