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
CzApp.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_APP_H_)
00015 #define _CZ_APP_H_
00016 
00017 /**
00018  @def   CZ_VERSION
00019 
00020  @brief Current AppEasy Version.
00021  */
00022 #define CZ_VERSION      0x00410
00023 
00024 #include "CzUtil.h"
00025 #include "CzActor.h"
00026 #include "CzScene.h"
00027 #include "CzCamera.h"
00028 #include "CzInput.h"
00029 #include "CzAudio.h"
00030 #include "CzXoml.h"
00031 #include "CzResources.h"
00032 
00033 /**
00034  @addtogroup Core
00035  @{
00036  */
00037  
00038 class CzUIBase;
00039 
00040 /**
00041  @class CzApp
00042 
00043  @brief CzApp acts as a controller for the whole game or app. You should derive your own version of this class.
00044 
00045  <h1>Introduction</h1>
00046  CzApp represents your app and manages all of the internals for you including the following:
00047  - Like Scenes manage Actors the CzApp class manages scenes
00048  - Managing and updating hardware sub systems such as user input, http, audio etc..
00049  - Managing and updating global resources
00050  - Stabilising frame rate
00051  - Watching for and notifying scenes about screen orientation / size change events
00052  - Layering and drawing scenes
00053 
00054  czApp is also a singleton so you can only ever have one instance of CzApp created and running at one time during your apps life time. It also makes it accessible from anywhere within your app.
00055 
00056  When you first begin using AppEasy Core your first job will be to create your own class that is derived from CzApp and implement the following methods:
00057  - Init() - App initialisation
00058  - Release() - App cleanup
00059  - Update() - Per frame app update
00060  - Draw() - Per frame app rendering
00061 
00062  <h1>Creating our own App</h1>
00063  Here is a short example showing how to derive your own CzApp Class:
00064 
00065  <b>App.h</b>
00066  @code
00067 class App : public CzApp
00068 {
00069     CDEFINE_SINGLETON(App)
00070 
00071 public:
00072     void            Init(bool enable_http);
00073     void            Release();
00074     bool            Update();
00075     void            Draw();
00076 };
00077  @endcode
00078 
00079  <b>App.cpp</b>
00080  @code
00081 #include "App.h"
00082 
00083 #define APP App::getInstance()  // Macros is used to provide more efficient access to the app singleton
00084 
00085 CDECLARE_SINGLETON(App)
00086 
00087 void App::Init(bool enable_http)
00088 {
00089     CzApp::Init(enable_http);   // Init base
00090 }
00091 
00092 void App::Release()
00093 {
00094     CzApp::Release();   // Release
00095 }
00096 
00097 bool App::Update()
00098 {
00099     if (!CzApp::Update())   // Update the app
00100         return false;
00101 
00102     return true;
00103 }
00104 
00105 void App::Draw()
00106 {
00107     CzApp::Draw();  // Draw the app
00108 }
00109  @endcode
00110 
00111  And finally we create a main.cpp that creates and runs our app:
00112 
00113  <br>Main.cpp</b>
00114  @code
00115 // App headers
00116 #include "App.h"
00117 
00118 // Include platform
00119 #include "CzPlatform.h"
00120 
00121 int main()
00122 {
00123     CzPlatform_Create();    // Init platform
00124 
00125     App::Create();          // Init App
00126     APP->Init(true);
00127 
00128     while (!PLATFORM_SYS->CheckAppQuit())   // Main Loop
00129     {
00130         if (!APP->Update()) // Update App
00131             break;
00132 
00133         APP->Draw();        // Draw App
00134     }
00135 
00136     APP->Release();         // Shut down App
00137     App::Destroy();
00138 
00139     CzPlatform_Destroy();   // Shut down platform
00140 
00141     return 0;
00142 }
00143  @endcode
00144 
00145  This style of init, update, draw, cleanup is used throughuot all AppEasy Core examples.
00146 
00147  <h1>System Variables</h1>
00148  CzApp handles all global resources as well as the global system variables which include:
00149  - system - System related variables sych as screen height and width
00150  - touches - Touch info for each available screen touch
00151 
00152  The system variables array is a special XOML array that contains information about the device that your app is running on. The array supports querying of the following information:
00153  - 0 - Display width
00154  - 1 - Display height
00155  - 2 - Size hint - This is a hint that can be used to separate resources into groups that can be used across different sized screens. The calculation is based on (width + height ) / 400
00156  - 3 - Standard graphics display mode name, for example HVGA, QXGVA etc..
00157  - 4 - Device type
00158   - Unsupported = -1
00159   - iPhone = 0
00160   - iPad = 1
00161   - Android = 2
00162   - Bada = 3
00163   - QNX = 4
00164   - Symbian = 5
00165   - WinMobile = 6
00166   - WebOS = 7
00167   - Windows = 8
00168   - OSX = 9
00169  - 5 - Multi-touch - If set to 1 then device supports multi-touch
00170  - 6 - Accelerometer - If set to 1 then device supports accelerometer
00171  - 7 - Compass - If set to 1 then device supports compass
00172  - 8 - Keys - If set to 1 then device supports keyboard
00173  - 9 - HasPointer - If set to 1 then the device has touch screen / pointer support
00174  - 10 – Last purchase ID – Contains the ID of the last item purchased via the Market, -1 for none
00175 
00176  Using the system array you can style your app to fit different device configurations.
00177 
00178  The system touches variables array is a special array that contains information about the position and state of up to 5 touches on the device. The array supports querying of the following information:
00179  - 0 – Touch 1 position x
00180  - 1 – Touch 1 position y
00181  - 2 – Touch 1 status (1 for touching, 0 for not touching)
00182  - 3 – Touch 2 position x
00183  - 4 – Touch 2 position y
00184  - 5 – Touch 2 status (1 for touching, 0 for not touching)
00185  - 6 – Touch 3 position x
00186  - 7 – Touch 3 position y
00187  - 8 – Touch 3 status (1 for touching, 0 for not touching)
00188  - 9 – Touch 4 position x
00189  - 10 – Touch 4 position y
00190  - 11 – Touch 4 status (1 for touching, 0 for not touching)
00191  - 12 – Touch 5 position x
00192  - 13 – Touch 5 position y
00193  - 14 – Touch 5 status (1 for touching, 0 for not touching)
00194 
00195  An example condition that checks for touch 1:
00196  
00197  @code
00198  <Variable Name="Touched1" Type="condition" Value="touches:2 == 1" />
00199  @endcode
00200 
00201  In this example Touched1 will evaluate to true if the user touches the screen
00202 
00203  System variables provide an easy way for XOML to change layouts and behaviours based on different system configurations.
00204 
00205  */
00206  
00207 /**
00208  @def   CZ_MAX_SCENE_LAYERS
00209 
00210  @brief Default maximum scene layers.
00211 
00212  */
00213 #define CZ_MAX_SCENE_LAYERS             10
00214 
00215 class CzApp : public IzXomlResource
00216 {
00217 public:
00218 
00219     /**
00220      @enum  eSysArrayIndex
00221     
00222      @brief Indices into the XOML system array
00223      */
00224 
00225     enum eSysArrayIndex
00226     {
00227         SYS_SCREEN_WIDTH            = 0,    ///< Current screen width
00228         SYS_SCREEN_HEIGHT           = 1,    ///< Current screen height
00229         SYS_SCREEN_MODE             = 2,    ///< Graphics mode, see CzUtils.cpp for list of modes
00230         SYS_SCREEN_SIZE_HINT        = 3,    ///< Screen size hint (banded using (width + height) / 400 calculation to give rough idea of what size components such as fonts to use)
00231         SYS_DEVICE_TYPE             = 4,    ///< Device type, see CzUtils.h for list of types
00232         SYS_HAS_MULTITOUCH          = 5,    ///< Supports multi-touch
00233         SYS_HAS_ACCELEROMETER       = 6,    ///< Supports accelerometer
00234         SYS_HAS_COMPASS             = 7,    ///< Supports compass
00235         SYS_HAS_KEYS                = 8,    ///< Supports keys
00236         SYS_HAS_POINTER             = 9,    ///< Supports pointer
00237         SYS_LAST_PURCHASE_ID        = 10,   ///< ID of last purchase attempt
00238         SYS_SCREEN_ORIENTATION      = 11,   ///< Screen orientation in degrees
00239         SYS_MAX                     = 12    ///< Maximum array indices
00240     };
00241 
00242     /**
00243      @enum  eSysTouchesArrayIndex
00244     
00245      @brief Indices into the XOML touches system array
00246      */
00247 
00248     enum eSysTouchesArrayIndex
00249     {
00250         TOUCH_POS_X0                = 0,    ///< Touch x-position 0
00251         TOUCH_POS_Y0                = 1,    ///< Touch y-position 0
00252         TOUCH_STATE0                = 2,    ///< Touch state 0
00253         TOUCH_POS_X1                = 3,    ///< Touch x-position 0
00254         TOUCH_POS_Y1                = 4,    ///< Touch y-position 0
00255         TOUCH_STATE1                = 5,    ///< Touch state 1
00256         TOUCH_POS_X2                = 6,    ///< Touch x-position 0
00257         TOUCH_POS_Y2                = 7,    ///< Touch y-position 0
00258         TOUCH_STATE2                = 8,    ///< Touch state 2
00259         TOUCH_POS_X3                = 9,    ///< Touch x-position 0
00260         TOUCH_POS_Y3                = 10,   ///< Touch y-position 0
00261         TOUCH_STATE3                = 11,   ///< Touch state 3
00262         TOUCH_POS_X4                = 12,   ///< Touch x-position 0
00263         TOUCH_POS_Y4                = 13,   ///< Touch y-position 0
00264         TOUCH_STATE4                = 14,   ///< Touch state 4
00265         TOUCH_MAX                   = 15    ///< Maximum array indices
00266     };
00267 
00268     // Public access for scene iteration
00269     typedef CzVector<CzScene*>::iterator    _Iterator;
00270     _Iterator                   begin() { return Scenes.begin(); }
00271     _Iterator                   end() { return Scenes.end(); }
00272 
00273 protected:
00274     // Properties
00275     CzIVec2                 ScreenSize;                             ///< Native screen size
00276     int                     ScreenOrientation;                      ///< Orientation of screen in degrees
00277     CzScene*                CurrentScene;                           ///< Scene that has current input focus
00278     CzScene*                NextScene;                              ///< Scene that we wish to switch focus to
00279     CzVector<CzScene*>      Scenes;                                 ///< A collection of game scenes
00280     CzActor*                TouchFocus[CZ_MAX_TOUCHES];             ///< Actor that has the current touch focus
00281     CzActor*                LastTappedActor[CZ_MAX_TOUCHES];        ///< Actor that was last tapped
00282     CzUIBase*               KeyFocus;                               ///< Actor that has the current key focus
00283     CzColour                BackgroundColour;                       ///< Background clear colour
00284     bool                    TouchPanEnabled;                        ///< Global touch pan enabled
00285     int                     MaxTouches;                             ///< Maximum number of tuoches to support
00286     bool                    GLCompatible;                           ///< Determine if this game should be compatible with OpenGL commands
00287     float                   FrameRate;                              ///< Estimated frame rate in frames per second
00288     unsigned int            TotalFrames;                            ///< Total number of frames that have been processed since the app began
00289     bool                    TimePaused;                             ///< Pauses time of set to true
00290 
00291 public:
00292     void                    addScene(CzScene *scene, bool bring_to_front = true);
00293     void                    removeScene(CzScene* scene);
00294     void                    removeScene(unsigned int name_hash);
00295     void                    removeAllScenes(const CzVector<CzScene*>& exclude_scenes);
00296     int                     findScenesOfType(int type, CzVector<CzScene*>& scenes);
00297     CzScene*                findScene(unsigned int name_hash);
00298     CzScene*                findScene(const char* name);
00299     CzScene*                findScene(int type);
00300     CzScene*                getScene(int index);
00301     void                    clearScenes();
00302     void                    changeScene(CzScene *new_scene);
00303     bool                    changeScene(unsigned int name_hash);
00304     CzScene*                getCurrentScene()                       { return CurrentScene; }
00305     void                    setTouchFocus(int index, CzActor* focus)    { TouchFocus[index] = focus; }
00306     CzActor*                getTouchFocus(int index)                { return TouchFocus[index]; }
00307     void                    releaseTouchFocus(int index, int x, int y, bool allow_bubble = true);
00308     CzActor*                getLastTappedActor(int index)           { return LastTappedActor[index]; }
00309     void                    setKeyFocus(CzUIBase* actor)            { KeyFocus = actor; }
00310     CzUIBase*               getKeyFocus()                           { return KeyFocus; }
00311     void                    setTouchPanEnabled(bool enabled)        { TouchPanEnabled = enabled; }
00312     bool                    isTouchPanEnabled() const               { return TouchPanEnabled; }
00313     void                    setMaxTouches(int max)                  { MaxTouches = max; }
00314     int                     getMaxTouches() const                   { return MaxTouches; }
00315     void                    setGLCompatible(bool enable)            { GLCompatible = enable; }
00316     bool                    getGLCompatible() const                 { return GLCompatible; }
00317     const CzIVec2&          getScreenSize() const                   { return ScreenSize; }
00318     int                     getScreenOrientation() const            { return ScreenOrientation; }
00319     float                   getFrameRate() const                    { return FrameRate; }
00320     unsigned int            getTotalFrames() const                  { return TotalFrames; }
00321     void                    setTimePaused(bool paused);
00322     bool                    isTimePaused() const                    { return TimePaused; }
00323     // Properties end
00324 
00325 protected:
00326     void                    CreateSystemArray();    // Creates an array that contains system variables that user can access from XOML
00327     CzList<CzScene*>        SceneRemovals;          // Scenes that were removed during this game frame
00328     uint64                  LastFrameTime;          // The time at which the last frame ended
00329     void                    ProcessTouch(int touch);
00330     void                    ProcessEvents(CzScene *scane);
00331     CzActor*                FindTappedActor(int x, int y);
00332     void                    UpdateSystemTouches();
00333     void                    GetAppEasySettings();
00334     CzSlotArray<CzScene*>   SceneList;              // Temp scene list, stored here to provent many allocs / frees
00335 
00336 public:
00337     virtual void            Init(bool enable_http = false, bool gl_conmpatible = false);
00338     virtual void            Release();
00339     virtual bool            Update();
00340     virtual void            Draw();
00341     virtual void            PostDraw();         // Called by Draw() after all rendering is finished, but before the frame bugger is swapped
00342     virtual void            Save() {}
00343     virtual void            Load() {}
00344 
00345 public:
00346     void                    SetBackgroundColour(uint8 r, uint8 g, uint8 b, uint8 a);
00347     void                    BringSceneToFront(CzScene* scene);
00348     void                    DisableFocus();                                 // Disables focus for all scenes
00349     void                    SetAllTimelines(CzAnimTimeline* timeline);      // Sets the timelines of all active scenes
00350 
00351     // Implementation of IzXomlClass interface
00352     int                     LoadFromXoml(IzXomlResource* parebt, bool load_children, CzXmlNode* node);
00353 
00354     // Event handlers
00355     virtual void            NotifyOrientationChanged();                     // This event is called when the app receives an orientation changed event
00356     virtual void            NotifySizeChanged();                            // This event is called when the app window resizes
00357 
00358 };
00359 
00360 class CzAppCreator : public IzXomlClassCreator
00361 {
00362 public:
00363     CzAppCreator()
00364     {
00365         setClassName("app");
00366     }
00367     IzXomlResource* CreateInstance(IzXomlResource* parent);
00368 };
00369 
00370 /// @}
00371 
00372 #endif // _CZ_APP_H_