Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / fpp-game / fpp-game-example.cpp
1 /*
2  * Copyright (c) 2020 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-model.h"
19 #include "game-renderer.h"
20 #include "game-scene.h"
21 #include "game-texture.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 const char* SCENE_URL =
32   {
33     DEMO_GAME_DIR "/scene.json"};
34
35 }
36 /* This example creates 3D environment with first person camera control
37    It contains following modules:
38
39    GameScene  - responsible for loading and managing the scene data,
40                 it wraps around window. Owns list of entities. Scene can be deserialised
41                 from json file ( see scene.json )
42    GameEntity - the renderable object that has also a transformation. It wraps DALi actors.
43
44    GameModel  - loads models ( '.mod' file format ) and wraps DALi Geometry object. 'mod' format
45                 is binary in order
46
47    GameTexture - manages textures. Loads them, creates samplers and wraps DALi TextureSet
48
49    GameRenderer - binds texture and model. It's created per entity. While renderer is always unique
50                   for entity, the texture and model may be reused
51
52    GameCamera - Wraps the CameraActor. It provides not only that but also handles user input and
53                 implements first-person-perspective camera behavior.
54                 GameCamera uses Dali::Timer to provide per-frame ( or rather every 16ms ) update tick.
55
56
57                                .-----------.
58                .---------------| GameScene |---------------.
59                |               '-----------'               |
60                |                     |                     |
61                v                     |                     v
62         .------------. .------------.|.------------. .------------.
63         | GameEntity | | GameEntity |v|    ...     | | GameEntity |
64         '------------' '------------' '------------' '------------'
65                |
66                v
67         .--------------.
68         | GameRenderer |
69         '--------------'
70                 |
71          <------'-------->
72 .--------------.  .--------------.
73 | GameTexture  |  |  GameModel   |
74 '--------------'  '--------------'
75  */
76 class GameController : public ConnectionTracker
77 {
78 public:
79   GameController(Application& application)
80   : mApplication(application)
81   {
82     // Connect to the Application's Init signal
83     mApplication.InitSignal().Connect(this, &GameController::Create);
84   }
85
86   ~GameController()
87   {
88   }
89
90   // The Init signal is received once (only) during the Application lifetime
91   void Create(Application& application)
92   {
93     // Get a handle to the window
94     mWindow = application.GetWindow();
95
96     mWindow.SetBackgroundColor(Color::BLACK);
97
98     // Use 3D layer
99     mWindow.GetRootLayer().SetProperty(Layer::Property::BEHAVIOR, Layer::LAYER_3D);
100
101     // Load game scene
102     mScene.Load(mWindow, SCENE_URL);
103
104     // Display tutorial
105     mTutorialController.DisplayTutorial(mWindow);
106
107     // Connect OnKeyEvent signal
108     mWindow.KeyEventSignal().Connect(this, &GameController::OnKeyEvent);
109   }
110
111   // Handle a quit key event
112   void OnKeyEvent(const KeyEvent& event)
113   {
114     if(event.GetState() == KeyEvent::DOWN)
115     {
116       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
117       {
118         mApplication.Quit();
119       }
120     }
121   }
122
123 private:
124   Application&              mApplication;
125   GameScene                 mScene;
126   Window                    mWindow;
127   FppGameTutorialController mTutorialController;
128 };
129
130 int DALI_EXPORT_API main(int argc, char** argv)
131 {
132   Application    application = Application::New(&argc, &argv);
133   GameController test(application);
134   application.MainLoop();
135   return 0;
136 }