[dali_1.0.12] Merge branch 'tizen'
[platform/core/uifw/dali-toolkit.git] / docs / content / programming-guide / hello-world.h
1 /*! \page hello-world Hello World - explained
2
3 The following steps are required for displaying the sentence 'Hello World' with Dali:
4
5 - initialize the Dali library
6 - create an Actor showing text
7 - add it to the Stage
8
9 To understand the basic building blocks of the UI make sure to read the chapter on \link fundamentals Dali Fundamentals\endlink first.
10
11 Let's take a look at the code for this test application.
12
13 <h2 class="pg"> Example code </h2>
14 \code
15 #include <dali/dali.h>
16
17 using namespace Dali;
18
19 /******************************************************
20  * Demonstrates how to display "Hello World" on screen
21  ******************************************************/
22
23 class ExampleApp
24 {
25 public:
26   ExampleApp(Application &app)
27   : mApp(app)
28   {
29     // Connect to Dali::Application init signal. Do not make calls to Dali before this signal is received.
30     app.SignalInit().Connect(this, &ExampleApp::Create);
31   }
32
33   ~ExampleApp()
34   {
35     // Remove Hello World TextActor from stage
36     Stage::GetCurrent().Remove(mTextActor);
37   }
38
39 public:
40
41   void Create(Application& app)
42   {
43     // Initialize the actor
44     mTextActor = TextActor::New("Hello World");
45
46     // Center the actor. Note: default anchor point is CENTER
47     mTextActor.SetParentOrigin(ParentOrigin::CENTER);
48
49     // Display the actor on the stage
50     Stage::GetCurrent().Add(mTextActor);
51   }
52
53 private:
54   Application& mApp;
55   TextActor mTextActor;
56 };
57
58 int
59 main(int argc, char **argv)
60 {
61   Application daliApp(&argc, &argv);
62
63   ExampleApp helloApp (daliApp);
64   daliApp.MainLoop();
65
66   return 0;
67 }
68
69 \endcode
70
71  There are a couple of steps which are very important to understand.
72
73  <h2 class="pg"> Initializing Dali </h2>
74  The application should not use the Dali library until it has sent the init complete signal!
75  That's why we connect our ExampleApp::Create callback to Dali::Application's SignalInit signal:
76  \code
77    ...
78    app.SignalInit().Connect(this, &ExampleApp::Create);
79    ...
80  \endcode
81
82  <h2 class="pg"> Reference counting </h2>
83  The application should store Actors' and resources' handles.
84  Dali objects are reference counted, which makes sure they exist only as long as they are needed.
85  That's why we store the Actor's handle:
86  \code
87    ...
88    mTextActor = TextActor::New("Hello World");
89    ...
90  \endcode
91  Even if the TextActor is removed from the stage, it will be kept alive through our reference.\n
92  You can read more about implicit smart-pointer semantics in chapter \link handle-body-idiom Handle – body\endlink.
93
94  <h2 class="pg"> Main loop </h2>
95  To 'run' the application, it's main loop should be started.
96  This ensures that images are displayed, events, signals are dispatched and captured and so on.
97  \code
98    ...
99    daliApp.MainLoop();
100    ...
101  \endcode
102  \n \n
103  On X11 platform you can compile the above example with:
104  \code
105  g++ `pkg-config --libs --cflags dali` hello-dali.cpp -o hello
106  \endcode
107
108  After running './hello' this should be visible on the screen:
109
110  \image html Text-Actor.png "Hello world example"
111
112 */