2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali/dali.h>
21 #include "generated/rendering-triangle-frag.h"
22 #include "generated/rendering-triangle-vert.h"
25 using namespace Toolkit;
27 // This example shows how to draw a triangle in actor's color
29 class DrawTriangleController : public ConnectionTracker
32 DrawTriangleController(Application& application)
33 : mApplication(application)
35 // Connect to the Application's Init signal
36 mApplication.InitSignal().Connect(this, &DrawTriangleController::Create);
39 ~DrawTriangleController()
41 // Nothing to do here;
44 // The Init signal is received once (only) during the Application lifetime
45 void Create(Application& application)
47 // Get a handle to the window
48 Window window = application.GetWindow();
49 window.SetBackgroundColor(Color::WHITE);
51 // Step 1. Create shader
52 CreateTriangleShader();
54 // Step 2. Prepare geometry
55 CreateTriangleGeometry();
57 // Step 3. Create a renderer
60 // Step 4. Create an Actor
63 // Respond to a click anywhere on the window
64 window.GetRootLayer().TouchedSignal().Connect(this, &DrawTriangleController::OnTouch);
66 // Respond to key events
67 window.KeyEventSignal().Connect(this, &DrawTriangleController::OnKeyEvent);
70 bool OnTouch(Actor actor, const TouchEvent& touch)
72 // quit the application
78 * @brief Called when any key event is received
80 * Will use this to quit the application if Back or the Escape key is received
81 * @param[in] event The key event information
83 void OnKeyEvent(const KeyEvent& event)
85 if(event.GetState() == KeyEvent::DOWN)
87 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
95 * This function creates a triangle geometry made of three vertices in order
96 * to draw a coloured triangle.
98 void CreateTriangleGeometry()
100 Vector2 vertices[] = {
101 Vector2(-1.0f, -1.0f),
103 Vector2(-1.0f, 1.0f)};
105 VertexBuffer vertexBuffer = VertexBuffer::New(Property::Map()
106 .Add("aPosition", Property::VECTOR2));
107 vertexBuffer.SetData(vertices, sizeof(vertices) / sizeof(Vector2));
109 mGeometry = Geometry::New();
110 mGeometry.AddVertexBuffer(vertexBuffer);
111 mGeometry.SetType(Geometry::TRIANGLES);
115 * Creates a shader using inlined variable VERTEX_SHADER and FRAGMENT_SHADER
117 * Shaders are very basic and all they do is transforming vertices and applying actor's colour.
119 void CreateTriangleShader()
121 mShader = Shader::New(SHADER_RENDERING_TRIANGLE_VERT, SHADER_RENDERING_TRIANGLE_FRAG);
125 * Function creates renderer.
127 void CreateRenderer()
129 mRenderer = Renderer::New(mGeometry, mShader);
133 * Creates new actor and attaches renderer.
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);
150 Application& mApplication;
158 int DALI_EXPORT_API main(int argc, char** argv)
160 Application application = Application::New(&argc, &argv);
161 DrawTriangleController test(application);
162 application.MainLoop();