2 * Copyright (c) 2017 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/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
22 using namespace Toolkit;
30 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
31 attribute mediump vec2 aPosition;\n // DALi shader builtin
32 uniform mediump mat4 uMvpMatrix;\n // DALi shader builtin
33 uniform mediump vec3 uSize;\n // DALi shader builtin
37 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
38 vertexPosition.xyz *= uSize;\n
39 gl_Position = uMvpMatrix * vertexPosition;\n
46 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
47 uniform mediump vec4 uColor;\n
51 gl_FragColor = uColor;\n
57 // This example shows how to draw a triangle in actor's color
59 class DrawTriangleController : public ConnectionTracker
63 DrawTriangleController( Application& application )
64 : mApplication( application )
66 // Connect to the Application's Init signal
67 mApplication.InitSignal().Connect( this, &DrawTriangleController::Create );
70 ~DrawTriangleController()
72 // Nothing to do here;
75 // The Init signal is received once (only) during the Application lifetime
76 void Create( Application& application )
78 // Get a handle to the stage
79 Stage stage = Stage::GetCurrent();
80 stage.SetBackgroundColor( Color::WHITE );
82 // Step 1. Create shader
83 CreateTriangleShader();
85 // Step 2. Prepare geometry
86 CreateTriangleGeometry();
88 // Step 3. Create a renderer
91 // Step 4. Create an Actor
94 // Respond to a click anywhere on the stage
95 stage.GetRootLayer().TouchSignal().Connect( this, &DrawTriangleController::OnTouch );
97 // Respond to key events
98 stage.KeyEventSignal().Connect( this, &DrawTriangleController::OnKeyEvent );
101 bool OnTouch( Actor actor, const TouchData& touch )
103 // quit the application
109 * @brief Called when any key event is received
111 * Will use this to quit the application if Back or the Escape key is received
112 * @param[in] event The key event information
114 void OnKeyEvent( const KeyEvent& event )
116 if( event.state == KeyEvent::Down )
118 if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
126 * This function creates a triangle geometry made of three vertices in order
127 * to draw a coloured triangle.
129 void CreateTriangleGeometry()
131 Vector2 vertices[] = {
132 Vector2( -1.0f, -1.0f ),
133 Vector2( 1.0f, 1.0f ),
134 Vector2( -1.0f, 1.0f )
137 PropertyBuffer vertexBuffer = PropertyBuffer::New( Property::Map()
138 .Add( "aPosition", Property::VECTOR2 ) );
139 vertexBuffer.SetData( vertices, sizeof(vertices) / sizeof(Vector2) );
141 mGeometry = Geometry::New();
142 mGeometry.AddVertexBuffer( vertexBuffer );
143 mGeometry.SetType( Geometry::TRIANGLES );
147 * Creates a shader using inlined variable VERTEX_SHADER and FRAGMENT_SHADER
149 * Shaders are very basic and all they do is transforming vertices and applying actor's colour.
151 void CreateTriangleShader()
153 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
157 * Function creates renderer.
159 void CreateRenderer()
161 mRenderer = Renderer::New( mGeometry, mShader );
165 * Creates new actor and attaches renderer.
169 Stage stage = Stage::GetCurrent();
170 Size size = stage.GetSize() * 0.25f;
171 mActor = Actor::New();
172 mActor.SetAnchorPoint( AnchorPoint::CENTER );
173 mActor.SetParentOrigin( ParentOrigin::CENTER );
174 mActor.SetPosition( Vector3( 0.0f, 0.0f, 0.0f ) );
175 mActor.SetColor( Color::RED );
176 mActor.SetSize( Vector3( size.x, size.x, size.x ) );
177 mActor.AddRenderer( mRenderer );
182 Application& mApplication;
190 void RunTest( Application& application )
192 DrawTriangleController test( application );
194 application.MainLoop();
197 // Entry point for Linux & Tizen applications
199 int DALI_EXPORT_API main( int argc, char **argv )
201 Application application = Application::New( &argc, &argv );
203 RunTest( application );