bindings/capi: fix c compatibility warnings.
[platform/core/graphics/tizenvg.git] / src / examples / testScene.cpp
1 #include "testCommon.h"
2
3 /************************************************************************/
4 /* Drawing Commands                                                     */
5 /************************************************************************/
6
7 void tvgDrawCmds(tvg::Canvas* canvas)
8 {
9     if (!canvas) return;
10
11     //Create a Scene
12     auto scene = tvg::Scene::gen();
13     scene->reserve(3);   //reserve 3 shape nodes (optional)
14
15     //Prepare Round Rectangle
16     auto shape1 = tvg::Shape::gen();
17     shape1->appendRect(0, 0, 400, 400, 50, 50);  //x, y, w, h, rx, ry
18     shape1->fill(0, 255, 0, 255);                //r, g, b, a
19     scene->push(move(shape1));
20
21     //Prepare Circle
22     auto shape2 = tvg::Shape::gen();
23     shape2->appendCircle(400, 400, 200, 200);    //cx, cy, radiusW, radiusH
24     shape2->fill(255, 255, 0, 255);              //r, g, b, a
25     scene->push(move(shape2));
26
27     //Prepare Ellipse
28     auto shape3 = tvg::Shape::gen();
29     shape3->appendCircle(600, 600, 150, 100);    //cx, cy, radiusW, radiusH
30     shape3->fill(0, 255, 255, 255);              //r, g, b, a
31     scene->push(move(shape3));
32
33     //Create another Scene
34     auto scene2 = tvg::Scene::gen();
35     scene2->reserve(2);   //reserve 2 shape nodes (optional)
36
37     //Star
38     auto shape4 = tvg::Shape::gen();
39
40     //Appends Paths
41     shape4->moveTo(199, 34);
42     shape4->lineTo(253, 143);
43     shape4->lineTo(374, 160);
44     shape4->lineTo(287, 244);
45     shape4->lineTo(307, 365);
46     shape4->lineTo(199, 309);
47     shape4->lineTo(97, 365);
48     shape4->lineTo(112, 245);
49     shape4->lineTo(26, 161);
50     shape4->lineTo(146, 143);
51     shape4->close();
52     shape4->fill(0, 0, 255, 255);
53     scene2->push(move(shape4));
54
55     //Circle
56     auto shape5 = tvg::Shape::gen();
57
58     auto cx = 550.0f;
59     auto cy = 550.0f;
60     auto radius = 125.0f;
61     auto halfRadius = radius * 0.552284f;
62
63     //Append Paths
64     shape5->moveTo(cx, cy - radius);
65     shape5->cubicTo(cx + halfRadius, cy - radius, cx + radius, cy - halfRadius, cx + radius, cy);
66     shape5->cubicTo(cx + radius, cy + halfRadius, cx + halfRadius, cy + radius, cx, cy+ radius);
67     shape5->cubicTo(cx - halfRadius, cy + radius, cx - radius, cy + halfRadius, cx - radius, cy);
68     shape5->cubicTo(cx - radius, cy - halfRadius, cx - halfRadius, cy - radius, cx, cy - radius);
69     shape5->fill(255, 0, 0, 255);
70     scene2->push(move(shape5));
71
72     //Push scene2 onto the scene
73     scene->push(move(scene2));
74
75     //Draw the Scene onto the Canvas
76     canvas->push(move(scene));
77 }
78
79
80 /************************************************************************/
81 /* Sw Engine Test Code                                                  */
82 /************************************************************************/
83
84 static unique_ptr<tvg::SwCanvas> swCanvas;
85
86 void tvgSwTest(uint32_t* buffer)
87 {
88     //Create a Canvas
89     swCanvas = tvg::SwCanvas::gen();
90     swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
91
92     /* Push the shape into the Canvas drawing list
93        When this shape is into the canvas list, the shape could update & prepare
94        internal data asynchronously for coming rendering.
95        Canvas keeps this shape node unless user call canvas->clear() */
96     tvgDrawCmds(swCanvas.get());
97 }
98
99 void drawSwView(void* data, Eo* obj)
100 {
101     if (swCanvas->draw() == tvg::Result::Success) {
102         swCanvas->sync();
103     }
104 }
105
106
107 /************************************************************************/
108 /* GL Engine Test Code                                                  */
109 /************************************************************************/
110
111 static unique_ptr<tvg::GlCanvas> glCanvas;
112
113 void initGLview(Evas_Object *obj)
114 {
115     static constexpr auto BPP = 4;
116
117     //Create a Canvas
118     glCanvas = tvg::GlCanvas::gen();
119     glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT);
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(glCanvas.get());
126 }
127
128 void drawGLview(Evas_Object *obj)
129 {
130     auto gl = elm_glview_gl_api_get(obj);
131     gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
132     gl->glClear(GL_COLOR_BUFFER_BIT);
133
134     if (glCanvas->draw() == tvg::Result::Success) {
135         glCanvas->sync();
136     }
137 }
138
139
140 /************************************************************************/
141 /* Main Code                                                            */
142 /************************************************************************/
143
144 int main(int argc, char **argv)
145 {
146     tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw;
147
148     if (argc > 1) {
149         if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl;
150     }
151
152     //Initialize ThorVG Engine
153     if (tvgEngine == tvg::CanvasEngine::Sw) {
154         cout << "tvg engine: software" << endl;
155     } else {
156         cout << "tvg engine: opengl" << endl;
157     }
158
159     //Threads Count
160     auto threads = std::thread::hardware_concurrency();
161
162     //Initialize ThorVG Engine
163     if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
164
165
166         elm_init(argc, argv);
167
168         if (tvgEngine == tvg::CanvasEngine::Sw) {
169             createSwView();
170         } else {
171             createGlView();
172         }
173
174         elm_run();
175         elm_shutdown();
176
177         //Terminate ThorVG Engine
178         tvg::Initializer::term(tvgEngine);
179
180     } else {
181         cout << "engine is not supported" << endl;
182     }
183     return 0;
184 }