Removed redundant RunTest methods from all examples
[platform/core/uifw/dali-demo.git] / examples / fpp-game / fpp-game-example.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include "game-renderer.h"
19 #include "game-model.h"
20 #include "game-texture.h"
21 #include "game-scene.h"
22
23 #include "fpp-game-tutorial-controller.h"
24
25 #include <dali-toolkit/dali-toolkit.h>
26
27 using namespace Dali;
28
29 namespace
30 {
31
32 const char* SCENE_URL =
33 {
34   DEMO_GAME_DIR "/scene.json"
35 };
36
37 }
38 /* This example creates 3D environment with first person camera control
39    It contains following modules:
40
41    GameScene  - responsible for loading and managing the scene data,
42                 it wraps around stage. Owns list of entities. Scene can be deserialised
43                 from json file ( see scene.json )
44    GameEntity - the renderable object that has also a transformation. It wraps DALi actors.
45
46    GameModel  - loads models ( '.mod' file format ) and wraps DALi Geometry object. 'mod' format
47                 is binary in order
48
49    GameTexture - manages textures. Loads them, creates samplers and wraps DALi TextureSet
50
51    GameRenderer - binds texture and model. It's created per entity. While renderer is always unique
52                   for entity, the texture and model may be reused
53
54    GameCamera - Wraps the CameraActor. It provides not only that but also handles user input and
55                 implements first-person-perspective camera behavior.
56                 GameCamera uses Dali::Timer to provide per-frame ( or rather every 16ms ) update tick.
57
58
59                                .-----------.
60                .---------------| GameScene |---------------.
61                |               '-----------'               |
62                |                     |                     |
63                v                     |                     v
64         .------------. .------------.|.------------. .------------.
65         | GameEntity | | GameEntity |v|    ...     | | GameEntity |
66         '------------' '------------' '------------' '------------'
67                |
68                v
69         .--------------.
70         | GameRenderer |
71         '--------------'
72                 |
73          <------'-------->
74 .--------------.  .--------------.
75 | GameTexture  |  |  GameModel   |
76 '--------------'  '--------------'
77  */
78 class GameController : public ConnectionTracker
79 {
80 public:
81
82   GameController( Application& application )
83   : mApplication( application )
84   {
85     // Connect to the Application's Init signal
86     mApplication.InitSignal().Connect( this, &GameController::Create );
87   }
88
89   ~GameController()
90   {
91   }
92
93   // The Init signal is received once (only) during the Application lifetime
94   void Create( Application& application )
95   {
96     // Disable indicator
97     Dali::Window winHandle = application.GetWindow();
98     winHandle.ShowIndicator( Dali::Window::INVISIBLE );
99
100     // Get a handle to the stage
101     mStage = Stage::GetCurrent();
102
103     mStage.SetBackgroundColor( Color::BLACK );
104
105     // Use 3D layer
106     mStage.GetRootLayer().SetBehavior( Layer::LAYER_3D );
107
108     // Load game scene
109     mScene.Load( SCENE_URL );
110
111     // Display tutorial
112     mTutorialController.DisplayTutorial();
113
114     // Connect OnKeyEvent signal
115     mStage.KeyEventSignal().Connect( this, &GameController::OnKeyEvent );
116   }
117
118   // Handle a quit key event
119   void OnKeyEvent(const KeyEvent& event)
120   {
121     if(event.state == KeyEvent::Down)
122     {
123       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
124       {
125         mApplication.Quit();
126       }
127     }
128   }
129
130 private:
131
132   Application&              mApplication;
133   GameScene                 mScene;
134   Stage                     mStage;
135   FppGameTutorialController mTutorialController;
136 };
137
138 int DALI_EXPORT_API main( int argc, char **argv )
139 {
140   Application application = Application::New( &argc, &argv );
141   GameController test( application );
142   application.MainLoop();
143   return 0;
144 }