Debian Codegraph @

modules/lsp-ws-lib/src/test/mtest/display/primitives.cpp

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-ws-lib
6 * Created on: 6 июл. 2022 г.
7 *
8 * lsp-ws-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-ws-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-ws-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/ws/factory.h>
23 #include <lsp-plug.in/ws/IEventHandler.h>
24 #include <lsp-plug.in/stdlib/math.h>
25 #include <lsp-plug.in/test-fw/mtest.h>
26
27 MTEST_BEGIN("ws.display", primitives)
28
29 class Handler: public ws::IEventHandler
30 {
31 private:
32 test_type_t *pTest;
33 ws::IWindow *pWnd;
34 bool bAntiAlias;
35 bool bTest;
36
37 public:
38 inline Handler(test_type_t *test, ws::IWindow *wnd)
39 {
40 pTest = test;
41 pWnd = wnd;
42 bAntiAlias = true;
43 bTest = false;
44 }
45
46 virtual status_t handle_event(const ws::event_t *ev)
47 {
48 switch (ev->nType)
49 {
50 case ws::UIE_REDRAW:
51 {
52 Color c(0.0f, 0.5f, 0.75f);
53 ws::ISurface *s = pWnd->get_surface();
54 if (s == NULL)
55 return STATUS_OK;
56
57 // Perform drawing
58 s->begin();
59 s->clear(c);
60 s->set_antialiasing(bAntiAlias);
61
62 if (bTest)
63 {
64 c.set_rgb24(0x00ff00);
65 s->fill_triangle(c,
66 50.0f, 50.0f,
67 50.0f, 150.0f,
68 200.0f, 50.0f);
69
70 s->fill_triangle(c,
71 400.0f, 50.0f,
72 250.0f, 50.0f,
73 400.0f, 150.0f);
74
75 s->fill_triangle(c,
76 200.0f, 300.0f,
77 50.0f, 200.0f,
78 50.0f, 300.0f);
79
80 s->fill_triangle(c,
81 250.0f, 300.0f,
82 400.0f, 300.0f,
83 400.0f, 200.0f);
84 }
85 else
86 {
87 ssize_t y = 8;
88
89 // Method 1: Filled solid sectors
90 c.set_rgb24(0xff0000);
91 for (size_t i=0; i<16; ++i)
92 {
93 c.alpha(i * 0.0625f);
94 s->fill_sector(
95 c, 8 + 40*i, y + 16, 16, i * M_PI / 16.0f, (i + 1) * M_PI / 6.0f);
96 }
97 y += 40;
98
99 // Method 2: Filled solid sectors with reverse angle
100 c.set_rgb24(0xff0000);
101 for (size_t i=0; i<16; ++i)
102 {
103 c.alpha(i * 0.0625f);
104 s->fill_sector(
105 c, 8 + 40*i, y + 16, 16, i * M_PI / -16.0f, (i + 1) * M_PI / -6.0f);
106 }
107 y += 40;
108
109 // Method 3: Filled solid triangles
110 c.set_rgb24(0x00ff00);
111 for (size_t i=0; i<16; ++i)
112 {
113 c.alpha(i * 0.0625f);
114 float a = M_PI * i / 8.0f;
115 float x0 = 16 * cosf(a), y0 = 16 * sinf(a);
116 float x1 = 16 * cosf(a + M_PI * 2.0f / 3.0f), y1 = 16 * sinf(a + M_PI * 2.0f / 3.0f);
117 float x2 = 16 * cosf(a + M_PI * 4.0f / 3.0f), y2 = 16 * sinf(a + M_PI * 4.0f / 3.0f);
118 s->fill_triangle(c,
119 x0 + 24.0f + 40*i, y0 + y + 16,
120 x1 + 24.0f + 40*i, y1 + y + 16,
121 x2 + 24.0f + 40*i, y2 + y + 16);
122 }
123 y += 40;
124
125 // Method 4: Filled gradient triangles
126 c.set_rgb24(0x00ff00);
127 for (size_t i=0; i<16; ++i)
128 {
129 ws::IGradient *g = s->linear_gradient(8 + 40*i, y, 8 + 40*(i+1), y + 40);
130 if (g == NULL)
131 continue;
132 lsp_finally { delete g; };
133 c.set_rgb24(0x0000ff);
134 c.alpha(i * 0.0625f);
135 g->set_start(c);
136 c.set_rgb24(0xffff00);
137 c.alpha(i * 0.0625f);
138 g->set_stop(c);
139
140 float a = M_PI * i / 8.0f + M_PI / 2.0f;
141 float x0 = 16 * cosf(a), y0 = 16 * sinf(a);
142 float x1 = 16 * cosf(a + M_PI * 2.0f / 3.0f), y1 = 16 * sinf(a + M_PI * 2.0f / 3.0f);
143 float x2 = 16 * cosf(a + M_PI * 4.0f / 3.0f), y2 = 16 * sinf(a + M_PI * 4.0f / 3.0f);
144 s->fill_triangle(g,
145 x0 + 24.0f + 40*i, y0 + y + 16,
146 x1 + 24.0f + 40*i, y1 + y + 16,
147 x2 + 24.0f + 40*i, y2 + y + 16);
148 }
149 y += 40;
150
151 // Method 5: Filled solid circles
152 c.set_rgb24(0xffffff);
153 for (size_t i=0; i<16; ++i)
154 {
155 float r = 12 + 4 * cosf(M_PI * i / 8.0f);
156 c.alpha(i * 0.0625f);
157 s->fill_circle(c, 24.0f + 40 * i, y + 16, r);
158 }
159 y += 40;
160
161 // Method 6: Filled gradient circles
162 c.set_rgb24(0xff00ff);
163 for (size_t i=0; i<16; ++i)
164 {
165 ws::IGradient *g = s->radial_gradient(8 + 40*i + 16, y + 16, 8 + 40*i + 16, y + 16, 20);
166 if (g == NULL)
167 continue;
168 lsp_finally { delete g; };
169 c.set_rgb24(0xff00ff);
170 c.alpha(i * 0.0625f);
171 g->set_start(c);
172 c.set_rgb24(0x00ffff);
173 c.alpha(i * 0.0625f);
174 g->set_stop(c);
175
176 float r = 12 + 4 * sinf(M_PI * i / 8.0f);
177 s->fill_circle(g, 24.0f + 40 * i, y + 16, r);
178 }
179 y += 40;
180
181 // Method 7: Wired arcs
182 c.set_rgb24(0xff0000);
183 for (size_t i=0; i<16; ++i)
184 {
185 c.set_rgb24(0x000000);
186 c.alpha(i * 0.0625f);
187
188 float w = 1 + (i >> 2);
189 s->wire_arc(
190 c, 8 + 40*i, y + 16, 16, i * M_PI / 16.0f, (i + 1) * M_PI / 6.0f, w);
191 }
192 y += 40;
193
194 // Method 8: Wired arcs with reverse angle
195 c.set_rgb24(0xff0000);
196 for (size_t i=0; i<16; ++i)
197 {
198 c.set_rgb24(0x000000);
199 c.alpha(i * 0.0625f);
200
201 float w = 1 + (i >> 2);
202 s->wire_arc(
203 c, 8 + 40*i, y + 16, 16, i * M_PI / -16.0f, (i + 1) * M_PI / -6.0f, w);
204 }
205 y += 40;
206 }
207
208 s->end();
209
210 return STATUS_OK;
211 }
212
213 case ws::UIE_MOUSE_CLICK:
214 {
215 // Left mouse button toggles anti-aliasing
216 if (ev->nCode == ws::MCB_LEFT)
217 {
218 bAntiAlias = !bAntiAlias;
219 pTest->printf("Anti-aliasing enabled: %s\n", (bAntiAlias) ? "true" : "false");
220 pWnd->invalidate();
221 }
222 else if (ev->nCode == ws::MCB_RIGHT)
223 {
224 bTest = !bTest;
225 pTest->printf("Test enabled: %s\n", (bTest) ? "true" : "false");
226 pWnd->invalidate();
227 }
228 break;
229 }
230
231 case ws::UIE_CLOSE:
232 {
233 pWnd->hide();
234 pWnd->display()->quit_main();
235 break;
236 }
237
238 default:
239 return IEventHandler::handle_event(ev);
240 }
241
242 return STATUS_OK;
243 }
244 };
245
246 MTEST_MAIN
247 {
248 ws::IDisplay *dpy = ws::create_display(0, NULL);
249 MTEST_ASSERT(dpy != NULL);
250 lsp_finally { ws::free_display(dpy); };
251
252 ws::IWindow *wnd = dpy->create_window();
253 MTEST_ASSERT(wnd != NULL);
254 lsp_finally {
255 wnd->destroy();
256 delete wnd;
257 };
258
259 MTEST_ASSERT(wnd->init() == STATUS_OK);
260 MTEST_ASSERT(wnd->set_caption("Test primitives") == STATUS_OK);
261 MTEST_ASSERT(wnd->set_window_actions(ws::WA_MOVE | ws::WA_CLOSE) == STATUS_OK);
262 MTEST_ASSERT(wnd->resize(640, 400) == STATUS_OK);
263 MTEST_ASSERT(wnd->set_size_constraints(640, 400, 640, 400) == STATUS_OK);
264
265 Handler h(this, wnd);
266 wnd->set_handler(&h);
267
268 MTEST_ASSERT(wnd->show() == STATUS_OK);
269
270 MTEST_ASSERT(dpy->main() == STATUS_OK);
271 }
272
273 MTEST_END
274
275
276
277
278