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