Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members

DemoActionEngine.h

Go to the documentation of this file.
00001 /*******************************************************
00002  *
00003  * $Id: DemoActionEngine.h,v 1.1.1.1 2000/12/09 09:28:39 christ Exp $
00004  *
00005  * This file has been placed in the public domain.
00006  *
00007  *******************************************************/
00008 
00009 
00010 #ifndef DEMOACTIONENGINE_H
00011 #define DEMOACTIONENGINE_H
00012 
00013 #include <PalmOS.h>
00014 
00015 #include "Customization.h"
00016 
00017 #include "ActionEngine.h"
00018 #include "HardKeyManager.h"
00019 #include "SpriteEngine.h"
00020 #include "SoundEngine.h"
00021 
00022 
00023 /**
00024  * DemoActionEngine demonstrates the implementation of a
00025  * customized ActionEngine. 
00026  */
00027 class DemoActionEngine : public ActionEngine
00028 {
00029     public:
00030 
00031     DemoActionEngine() : ActionEngine()
00032     {
00033         // Prepare two animated Sprites (both with the same frames of animation),
00034         // and add them to a SpriteGroup for optimized drawing,
00035         tuxFrames = new AnimFrames(3, 2000, 2005, 42, 75, AnimFrames::optBUFFER);
00036         sprite1 = new Sprite(*tuxFrames, true);
00037         sprite2 = new Sprite(*tuxFrames, true);
00038 
00039         spriteGroup = new SpriteGroup(2);
00040         spriteGroup->addSprite(*sprite1);
00041         spriteGroup->addSprite(*sprite2);
00042     }
00043 
00044 
00045     ~DemoActionEngine()
00046     {
00047         delete spriteGroup;
00048         delete sprite1;
00049         delete sprite2;
00050         delete tuxFrames;
00051     }
00052 
00053     
00054     void restart()
00055     {
00056         state.periodCounter = state.lastMoved = 0;
00057         state.xCoord = 80;
00058         state.yCoord = 80;
00059 
00060         SoundEngine::playSong(0);
00061     }
00062 
00063 
00064     void nextPeriod()
00065     {
00066         state.periodCounter++;
00067 
00068 
00069         // Evaluate the currently pressed keys
00070         UInt32 keyState = HardKeyManager::getCurrentState();
00071 
00072         if (keyState & (keyBitHard1 | keyBitHard2 | keyBitHard3 | keyBitHard4 | keyBitPageDown | keyBitPageUp))
00073         {
00074             state.lastMoved = state.periodCounter;
00075                 
00076             if (keyState & keyBitHard1)
00077                 state.xCoord -= 5;
00078             else if (keyState & keyBitHard2)
00079                 state.xCoord += 5;
00080 
00081             if (keyState & keyBitPageDown)
00082                 state.yCoord += 5;
00083             else if (keyState & keyBitPageUp)
00084                 state.yCoord -= 5;
00085 
00086 
00087             if (keyState & keyBitHard3)
00088             {
00089                 SoundEngine::playFx(0);
00090                 sprite1->setVisibility(!(sprite1->isVisible()));
00091             }
00092 
00093             if (keyState & keyBitHard4)
00094                 sprite2->setVisibility(!(sprite2->isVisible()));
00095         }
00096 
00097                 
00098         // Walk around if the user hasn't moved us for a while...
00099         if ((state.periodCounter - state.lastMoved) > 100)
00100             state.xCoord += 4;
00101 
00102 
00103         // Wrap around at screen edge
00104         if (state.xCoord > 200)
00105             state.xCoord = -40;
00106 
00107         if (state.yCoord > 220)
00108             state.yCoord = -60;
00109         
00110         sprite1->move(state.xCoord, state.yCoord);
00111         sprite2->move(80 - (state.xCoord - 80), state.yCoord + 20);
00112 
00113         sprite1->setFrame((state.periodCounter >> 2) % 3);
00114     }
00115 
00116 
00117     WinLockInitType getWinLockMode() const
00118     {
00119         return winLockErase;   // We always want a clear background.
00120     }
00121 
00122 
00123     void draw(RectangleType *bounds) const
00124     {
00125         // Paint sprites
00126         spriteGroup->draw(bounds);
00127     }
00128 
00129 
00130     void receiveEvent(const EventType* /* evtPtr */)
00131     {
00132         // We're not interested in any events
00133     }
00134 
00135 
00136 // ================================== Save & Restore =============================================================
00137 
00138     void* getRestoreStateBuffer()
00139     {
00140         return &state;
00141     }
00142     
00143     void releaseRestoreStateBuffer(void* /* stateBuffer */) const
00144     {
00145         // The state buffer is static. Do nothing.
00146     }
00147     
00148     void* getSaveStateBuffer()
00149     {
00150         return &state;
00151     }
00152     
00153     void releaseSaveStateBuffer(void* /* stateBuffer */) const
00154     {
00155         // The state buffer is static. Do nothing.
00156     }
00157     
00158     Int16 getStateBufferSize() const
00159     {
00160         return sizeof(state);
00161     }
00162 
00163     void restoreState(void *stateBuffer)
00164     {
00165         ErrNonFatalDisplayIf(stateBuffer != &state, "State buffer mismatch!");
00166 
00167         sprite1->move(state.xCoord, state.yCoord);
00168         sprite2->move(80 - (state.xCoord - 80), state.yCoord + 20);
00169     }
00170 
00171 
00172     private:    
00173 
00174     struct
00175     {
00176         UInt32 periodCounter;
00177         UInt32 lastMoved;
00178     
00179         Coord xCoord;
00180         Coord yCoord;
00181     } state;
00182 
00183 
00184     AnimFrames *tuxFrames;
00185     Sprite *sprite1;
00186     Sprite *sprite2;
00187     SpriteGroup *spriteGroup;
00188 };
00189 
00190 
00191 #endif

Razor! Engine Developer's Guide. Copyright © by Tilo Christ. All Rights Reserved. Last updated: 17 Dec 2000