2 * Copyright (c) 2020 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/public-api/actors/drawable-actor.h>
21 #include "native-renderer.h"
23 // Include the GLES header
24 #include <GLES3/gl3.h>
25 #include <dali/graphics-api/graphics-buffer-create-info.h>
27 #include <dali/devel-api/common/stage-devel.h>
29 using TextLabel = Dali::Toolkit::TextLabel;
32 // This example shows DrawableActor using native GL code to clear screen to red
33 // and renders TextLabel on top of it.
35 class DrawableActorExampleController : public ConnectionTracker
39 explicit DrawableActorExampleController(Application& application)
40 : mApplication(application)
42 // Connect to the Application's Init signal
43 mApplication.InitSignal().Connect(this, &DrawableActorExampleController::Create);
46 ~DrawableActorExampleController() override = default; // Nothing to do in destructor
48 // The Init signal is received once (only) during the Application lifetime
49 void Create(Application& application)
51 // Get a handle to the window
52 Window window = application.GetWindow();
53 window.SetBackgroundColor(Color::WHITE);
55 // Create native renderer
56 mRenderer = std::make_unique<NativeRenderer>(window.GetSize().GetWidth(), window.GetSize().GetHeight());
58 // Create render callback
59 mRenderCallback = RenderCallback::New<NativeRenderer>( mRenderer.get(), &NativeRenderer::OnRender );
61 // Create drawable actor
62 mGLActor = DrawableActor::New( *mRenderCallback );
64 mGLActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
65 mGLActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
67 // Set size on the actor (half the window size to show that glClear() and scissor test work together)
68 mGLActor.SetProperty( Actor::Property::SIZE, Size( window.GetSize() ) * 0.5f);
70 // Add actor to the scene
74 mTextLabel = TextLabel::New("This text overlays DrawableActor");
75 mTextLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
76 mTextLabel.SetProperty(Dali::Actor::Property::NAME, "SomeTextLabel");
77 window.Add(mTextLabel);
79 // Respond to a touch anywhere on the window
80 window.GetRootLayer().TouchedSignal().Connect(this, &DrawableActorExampleController::OnTouch);
82 // Respond to key events
83 window.KeyEventSignal().Connect(this, &DrawableActorExampleController::OnKeyEvent);
86 bool OnTouch(Actor actor, const TouchEvent& touch)
88 // quit the application
93 void OnKeyEvent(const KeyEvent& event)
95 if(event.GetState() == KeyEvent::DOWN)
97 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
104 TextLabel mTextLabel;
105 DrawableActor mGLActor;
107 std::unique_ptr<RenderCallback> mRenderCallback;
108 std::unique_ptr<NativeRenderer> mRenderer{nullptr};
111 Application& mApplication;
114 int DALI_EXPORT_API main(int argc, char** argv)
116 Application application = Application::New(&argc, &argv);
117 DrawableActorExampleController test(application);
118 application.MainLoop();