2 * Copyright (c) 2023 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
38 explicit DrawableActorExampleController(Application& application)
39 : mApplication(application)
41 // Connect to the Application's Init signal
42 mApplication.InitSignal().Connect(this, &DrawableActorExampleController::Create);
45 ~DrawableActorExampleController() override = default; // Nothing to do in destructor
47 // The Init signal is received once (only) during the Application lifetime
48 void Create(Application& application)
50 // Get a handle to the window
51 Window window = application.GetWindow();
52 window.SetBackgroundColor(Color::WHITE);
54 // Create native renderer
55 mRenderer = std::make_unique<NativeRenderer>(window.GetSize().GetWidth(), window.GetSize().GetHeight());
57 // Create render callback
58 mRenderCallback = RenderCallback::New<NativeRenderer>(mRenderer.get(), &NativeRenderer::OnRender);
60 // Create drawable actor
61 mGLActor = DrawableActor::New(*mRenderCallback);
63 mGLActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
64 mGLActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
66 // Set size on the actor (half the window size to show that glClear() and scissor test work together)
67 mGLActor.SetProperty(Actor::Property::SIZE, Size(window.GetSize()) * 0.75f);
68 mGLActor[Actor::Property::POSITION] = Vector3(50.0f, 50.0f, 0.0f);
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();