AppEasy Core SDK  1.5.0
Cross platform mobile and desktop app and game development SDK - The easy way to make apps
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines
CzProgram.h
Go to the documentation of this file.
00001 // 
00002 //
00003 // AppEasy SDK - Cross Platform Multi-purpose Game and App Engine
00004 //
00005 // Developed by Matthew Hopwood of Pocketeers Limited - www.pocketeers.co.uk
00006 //
00007 // For updates, tutorials and more details check out www.appeasymobile.com
00008 //
00009 // This code is provided free of charge and without any warranty whatsoever. You must ensure that this whole notice is present in all files and derivatives, so the reader knows its origin.
00010 // If you use this SDK in your product then please ensure that you credit AppEasy's usage appropriately. Please see www.appeasymobile.com for licensing details and support
00011 //
00012 //
00013 
00014 #if !defined(_CZ_PROGRAM_H_)
00015 #define _CZ_PROGRAM_H_
00016 
00017 #include "CzUtil.h"
00018 #include "CzString.h"
00019 #include "CzXoml.h"
00020 #include "CzXomlVariables.h"
00021 
00022 class CzProgram;
00023 class CzProgramManager;
00024 
00025 // 
00026 // 
00027 //
00028 //
00029 // IzCommandExecutor - Common base classs used by objects that execute a list of CzCommand's
00030 // 
00031 // 
00032 // 
00033 //
00034 class IzCommandExecutor
00035 {
00036 public:
00037     // Public access to iteration
00038     typedef CzVector<IzCommandExecutor*>::iterator _Iterator;
00039     _Iterator               begin()     { return Commands.begin(); }
00040     _Iterator               end()       { return Commands.end(); }
00041 
00042 protected:
00043     // Properties
00044     CzProgram*                  Program;            // Parent container program
00045     IzCommandExecutor*          Parent;             // Parent command executor
00046     CzVector<IzCommandExecutor*> Commands;          // A collection of commands
00047     int                         CurrentCommand;     // Index of currently executing command
00048     bool                        Parallel;           // if true then child commands will be executed in parallel
00049     unsigned int                Conditions;         // Name of conditions variable that defines a set of conditions that must be met for this command to be executed
00050     CzString                    _Conditions;
00051     bool                        Not;                // if true then result of condition will be inverted
00052 #if defined (_DEBUG)
00053     CzString                    CommandName;        // Command name
00054 #endif  // _DEBUG
00055     unsigned int                CommandNameHash;    // Command name hash
00056     bool                        Initialised;        // Initialised state
00057     bool                        Finished;           // Command has finished and is in a waiting state
00058     int                         ReturnValue;        // Return value
00059     IzCommandExecutor*          IfReturn;           // Command that we check return value of
00060     int                         IfReturnValue;      // Value to check return again against
00061     int                         CommandIndex;       // Command index in parent program
00062 
00063 public:
00064     void                    setProgram(CzProgram* program)          { Program = program; }
00065     CzProgram*              getProgram()                            { return Program; }
00066     void                    setParent(IzCommandExecutor* parent)    { Parent = parent; }
00067     IzCommandExecutor*      getParent()                             { return Parent; }
00068     void                    setCommandName(const char* name)
00069     {
00070 #if defined (_DEBUG)
00071         CommandName = name;
00072 #endif  // _DEBUG
00073         CommandNameHash = CzString::CalculateHash(name);
00074     }
00075 #if defined (_DEBUG)
00076     CzString&               getCommandName()                    { return CommandName; }
00077 #endif  // _DEBUG
00078     unsigned int            getCommandNameHash()                { return CommandNameHash; }
00079     void                    setInitialised(bool init)           { Initialised = init; }
00080     bool                    isInitialised() const               { return Initialised; }
00081     void                    setFinished(bool finished)          { Finished = finished; }
00082     bool                    isFinished() const                  { return Finished; }
00083     void                    setParallel(bool parallel)          { Parallel = parallel; }
00084     bool                    isParallel() const                  { return Parallel; }
00085     void                    addCommand(IzCommandExecutor* command);
00086     bool                    removeCommand(IzCommandExecutor* command);
00087     IzCommandExecutor*      findCommand(unsigned int name_hash);
00088     IzCommandExecutor*      findCommand(const char* name);
00089     bool                    gotoCommand();
00090     virtual void            restart();
00091     void                    clearCommands();
00092     bool                    nextCommand();
00093     void                    setCurrentCommand(IzCommandExecutor* command);
00094     void                    setCurrentCommand(int index);
00095     int                     getReturnValue() const              { return ReturnValue; }
00096     IzCommandExecutor*      getIfReturn()                       { return IfReturn; }
00097     void                    setIfReturn(IzCommandExecutor* command, int value);
00098     int                     getIfReturnValue() const            { return IfReturnValue; }
00099     void                    setCommandIndex(int index)          { CommandIndex = index; }
00100     int                     getCommandIndex() const             { return CommandIndex; }
00101     void                    setConditions(const char* cond, bool _not = false)
00102     {
00103         _Conditions = cond;
00104         Conditions = CZ_HASH(cond);
00105         Not = _not;
00106     }
00107     unsigned int            getConditions() const   { return Conditions; }
00108 
00109     // Properties end
00110 protected:
00111 public:
00112     IzCommandExecutor() : Parent(NULL), Initialised(false), Parallel(false), Finished(false), CommandNameHash(0), Program(NULL), ReturnValue(0), IfReturnValue(0), IfReturn(NULL), Conditions(0), Not(false)    { CurrentCommand = 0; CommandIndex = -1; }
00113     virtual ~IzCommandExecutor()                            { clearCommands(); }
00114 
00115     virtual void            Init() { Finished = false; }        // Initialise the command
00116     virtual bool            Execute(float dt) = 0;              // Execute the command
00117     void                    Uninit();                           // Unitialises this command all children
00118 
00119     // Utility
00120     void                    RebuildCommandIndices();
00121 };
00122 
00123 //
00124 //
00125 //
00126 //
00127 // CzCommand - A program command - Derive program commands from CzCommand so they can be added to a CzProgram or another CzCommand derived command
00128 //
00129 //
00130 //
00131 //
00132 class CzCommand : public IzCommandExecutor
00133 {
00134 public:
00135     // Public access to iteration
00136 
00137     // Properties
00138 protected:
00139     CzString            Params[5];                  // Commands parameters
00140     CzXomlVariable*     Variables[5];               // Cached variable parameters (prevents continually searching for variables)
00141 public:
00142     void                setParameter(int index, const CzString& data)   { Params[index] = data; }
00143     void                setParameter(int index, const char* data)       { Params[index] = data; }
00144     // Properties End
00145 public:
00146     CzCommand() : IzCommandExecutor() { for (int t = 0; t < 5; t++) Variables[t] = (CzXomlVariable*)1; } // Chances of pointer at address 1 is none existent 
00147     CzString&       getParameter1(IzXomlResource* parent);
00148     CzString&       getParameter2(IzXomlResource* parent);
00149     CzString&       getParameter3(IzXomlResource* parent);
00150     CzString&       getParameter4(IzXomlResource* parent);
00151     CzString&       getParameter5(IzXomlResource* parent);
00152 };
00153 
00154 // 
00155 // 
00156 //
00157 //
00158 // CzCommandCreator - Base class that is used by classes that create an instance of a type of command
00159 //
00160 //
00161 //
00162 //
00163 class CzCommandCreator
00164 {
00165 #if defined(_DEBUG)
00166     CzString            CommandName;                // Name of command
00167 #endif
00168     unsigned int        CommandNameHash;            // Hashed name of class
00169 public:
00170     void                setCommandName(const char* name)
00171     {
00172 #if defined(_DEBUG)
00173         CommandName = name;
00174 #endif
00175         CommandNameHash = CzString::CalculateHash(name);
00176     }
00177     unsigned int        getCommandNameHash() const              { return CommandNameHash; }
00178 #if defined(_DEBUG)
00179     CzString&           getCommandtName()                       { return CommandName; }
00180 #endif
00181 public:
00182     virtual CzCommand* CreateInstance() = 0;
00183 };
00184 
00185 
00186 // 
00187 // 
00188 //
00189 //
00190 // IzProgram - Manages a collection of commands (a program)
00191 //
00192 //
00193 //
00194 //
00195 class CzProgram : public IzXomlResource, public IzCommandExecutor
00196 {
00197 public:
00198     enum eProgram_Status
00199     {
00200         PS_Stopped, 
00201         PS_Running, 
00202         PS_Paused, 
00203     };
00204 
00205 protected:
00206     // Properties
00207     CzProgramManager*       Manager;            // Parent program manager
00208     eProgram_Status         Status;             // Status of the program
00209     IzCommandExecutor*      LastCommand;        // Last executed command
00210     CzProgram*              Caller;             // If this program was called from another then Caller represents the calling program
00211 public:
00212     void                    setManager(CzProgramManager* manager)       { Manager = manager; }
00213     CzProgramManager*       getManager()                                { return Manager; }
00214     void                    setCaller(CzProgram* program)               { Caller = program; }
00215     CzProgram*              getCaller()                                 { return Caller; }
00216     void                    stop()                                      { Status = PS_Stopped; }
00217     void                    start()                                     { Status = PS_Running; }
00218     void                    pause()                                     { Status = PS_Paused; }
00219     void                    restart();
00220     bool                    isRunning() const                           { return Status == PS_Running; }
00221     bool                    isPaused() const                            { return Status == PS_Paused; }
00222     bool                    isStopped() const                           { return Status == PS_Stopped; }
00223     void                    call(CzProgram* program);
00224     void                    returnToCaller();
00225     void                    gotoNextCommand();
00226     void                    setLastCommand(IzCommandExecutor* command)  { LastCommand = command; }
00227     IzCommandExecutor*      getLastCommand()                            { return LastCommand; }
00228 
00229     // Properties end
00230 protected:
00231     bool                    ProcessCommands(CzCommand* parent, bool load_children, CzXmlNode* node);
00232 
00233 public:
00234     CzProgram() : IzXomlResource(), Status(PS_Paused), Manager(NULL), LastCommand(NULL), Caller(NULL) { setClassType("program"); CurrentCommand = 0; }
00235     virtual ~CzProgram() { clearCommands(); }
00236 
00237     bool                    Execute(float dt);
00238 
00239     // Implementation of IzXomlResource interface
00240     int                     LoadFromXoml(IzXomlResource* parent, bool load_children, CzXmlNode* node);
00241 };
00242 
00243 //
00244 //  CzProgramCreator - Creates an instance of a program
00245 //
00246 class CzProgramCreator : public IzXomlClassCreator
00247 {
00248 public:
00249     CzProgramCreator()
00250     {
00251         setClassName("program");
00252     }
00253     IzXomlResource* CreateInstance(IzXomlResource* parent)  { return new CzProgram(); }
00254 };
00255 
00256 // 
00257 // 
00258 //
00259 //
00260 // CzProgramManager - Manages a collection of programs
00261 //
00262 //
00263 //
00264 //
00265 class CzProgramManager
00266 {
00267 public:
00268     // Public access to iteration
00269     typedef CzList<CzProgram*>::iterator _Iterator;
00270     _Iterator               begin()     { return Programs.begin(); }
00271     _Iterator               end()       { return Programs.end(); }
00272 
00273 protected:
00274     // Properties
00275     IzXomlResource* Parent;                 // Parent container scene
00276     CzProgram*          PriorityProgram;        // This program has priority and is ran before all other programs
00277     CzList<CzProgram*> Programs;                // A collection of programms
00278 public:
00279     void                    setPriorityProgram(CzProgram* program)      { PriorityProgram = program; }
00280     CzProgram*              getPriorityProgram()                        { return PriorityProgram; }
00281     void                    addProgram(CzProgram* Program);
00282     bool                    removeProgram(CzProgram* Program);
00283     CzProgram*              findProgram(unsigned int name_hash);
00284     CzProgram*              findProgram(const char* name);
00285     void                    clearPrograms();
00286     void                    stopAll();
00287     void                    startAll();
00288     void                    pauseAll();
00289     void                    setParent(IzXomlResource* scene)        { Parent = scene; }
00290     IzXomlResource* getParent()                                     { return Parent; }
00291     // Properties end
00292 protected:
00293 
00294 public:
00295     CzProgramManager() : PriorityProgram(NULL), Parent(NULL) {}
00296     virtual ~CzProgramManager() { clearPrograms(); }
00297 
00298     void                    Execute(float dt);
00299 
00300     // Utility
00301     static CzProgram*   FindProgram(const char* name, IzXomlResource* container);
00302     static CzProgram*   FindProgram(unsigned int name_hash, IzXomlResource* container);
00303 
00304 };
00305 
00306 // 
00307 // 
00308 //
00309 //
00310 // CzPrograms - CzPrograms is the main controller responsible for storing command creators and creating instances of commands
00311 // 
00312 // 
00313 // 
00314 //
00315 #define CZ_PROGRAMS CzPrograms::getInstance()
00316 class CzPrograms
00317 {
00318 public:
00319     CDEFINE_SINGLETON(CzPrograms)
00320 
00321     // Public access to command creator iteration
00322     typedef CzList<CzCommandCreator*>::iterator _Iterator;
00323     _Iterator               begin() { return CommandCreators.begin(); }
00324     _Iterator               end() { return CommandCreators.end(); }
00325 protected:
00326     // Properties
00327 public:
00328     void                    addCommand(CzCommandCreator* creator)
00329     {
00330 #if defined(_DEBUG)
00331         if (findCreator(creator->getCommandNameHash()))
00332         {
00333             CzDebug::Log(CZ_DEBUG_CHANNEL_WARNING, "Command creator already exists in command creator list - ", creator->getCommandtName().c_str());
00334             delete creator;
00335             return;
00336         }
00337 #endif  //(_DEBUG)
00338         CommandCreators.push_back(creator);
00339     }
00340     CzCommandCreator*       findCreator(unsigned int name_hash);
00341     CzCommandCreator*       findCreator(const char* name);
00342     // Properties end
00343 
00344 protected:
00345     CzList<CzCommandCreator*> CommandCreators;
00346 
00347 public:
00348     void            Init();
00349     void            Release();
00350 };
00351 
00352 
00353 #endif  // _CZ_PROGRAM_H_