test scene: remove duplicated engine initialization.
[platform/core/graphics/tizenvg.git] / test / testSceneTransform.cpp
1 #include <tizenvg.h>
2 #include <Elementary.h>
3
4 using namespace std;
5
6 #define WIDTH 800
7 #define HEIGHT 800
8
9 static uint32_t buffer[WIDTH * HEIGHT];
10 unique_ptr<tvg::SwCanvas> canvas = nullptr;
11 tvg::Scene* pScene1 = nullptr;
12 tvg::Scene* pScene2 = nullptr;
13
14 void tvgtest()
15 {
16     //Create a Canvas
17     canvas = tvg::SwCanvas::gen();
18     canvas->target(buffer, WIDTH, WIDTH, HEIGHT);
19
20     //Create a Scene1
21     auto scene = tvg::Scene::gen();
22     pScene1 = scene.get();
23     scene->reserve(3);   //reserve 3 shape nodes (optional)
24
25     //Prepare Round Rectangle (Scene1)
26     auto shape1 = tvg::Shape::gen();
27     shape1->appendRect(-235, -250, 400, 400, 50);      //x, y, w, h, cornerRadius
28     shape1->fill(0, 255, 0, 255);                //r, g, b, a
29     scene->push(move(shape1));
30
31     //Prepare Circle (Scene1)
32     auto shape2 = tvg::Shape::gen();
33     shape2->appendCircle(-165, -150, 200, 200);    //cx, cy, radiusW, radiusH
34     shape2->fill(255, 255, 0, 255);              //r, g, b, a
35     scene->push(move(shape2));
36
37     //Prepare Ellipse (Scene1)
38     auto shape3 = tvg::Shape::gen();
39     shape3->appendCircle(265, 250, 150, 100);    //cx, cy, radiusW, radiusH
40     shape3->fill(0, 255, 255, 255);              //r, g, b, a
41     scene->push(move(shape3));
42
43     scene->translate(350, 350);
44     scene->scale(0.5);
45
46     //Create Scene2
47     auto scene2 = tvg::Scene::gen();
48     pScene2 = scene2.get();
49     scene2->reserve(2);   //reserve 2 shape nodes (optional)
50
51     //Star (Scene2)
52     auto shape4 = tvg::Shape::gen();
53
54     //Appends Paths
55     shape4->moveTo(0, -114.5);
56     shape4->lineTo(54, -5.5);
57     shape4->lineTo(175, 11.5);
58     shape4->lineTo(88, 95.5);
59     shape4->lineTo(108, 216.5);
60     shape4->lineTo(0, 160.5);
61     shape4->lineTo(-102, 216.5);
62     shape4->lineTo(-87, 96.5);
63     shape4->lineTo(-173, 12.5);
64     shape4->lineTo(-53, -5.5);
65     shape4->close();
66     shape4->fill(0, 0, 127, 127);
67     scene2->push(move(shape4));
68
69     //Circle (Scene2)
70     auto shape5 = tvg::Shape::gen();
71
72     auto cx = -150.0f;
73     auto cy = -150.0f;
74     auto radius = 100.0f;
75     auto halfRadius = radius * 0.552284f;
76
77     //Append Paths
78     shape5->moveTo(cx, cy - radius);
79     shape5->cubicTo(cx + halfRadius, cy - radius, cx + radius, cy - halfRadius, cx + radius, cy);
80     shape5->cubicTo(cx + radius, cy + halfRadius, cx + halfRadius, cy + radius, cx, cy+ radius);
81     shape5->cubicTo(cx - halfRadius, cy + radius, cx - radius, cy + halfRadius, cx - radius, cy);
82     shape5->cubicTo(cx - radius, cy - halfRadius, cx - halfRadius, cy - radius, cx, cy - radius);
83     shape5->fill(127, 0, 0, 127);
84     scene2->push(move(shape5));
85
86     scene2->translate(500, 350);
87
88     //Push scene2 onto the scene
89     scene->push(move(scene2));
90
91     //Draw the Scene onto the Canvas
92     canvas->push(move(scene));
93
94     canvas->draw();
95     canvas->sync();
96 }
97
98 void transit_cb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
99 {
100     /* Update scene directly.
101        You can update only necessary properties of this scene,
102        while retaining other properties. */
103
104     pScene1->rotate(360 * progress);
105     pScene2->rotate(360 * progress);
106
107     //Update shape for drawing (this may work asynchronously)
108     canvas->update(pScene1);
109
110     //Draw Next frames
111     canvas->draw();
112     canvas->sync();
113
114     //Update Efl Canvas
115     Eo* img = (Eo*) effect;
116     evas_object_image_data_update_add(img, 0, 0, WIDTH, HEIGHT);
117 }
118
119 void
120 win_del(void *data, Evas_Object *o, void *ev)
121 {
122     elm_exit();
123 }
124
125 int main(int argc, char **argv)
126 {
127     //Initialize TizenVG Engine
128     tvg::Engine::init();
129
130     tvgtest();
131
132     //Show the result using EFL...
133     elm_init(argc, argv);
134
135     Eo* win = elm_win_util_standard_add(NULL, "TizenVG Test");
136     evas_object_smart_callback_add(win, "delete,request", win_del, 0);
137
138     Eo* img = evas_object_image_filled_add(evas_object_evas_get(win));
139     evas_object_image_size_set(img, WIDTH, HEIGHT);
140     evas_object_image_data_set(img, buffer);
141     evas_object_size_hint_weight_set(img, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
142     evas_object_show(img);
143
144     elm_win_resize_object_add(win, img);
145     evas_object_geometry_set(win, 0, 0, WIDTH, HEIGHT);
146     evas_object_show(win);
147
148     Elm_Transit *transit = elm_transit_add();
149     elm_transit_effect_add(transit, transit_cb, img, nullptr);
150     elm_transit_duration_set(transit, 2);
151     elm_transit_repeat_times_set(transit, -1);
152     elm_transit_go(transit);
153
154     elm_run();
155     elm_shutdown();
156
157     //Terminate TizenVG Engine
158     tvg::Engine::term();
159 }