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