Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / rendering-triangle / rendering-triangle.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 <dali-toolkit/dali-toolkit.h>
19 #include <dali/dali.h>
20
21 using namespace Dali;
22 using namespace Toolkit;
23
24 namespace
25 {
26 // clang-format off
27
28 /*
29  * Vertex shader
30  */
31 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
32 attribute mediump vec2 aPosition;\n // DALi shader builtin
33 uniform   mediump mat4 uMvpMatrix;\n // DALi shader builtin
34 uniform   mediump vec3 uSize;\n // DALi shader builtin
35 \n
36 void main()\n
37 {\n
38   mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
39   vertexPosition.xyz *= uSize;\n
40   gl_Position = uMvpMatrix * vertexPosition;\n
41 }\n
42 );
43
44 /*
45  * Fragment shader
46  */
47 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
48 uniform mediump vec4 uColor;\n
49 \n
50 void main()\n
51 {\n
52   gl_FragColor = uColor;\n
53 }\n
54 );
55 // clang-format on
56
57 } // namespace
58
59 // This example shows how to draw a triangle in actor's color
60 //
61 class DrawTriangleController : public ConnectionTracker
62 {
63 public:
64   DrawTriangleController(Application& application)
65   : mApplication(application)
66   {
67     // Connect to the Application's Init signal
68     mApplication.InitSignal().Connect(this, &DrawTriangleController::Create);
69   }
70
71   ~DrawTriangleController()
72   {
73     // Nothing to do here;
74   }
75
76   // The Init signal is received once (only) during the Application lifetime
77   void Create(Application& application)
78   {
79     // Get a handle to the window
80     Window window = application.GetWindow();
81     window.SetBackgroundColor(Color::WHITE);
82
83     // Step 1. Create shader
84     CreateTriangleShader();
85
86     // Step 2. Prepare geometry
87     CreateTriangleGeometry();
88
89     // Step 3. Create a renderer
90     CreateRenderer();
91
92     // Step 4. Create an Actor
93     CreateActor();
94
95     // Respond to a click anywhere on the window
96     window.GetRootLayer().TouchedSignal().Connect(this, &DrawTriangleController::OnTouch);
97
98     // Respond to key events
99     window.KeyEventSignal().Connect(this, &DrawTriangleController::OnKeyEvent);
100   }
101
102   bool OnTouch(Actor actor, const TouchEvent& touch)
103   {
104     // quit the application
105     mApplication.Quit();
106     return true;
107   }
108
109   /**
110    * @brief Called when any key event is received
111    *
112    * Will use this to quit the application if Back or the Escape key is received
113    * @param[in] event The key event information
114    */
115   void OnKeyEvent(const KeyEvent& event)
116   {
117     if(event.GetState() == KeyEvent::DOWN)
118     {
119       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
120       {
121         mApplication.Quit();
122       }
123     }
124   }
125
126   /**
127    * This function creates a triangle geometry made of three vertices in order
128    * to draw a coloured triangle.
129    */
130   void CreateTriangleGeometry()
131   {
132     Vector2 vertices[] = {
133       Vector2(-1.0f, -1.0f),
134       Vector2(1.0f, 1.0f),
135       Vector2(-1.0f, 1.0f)};
136
137     VertexBuffer vertexBuffer = VertexBuffer::New(Property::Map()
138                                                     .Add("aPosition", Property::VECTOR2));
139     vertexBuffer.SetData(vertices, sizeof(vertices) / sizeof(Vector2));
140
141     mGeometry = Geometry::New();
142     mGeometry.AddVertexBuffer(vertexBuffer);
143     mGeometry.SetType(Geometry::TRIANGLES);
144   }
145
146   /**
147    * Creates a shader using inlined variable VERTEX_SHADER and FRAGMENT_SHADER
148    *
149    * Shaders are very basic and all they do is transforming vertices and applying actor's colour.
150    */
151   void CreateTriangleShader()
152   {
153     mShader = Shader::New(VERTEX_SHADER, FRAGMENT_SHADER);
154   }
155
156   /**
157    * Function creates renderer.
158    */
159   void CreateRenderer()
160   {
161     mRenderer = Renderer::New(mGeometry, mShader);
162   }
163
164   /**
165    * Creates new actor and attaches renderer.
166    */
167   void CreateActor()
168   {
169     Window window = mApplication.GetWindow();
170     Size   size   = Vector2(window.GetSize()) * 0.25f;
171     mActor        = Actor::New();
172     mActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
173     mActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
174     mActor.SetProperty(Actor::Property::POSITION, Vector3(0.0f, 0.0f, 0.0f));
175     mActor.SetProperty(Actor::Property::COLOR, Color::RED);
176     mActor.SetProperty(Actor::Property::SIZE, Vector3(size.x, size.x, size.x));
177     mActor.AddRenderer(mRenderer);
178     window.Add(mActor);
179   }
180
181 private:
182   Application& mApplication;
183
184   Renderer mRenderer;
185   Shader   mShader;
186   Geometry mGeometry;
187   Actor    mActor;
188 };
189
190 int DALI_EXPORT_API main(int argc, char** argv)
191 {
192   Application            application = Application::New(&argc, &argv);
193   DrawTriangleController test(application);
194   application.MainLoop();
195   return 0;
196 }