bindings/capi: fix c compatibility warnings.
[platform/core/graphics/tizenvg.git] / src / examples / testSceneTransform.cpp
1 #include "testCommon.h"
2
3 /************************************************************************/
4 /* Drawing Commands                                                     */
5 /************************************************************************/
6 tvg::Scene* pScene1 = nullptr;
7 tvg::Scene* pScene2 = nullptr;
8
9 void tvgDrawCmds(tvg::Canvas* canvas)
10 {
11     if (!canvas) return;
12
13     //Create a Scene1
14     auto scene = tvg::Scene::gen();
15     pScene1 = scene.get();
16     scene->reserve(3);   //reserve 3 shape nodes (optional)
17
18     //Prepare Round Rectangle (Scene1)
19     auto shape1 = tvg::Shape::gen();
20     shape1->appendRect(-235, -250, 400, 400, 50, 50);  //x, y, w, h, rx, ry
21     shape1->fill(0, 255, 0, 255);                      //r, g, b, a
22     shape1->stroke(5);                                 //width
23     shape1->stroke(255, 255, 255, 255);                //r, g, b, a
24     scene->push(move(shape1));
25
26     //Prepare Circle (Scene1)
27     auto shape2 = tvg::Shape::gen();
28     shape2->appendCircle(-165, -150, 200, 200);    //cx, cy, radiusW, radiusH
29     shape2->fill(255, 255, 0, 255);                //r, g, b, a
30     scene->push(move(shape2));
31
32     //Prepare Ellipse (Scene1)
33     auto shape3 = tvg::Shape::gen();
34     shape3->appendCircle(265, 250, 150, 100);      //cx, cy, radiusW, radiusH
35     shape3->fill(0, 255, 255, 255);                //r, g, b, a
36     scene->push(move(shape3));
37
38     scene->translate(350, 350);
39     scene->scale(0.5);
40
41     //Create Scene2
42     auto scene2 = tvg::Scene::gen();
43     pScene2 = scene2.get();
44     scene2->reserve(2);   //reserve 2 shape nodes (optional)
45
46     //Star (Scene2)
47     auto shape4 = tvg::Shape::gen();
48
49     //Appends Paths
50     shape4->moveTo(0, -114.5);
51     shape4->lineTo(54, -5.5);
52     shape4->lineTo(175, 11.5);
53     shape4->lineTo(88, 95.5);
54     shape4->lineTo(108, 216.5);
55     shape4->lineTo(0, 160.5);
56     shape4->lineTo(-102, 216.5);
57     shape4->lineTo(-87, 96.5);
58     shape4->lineTo(-173, 12.5);
59     shape4->lineTo(-53, -5.5);
60     shape4->close();
61     shape4->fill(0, 0, 255, 127);
62     shape4->stroke(3);                             //width
63     shape4->stroke(0, 0, 255, 255);                //r, g, b, a
64     scene2->push(move(shape4));
65
66     //Circle (Scene2)
67     auto shape5 = tvg::Shape::gen();
68
69     auto cx = -150.0f;
70     auto cy = -150.0f;
71     auto radius = 100.0f;
72     auto halfRadius = radius * 0.552284f;
73
74     //Append Paths
75     shape5->moveTo(cx, cy - radius);
76     shape5->cubicTo(cx + halfRadius, cy - radius, cx + radius, cy - halfRadius, cx + radius, cy);
77     shape5->cubicTo(cx + radius, cy + halfRadius, cx + halfRadius, cy + radius, cx, cy+ radius);
78     shape5->cubicTo(cx - halfRadius, cy + radius, cx - radius, cy + halfRadius, cx - radius, cy);
79     shape5->cubicTo(cx - radius, cy - halfRadius, cx - halfRadius, cy - radius, cx, cy - radius);
80     shape5->close();
81     shape5->fill(255, 0, 0, 127);
82     scene2->push(move(shape5));
83
84     scene2->translate(500, 350);
85
86     //Push scene2 onto the scene
87     scene->push(move(scene2));
88
89     //Draw the Scene onto the Canvas
90     canvas->push(move(scene));
91 }
92
93 void tvgUpdateCmds(tvg::Canvas* canvas, float progress)
94 {
95     if (!canvas) return;
96
97     /* Update scene directly.
98        You can update only necessary properties of this scene,
99        while retaining other properties. */
100
101     pScene1->rotate(360 * progress);
102     pScene2->rotate(360 * progress);
103
104     //Update shape for drawing (this may work asynchronously)
105     canvas->update(pScene1);
106 }
107
108
109 /************************************************************************/
110 /* Sw Engine Test Code                                                  */
111 /************************************************************************/
112
113 static unique_ptr<tvg::SwCanvas> swCanvas;
114
115 void tvgSwTest(uint32_t* buffer)
116 {
117     //Create a Canvas
118     swCanvas = tvg::SwCanvas::gen();
119     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
120
121     /* Push the shape into the Canvas drawing list
122        When this shape is into the canvas list, the shape could update & prepare
123        internal data asynchronously for coming rendering.
124        Canvas keeps this shape node unless user call canvas->clear() */
125     tvgDrawCmds(swCanvas.get());
126 }
127
128 void transitSwCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
129 {
130     tvgUpdateCmds(swCanvas.get(), progress);
131
132     //Update Efl Canvas
133     Eo* img = (Eo*) effect;
134     evas_object_image_data_update_add(img, 0, 0, WIDTH, HEIGHT);
135     evas_object_image_pixels_dirty_set(img, EINA_TRUE);
136 }
137
138 void drawSwView(void* data, Eo* obj)
139 {
140     if (swCanvas->draw() == tvg::Result::Success) {
141         swCanvas->sync();
142     }
143 }
144
145
146 /************************************************************************/
147 /* GL Engine Test Code                                                  */
148 /************************************************************************/
149
150 static unique_ptr<tvg::GlCanvas> glCanvas;
151
152 void initGLview(Evas_Object *obj)
153 {
154     static constexpr auto BPP = 4;
155
156     //Create a Canvas
157     glCanvas = tvg::GlCanvas::gen();
158     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
159
160     /* Push the shape into the Canvas drawing list
161        When this shape is into the canvas list, the shape could update & prepare
162        internal data asynchronously for coming rendering.
163        Canvas keeps this shape node unless user call canvas->clear() */
164     tvgDrawCmds(glCanvas.get());
165 }
166
167 void drawGLview(Evas_Object *obj)
168 {
169     auto gl = elm_glview_gl_api_get(obj);
170     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
171     gl->glClear(GL_COLOR_BUFFER_BIT);
172
173     if (glCanvas->draw() == tvg::Result::Success) {
174         glCanvas->sync();
175     }
176 }
177
178 void transitGlCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress)
179 {
180     tvgUpdateCmds(glCanvas.get(), progress);
181 }
182
183
184 /************************************************************************/
185 /* Main Code                                                            */
186 /************************************************************************/
187
188 int main(int argc, char **argv)
189 {
190     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
191
192     if (argc > 1) {
193         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
194     }
195
196     //Initialize ThorVG Engine
197     if (tvgEngine == tvg::CanvasEngine::Sw) {
198         cout << "tvg engine: software" << endl;
199     } else {
200         cout << "tvg engine: opengl" << endl;
201     }
202
203     //Threads Count
204     auto threads = std::thread::hardware_concurrency();
205
206     //Initialize ThorVG Engine
207     if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
208
209         elm_init(argc, argv);
210
211         Elm_Transit *transit = elm_transit_add();
212
213         if (tvgEngine == tvg::CanvasEngine::Sw) {
214             auto view = createSwView();
215             elm_transit_effect_add(transit, transitSwCb, view, nullptr);
216         } else {
217             auto view = createGlView();
218             elm_transit_effect_add(transit, transitGlCb, view, nullptr);
219         }
220
221         elm_transit_duration_set(transit, 2);
222         elm_transit_repeat_times_set(transit, -1);
223         elm_transit_auto_reverse_set(transit, EINA_TRUE);
224         elm_transit_go(transit);
225
226         elm_run();
227         elm_shutdown();
228
229         //Terminate ThorVG Engine
230         tvg::Initializer::term(tvgEngine);
231
232     } else {
233         cout << "engine is not supported" << endl;
234     }
235     return 0;
236 }