1
0
forked from 0ad/0ad
0ad/source/tools/atlas/GameInterface/CommandProc.h

170 lines
4.4 KiB
C
Raw Normal View History

# Added tool for viewing models and animations outside the game. Atlas: Added ActorViewer. Moved GL canvas into separate class for shared use. Disabled message-handling callback while blocked on the game, and stopped creating dialog boxes inside the game thread in order to avoid deadlocks (hopefully). Support multiple Views (for independent sets of camera/update/render code). Recalculate territory boundaries when necessary. Changed default list of animations to match those currently used by actors. # Tidied up more code. Moved some more #includes out of .h files, to minimise unnecessary compilation. MathUtil: Deleted unused/unuseful macros (M_PI (use PI instead), M_PI_2 (use PI/2), MAX3, ABS (use abs)). ObjectManager: Removed some ScEd-specific things. Unit: Moved creation out of UnitManager, so units can be created without adding to the manager. Changed CStr8 to the more conventional CStr. app_hooks: Removed warning for setting multiple times. win: Restored SEH catcher. GameSetup, GameView: Removed RenderNoCull, because it doesn't seem to do what it says it does ("force renderer to load everything") since we're loading-on-demand most stuff and it doesn't seem especially useful since we'd prefer to minimise loading times (but feel free to correct me if I'm wrong). (And because it crashes when things need to be initialised in a different order, so it's easier to remove than to understand and fix it.) PatchRData, Renderer: Work sensibly when there's no game (hence no LOS manager, water, etc). LOSManager: Use entity position instead of actor position when possible. TerritoryManager: Allow delayed recalculations (so Atlas can issue lots of move+recalculate commands per frame). Cinematic: Non-pointer wxTimer, so it doesn't leak and doesn't have to be deleted manually. This was SVN commit r4261.
2006-08-28 19:36:42 +02:00
#ifndef COMMANDPROC_H__
#define COMMANDPROC_H__
#include <string>
#include <list>
#include <map>
#include "SharedMemory.h"
namespace AtlasMessage
{
struct Command
{
virtual ~Command() {}
virtual void Do() = 0;
virtual void Undo() = 0;
virtual void Redo() = 0;
virtual void Merge(Command* prev) = 0;
virtual const char* GetType() const = 0;
};
class CommandProc
{
public:
CommandProc();
~CommandProc();
// Should be called before shutting down, so it can free
// references to entities/etc that are stored in commands
void Destroy();
void Submit(Command* cmd);
void Undo();
void Redo();
void Merge();
private:
std::list<Command*> m_Commands;
typedef std::list<Command*>::iterator cmdIt;
// The 'current' command is the latest one which has been executed
// (ignoring any that have been undone)
cmdIt m_CurrentCommand;
};
typedef Command* (*cmdHandler)(const void*);
typedef std::map<std::string, cmdHandler> cmdHandlers;
extern cmdHandlers& GetCmdHandlers();
CommandProc& GetCommandProc();
/*
We want the command handler implementations to be as pretty as possible - so let people write
BEGIN_COMMAND(CommandName)
{
int member;
cCommandName() { ... } // } both are optional; neither may
~cCommandName() { ... } // } make use of this->msg
void Do() { ... interact with this->msg ... }
void Undo() { ... }
void Redo() { ... }
void MergeIntoPrevious(cCommandName* prev) { ... } // iff this command is a mergeable one
};
END_COMMAND(CommandName)
which looks almost exactly like a class definition, and is about the simplest
system I can find.
The following macros convert that into:
class cCommandName_base : public Command
{
protected:
// Storage for data passed into this command
dCommandName* msg;
public:
// Ensure msg is initialised to something 'safe'
cCommandName_base : msg(NULL) {}
// MergeIntoPrevious should be overridden by mergeable commands, and implemented
// to update 'prev' to include the effects of 'this'. (A subclass overriding
// any function named 'MergeIntoPrevious' will hide this function, even if
// the types differ.)
void MergeIntoPrevious(void*) { ...error - needs to be overridden... }
};
// Use 'struct' for automatic publicness - we want users to write as little as possible
struct cCommandName : public cCommandName_base
{
// (user's command code - mostly overriding virtual methods from Command)
}
// Subclass the command to add things which require knowledge of the
// complete class definition
class cCommandName_sub : public cCommandName
{
public:
cCommandName_sub(dCommandName *data) { ...set msg... }
~cCommandName_sub() { ...clear msg... }
// Implement the relevant virtual methods from the Command base class,
// with automatic casting to the correct types and stuff
virtual void Merge(Command* prev) { ...call MergeIntoPrevious... }
virtual const char* GetType() const { return "CommandName"; }
// Factory method for creating an instance of this class, casting the
// data pointer into the right form (to avoid forcing the generic
// command-handling support code to worry about the types)
static Command* Create(const void* data) { ...return new cCommandName_sub... }
};
// Register.cpp wants to get that Create method, but it doesn't want to
// load all the class definitions; so define a simple method that just
// returns the address of Create
cmdHandler cCommandName_create()
{
return &cCommandName_sub::Create;
}
// (TODO: make sure this stays in sync with the code below)
*/
#define BEGIN_COMMAND(t) \
class c##t##_base : public Command \
{ \
protected: \
d##t* msg; \
public: \
c##t##_base() : msg(NULL) {} \
void MergeIntoPrevious(void*) { debug_warn("MergeIntoPrevious unimplemented in command " #t); } \
}; \
struct c##t : public c##t##_base
#define END_COMMAND(t) \
class c##t##_sub : public c##t \
{ \
public: \
c##t##_sub(d##t* data) \
{ \
msg = data; \
} \
~c##t##_sub() \
{ \
/* (msg was allocated in mDoCommand(), using SHAREABLE_NEW) */ \
AtlasMessage::ShareableDelete(msg); \
msg = NULL; \
} \
virtual void Merge(Command* prev) { MergeIntoPrevious(static_cast<c##t##_sub*>(prev)); } \
virtual const char* GetType() const { return #t; } \
static Command* Create(const void* data) \
{ \
return new c##t##_sub (reinterpret_cast<d##t*>(const_cast<void*>(data))); \
} \
}; \
cmdHandler c##t##_create() \
{ \
return &c##t##_sub ::Create; \
}
}
# Added tool for viewing models and animations outside the game. Atlas: Added ActorViewer. Moved GL canvas into separate class for shared use. Disabled message-handling callback while blocked on the game, and stopped creating dialog boxes inside the game thread in order to avoid deadlocks (hopefully). Support multiple Views (for independent sets of camera/update/render code). Recalculate territory boundaries when necessary. Changed default list of animations to match those currently used by actors. # Tidied up more code. Moved some more #includes out of .h files, to minimise unnecessary compilation. MathUtil: Deleted unused/unuseful macros (M_PI (use PI instead), M_PI_2 (use PI/2), MAX3, ABS (use abs)). ObjectManager: Removed some ScEd-specific things. Unit: Moved creation out of UnitManager, so units can be created without adding to the manager. Changed CStr8 to the more conventional CStr. app_hooks: Removed warning for setting multiple times. win: Restored SEH catcher. GameSetup, GameView: Removed RenderNoCull, because it doesn't seem to do what it says it does ("force renderer to load everything") since we're loading-on-demand most stuff and it doesn't seem especially useful since we'd prefer to minimise loading times (but feel free to correct me if I'm wrong). (And because it crashes when things need to be initialised in a different order, so it's easier to remove than to understand and fix it.) PatchRData, Renderer: Work sensibly when there's no game (hence no LOS manager, water, etc). LOSManager: Use entity position instead of actor position when possible. TerritoryManager: Allow delayed recalculations (so Atlas can issue lots of move+recalculate commands per frame). Cinematic: Non-pointer wxTimer, so it doesn't leak and doesn't have to be deleted manually. This was SVN commit r4261.
2006-08-28 19:36:42 +02:00
#endif // COMMANDPROC_H__