AppEasy Core SDK
1.5.0
Cross platform mobile and desktop app and game development SDK - The easy way to make apps
|
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_SCENE_H_) 00015 #define _CZ_SCENE_H_ 00016 00017 #include "CzSprite.h" 00018 #include "CzSpriteManager.h" 00019 #include "CzAnim.h" 00020 #include "CzImage.h" 00021 #include "CzUtil.h" 00022 #include "CzString.h" 00023 #include "CzXoml.h" 00024 #include "CzResources.h" 00025 #include "CzBox2d.h" 00026 #include "CzActions.h" 00027 #include "CzEvents.h" 00028 #include "CzModifier.h" 00029 #include "CzProgram.h" 00030 #include "CzXomlBindings.h" 00031 #include "CzUserProperty.h" 00032 #include "CzTimer.h" 00033 00034 class CzActor; 00035 class CzCamera; 00036 class IzScriptEngine; 00037 00038 /** 00039 @addtogroup Scenes 00040 @{ 00041 */ 00042 00043 /** 00044 @class CzScene 00045 00046 @brief A scene contains, handles and tracks a group of actors. 00047 00048 <h1>Introduction</h1> 00049 Sometimes understanding concepts are much easier when compared to something that one is already familiar with. In this case we will compare the making of apps and 00050 games using XOML to the making of a movie. In the movie business a scene (or set) is a place where the action takes place, it contains all of the actors, the cameras and 00051 scenery. Each actor has its own specific instructions, behaviours and purpose. 00052 00053 A CzScene is responsible for the following functionality: 00054 00055 - Setup and handling of the virtual canvas 00056 - Managing, updating and cleaning up actors 00057 - Managing, updating, rendering and cleaning up sprites 00058 - Managing and clean up of animation data 00059 - Managing and clean up of animation timelines 00060 - Managing and clean up of animation timers 00061 - Managing and clean up of events / actions 00062 - Managing and clean up of resources such as images and sounds 00063 - Managing and clean up of physics materials and shapes 00064 - Managing and clean up of XOML variables 00065 - Managing and clean up of XOML data bindings 00066 - Managing and clean up of XOML programs 00067 - Managing and clean up of scripts 00068 - Clipping sprites against the scenes visible extents 00069 - Updating the camera 00070 - Tracking actors that can potentially collide 00071 - Transitions between scenes 00072 - Updating the scenes animation timeline 00073 - Instantiating itself from XOML 00074 - Updating physics 00075 00076 Its worth noting at this point that any actors that live inside a scene will be transformed by the same transform as the scene, so if you move, scale or spin the scene then 00077 the contained actors will also move, scale and spin with it. The same can also be said about the base colour of the scene. 00078 00079 <h1>Creating a Scene</h1> 00080 00081 There are a number of ways to create a scene, lets take a look at some basic examples: 00082 00083 @par XOML Example: 00084 @code 00085 <?xml version="1.0"?> 00086 <xml> 00087 <Scene Name="AppScene" Current="true" CanvasSize="800, 480"> 00088 </Scene> 00089 </xml> 00090 @endcode 00091 00092 @par Code Example: 00093 @code 00094 CzScene* scene = new CzScene(); 00095 scene->setName("AppScene"); 00096 scene->setAllowFocus(true); 00097 scene->Init(); 00098 scene->setVirtualTransform(800, 400, 0, CzScene::Fit_None); 00099 APP->addScene(scene); 00100 APP->changeScene(scene); 00101 @endcode 00102 00103 @par Code Example (using macros): 00104 @code 00105 CZ_NEW_SCENE_CUSTOM(scene, "AppScene", 800, 400, CzScene::Fit_None); 00106 APP->addScene(scene); 00107 APP->changeScene(scene); 00108 @endcode 00109 00110 @par Lua Script Example: 00111 @code 00112 local my_scene = scene.create("Scene1", 800, 600, "best", "centre", false, false, "lua") 00113 scene.setCurrent(my_scene) 00114 @endcode 00115 00116 Here we create a scene called AppScene that has a canvas size of 800x480 pixels. Once a scene is set-up we can begin adding the actors and other content that form 00117 our game or app. 00118 00119 <h1>Scene Properties</h1> 00120 00121 Now that we know how to create a scene lets take a look at the extensive list of properties that scenes support: 00122 00123 <b>General Properties</b> 00124 - Name - The name of the scene is a very important property and should be chosen so that it does not conflict with names of other scenes that are present at the same time. 00125 It is possible to have multiple scenes with the same name, but only one of those scenes can be created. It is possible to destroy a scene and create a new scene of the same 00126 name. A scene name should also be memorable as you may want to refer to the scene from other areas of XOML or even script. 00127 - Type (whole number) - The type of scene is a simple number that can be used to identify specific scene types. For example, you could create multiple scenes that are all 00128 related and assign them all the same type. 00129 - Extents (x, y, width, height) - A rectangular area that describes the extents of the scenes world 00130 - AllowSuspend (boolean) - Determines if the scene can be suspended when other scenes are activated 00131 - Clipping (x, y, width, height) - A rectangular area that represents the visible area of the scene 00132 - ClipStatic (boolean) – When true camera transform is not applied to the clipping rect 00133 - Active - The active state of a scene determines if the scene is processing its contents. If not active then scene processing will stop and any content that is contained 00134 within the scene will also stop processing 00135 - Visible - If a scene is not visible then it will be hidden from view, although the scene and its contents will still be processed. 00136 - Layers (number, read-only) - The number of visible actor layers that the scene should use (10 is default value) 00137 - Current (boolean) - If true then the scene is made the current scene 00138 - Camera (camera name) - Current camera 00139 - Batch (boolean) - Tells the system to batch sprites for optimised rendering, default is disabled 00140 - AllowFocus (boolean) - If set to true then this scene will receive input focus events that the current scene would usually receive exclusively. This is useful if you have 00141 a HUD overlay that has functionality but it cannot be the current scene as the game scene is currently the current scene 00142 - Style (style) - Sets scene properties from a pre-defined style (see Style tag) 00143 - UserProperties (object, read-only) – The scenes user properties list 00144 - TargetX (actor name, write-only) – Sets the actor that the camera will be used to track its x-axis 00145 - TargetY (actor name, write-only) – Sets the actor that the camera will be used to track its y-axis 00146 - Bindings (bindings name) – Defines a bindings list that binds variables to scene properties. Reading this property will return a bindings object 00147 - TimeScale (number) – Changes the speed at which an attached timeline is played. This property cannot be set when initially declaring the scene 00148 00149 <b>Visual Properties</b> 00150 - Position (x, y) - The position of the scene on screen 00151 - PositionX (number) - The X position of the scene on screen 00152 - PositionY (number) - The Y position of the scene on screen 00153 - Angle (degrees) - The orientation of the scene in degrees 00154 - Scale (scale) - The scale of the scene 00155 - CanvasSize (x, y) - The virtual canvas size of the screen 00156 - CanvasFit (none, best, both, width, or height) - The type of method to use when scaling the canvas to the devices native screen resolution 00157 - CanvasOrigin (top, left, topleft or centre) - Where to locate the canvas origin 00158 - Layer (whole number)- The visual layer that this scene should be rendered on (Valid values are 0 to 9) 00159 - Colour (r, g, b, a)- The colour of the scene, Each component red, green, blue and alpha should be between the value of 0 and 255 (0 is no colour, whilst 255 is full colour) 00160 - Opacity (opacity) - The opacity of the scene (how see-through the scene appears). Opacity value ranges from 0 (invisible) to 255 (fully visible) 00161 - Timeline (timeline) - The time line that should be used to animate the scene. Reading this property will return a timeline object 00162 00163 <b>Event Properties</b> 00164 - OnSuspend (actions list) - Provides an actions group that is called when the scene is suspended 00165 - OnResume (actions list) - Provides an actions group that is called when the scene is resumed 00166 - OnCreate (actions list) - Provides an actions group that is called when the scene is created 00167 - OnDestroy (actions list) - Provides an actions group that is called when the scene is destroyed 00168 - OnKeyBack (actions list) - Provides an actions group that is called when the user presses the back key 00169 - OnKeyMenu (actions list) - Provides an actions group that is called when the user presses the menu key 00170 - OnOrientationChanged (actions list) - Provides an actions group that is called when the user changes the devices orientation 00171 - OnTick (actions list) - Provides an actions group that is called every time the scene is updated (30 to 60 times per second) 00172 - OnDeviceSuspended (actions list, write-only) - Provides an actions group that is called when the app is suspended 00173 - OnDeviceResumed (actions list, write-only) - Provides an actions group that is called when the app is resumed 00174 - OnDeviceVolumeUp (actions list, write-only) - Provides an actions group that is called when the volume up button was pressed 00175 - OnDeviceVolumeDown (actions list, write-only) - Provides an actions group that is called when the volume down button was pressed 00176 - OnDeviceNetworkOnline (actions list, write-only) - Provides an actions group that is called when the network goes online 00177 - OnDeviceNetworkOffline (actions list, write-only) - Provides an actions group that is called when the network goes offline 00178 00179 <b>Physical Properties</b> 00180 - Gravity (x, y) - Box2D directional world gravity 00181 - WorldScale (x, y) - Box2D world scale 00182 - DoSleep (boolean) - If set to true then actors that utilise physics will be allowed to sleep when they are not moving / interacting 00183 - Physics (boolean) - Enables or disables physics processing in the scene 00184 - PhysicsTimestep (number) - Sets how fast the physics engine updates the physics world. This value is specified in number of 1/60th's of a second. So passing a value of 00185 1 will update physics as though the frame rate of the app is running at 60 frames per second. A value of 3 would would update physics at a rate of 20 frames per second. 00186 Setting this to a value of 0 will use a variable time step based on the current frame rate of the app. The default value is 2.0 We will now take a closer look at how these 00187 properties function. 00188 00189 <h1>Scene Virtual Canvas</h1> 00190 Targeting a large selection of different phones, tablets and other devices with a variety of different screen sizes and aspect ratios can be difficult to manage. Luckily scenes 00191 can take care of this for you. A scene is quite clever in that it can render itself to any sized / configuration display using the virtual canvas concept. A virtual canvas is 00192 basically our own ideal screen size that we want to render to. The scene will scale and translate its visuals to fit our canvas onto the devices display allowing us to get 00193 on with developing our app using a static resolution. Lets take a look at an example that creates a scene with a virtual canvas of 800x480: 00194 00195 @code 00196 <xml> 00197 <!-- Create a scene to hold our game / app actors --> 00198 <Scene Name="AppScene" Current="true" CanvasSize="800, 480" CanvasFit="best" > 00199 <!-- Actors go here --> 00200 </Scene> 00201 </xml> 00202 @endcode 00203 00204 Using the AppScene above regardless of whatever size screen we run our app on our content will be scaled to fit an 800x480 pixel display resolution because we specified 00205 a CanvasSize of "800x480". We also told the scene to use its best judgement when scaling the scene by specifying CanvasFit="best". As an example if we run our app on an 00206 iPhone retina display at 960x640 pixels and using a canvas fit method of best fit then our app will be scaled to best fit the 960x640 resolution, which would be 960x576 00207 pixels. The scene would be centred on the screen leaving a little space at the top and bottom of the display. 00208 00209 Scenes support a number of ways of fitting the the virtual canvas to the device screen including: 00210 - none - No scaling is performed. This is ideal if you want a 1:1 pixel ratio and mostly used by apps and games that use proportional sizing and positioning. 00211 - both - The scene is scaled to fit the exact size of the display ignoring aspect ratio (aspect ratio is the ratio of the screens width to the screens height). The problem 00212 with this method of scaling is that the scene will be stretched on displays with different aspect ratios which can make certain apps look odd. 00213 - best - The scene is scaled to best fit the devices display size whilst maintaining aspect ratio. This method of scaling prevents stretched graphics, however it can leave 00214 gaps to the top and bottom or left and right 00215 - width / height - This method of scaling is the same as best except scaling is locked to either the width or the height. 00216 00217 Scenes also provide a way to allow you to specify how coordinates are interpreted allowing you to place content relative to either the centre, left, top-left or top of the 00218 screen. The scenes origin is by default set to the centre of the scene, but it can be changed by supplying the CanvasOrigin property when declaring the scene, e.g.: 00219 00220 @code 00221 <Scene Name="AppScene" Current="true" CanvasOrigin="topleft"> 00222 @endcode 00223 00224 This scene places actors relative to the top-left hand corner of the scene instead of relative to the centre. However, we find that having the scenes origin at the centre 00225 is much better and feels more natural when designing apps an games for a variety of screen sizes as content can be allowed to extend from the centre and beyond the scenes 00226 size. The default canvas size for a scene is the devices native display resolution 00227 00228 <h1>Scene Layers and Actor Layering</h1> 00229 Because an app can contain multiple scenes that are visible at the same time, its sometimes required to sort the order in which these scenes are displayed. For 00230 example, you may have a game that has a background layer, a game object layer and a foreground layer. The visibility ordering of these scenes will be displayed in the 00231 opposite order to which you create them. For example, if you create the foreground scene and then the background scene afterwards then the background scene will be 00232 drawn on top of the foreground scene, which is the wrong way around. You could remedy this problem by adding the background scene first, but sometimes this is not 00233 practical. To eliminate the problem, scenes can be placed on a layer of depth by setting the Layer="layer number" (layer numbers 0 to 9 are supported with 9 being 00234 the highest layer) property of the scene. Scenes that are placed on the same layer will be sorted in the order they were added, with more recent additions appearing 00235 on top. Each scene also determines how actors are sorted in a scene (we will discuss actors in great depth later). We can define how many layers are available for 00236 the actor system to use by adding the Layers="number of layers" property to the scene definition. The default is 16 which is fine for most applications. 00237 00238 An example has been provided that demonstrates scene layering (see SceneLayers example). Lets take a quick look at an extract of the XOML for this example: 00239 @code 00240 <!-- Create scene 1 on highest layer --> 00241 <Scene Name="Scene1" Layer="9"> 00242 </Scene> 00243 <!-- Create scene 2 on middle layer --> 00244 <Scene Name="Scene2" Layer="5"> 00245 </Scene> 00246 <!-- Create scene 3 on highest layer --> 00247 <Scene Name="Scene3" Layer="0"> 00248 </Scene> 00249 @endcode 00250 00251 Here we create 3 scenes and assign each one a different layer. 00252 00253 <h1>Current Scene</h1> 00254 An app generally consists of a number of different scenes that all contain their own objects. However only one scene can normally receive input. This scene is 00255 known as the current or focus scene. When you declare a scene it can be made to be the current scene by adding Current="true" to the scene definition. By 00256 comparison, you could think of all of the different windows on your PC or Mac as separate scenes, but only one of those windows can usually accept user input 00257 at one time. When a scene is made current it is brought to the front of any scenes that are on the same layer (more on layers later). 00258 00259 It is also possible to mark scenes so that more than one scene can receive input events at the same time. This can be useful if you have two scenes on lower 00260 layers that need to also receive input. For example, you may have a heads-up display that has functional buttons on the highest layer and the game scene on a 00261 lower layer that also needs to receive input events. To mark a scene to receive input events you need to add the AllowFocus="true" attribute to your scene 00262 definition. 00263 00264 <h1>Suspending and Resuming Scenes</h1> 00265 By default all active scenes in your app will be processed continuously. However this is not always required and for apps that contain a lot of active scenes 00266 it can slow down your app. Scenes can be suspended so that they stop processing (they will still be visible). In fact, scenes that allow suspending / resuming 00267 are automatically suspended when you change the current scene to another scene. To enable suspending / resuming for a scene you should set its 00268 AllowSuspend="true" property. When a scene is suspended or resumed it will fire a suspend or resume event which allows you to perform actions (more on this later). 00269 00270 The follownig two methods handle scene suspend / resume: 00271 - NotifySuspending(CzScene* new_scene); - This event is called when this scene is being suspended 00272 - NotifyResuming(CzScene* old_scene); - This event is called when this scene is being resumed 00273 00274 Like all event handlers its possible to override these if you derive your own scene class from CzScene and handle these events as you see fit. 00275 00276 Scenes can be suspended and resumed using the following actions in XOML: 00277 - ChangeScene - The previous scene will be suspended and the new scene will be resumed (provided that the scenes have AllowSuspend="true" defined) 00278 - SuspendScene - Suspends the scene even if suspend / resume is disabled for the scene 00279 - ResumeScene - Resumes the scene even if suspend / resume is disabled for the scene 00280 00281 <h1>Scene Extents</h1> 00282 Scenes can be as large or as small as you like, they can even be many times larger than the devices screen size. This enables you to create content that exists 00283 in a much larger space than the screen can display. You can then allow the user to pan around the scene to view more of the content. The size of your scenes 00284 world can be set using the Extents property which accepts 4 values which represent the rectangular area that objects can exist in. Actors can be made so that 00285 when they hit the edge of the scenes extents they re-appear on the opposite side (much like asteroids in the old Atari game Asteroids does), this is called 00286 wrapping. An example called SceneExtents has been provided that shows an actor moving across the scene, it wraps back around to the opposite end of the scene 00287 when it hits the scenes extents. Lets take a quick look at the XOML for this example: 00288 00289 @code 00290 <!-- Create scene with narrow extents --> 00291 <Scene Name="Scene1" Extents="-100, -100, 200, 200" Current="true"> 00292 <!-- Create a label object --> 00293 <Label Font="serif" Background="Button1Brush" BackgroundColour="255, 80, 80, 255" Text="Wrapping" WrapPosition="true" OnTick="Update"> 00294 <Actions Name="Update"> 00295 <Action Method="AddProperty" Param1="Position" Param2="2, 2" /> 00296 </Actions> 00297 </Label> 00298 </Scene> 00299 @endcode 00300 00301 This example introduces some concepts that you have not yet met such as actions and events. You do not need to worry about these for now as we are only focusing 00302 on the bold sections. Note how we have set the scenes extents using Extents="-100, -100, 200, 200". This collection of 4 value represent the left, top, width 00303 and height of the scenes extents. Also note that our Label has been given a new property called WrapPosition which is set to true. This tells the Label actor 00304 to wrap its position when it hits the edges of the scenes extents. 00305 00306 <h1>Scene Clipping</h1> 00307 When a scene is smaller than the devices display size its sometimes useful to be able to ensure that anything that is drawn within the scene is not drawn outside 00308 the scene. To accomplish this we need to clip content that falls outside the scenes area of coverage. To tell a Scene to clip its contents you add the Clipping 00309 property which takes 4 values (left, top, width, height). An example has been provided called SceneClipping which demonstrates how to apply scene clipping. Lets 00310 take a quick look at this example: 00311 00312 @code 00313 <!-- Create scene with narrow extents --> 00314 <Scene Name="Scene1" Extents="-100, -100, 200, 200" Clipping="-100, -100, 200, 200" Current="true" /> 00315 @endcode 00316 00317 In this example we limit the scenes extents and set the clipping area to -100, -100, 200, 200. By default the scenes clipping area will move around with the 00318 scene, although you can modify this behaviour to force the clipping area to remain in place on screen. To force clipping to remain in place add 00319 ClipStatic="true" to your scene definition. 00320 00321 <h1>Scene Events</h1> 00322 00323 An event is something that occurs in your app that you may or may not want to know about and act upon. For example, when the scene is first created the 00324 OnCreate event is raised. Using actions (explained later) you can react to these events and modify your apps behaviour. 00325 00326 Internally the scene handles events using the Events Manager (EventsManager). This manager handles the mapping between events and actions lists that should 00327 be called when the events take place. To add an event to the events manager we call 00328 EventsManager->addEvent(event_name, action_list_name), e.g: 00329 00330 @code 00331 EventsManager->addEvent("OnSuspend", "SuspendedActions"); 00332 @endcode 00333 00334 This example ties the SuspendedActions list that was previously defined in XOML using the Actions tag to the OnSuspend event. When an event takes place 00335 the corresponding event notification method of CzScene is called, for example NotifyCreate() is called when the scene is created. This in turn calls 00336 CzScene::ProcessEventActions() to handle the event. ProcessEventActions examines its list of subscribed events to see if the event has been subsribed to. 00337 If it has then the corresponding actions list will be called. if you do not wish to use actions lists to respond to events then yuo can simply derive 00338 your own sceen class from CzScene, override the event notification methods such as NotifyCreate() and write your own implementation. 00339 00340 You can listen to events by adding an OnEvent property to the scene. Lets take a look at the list of events that can occur in a scene: 00341 - OnSuspend - This event is raised when the scene is suspended. A scene is suspended if it is the current scene and another scene becomes the current 00342 scene (for scenes that support suspend / resume) or when a SuspendScene action is called 00343 - OnResume - This event is raised when a scene is resumed (become the current scene) or when a ResumeScene action is called 00344 - OnCreate - This event is raised when the scene is first created and gives you the opportunity to handle post scene creation logic. You could use this 00345 event to for example play a sound or hide another scene etc.. 00346 - OnDestroy - This event is raised when the scene is about to be destroyed, this gives you the opportunity to clean-up certain elements 00347 - OnKeyBack - This event is raised when the user pressed the back button 00348 - OnKeyMenu - This event is raised when the user pressed the menu button 00349 - OnOrientationChanged - This event is raised when the devices orientation changes an allows yo to for example modify the scenes layout. 00350 - OnTick - This event handler is raised every time the scene is updated (30 to 60 times per second) 00351 - OnDeviceSuspended (actions list, write-only) - Provides an actions group that is called when the app is suspended 00352 - OnDeviceResumed (actions list, write-only) - Provides an actions group that is called when the app is resumed 00353 - OnDeviceVolumeUp (actions list, write-only) - Provides an actions group that is called when the volume up button was pressed 00354 - OnDeviceVolumeDown (actions list, write-only) - Provides an actions group that is called when the volume down button was pressed 00355 - OnDeviceNetworkOnline (actions list, write-only) - Provides an actions group that is called when the network goes online 00356 - OnDeviceNetworkOffline (actions list, write-only) - Provides an actions group that is called when the network goes offline 00357 00358 The SceneEvents example shows an example showing how to handle the scenes OnTick event. 00359 00360 <h1>Scene Animation Properties</h1> 00361 Scenes can be animated using animations defined in XOML. Whilst not all properties of a scene can be animated many can including: 00362 - Position (x, y) - The position of the scene on screen 00363 - Angle (degrees) - The orientation of the scene in degrees 00364 - Scale (scale) - The scale of the scene 00365 - Colour (r, g, b, a)- The colour of the scene, Each component red, green, blue and alpha should be between the value of 0 and 255 (0 is no colour, whilst 255 00366 is full colour) 00367 - Opacity (opacity)- The opacity of the scene (how see-through the scene appears). Opacity value ranges from 0 (invisible) to 255 (fully visible) 00368 - Clipping (x, y, width, height) - The clipping area of the scene 00369 - Visible (boolean) - The visibility of the scene 00370 - Timeline (timeline) - The animation timeline that is currently being used to animate the scene. Timelines are great for creating complex scene transitions 00371 - Camera (camera) - The current camera that is viewing the scene The SceneAnimation example has been provided that shows how to apply animation to a scene 00372 - PhysicsTimestep (number) - This can be used to modify the speed at which physics updates allowing you to slow down or speed up physics. 00373 00374 <h1>Scene Modifiable Properties</h1> 00375 Scenes can be queried and modified after their creation using actions, command and scripts. The following properties are includes: 00376 00377 <b>General Properties</b> 00378 - Name (string) 00379 - Type (number) 00380 - Extents (x, y, width, height) 00381 - AllowSuspend (boolean) 00382 - Clipping (x, y, width, height) 00383 - Active (boolean) 00384 - Visible (boolean) 00385 - Current (boolean) 00386 - Camera (camera) 00387 - AllowFocus (boolean) 00388 00389 <b>Visual Properties</b> 00390 - Layer (number) 00391 - Colour (r, g, b, a) 00392 - Opacity (opacity) 00393 - Timeline (timeline) 00394 - TimeScale (number) - Speed at which to play back any attached timelines 00395 00396 <b>Event Properties</b> 00397 - OnCreate (actions list) 00398 - OnDestroy (actions list) 00399 - OnKeyBack (actions list) 00400 - OnKeyMenu (actions list) 00401 - OnOrientationChanged (actions list) 00402 - OnTick (actions list) 00403 00404 <b>Physical Properties</b> 00405 - Gravity (x, y) 00406 - WorldScale (x, y) 00407 00408 <h1>Scene Rendering</h1> 00409 A scene has no actual visible component, instead it renders all of its contained actor objects. All actor objects are moved, rotated and scaled by the 00410 same amount that the scene is. For example, if you move the scene to the left, all contained actors will also moved to the left. If you rotate the scene 00411 to the right 45 degrees then all actors also rotate 45 degrees to the right. In addition, all scene actors will be scaled by the scenes colour and opacity 00412 settings, so if you fad down the scene all actors will also fade down with it. 00413 00414 Scenes support a method of optimisation known as batch rendering. This is the process of combining actors together at render time to render them all in one go, 00415 which can substantially speed up rendering for scenes that contain many actors. To force a scene to use batch rendering add Batch="true" to the scene definition. 00416 However, scenes that are marked as batch enabled will not sort their contained actors properly with the likes of fonts, so you will need to use actor layers 00417 to force the sorting. 00418 00419 When a scene is hidden from view, all of its actors will also be hidden. When a scene is deleted all of its actors are also deleted. 00420 00421 <h1>Scene Cameras</h1> 00422 A camera is a view into the scene from a specific position, angle and scale. In order to move, rotate and scale a scene a camera should be created and 00423 attached to it. To create a camera you use the Camera XOML tag: 00424 00425 @code 00426 <!-- Create a camera --> 00427 <Camera Name="Camera1" /> 00428 @endcode 00429 00430 To attach the camera to the scene we add the Camera attribute to the scene definition: 00431 @code 00432 <Scene Name="Scene1" Current="true" Camera="Camera1"> 00433 @endcode 00434 00435 When we navigate a scene we are actually moving the camera view within the scene. When we move the camera objects tend to move in the opposite direction, for 00436 example moving the camera left moves the scene actors move to the right. If you think about how a real camera works, when you move the camera in one direction 00437 the view seen by the camera moves in the opposite direction. 00438 00439 Its possible to create a number of different cameras and switch between them to offer different views into the scene. Cameras offer a great out of the box 00440 feature called touch panning, which enables the user to pan the camera a round a scene on the x and y axis by dragging their finger around the scene. Take a 00441 look at the ScenePanning example for an example showing how to use touch panning. 00442 00443 <h1>Scene Augmentation</h1> 00444 A scene once declared in XOML can later be updated / augmented with additional XOML elsewhere. For example, lets say that you declare some common scene that 00445 contains a basic background and some other elements that are common across a number of screens. You can later load the scene and then augment it by declaring 00446 the scene again supplying the additional elements inside the newly declared scene: 00447 00448 @code 00449 <Scene Name="CommonScene" ............ > 00450 <Original_Element1 /> 00451 <Original_Element2 /> 00452 <Original_Element3 /> 00453 </Scene> 00454 @endcode 00455 00456 Now declare a 2nd scene with the same name: 00457 @code 00458 <Scene Name="CommonScene"> 00459 <Extra_Element1 /> 00460 <Extra_Element2 /> 00461 <Extra_Element3 /> 00462 </Scene> 00463 @endcode 00464 00465 In memory the scene now looks like this: 00466 @code 00467 <Scene Name="CommonScene" ............ > 00468 <Original_Element1 /> 00469 <Original_Element2 /> 00470 <Original_Element3 /> 00471 <Extra_Element1 /> 00472 <Extra_Element2 /> 00473 <Extra_Element3 /> 00474 </Scene> 00475 @endcode 00476 00477 For a working example of augmented scenes take a look at the SceneAugmentation example. 00478 00479 <h1>Scene Physics</h1> 00480 XOML scenes can run actors that are under the influence of the Box2D physics systems. The scene system allows you to specify some scene global information 00481 about the physics simulation. The following scene global properties can be set by adding the relevant tags to the scene definition: 00482 - Gravity (x, y) - Box2D directional world gravity. Box2D uses directional gravity which means you can have gravity act in any direction. 00483 - WorldScale (x, y) - Box2D world scale. This value determines how your visible world scales to the Box2D world (default is 10, 10) 00484 - DoSleep (boolean) - If set to true then actors that utilise physics will be allowed to sleep when they are not moving / interacting, this can help speed up 00485 scenes that contain many actors that are under Box2D control. 00486 - Physics (boolean) - Enables or disables physics processing in the scene, enabled by default. Disable in scenes that do not use physics to maximise performance. 00487 - PhysicsTimestep (number) - Sets how fast the physics engine updates the physics world. This value is specified in number of 1/60th's of a second. So passing 00488 a value of 1 will update physics as though the frame rate of the app is running at 60 frames per second. A value of 3 would would update physics at a rate of 00489 20 frames per second. Setting this to a value of 0 will use a variable time step based on the current frame rate of the app. The default value is 2.0 To see 00490 how scene physics is used please take a look at the ScenePhysics example. 00491 00492 <h1>Creating a Custom Scene</h1> 00493 00494 Generally and for the most part a standard CzScene can be used to accomplish most things with AppEasy scenes, especiallu if devloping most if not all of your aoo in 00495 XOML / Lua, however for those that require a little more control its possible to derive your own scene from CzScene and implement the supplied virtual methods to 00496 override certain functionality. The main 3 methods that you should consider re-implementing includes: 00497 - int Init(int max_collidables = 128, int max_layers = 10, bool doSleep = true) 00498 - void Update(float dt) 00499 - void Draw() 00500 00501 Here's a quick example: 00502 00503 @code 00504 class MyGameScene : public CzScene 00505 { 00506 public: 00507 MyGameScene() : CzScene() {} 00508 virtual ~MyGameScene(); 00509 00510 int Init(int max_collidables = 128, int max_layers = 10, bool doSleep = true) 00511 { 00512 CzScene::Init(max_collidables, max_layers, doSleep); 00513 } 00514 void Update(float dt) 00515 { 00516 CzScene::Update(dt); 00517 } 00518 void Draw() 00519 { 00520 CzScene::Draw(); 00521 } 00522 }; 00523 @endcode 00524 00525 00526 We have provided a very basic implementation of Init(), Update() and Draw() which call the base CzScene class methods so we keep its functionality in-tact. 00527 00528 You can take the implementation one step further (or maybe two) by implementing both the IzXomlResource and IzAnimTarget interfaces to allow instantiation of your custom class from XOML 00529 and to allow your class to be a target for animation time lines. 00530 00531 Firstly lets take a look at XOML enabling your custom scene class. To get AppEasy to recognise your class whilst parsing XOML files you need to do a few things: 00532 - Derive your class from IzXomlResource and implement the LoadFromXoml method 00533 - Create a class creator that creates an instance of your class then add this to the XOML engine 00534 00535 Lets start by taking a look at step 1. 00536 00537 Because we have derived our class from CzScene (which is derived from IzXomlResource) we already have the support for step 1. However we would like to insert our own custom attribute 00538 tags so we need to make a few changes. 00539 00540 Lets take a look at our new class with those changes: 00541 00542 @code 00543 class MyGameScene : public CzScene 00544 { 00545 public: 00546 // Properties 00547 protected: 00548 float Gravity; 00549 public: 00550 void setGravity(float gravity) { Gravity = gravity; } 00551 float getGravity() const { return Gravity; } 00552 // Properties End 00553 public: 00554 MyGameScene() : CzScene(), Gravity(10.0f) {} 00555 virtual ~MyGameScene(); 00556 00557 virtual int Init(int max_collidables = 128, int max_layers = 10, bool doSleep = true) 00558 { 00559 CzScene::Init(max_collidables, max_layers, doSleep); 00560 } 00561 virtual void Update(float dt) 00562 { 00563 CzScene::Update(dt); 00564 } 00565 virtual void Draw() 00566 { 00567 CzScene::Draw(); 00568 } 00569 00570 // Implementation of IzXomlResource interface 00571 bool LoadFromXoml(IzXomlResource* parent, bool load_children, CzXmlNode* node) 00572 { 00573 if (!CzScene::LoadFromXoml(parent, load_children, node)) 00574 return false; 00575 00576 // Add our own custom attribute parsing 00577 for (CzXmlNode::_AttribIterator it = node->attribs_begin(); it != node->attribs_end(); it++) 00578 { 00579 unsigned int name_hash = (*it)->getName().getHash(); 00580 00581 if (name_hash == CzString::CalculateHash("Gravity")) 00582 { 00583 setGravity((*it)->getValueAsFloat()); 00584 } 00585 } 00586 00587 return true; 00588 } 00589 }; 00590 @endcode 00591 00592 Our new class now basically supports a Gravity attribute that we will eventually be able to set in XOML using something like: 00593 00594 @code 00595 <MyGameScene Name="GameScene" Gravity="9.8"> 00596 </MyGameScene> 00597 @endcode 00598 00599 However, before we can do that we need to let the XOML system know about our new type of class (MyGameScene), so it can be instantiated when the XOM parser comes across it. To do 00600 this we need to create a creator: 00601 00602 @code 00603 class MyGameSceneCreator : public IzXomlClassCreator 00604 { 00605 public: 00606 MyGameSceneCreator() 00607 { 00608 setClassName("MyGameScene"); 00609 } 00610 IzXomlResource* CreateInstance(IzXomlResource* parent) { return new MyGameScene(); } 00611 }; 00612 @endcode 00613 00614 The creator basically defines the tag name "MyGameScene" and returns an instance of the MyGameScene class when CreateInstance() is called. 00615 00616 To get the XOML system to recognise our creator we need to add it to the XOML parsing system using: 00617 00618 @code 00619 // Add custom MyGameScene to XOML system 00620 CZ_XOML->addClass(new MyGameSceneCreator()); 00621 @endcode 00622 00623 Now XOML integration is out of the way, lets take a quick look at enabling our class as an animation target. 00624 00625 To enable a class as an animation target we derive it from IzAnimTarget and implement the UpdateFromAnimation() method. Luckily we derived our MyGameScene class from the CzScene 00626 class which already provides this functionality. Lets take a quick look at how we extend the animation update method to account for animating our gravity variable. 00627 00628 @code 00629 bool MyGameScene::UpdateFromAnimation(CzAnimInstance *animation) 00630 { 00631 if (CzScene::UpdateFromAnimation(animation)) 00632 return true; 00633 00634 // Add our own custom animating property 00635 unsigned int property_name = animation->getTargetPropertyHash(); 00636 00637 if (property_name == CzString::CalculateHash("Gravity")) 00638 { 00639 CzAnimFrameFloat* frame = (CzAnimFrameFloat*)animation->getCurrentData(); 00640 setGravity(frame->data); 00641 return true; 00642 } 00643 00644 return false; 00645 } 00646 @endcode 00647 00648 We added the above code to our MyGameScene class definition. We begin by calling the base UpdateFromAnimation() method so we can keep the existing animation properties of the 00649 scene. We then add our own custom check for the Gravity variable. If the animation property matches Gravity then we set the gravity to the provided interpolated value. 00650 00651 00652 00653 00654 */ 00655 00656 class CzScene : public IzXomlResource, public IzAnimTarget 00657 { 00658 public: 00659 // Public access to actor iteration 00660 typedef CzList<CzActor*>::iterator _Iterator; 00661 _Iterator begin() { return Actors.begin(); } 00662 _Iterator end() { return Actors.end(); } 00663 00664 /** 00665 @enum eCanvasFit 00666 00667 @brief Values that define how the virtual canvas is fit to the devices screen. 00668 */ 00669 enum eCanvasFit 00670 { 00671 Fit_None, ///< No scaling is performed. This is ideal if you want a 1:1 pixel ratio and mostly used by apps and games that use proportional sizing and positioning. 00672 Fit_Width, ///< This method of scaling is the same as best except scaling is locked to the width 00673 Fit_Height, ///< This method of scaling is the same as best except scaling is locked to the height 00674 Fit_Both, ///< The scene is scaled to fit the exact size of the display ignoring aspect ratio (aspect ratio is the ratio of the screens width to the screens height). The problem with this method of scaling is that the scene will be stretched on displays with different aspect ratios which can make certain apps look odd. 00675 Fit_Best, ///< The scene is scaled to best fit the devices display size whilst maintaining aspect ratio. This method of scaling prevents stretched graphics, however it can leave gaps to the top and bottom or left and right 00676 }; 00677 00678 /** 00679 @enum eCanvasOrigin 00680 00681 @brief Values that define the placement of teh canvas origin. 00682 */ 00683 enum eCanvasOrigin 00684 { 00685 Origin_Centre, ///< Origin is at the centre of the screen 00686 Origin_Top, ///< Origin is at the top centre of the screen 00687 Origin_Left, ///< Origin is at the left middle of the screen 00688 Origin_TopLeft, ///< Origin is at the rop left of the screen 00689 }; 00690 00691 /** 00692 @enum eOrientation 00693 00694 @brief Values that define the orientation of the screen. 00695 */ 00696 enum eOrientation 00697 { 00698 Orientation_Landscape, ///< Landscape orientation (width > height) 00699 Orientation_Portrait, ///< Portrait orientation (width < height) 00700 }; 00701 00702 /** 00703 @enum eDocking 00704 00705 @brief Values that define acor docking. 00706 */ 00707 enum eDocking 00708 { 00709 Dock_None, 00710 Dock_Top, 00711 Dock_Bottom, 00712 Dock_Left, 00713 Dock_Right, 00714 Dock_TopLeft, 00715 Dock_TopRight, 00716 Dock_BottomLeft, 00717 Dock_BottomRight, 00718 }; 00719 static const char* DockingNames[]; 00720 00721 00722 // Properties 00723 protected: 00724 CzSpriteManager* SpriteManager; ///< Manages sprites for the whole scene 00725 CzXomlResourceManager* ResourceManager; ///< Manages all types of resources 00726 CzXomlVariableManager* VariableManager; ///< Manages XOML variables 00727 CzActionsManager* ActionsManager; ///< Manages actions 00728 CzAnimTimelinesManager* TimelinesManager; ///< Manages timelines 00729 CzTimersManager* TimersManager; ///< Manages attached timers 00730 CzProgramManager* ProgramManager; ///< Manages the execution of a collection of programs 00731 CzXomlBindings* Bindings; ///< Manages any attached bindings 00732 IzScriptEngine* ScriptEngine; ///< Mnaages scene scripts 00733 CzApp* Parent; ///< Parent game controller 00734 int Type; ///< Type of scene 00735 eOrientation Orientation; ///< Current display orientation 00736 CzIVec2 ScreenSize; ///< Native screen size 00737 CzIVec2 VirtualSize; ///< The virtual size is not the actual size of the scene. but a static pretend size that we can use to render to without having to cater for different sized displays 00738 CzMatrix3 VirtualTransform; ///< Virtual transform is used to scale, translate and rotate scene to fit different display sizes and orientations 00739 CzMatrix3 Transform; ///< Scene transform 00740 CzList<CzActor*> Actors; ///< Collection of scene actors 00741 CzIRect Extents; ///< Extents of scenes world 00742 CzCamera* Camera; ///< Current camera 00743 CzActor** Collidables; ///< List of collidable objects built this frame 00744 bool AllowSuspend; ///< If true then this processing of this scene will be suspended when the scene manager changes away from this scene 00745 CzIRect ClippingArea; ///< A rectangular area of the virtual screen that this scene will be clipped to 00746 bool ClipStatic; ///< If ClipStatic is true then camera transform is not applied to clipping rect 00747 bool IsActive; ///< Active state of scene 00748 bool IsVisible; ///< Visible state of scene 00749 bool AllowFocus; ///< If true this scene can receive input events if it is not the current scene 00750 CzColour Colour; ///< Colour 00751 CzAnimTimeline* Timeline; ///< Timeline, controls animation of the scene 00752 int RunCount; ///< NUmber of times the scene has been updated 00753 bool Destroyed; ///< A scene is marked asd destroyed when it has been marked for deletion 00754 int Layer; ///< Scenes layer 00755 bool PhysicsEnabled; ///< True if physics is enabled in this scene 00756 bool IsDragging; ///< True if user is draggnig their finger on the scene 00757 bool PrevTouching; ///< True if user is draggnig their finger on the scene 00758 CzVec2 PrevTouchVel; ///< Previous touch velocity 00759 float ConstantTimeStep; ///< If 0 then time step will be calculated from frame rate 00760 CzBox2dWorld* Box2dWorld; 00761 CzUserPropertyList* UserPropertyList; ///< User properties list 00762 CzEventManager* EventsManager; ///< List of events that the scene handles 00763 CzModifierManager* Modifiers; ///< Class modifiers manager 00764 public: 00765 00766 void setParent(CzApp* parent) { Parent = parent; } 00767 CzApp* getParent() { return Parent; } 00768 CzSpriteManager* getSpriteManager() { return SpriteManager; } 00769 CzXomlResourceManager* getResourceManager() { return ResourceManager; } 00770 CzActionsManager* getActionsManager() { return ActionsManager; } 00771 CzAnimTimelinesManager* getTimelinesManager() { return TimelinesManager; } 00772 CzTimersManager* getTimersManager() { return TimersManager; } 00773 CzXomlVariableManager* getVariableManager() { return VariableManager; } 00774 CzProgramManager* getProgramManager() { return ProgramManager; } 00775 IzScriptEngine* getScriptEngine() { return ScriptEngine; } 00776 void setScriptEngine(const CzString& type); 00777 void setType(int type) { Type = type; } 00778 int getType() const { return Type; } 00779 CzIVec2 getScreenSize() const { return ScreenSize; } 00780 eOrientation getOrientation() const { return Orientation; } 00781 CzIVec2 getVirtualSize() const { return VirtualSize; } 00782 void setVirtualTransform(int required_width, int required_height, float angle, bool fix_aspect = false, bool lock_width = false, eCanvasOrigin canvas_origin = Origin_Centre); 00783 void setVirtualTransform(int required_width, int required_height, float angle, eCanvasFit fit = Fit_Best, eCanvasOrigin canvas_origin = Origin_Centre); 00784 CzMatrix3& getVirtualTransform() { return VirtualTransform; } 00785 CzMatrix3& getTransform() { return Transform; } 00786 void setCanvasFit(eCanvasFit fit) { CanvasFit = fit; } 00787 eCanvasFit getCanvasFit() const { return CanvasFit; } 00788 eCanvasOrigin getCanvasOrigin() const { return CanvasOrigin; } 00789 void setCanvasOrigin(eCanvasOrigin org) { CanvasOrigin = org; } 00790 CzList<CzActor*>& getActors() { return Actors; } 00791 void addActor(CzActor *actor); 00792 void removeActorNoDelete(CzActor* actor); 00793 void removeActor(CzActor* actor, bool instant_delete = false); 00794 void removeActor(unsigned int name_hash, bool instant_delete = false); 00795 void removeActors(unsigned int tag_hash); 00796 void fixActors(CzActor* parent); 00797 void removeLinkedActors(CzActor* actor, bool instant_delete = false); 00798 bool findActorInRemovals(CzActor* actor); 00799 int checkActorState(CzActor* actor); 00800 CzActor* findActor(const char* name); 00801 CzActor* findActor(unsigned int name_hash); 00802 CzActor* findActor(int type); 00803 int findActors(unsigned int tag_hash, CzVector<CzActor*>& actors); 00804 int findActorsOfType(int type, CzVector<CzActor*>& actors); 00805 CzActor* findClosestActor(int x, int y, int type); 00806 CzActor* findFurthestActor(int x, int y, int type); 00807 void clearActors(bool ignore_visuals = false); 00808 void setExtents(int x, int y, int w, int h) { Extents.x = x; Extents.y = y; Extents.w = w; Extents.h = h; } 00809 CzIRect getExtents() const { return Extents; } 00810 void setCamera(CzCamera* camera); 00811 CzCamera* getCamera() { return Camera; } 00812 bool addCollideable(CzActor* actor); 00813 CzActor** getCollidables() { return Collidables; } 00814 int getTotalCollidables() const { return NextFreeCollidable; } 00815 void setAllowSuspend(bool allow_suspend) { AllowSuspend = allow_suspend; } 00816 bool getAllowSuspend() const { return AllowSuspend; } 00817 void setClippingArea(int x, int y, int w, int h) { ClippingArea.x = x; ClippingArea.y = y; ClippingArea.w = w; ClippingArea.h = h; } 00818 CzIRect getClippingArea() { return ClippingArea; } 00819 void setClipStatic(bool is_static) { ClipStatic = is_static; } 00820 bool isClipStatic() const { return ClipStatic; } 00821 void setActive(bool active) { IsActive = active; } 00822 bool isActive() const { return IsActive; } 00823 void setVisible(bool visible) { IsVisible = visible; } 00824 bool isVisible() const { return IsVisible; } 00825 void setColour(const CzColour& colour) { Colour = colour; } 00826 void setColour(uint8 r, uint8 g, uint8 b, uint8 a) { Colour.r = r; Colour.g = g; Colour.b = b; Colour.a = a; } 00827 void setOpacity(int opacity) { Colour.a = opacity; } 00828 int getOpacity() const { return Colour.a; } 00829 CzColour getColour() const { return Colour; } 00830 void setTimeline(CzAnimTimeline* timeline) { Timeline = timeline; } 00831 CzAnimTimeline* getTimeline() { return Timeline; } 00832 void setBindings(CzXomlBindings* bindings) { Bindings = bindings; } 00833 CzXomlBindings* getBindings() { return Bindings; } 00834 void setRunCount(int count) { RunCount = count; } 00835 int getRunCount() const { return RunCount; } 00836 void setDestroyed(bool destroyed) { Destroyed = destroyed; } 00837 bool isDestroyed() const { return Destroyed; } 00838 void setAllowFocus(bool allow) { AllowFocus = allow; } 00839 bool getAllowFocus() const { return AllowFocus; } 00840 void setLayer(int layer) { Layer = layer; } 00841 int getLayer() const { return Layer; } 00842 bool isDragging() const { return IsDragging; } 00843 void setConstantTimeStep(float step) { ConstantTimeStep = step; } 00844 float getConstantTimeStep() const { return ConstantTimeStep; } 00845 CzBox2dWorld* getBox2dWorld() { return Box2dWorld; } 00846 void setUserPropertyList(CzUserPropertyList* props) { SAFE_DELETE(UserPropertyList) UserPropertyList = props; } 00847 CzUserPropertyList* getUserPropertyList() { return UserPropertyList; } 00848 CzEventManager* getEventsManager() { return EventsManager; } 00849 void setModifiers(CzModifierManager* mods) { Modifiers = mods; } 00850 CzModifierManager* getModifiers() { return Modifiers; } 00851 bool setProperty(const char* property_name, const CzString& data, bool delta); 00852 virtual bool setProperty(unsigned int property_name, const CzString& data, bool delta); 00853 virtual bool setProperty(unsigned int property_name, const CzXomlProperty& data, bool delta); 00854 bool getProperty(const char* property_name, CzXomlProperty& prop); 00855 virtual bool getProperty(unsigned int property_name, CzXomlProperty& prop); 00856 void setBatching(bool enabled) { if (SpriteManager != NULL) SpriteManager->setBatching(enabled); } 00857 bool isBatching() const { if (SpriteManager != NULL) return SpriteManager->getBatching(); return false; } 00858 int getMaxLayers() const { return MaxLayers; } 00859 bool getTickEnabled() const { return TickEnabled; } 00860 CzIVec2 getOriginalSize() const { return OriginalSize; } 00861 void setPhysicsEnabled(bool enable) { PhysicsEnabled = enable; } 00862 bool isPhysicsEnabled() const { return PhysicsEnabled; } 00863 // Properties end 00864 00865 protected: 00866 bool TickEnabled; ///< true if OnTick event specified 00867 CzIVec2 OriginalSize; ///< Original size of scene 00868 eCanvasFit CanvasFit; ///< Fit method for scalnig canvas 00869 eCanvasOrigin CanvasOrigin; ///< Origin type 00870 CzVec2 TransformedClipRect[2]; ///< Transformed clipping rect for the scene 00871 CzList<CzActor*> Removals; 00872 int MaxCollidables; ///< Maximum allowed collidables 00873 int NextFreeCollidable; ///< Points to next free slot in sollidables list pool 00874 int MaxLayers; ///< Maximum visible layers 00875 virtual void UpdateBindings(bool force_modified = false); ///< Update data bindings 00876 void UpdateActorResizing(); ///< Updates the parents of any actors that have resized themselves 00877 00878 public: 00879 CzScene() : IzXomlResource(), Parent(NULL), Collidables(NULL), ResourceManager(NULL), NextFreeCollidable(0), Camera(NULL), MaxCollidables(0), MaxLayers(0), AllowSuspend(false), IsActive(true), 00880 IsVisible(true), RunCount(0), EventsManager(NULL), VariableManager(NULL), ActionsManager(NULL), Bindings(NULL), Type(0), AllowFocus(false), Destroyed(false), Timeline(NULL), 00881 SpriteManager(NULL), ClipStatic(false), Layer(0), Modifiers(NULL), ConstantTimeStep(1.0f / 30.0f), CanvasFit(Fit_Best), CanvasOrigin(Origin_Centre), TimelinesManager(NULL), 00882 ProgramManager(NULL), IsDragging(false), ScriptEngine(NULL), TickEnabled(false), PrevTouching(false), UserPropertyList(NULL), Box2dWorld(NULL), PhysicsEnabled(true), TimersManager(NULL) 00883 { 00884 setClassType("scene"); 00885 EventsManager = new CzEventManager(); 00886 } 00887 virtual ~CzScene(); 00888 00889 // After creating the scene, call Init() to initialise it, passing the maximum number of actors that you expect can collide 00890 virtual int Init(int max_collidables = 128, int max_layers = 10, bool doSleep = true); 00891 00892 // Update physics 00893 void UpdatePhysics(float dt); 00894 00895 // Update() will update the scene and all of its contained actors 00896 virtual void Update(float dt); 00897 00898 // Draw() will draw all of the scenes actors 00899 virtual void Draw(); 00900 00901 // PreDestroy() is called when the scene has been marked for deletion 00902 virtual void PreDestroy() {} 00903 00904 // Coordinate transform helper method 00905 CzVec2 ScreenToVirtual(float screen_x, float screen_y, bool ignore_translation = false); ///< Returns a virtual position from a screen position 00906 CzVec2 VirtualToScreen(float virtual_x, float virtual_y, bool ignore_translation = false); ///< Returns a screen position frmo a virtual position 00907 CzVec2 ScreenToCamera(float pos_x, float pos_y, bool ignore_translation = false); ///< Returns a virtual position from a position taking into account camera position 00908 00909 // Event handlers (Only implement these if you need to know about these specific events) 00910 void ProcessEventActions(unsigned int event_name); 00911 virtual void NotifyPan(); ///< This event is called when this scene is panned 00912 virtual void NotifyCreate(); ///< This event is called when this scene is being created 00913 virtual void NotifyDestroy(); ///< This event is called when this scene is being destroyed 00914 virtual void NotifySuspending(CzScene* new_scene); ///< This event is called when this scene is being suspended 00915 virtual void NotifyResuming(CzScene* old_scene); ///< This event is called when this scene is being resumed 00916 virtual void NotifyLostFocus(CzScene* new_scene); ///< This event is called when this scene has just lost focus 00917 virtual void NotifyGainedFocus(CzScene* old_scene); ///< This event is called when this scene has just gained focus 00918 virtual void NotifyKeyBack(); ///< This event is called when this scene receives a back key press 00919 virtual void NotifyKeyMenu(); ///< This event is called when this scene receives a menu key press 00920 virtual void NotifyOrientationChanged(); ///< This event is called when this scene receives an orientatin changed event 00921 virtual void NotifySizeChanged(); ///< This event is called when this scene receives a size changed event 00922 virtual void NotifyDeviceSuspended(); ///< This event is called when this scene receives a device suspended event 00923 virtual void NotifyDeviceResumed(); ///< This event is called when this scene receives a device resumed event 00924 virtual void NotifyDeviceVolumeUp(); ///< This event is called when this scene receives a device volume up event 00925 virtual void NotifyDeviceVolumeDown(); ///< This event is called when this scene receives a device volume down event 00926 virtual void NotifyDeviceNetworkOnline(); ///< This event is called when this scene receives a network online event 00927 virtual void NotifyDeviceNetworkOffline(); ///< This event is called when this scene receives a network offline event 00928 00929 // Utility 00930 void Suspend(CzScene* new_scene); 00931 void Resume(CzScene* old_scene); 00932 CzActor* FindTappedActor(int x, int y); 00933 void CleanupRemovedActors(); 00934 void CheckforInvalidRemovals(const char* check_point); 00935 00936 // Implementation of IzXomlResource interface 00937 int LoadFromXoml(IzXomlResource* parent, bool load_children, CzXmlNode* node); 00938 bool PostLoadFromXoml(IzXomlResource* parent, CzXmlNode* node); 00939 00940 // Implementation of IzAnimTarget interface 00941 bool UpdateFromAnimation(CzAnimInstance *animation); 00942 00943 // Internal 00944 void RestoreClipping(); 00945 00946 // Internal (used by XOML system to setup and cleanup the XOML class properties system 00947 protected: 00948 static CzXomlClassDef* ClassDef; // XOML class definition 00949 public: 00950 static void InitClass(); 00951 static void ReleaseClass(); 00952 // XOML property settings / getters 00953 static bool _setName(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00954 static CzXomlProperty _getName(IzXomlResource* target); 00955 static bool _setType(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00956 static CzXomlProperty _getType(IzXomlResource* target); 00957 static bool _setLayer(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00958 static CzXomlProperty _getLayer(IzXomlResource* target); 00959 static bool _setPosition(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00960 static CzXomlProperty _getPosition(IzXomlResource* target); 00961 static bool _setPositionX(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00962 static CzXomlProperty _getPositionX(IzXomlResource* target); 00963 static bool _setPositionY(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00964 static CzXomlProperty _getPositionY(IzXomlResource* target); 00965 static bool _setAngle(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00966 static CzXomlProperty _getAngle(IzXomlResource* target); 00967 static bool _setScale(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00968 static CzXomlProperty _getScale(IzXomlResource* target); 00969 static bool _setColour(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00970 static CzXomlProperty _getColour(IzXomlResource* target); 00971 static bool _setOpacity(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00972 static CzXomlProperty _getOpacity(IzXomlResource* target); 00973 static bool _setTargetX(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00974 static bool _setTargetY(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00975 static bool _setCanvasSize(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00976 static CzXomlProperty _getCanvasSize(IzXomlResource* target); 00977 static bool _setCanvasFit(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00978 static CzXomlProperty _getCanvasFit(IzXomlResource* target); 00979 static bool _setCanvasOrigin(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00980 static CzXomlProperty _getCanvasOrigin(IzXomlResource* target); 00981 static bool _setExtents(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00982 static CzXomlProperty _getExtents(IzXomlResource* target); 00983 static bool _setAllowSuspend(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00984 static CzXomlProperty _getAllowSuspend(IzXomlResource* target); 00985 static bool _setClipping(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00986 static CzXomlProperty _getClipping(IzXomlResource* target); 00987 static bool _setClipStatic(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00988 static CzXomlProperty _getClipStatic(IzXomlResource* target); 00989 static bool _setActive(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00990 static CzXomlProperty _getActive(IzXomlResource* target); 00991 static bool _setVisible(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00992 static CzXomlProperty _getVisible(IzXomlResource* target); 00993 static CzXomlProperty _getLayers(IzXomlResource* target); 00994 static bool _setCurrent(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00995 static CzXomlProperty _getCurrent(IzXomlResource* target); 00996 static bool _setTimeline(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00997 static CzXomlProperty _getTimeline(IzXomlResource* target); 00998 static bool _setBindings(IzXomlResource* target, const CzXomlProperty& prop, bool add); 00999 static CzXomlProperty _getBindings(IzXomlResource* target); 01000 static bool _setTimeScale(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01001 static CzXomlProperty _getTimeScale(IzXomlResource* target); 01002 static bool _setCamera(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01003 static CzXomlProperty _getCamera(IzXomlResource* target); 01004 static bool _setBatch(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01005 static CzXomlProperty _getBatch(IzXomlResource* target); 01006 static bool _setAllowFocus(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01007 static CzXomlProperty _getAllowFocus(IzXomlResource* target); 01008 static bool _setOnSuspend(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01009 static bool _setOnResume(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01010 static bool _setOnLostFocus(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01011 static bool _setOnGainedFocus(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01012 static bool _setOnCreate(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01013 static bool _setOnDestroy(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01014 static bool _setOnKeyBack(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01015 static bool _setOnKeyMenu(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01016 static bool _setOnOrientationChange(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01017 static bool _setOnTick(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01018 static bool _setOnPan(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01019 static bool _setOnDeviceSuspended(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01020 static bool _setOnDeviceResumed(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01021 static bool _setOnDeviceVolumeUp(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01022 static bool _setOnDeviceVolumeDown(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01023 static bool _setOnDeviceNetworkOnline(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01024 static bool _setOnDeviceNetworkOffline(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01025 static bool _setScriptEngine(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01026 static CzXomlProperty _getScriptEngine(IzXomlResource* target); 01027 static bool _setWorldScale(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01028 static CzXomlProperty _getWorldScale(IzXomlResource* target); 01029 static bool _setPhysics(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01030 static CzXomlProperty _getPhysics(IzXomlResource* target); 01031 static bool _setGravity(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01032 static CzXomlProperty _getGravity(IzXomlResource* target); 01033 static bool _setPhysicsTimeStep(IzXomlResource* target, const CzXomlProperty& prop, bool add); 01034 static CzXomlProperty _getPhysicsTimeStep(IzXomlResource* target); 01035 static CzXomlProperty _getUserProperties(IzXomlResource* target); 01036 01037 }; 01038 01039 /** 01040 @class CzSceneCreator 01041 01042 @brief Creates an instance of a scene ovject. 01043 01044 CzSceneCreator enables scenes to be instantiated from XOML. 01045 01046 */ 01047 01048 class CzSceneCreator : public IzXomlClassCreator 01049 { 01050 public: 01051 CzSceneCreator() 01052 { 01053 setClassName("scene"); 01054 } 01055 IzXomlResource* CreateInstance(IzXomlResource* parent) { return new CzScene(); } 01056 }; 01057 01058 // 01059 // 01060 // 01061 // 01062 // Helper macros 01063 // 01064 // 01065 // 01066 // 01067 01068 /** 01069 @def CZ_NEW_SCENE(scene, name) 01070 01071 @brief Helper macros that simplifies creating a scene in code. 01072 01073 This macros can be used to create a scene using a snigle line of code. Note that once the scene has been created it should be attached to the app using 01074 CzApp::addScene() 01075 01076 @param scene The scene. 01077 @param name The name of the scene. 01078 */ 01079 01080 #define CZ_NEW_SCENE(scene, name) \ 01081 CzScene* scene = new CzScene(); \ 01082 scene->setName(name); \ 01083 scene->setAllowFocus(true); \ 01084 scene->Init(); \ 01085 scene->setVirtualTransform(PLATFORM_DISPLAY->getCurrentWidth(), PLATFORM_DISPLAY->getCurrentHeight(), 0, CzScene::Fit_Best); 01086 01087 /** 01088 @def CZ_NEW_SCENE_CUSTOM(scene, name, width, height, fit) CzScene* scene = new CzScene(); 01089 01090 @brief Helper macros that simplifies creating a custom scene in code. 01091 01092 This macros can be used to create a custom scene using a snigle line of code. Note that once the scene has been created it should be attached to the app using CzApp::addScene() 01093 01094 @author Mat 01095 @date 08/01/2013 01096 01097 @param scene The scene. 01098 @param name The name of the scene. 01099 @param width The virtual canvas width. 01100 @param height The virtual canvas height. 01101 @param fit The virtual canvas fit method. 01102 */ 01103 01104 #define CZ_NEW_SCENE_CUSTOM(scene, name, width, height, fit) \ 01105 CzScene* scene = new CzScene(); \ 01106 scene->setName(name); \ 01107 scene->setAllowFocus(true); \ 01108 scene->Init(); \ 01109 scene->setVirtualTransform(width, height, 0, fit); 01110 01111 /** 01112 @def CZ_NEW_SCENE_WITH_CAM(scene, name) 01113 01114 @brief Helper macros that simplifies creating a scene in code. 01115 01116 This macros can be used to create a scene containing a camera using a snigle line of code. The created camera is called "Cam". Note that once the scene has been 01117 created it should be attached to the app using CzApp::addScene() 01118 01119 @param scene The scene. 01120 @param name The name of the scene. 01121 */ 01122 01123 #define CZ_NEW_SCENE_WITH_CAM(scene, name) \ 01124 CzCamera* cam = new CzCamera(); \ 01125 cam->setName("Cam"); \ 01126 CzScene* scene = new CzScene(); \ 01127 scene->setName(name); \ 01128 scene->setAllowFocus(true); \ 01129 scene->Init(); \ 01130 scene->setCamera(cam); \ 01131 scene->setVirtualTransform(PLATFORM_DISPLAY->getCurrentWidth(), PLATFORM_DISPLAY->getCurrentHeight(), 0, CzScene::Fit_Best); 01132 01133 01134 /// @} 01135 01136 #endif // _CZ_SCENE_H_