1 | /*
|
---|
2 | * Copyright (c) 2020 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup gfxdemo
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file Graphic demo
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <congfx/console.h>
|
---|
36 | #include <display.h>
|
---|
37 | #include <fibril.h>
|
---|
38 | #include <gfx/bitmap.h>
|
---|
39 | #include <gfx/color.h>
|
---|
40 | #include <gfx/render.h>
|
---|
41 | #include <gfx/font.h>
|
---|
42 | #include <gfx/text.h>
|
---|
43 | #include <gfx/typeface.h>
|
---|
44 | #include <io/console.h>
|
---|
45 | #include <io/pixelmap.h>
|
---|
46 | #include <stdbool.h>
|
---|
47 | #include <stdlib.h>
|
---|
48 | #include <str.h>
|
---|
49 | #include <task.h>
|
---|
50 | #include <ui/ui.h>
|
---|
51 | #include <ui/window.h>
|
---|
52 | #include <ui/wdecor.h>
|
---|
53 |
|
---|
54 | static void wnd_close_event(void *);
|
---|
55 | static void wnd_kbd_event(void *, kbd_event_t *);
|
---|
56 |
|
---|
57 | static display_wnd_cb_t wnd_cb = {
|
---|
58 | .close_event = wnd_close_event,
|
---|
59 | .kbd_event = wnd_kbd_event
|
---|
60 | };
|
---|
61 |
|
---|
62 | static void uiwnd_close_event(ui_window_t *, void *);
|
---|
63 | static void uiwnd_kbd_event(ui_window_t *, void *, kbd_event_t *);
|
---|
64 |
|
---|
65 | static ui_window_cb_t ui_window_cb = {
|
---|
66 | .close = uiwnd_close_event,
|
---|
67 | .kbd = uiwnd_kbd_event
|
---|
68 | };
|
---|
69 |
|
---|
70 | static bool quit = false;
|
---|
71 |
|
---|
72 | /** Clear screen.
|
---|
73 | *
|
---|
74 | * @param gc Graphic context
|
---|
75 | * @param w Screen width
|
---|
76 | * @param h Screen height
|
---|
77 | */
|
---|
78 | static errno_t clear_scr(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
|
---|
79 | {
|
---|
80 | gfx_color_t *color = NULL;
|
---|
81 | gfx_rect_t rect;
|
---|
82 | errno_t rc;
|
---|
83 |
|
---|
84 | rc = gfx_color_new_rgb_i16(0, 0, 0, &color);
|
---|
85 | if (rc != EOK)
|
---|
86 | goto error;
|
---|
87 |
|
---|
88 | rc = gfx_set_color(gc, color);
|
---|
89 | if (rc != EOK)
|
---|
90 | goto error;
|
---|
91 |
|
---|
92 | rect.p0.x = 0;
|
---|
93 | rect.p0.y = 0;
|
---|
94 | rect.p1.x = w;
|
---|
95 | rect.p1.y = h;
|
---|
96 |
|
---|
97 | rc = gfx_fill_rect(gc, &rect);
|
---|
98 | if (rc != EOK)
|
---|
99 | goto error;
|
---|
100 |
|
---|
101 | gfx_color_delete(color);
|
---|
102 | return EOK;
|
---|
103 | error:
|
---|
104 | if (color != NULL)
|
---|
105 | gfx_color_delete(color);
|
---|
106 | return rc;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /** Run rectangle demo on a graphic context.
|
---|
110 | *
|
---|
111 | * @param gc Graphic context
|
---|
112 | * @param w Width
|
---|
113 | * @param h Height
|
---|
114 | */
|
---|
115 | static errno_t demo_rects(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
|
---|
116 | {
|
---|
117 | gfx_color_t *color = NULL;
|
---|
118 | gfx_rect_t rect;
|
---|
119 | int i, j;
|
---|
120 | errno_t rc;
|
---|
121 |
|
---|
122 | rc = clear_scr(gc, w, h);
|
---|
123 | if (rc != EOK)
|
---|
124 | return rc;
|
---|
125 |
|
---|
126 | for (j = 0; j < 10; j++) {
|
---|
127 | rc = gfx_color_new_rgb_i16(rand() % 0x10000, rand() % 0x10000,
|
---|
128 | rand() % 0x10000, &color);
|
---|
129 | if (rc != EOK)
|
---|
130 | return rc;
|
---|
131 |
|
---|
132 | rc = gfx_set_color(gc, color);
|
---|
133 | if (rc != EOK)
|
---|
134 | return rc;
|
---|
135 |
|
---|
136 | for (i = 0; i < 10; i++) {
|
---|
137 | rect.p0.x = rand() % (w - 1);
|
---|
138 | rect.p0.y = rand() % (h - 1);
|
---|
139 | rect.p1.x = rect.p0.x + rand() % (w - 1 - rect.p0.x);
|
---|
140 | rect.p1.y = rect.p0.y + rand() % (h - 1 - rect.p0.y);
|
---|
141 |
|
---|
142 | rc = gfx_fill_rect(gc, &rect);
|
---|
143 | if (rc != EOK)
|
---|
144 | return rc;
|
---|
145 | }
|
---|
146 |
|
---|
147 | gfx_color_delete(color);
|
---|
148 |
|
---|
149 | fibril_usleep(500 * 1000);
|
---|
150 |
|
---|
151 | if (quit)
|
---|
152 | break;
|
---|
153 | }
|
---|
154 |
|
---|
155 | return EOK;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /** Fill bitmap with tartan pattern.
|
---|
159 | *
|
---|
160 | * @param bitmap Bitmap
|
---|
161 | * @param w Bitmap width
|
---|
162 | * @param h Bitmap height
|
---|
163 | * @return EOK on success or an error code
|
---|
164 | */
|
---|
165 | static errno_t bitmap_tartan(gfx_bitmap_t *bitmap, gfx_coord_t w, gfx_coord_t h)
|
---|
166 | {
|
---|
167 | int i, j;
|
---|
168 | pixelmap_t pixelmap;
|
---|
169 | gfx_bitmap_alloc_t alloc;
|
---|
170 | errno_t rc;
|
---|
171 |
|
---|
172 | rc = gfx_bitmap_get_alloc(bitmap, &alloc);
|
---|
173 | if (rc != EOK)
|
---|
174 | return rc;
|
---|
175 |
|
---|
176 | /* In absence of anything else, use pixelmap */
|
---|
177 | pixelmap.width = w;
|
---|
178 | pixelmap.height = h;
|
---|
179 | pixelmap.data = alloc.pixels;
|
---|
180 |
|
---|
181 | for (i = 0; i < w; i++) {
|
---|
182 | for (j = 0; j < h; j++) {
|
---|
183 | pixelmap_put_pixel(&pixelmap, i, j,
|
---|
184 | PIXEL(255, (i % 30) < 3 ? 255 : 0,
|
---|
185 | (j % 30) < 3 ? 255 : 0, i / 2));
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | return EOK;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /** Fill bitmap with moire pattern.
|
---|
193 | *
|
---|
194 | * @param bitmap Bitmap
|
---|
195 | * @param w Bitmap width
|
---|
196 | * @param h Bitmap height
|
---|
197 | * @return EOK on success or an error code
|
---|
198 | */
|
---|
199 | static errno_t bitmap_moire(gfx_bitmap_t *bitmap, gfx_coord_t w, gfx_coord_t h)
|
---|
200 | {
|
---|
201 | int i, j;
|
---|
202 | int k;
|
---|
203 | pixelmap_t pixelmap;
|
---|
204 | gfx_bitmap_alloc_t alloc;
|
---|
205 | errno_t rc;
|
---|
206 |
|
---|
207 | rc = gfx_bitmap_get_alloc(bitmap, &alloc);
|
---|
208 | if (rc != EOK)
|
---|
209 | return rc;
|
---|
210 |
|
---|
211 | /* In absence of anything else, use pixelmap */
|
---|
212 | pixelmap.width = w;
|
---|
213 | pixelmap.height = h;
|
---|
214 | pixelmap.data = alloc.pixels;
|
---|
215 |
|
---|
216 | for (i = 0; i < w; i++) {
|
---|
217 | for (j = 0; j < h; j++) {
|
---|
218 | k = i * i + j * j;
|
---|
219 | pixelmap_put_pixel(&pixelmap, i, j,
|
---|
220 | PIXEL(255, k, k, k));
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | return EOK;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /** Render circle to a bitmap.
|
---|
228 | *
|
---|
229 | * @param bitmap Bitmap
|
---|
230 | * @param w Bitmap width
|
---|
231 | * @param h Bitmap height
|
---|
232 | * @return EOK on success or an error code
|
---|
233 | */
|
---|
234 | static errno_t bitmap_circle(gfx_bitmap_t *bitmap, gfx_coord_t w, gfx_coord_t h)
|
---|
235 | {
|
---|
236 | int i, j;
|
---|
237 | int k;
|
---|
238 | pixelmap_t pixelmap;
|
---|
239 | gfx_bitmap_alloc_t alloc;
|
---|
240 | errno_t rc;
|
---|
241 |
|
---|
242 | rc = gfx_bitmap_get_alloc(bitmap, &alloc);
|
---|
243 | if (rc != EOK)
|
---|
244 | return rc;
|
---|
245 |
|
---|
246 | /* In absence of anything else, use pixelmap */
|
---|
247 | pixelmap.width = w;
|
---|
248 | pixelmap.height = h;
|
---|
249 | pixelmap.data = alloc.pixels;
|
---|
250 |
|
---|
251 | for (i = 0; i < w; i++) {
|
---|
252 | for (j = 0; j < h; j++) {
|
---|
253 | k = i * i + j * j;
|
---|
254 | pixelmap_put_pixel(&pixelmap, i, j,
|
---|
255 | k < w * w / 2 ? PIXEL(255, 0, 255, 0) :
|
---|
256 | PIXEL(0, 255, 0, 255));
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | return EOK;
|
---|
261 | }
|
---|
262 |
|
---|
263 | /** Run bitmap demo on a graphic context.
|
---|
264 | *
|
---|
265 | * @param gc Graphic context
|
---|
266 | * @param w Width
|
---|
267 | * @param h Height
|
---|
268 | */
|
---|
269 | static errno_t demo_bitmap(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
|
---|
270 | {
|
---|
271 | gfx_bitmap_t *bitmap;
|
---|
272 | gfx_bitmap_params_t params;
|
---|
273 | int i, j;
|
---|
274 | gfx_coord2_t offs;
|
---|
275 | gfx_rect_t srect;
|
---|
276 | errno_t rc;
|
---|
277 |
|
---|
278 | rc = clear_scr(gc, w, h);
|
---|
279 | if (rc != EOK)
|
---|
280 | return rc;
|
---|
281 |
|
---|
282 | gfx_bitmap_params_init(¶ms);
|
---|
283 | params.rect.p0.x = 0;
|
---|
284 | params.rect.p0.y = 0;
|
---|
285 | params.rect.p1.x = w;
|
---|
286 | params.rect.p1.y = h;
|
---|
287 |
|
---|
288 | rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
|
---|
289 | if (rc != EOK)
|
---|
290 | return rc;
|
---|
291 |
|
---|
292 | rc = bitmap_tartan(bitmap, w, h);
|
---|
293 | if (rc != EOK)
|
---|
294 | goto error;
|
---|
295 |
|
---|
296 | for (j = 0; j < 10; j++) {
|
---|
297 | for (i = 0; i < 5; i++) {
|
---|
298 | srect.p0.x = rand() % (w - 40);
|
---|
299 | srect.p0.y = rand() % (h - 20);
|
---|
300 | srect.p1.x = srect.p0.x + rand() % (w - srect.p0.x);
|
---|
301 | srect.p1.y = srect.p0.y + rand() % (h - srect.p0.y);
|
---|
302 | offs.x = 0;
|
---|
303 | offs.y = 0;
|
---|
304 |
|
---|
305 | rc = gfx_bitmap_render(bitmap, &srect, &offs);
|
---|
306 | if (rc != EOK)
|
---|
307 | goto error;
|
---|
308 | fibril_usleep(250 * 1000);
|
---|
309 |
|
---|
310 | if (quit)
|
---|
311 | break;
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | gfx_bitmap_destroy(bitmap);
|
---|
316 |
|
---|
317 | return EOK;
|
---|
318 | error:
|
---|
319 | gfx_bitmap_destroy(bitmap);
|
---|
320 | return rc;
|
---|
321 | }
|
---|
322 |
|
---|
323 | /** Run second bitmap demo on a graphic context.
|
---|
324 | *
|
---|
325 | * @param gc Graphic context
|
---|
326 | * @param w Width
|
---|
327 | * @param h Height
|
---|
328 | */
|
---|
329 | static errno_t demo_bitmap2(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
|
---|
330 | {
|
---|
331 | gfx_bitmap_t *bitmap;
|
---|
332 | gfx_bitmap_params_t params;
|
---|
333 | int i, j;
|
---|
334 | gfx_coord2_t offs;
|
---|
335 | errno_t rc;
|
---|
336 |
|
---|
337 | rc = clear_scr(gc, w, h);
|
---|
338 | if (rc != EOK)
|
---|
339 | return rc;
|
---|
340 |
|
---|
341 | gfx_bitmap_params_init(¶ms);
|
---|
342 | params.rect.p0.x = 0;
|
---|
343 | params.rect.p0.y = 0;
|
---|
344 | params.rect.p1.x = 40;
|
---|
345 | params.rect.p1.y = 20;
|
---|
346 |
|
---|
347 | rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
|
---|
348 | if (rc != EOK)
|
---|
349 | return rc;
|
---|
350 |
|
---|
351 | rc = bitmap_moire(bitmap, 40, 20);
|
---|
352 | if (rc != EOK)
|
---|
353 | goto error;
|
---|
354 |
|
---|
355 | for (j = 0; j < 10; j++) {
|
---|
356 | for (i = 0; i < 10; i++) {
|
---|
357 | offs.x = rand() % (w - 40);
|
---|
358 | offs.y = rand() % (h - 20);
|
---|
359 |
|
---|
360 | rc = gfx_bitmap_render(bitmap, NULL, &offs);
|
---|
361 | if (rc != EOK)
|
---|
362 | goto error;
|
---|
363 | }
|
---|
364 |
|
---|
365 | fibril_usleep(500 * 1000);
|
---|
366 |
|
---|
367 | if (quit)
|
---|
368 | break;
|
---|
369 | }
|
---|
370 |
|
---|
371 | gfx_bitmap_destroy(bitmap);
|
---|
372 |
|
---|
373 | return EOK;
|
---|
374 | error:
|
---|
375 | gfx_bitmap_destroy(bitmap);
|
---|
376 | return rc;
|
---|
377 | }
|
---|
378 |
|
---|
379 | /** Run bitmap color key demo on a graphic context.
|
---|
380 | *
|
---|
381 | * @param gc Graphic context
|
---|
382 | * @param w Width
|
---|
383 | * @param h Height
|
---|
384 | */
|
---|
385 | static errno_t demo_bitmap_kc(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
|
---|
386 | {
|
---|
387 | gfx_bitmap_t *bitmap;
|
---|
388 | gfx_bitmap_params_t params;
|
---|
389 | int i, j;
|
---|
390 | gfx_coord2_t offs;
|
---|
391 | errno_t rc;
|
---|
392 |
|
---|
393 | rc = clear_scr(gc, w, h);
|
---|
394 | if (rc != EOK)
|
---|
395 | return rc;
|
---|
396 |
|
---|
397 | gfx_bitmap_params_init(¶ms);
|
---|
398 | params.rect.p0.x = 0;
|
---|
399 | params.rect.p0.y = 0;
|
---|
400 | params.rect.p1.x = 40;
|
---|
401 | params.rect.p1.y = 40;
|
---|
402 | params.flags = bmpf_color_key;
|
---|
403 | params.key_color = PIXEL(0, 255, 0, 255);
|
---|
404 |
|
---|
405 | rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
|
---|
406 | if (rc != EOK)
|
---|
407 | return rc;
|
---|
408 |
|
---|
409 | rc = bitmap_circle(bitmap, 40, 40);
|
---|
410 | if (rc != EOK)
|
---|
411 | goto error;
|
---|
412 |
|
---|
413 | for (j = 0; j < 10; j++) {
|
---|
414 | for (i = 0; i < 10; i++) {
|
---|
415 | offs.x = j * 20 + i * 20;
|
---|
416 | offs.y = i * 20;
|
---|
417 |
|
---|
418 | rc = gfx_bitmap_render(bitmap, NULL, &offs);
|
---|
419 | if (rc != EOK)
|
---|
420 | goto error;
|
---|
421 | }
|
---|
422 |
|
---|
423 | fibril_usleep(500 * 1000);
|
---|
424 |
|
---|
425 | if (quit)
|
---|
426 | break;
|
---|
427 | }
|
---|
428 |
|
---|
429 | gfx_bitmap_destroy(bitmap);
|
---|
430 |
|
---|
431 | return EOK;
|
---|
432 | error:
|
---|
433 | gfx_bitmap_destroy(bitmap);
|
---|
434 | return rc;
|
---|
435 | }
|
---|
436 |
|
---|
437 | /** Run text demo on a graphic context.
|
---|
438 | *
|
---|
439 | * @param gc Graphic context
|
---|
440 | * @param w Width
|
---|
441 | * @param h Height
|
---|
442 | */
|
---|
443 | static errno_t demo_text(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
|
---|
444 | {
|
---|
445 | gfx_color_t *color = NULL;
|
---|
446 | gfx_rect_t rect;
|
---|
447 | gfx_typeface_t *tface = NULL;
|
---|
448 | gfx_font_info_t *finfo;
|
---|
449 | gfx_font_t *font = NULL;
|
---|
450 | gfx_coord2_t pos;
|
---|
451 | gfx_text_fmt_t fmt;
|
---|
452 | int i;
|
---|
453 | errno_t rc;
|
---|
454 |
|
---|
455 | rc = gfx_typeface_open(gc, "/data/font/helena.tpf", &tface);
|
---|
456 | if (rc != EOK) {
|
---|
457 | printf("Error opening typeface\n");
|
---|
458 | goto error;
|
---|
459 | }
|
---|
460 |
|
---|
461 | finfo = gfx_typeface_first_font(tface);
|
---|
462 | if (finfo == NULL) {
|
---|
463 | printf("Typeface contains no font.\n");
|
---|
464 | rc = ENOENT;
|
---|
465 | goto error;
|
---|
466 | }
|
---|
467 |
|
---|
468 | rc = gfx_font_open(finfo, &font);
|
---|
469 | if (rc != EOK) {
|
---|
470 | printf("Error opening font.\n");
|
---|
471 | goto error;
|
---|
472 | }
|
---|
473 |
|
---|
474 | rc = clear_scr(gc, w, h);
|
---|
475 | if (rc != EOK)
|
---|
476 | goto error;
|
---|
477 |
|
---|
478 | /* Vertical bars */
|
---|
479 |
|
---|
480 | for (i = 0; i < 20; i++) {
|
---|
481 | rc = gfx_color_new_rgb_i16(0, 0x8000 * i / 20,
|
---|
482 | 0x8000 * i / 20, &color);
|
---|
483 | if (rc != EOK)
|
---|
484 | goto error;
|
---|
485 |
|
---|
486 | rc = gfx_set_color(gc, color);
|
---|
487 | if (rc != EOK)
|
---|
488 | goto error;
|
---|
489 |
|
---|
490 | rect.p0.x = w * i / 20;
|
---|
491 | rect.p0.y = 0;
|
---|
492 | rect.p1.x = w * (i + 1) / 20;
|
---|
493 | rect.p1.y = h;
|
---|
494 |
|
---|
495 | rc = gfx_fill_rect(gc, &rect);
|
---|
496 | if (rc != EOK)
|
---|
497 | goto error;
|
---|
498 |
|
---|
499 | gfx_color_delete(color);
|
---|
500 | }
|
---|
501 |
|
---|
502 | rc = gfx_color_new_rgb_i16(0, 0, 0x8000, &color);
|
---|
503 | if (rc != EOK)
|
---|
504 | goto error;
|
---|
505 |
|
---|
506 | rc = gfx_set_color(gc, color);
|
---|
507 | if (rc != EOK)
|
---|
508 | goto error;
|
---|
509 |
|
---|
510 | rect.p0.x = w / 20;
|
---|
511 | rect.p0.y = 2 * h / 15;
|
---|
512 | rect.p1.x = w - w / 20;
|
---|
513 | rect.p1.y = 5 * h / 15;
|
---|
514 |
|
---|
515 | rc = gfx_fill_rect(gc, &rect);
|
---|
516 | if (rc != EOK)
|
---|
517 | goto error;
|
---|
518 |
|
---|
519 | gfx_color_delete(color);
|
---|
520 |
|
---|
521 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
|
---|
522 | if (rc != EOK)
|
---|
523 | goto error;
|
---|
524 |
|
---|
525 | rc = gfx_set_color(gc, color);
|
---|
526 | if (rc != EOK)
|
---|
527 | goto error;
|
---|
528 |
|
---|
529 | gfx_text_fmt_init(&fmt);
|
---|
530 |
|
---|
531 | pos.x = rect.p0.x;
|
---|
532 | pos.y = rect.p0.y;
|
---|
533 | rc = gfx_puttext(font, &pos, &fmt, "Top left");
|
---|
534 | if (rc != EOK) {
|
---|
535 | printf("Error rendering text.\n");
|
---|
536 | goto error;
|
---|
537 | }
|
---|
538 |
|
---|
539 | pos.x = (rect.p0.x + rect.p1.x - 1) / 2;
|
---|
540 | pos.y = rect.p0.y;
|
---|
541 | fmt.halign = gfx_halign_center;
|
---|
542 | rc = gfx_puttext(font, &pos, &fmt, "Top center");
|
---|
543 | if (rc != EOK)
|
---|
544 | goto error;
|
---|
545 |
|
---|
546 | pos.x = rect.p1.x - 1;
|
---|
547 | pos.y = rect.p0.y;
|
---|
548 | fmt.halign = gfx_halign_right;
|
---|
549 | rc = gfx_puttext(font, &pos, &fmt, "Top right");
|
---|
550 | if (rc != EOK)
|
---|
551 | goto error;
|
---|
552 |
|
---|
553 | fmt.valign = gfx_valign_center;
|
---|
554 |
|
---|
555 | pos.x = rect.p0.x;
|
---|
556 | pos.y = (rect.p0.y + rect.p1.y - 1) / 2;
|
---|
557 | fmt.halign = gfx_halign_left;
|
---|
558 | rc = gfx_puttext(font, &pos, &fmt, "Center left");
|
---|
559 | if (rc != EOK)
|
---|
560 | goto error;
|
---|
561 |
|
---|
562 | pos.x = (rect.p0.x + rect.p1.x - 1) / 2;
|
---|
563 | pos.y = (rect.p0.y + rect.p1.y - 1) / 2;
|
---|
564 | fmt.halign = gfx_halign_center;
|
---|
565 | rc = gfx_puttext(font, &pos, &fmt, "Center");
|
---|
566 | if (rc != EOK)
|
---|
567 | goto error;
|
---|
568 |
|
---|
569 | pos.x = rect.p1.x - 1;
|
---|
570 | pos.y = (rect.p0.y + rect.p1.y - 1) / 2;
|
---|
571 | fmt.halign = gfx_halign_right;
|
---|
572 | rc = gfx_puttext(font, &pos, &fmt, "Center right");
|
---|
573 | if (rc != EOK)
|
---|
574 | goto error;
|
---|
575 |
|
---|
576 | fmt.valign = gfx_valign_bottom;
|
---|
577 |
|
---|
578 | pos.x = rect.p0.x;
|
---|
579 | pos.y = rect.p1.y - 1;
|
---|
580 | fmt.halign = gfx_halign_left;
|
---|
581 | rc = gfx_puttext(font, &pos, &fmt, "Bottom left");
|
---|
582 | if (rc != EOK)
|
---|
583 | goto error;
|
---|
584 |
|
---|
585 | pos.x = (rect.p0.x + rect.p1.x - 1) / 2;
|
---|
586 | pos.y = rect.p1.y - 1;
|
---|
587 | fmt.halign = gfx_halign_center;
|
---|
588 | rc = gfx_puttext(font, &pos, &fmt, "Bottom center");
|
---|
589 | if (rc != EOK)
|
---|
590 | goto error;
|
---|
591 |
|
---|
592 | pos.x = rect.p1.x - 1;
|
---|
593 | pos.y = rect.p1.y - 1;
|
---|
594 | fmt.halign = gfx_halign_right;
|
---|
595 | rc = gfx_puttext(font, &pos, &fmt, "Bottom right");
|
---|
596 | if (rc != EOK)
|
---|
597 | goto error;
|
---|
598 |
|
---|
599 | gfx_color_delete(color);
|
---|
600 |
|
---|
601 | gfx_text_fmt_init(&fmt);
|
---|
602 |
|
---|
603 | for (i = 0; i < 8; i++) {
|
---|
604 | rc = gfx_color_new_rgb_i16((i & 4) ? 0xffff : 0,
|
---|
605 | (i & 2) ? 0xffff : 0, (i & 1) ? 0xffff : 0, &color);
|
---|
606 | if (rc != EOK)
|
---|
607 | goto error;
|
---|
608 |
|
---|
609 | rc = gfx_set_color(gc, color);
|
---|
610 | if (rc != EOK)
|
---|
611 | goto error;
|
---|
612 |
|
---|
613 | pos.x = w / 20;
|
---|
614 | pos.y = (7 + i) * h / 15;
|
---|
615 | rc = gfx_puttext(font, &pos, &fmt, "The quick brown fox jumps over the lazy dog.");
|
---|
616 | if (rc != EOK)
|
---|
617 | goto error;
|
---|
618 |
|
---|
619 | gfx_color_delete(color);
|
---|
620 | }
|
---|
621 |
|
---|
622 | for (i = 0; i < 10; i++) {
|
---|
623 | fibril_usleep(500 * 1000);
|
---|
624 | if (quit)
|
---|
625 | break;
|
---|
626 | }
|
---|
627 |
|
---|
628 | gfx_font_close(font);
|
---|
629 | gfx_typeface_destroy(tface);
|
---|
630 | return EOK;
|
---|
631 | error:
|
---|
632 | if (font != NULL)
|
---|
633 | gfx_font_close(font);
|
---|
634 | if (tface != NULL)
|
---|
635 | gfx_typeface_destroy(tface);
|
---|
636 | return rc;
|
---|
637 | }
|
---|
638 |
|
---|
639 | /** Run demo loop on a graphic context.
|
---|
640 | *
|
---|
641 | * @param gc Graphic context
|
---|
642 | * @param w Width
|
---|
643 | * @param h Height
|
---|
644 | */
|
---|
645 | static errno_t demo_loop(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
|
---|
646 | {
|
---|
647 | errno_t rc;
|
---|
648 |
|
---|
649 | while (!quit) {
|
---|
650 | rc = demo_rects(gc, w, h);
|
---|
651 | if (rc != EOK)
|
---|
652 | return rc;
|
---|
653 |
|
---|
654 | rc = demo_bitmap(gc, w, h);
|
---|
655 | if (rc != EOK)
|
---|
656 | return rc;
|
---|
657 |
|
---|
658 | rc = demo_bitmap2(gc, w, h);
|
---|
659 | if (rc != EOK)
|
---|
660 | return rc;
|
---|
661 |
|
---|
662 | rc = demo_bitmap_kc(gc, w, h);
|
---|
663 | if (rc != EOK)
|
---|
664 | return rc;
|
---|
665 |
|
---|
666 | rc = demo_text(gc, w, h);
|
---|
667 | if (rc != EOK)
|
---|
668 | return rc;
|
---|
669 | }
|
---|
670 |
|
---|
671 | return EOK;
|
---|
672 | }
|
---|
673 |
|
---|
674 | /** Run demo on console. */
|
---|
675 | static errno_t demo_console(void)
|
---|
676 | {
|
---|
677 | console_ctrl_t *con = NULL;
|
---|
678 | console_gc_t *cgc = NULL;
|
---|
679 | gfx_context_t *gc;
|
---|
680 | errno_t rc;
|
---|
681 |
|
---|
682 | printf("Init console..\n");
|
---|
683 | con = console_init(stdin, stdout);
|
---|
684 | if (con == NULL)
|
---|
685 | return EIO;
|
---|
686 |
|
---|
687 | printf("Create console GC\n");
|
---|
688 | rc = console_gc_create(con, stdout, &cgc);
|
---|
689 | if (rc != EOK)
|
---|
690 | return rc;
|
---|
691 |
|
---|
692 | gc = console_gc_get_ctx(cgc);
|
---|
693 |
|
---|
694 | rc = demo_loop(gc, 80, 25);
|
---|
695 | if (rc != EOK)
|
---|
696 | return rc;
|
---|
697 |
|
---|
698 | rc = console_gc_delete(cgc);
|
---|
699 | if (rc != EOK)
|
---|
700 | return rc;
|
---|
701 |
|
---|
702 | return EOK;
|
---|
703 | }
|
---|
704 |
|
---|
705 | /** Run demo on UI. */
|
---|
706 | static errno_t demo_ui(const char *display_spec)
|
---|
707 | {
|
---|
708 | ui_t *ui = NULL;
|
---|
709 | ui_wnd_params_t params;
|
---|
710 | ui_window_t *window = NULL;
|
---|
711 | gfx_context_t *gc;
|
---|
712 | gfx_rect_t rect;
|
---|
713 | gfx_rect_t wrect;
|
---|
714 | gfx_coord2_t off;
|
---|
715 | errno_t rc;
|
---|
716 |
|
---|
717 | printf("Init UI..\n");
|
---|
718 |
|
---|
719 | rc = ui_create(display_spec, &ui);
|
---|
720 | if (rc != EOK) {
|
---|
721 | printf("Error initializing UI (%s)\n", display_spec);
|
---|
722 | goto error;
|
---|
723 | }
|
---|
724 |
|
---|
725 | rect.p0.x = 0;
|
---|
726 | rect.p0.y = 0;
|
---|
727 | rect.p1.x = 400;
|
---|
728 | rect.p1.y = 300;
|
---|
729 |
|
---|
730 | ui_wnd_params_init(¶ms);
|
---|
731 | params.caption = "GFX Demo";
|
---|
732 |
|
---|
733 | /*
|
---|
734 | * Compute window rectangle such that application area corresponds
|
---|
735 | * to rect
|
---|
736 | */
|
---|
737 | ui_wdecor_rect_from_app(&rect, &wrect);
|
---|
738 | off = wrect.p0;
|
---|
739 | gfx_rect_rtranslate(&off, &wrect, ¶ms.rect);
|
---|
740 |
|
---|
741 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
742 | if (rc != EOK) {
|
---|
743 | printf("Error creating window.\n");
|
---|
744 | goto error;
|
---|
745 | }
|
---|
746 |
|
---|
747 | ui_window_set_cb(window, &ui_window_cb, NULL);
|
---|
748 |
|
---|
749 | rc = ui_window_get_app_gc(window, &gc);
|
---|
750 | if (rc != EOK) {
|
---|
751 | printf("Error creating graphic context.\n");
|
---|
752 | goto error;
|
---|
753 | }
|
---|
754 |
|
---|
755 | task_retval(0);
|
---|
756 |
|
---|
757 | rc = demo_loop(gc, rect.p1.x, rect.p1.y);
|
---|
758 | if (rc != EOK)
|
---|
759 | goto error;
|
---|
760 |
|
---|
761 | ui_window_destroy(window);
|
---|
762 | ui_destroy(ui);
|
---|
763 |
|
---|
764 | return EOK;
|
---|
765 | error:
|
---|
766 | if (window != NULL)
|
---|
767 | ui_window_destroy(window);
|
---|
768 | if (ui != NULL)
|
---|
769 | ui_destroy(ui);
|
---|
770 | return rc;
|
---|
771 | }
|
---|
772 |
|
---|
773 | /** Run demo on display server. */
|
---|
774 | static errno_t demo_display(const char *display_svc)
|
---|
775 | {
|
---|
776 | display_t *display = NULL;
|
---|
777 | gfx_context_t *gc;
|
---|
778 | display_wnd_params_t params;
|
---|
779 | display_window_t *window = NULL;
|
---|
780 | errno_t rc;
|
---|
781 |
|
---|
782 | printf("Init display..\n");
|
---|
783 |
|
---|
784 | rc = display_open(display_svc, &display);
|
---|
785 | if (rc != EOK) {
|
---|
786 | printf("Error opening display.\n");
|
---|
787 | return rc;
|
---|
788 | }
|
---|
789 |
|
---|
790 | display_wnd_params_init(¶ms);
|
---|
791 | params.rect.p0.x = 0;
|
---|
792 | params.rect.p0.y = 0;
|
---|
793 | params.rect.p1.x = 400;
|
---|
794 | params.rect.p1.y = 300;
|
---|
795 |
|
---|
796 | rc = display_window_create(display, ¶ms, &wnd_cb, NULL, &window);
|
---|
797 | if (rc != EOK) {
|
---|
798 | printf("Error creating window.\n");
|
---|
799 | return rc;
|
---|
800 | }
|
---|
801 |
|
---|
802 | rc = display_window_get_gc(window, &gc);
|
---|
803 | if (rc != EOK) {
|
---|
804 | printf("Error getting graphics context.\n");
|
---|
805 | return rc;
|
---|
806 | }
|
---|
807 |
|
---|
808 | task_retval(0);
|
---|
809 |
|
---|
810 | rc = demo_loop(gc, 400, 300);
|
---|
811 | if (rc != EOK)
|
---|
812 | return rc;
|
---|
813 |
|
---|
814 | rc = gfx_context_delete(gc);
|
---|
815 | if (rc != EOK)
|
---|
816 | return rc;
|
---|
817 |
|
---|
818 | display_window_destroy(window);
|
---|
819 | display_close(display);
|
---|
820 |
|
---|
821 | return EOK;
|
---|
822 | }
|
---|
823 |
|
---|
824 | static void wnd_close_event(void *arg)
|
---|
825 | {
|
---|
826 | printf("Close event\n");
|
---|
827 | quit = true;
|
---|
828 | }
|
---|
829 |
|
---|
830 | static void wnd_kbd_event(void *arg, kbd_event_t *event)
|
---|
831 | {
|
---|
832 | printf("Keyboard event type=%d key=%d\n", event->type, event->key);
|
---|
833 | if (event->type == KEY_PRESS)
|
---|
834 | quit = true;
|
---|
835 | }
|
---|
836 |
|
---|
837 | static void uiwnd_close_event(ui_window_t *window, void *arg)
|
---|
838 | {
|
---|
839 | printf("Close event\n");
|
---|
840 | quit = true;
|
---|
841 | }
|
---|
842 |
|
---|
843 | static void uiwnd_kbd_event(ui_window_t *window, void *arg, kbd_event_t *event)
|
---|
844 | {
|
---|
845 | printf("Keyboard event type=%d key=%d\n", event->type, event->key);
|
---|
846 | if (event->type == KEY_PRESS)
|
---|
847 | quit = true;
|
---|
848 | }
|
---|
849 |
|
---|
850 | static void print_syntax(void)
|
---|
851 | {
|
---|
852 | printf("Syntax: gfxdemo [-d <display>] {canvas|console|display}\n");
|
---|
853 | }
|
---|
854 |
|
---|
855 | int main(int argc, char *argv[])
|
---|
856 | {
|
---|
857 | errno_t rc;
|
---|
858 | const char *display_svc = DISPLAY_DEFAULT;
|
---|
859 | int i;
|
---|
860 |
|
---|
861 | i = 1;
|
---|
862 | while (i < argc && argv[i][0] == '-') {
|
---|
863 | if (str_cmp(argv[i], "-d") == 0) {
|
---|
864 | ++i;
|
---|
865 | if (i >= argc) {
|
---|
866 | printf("Argument missing.\n");
|
---|
867 | print_syntax();
|
---|
868 | return 1;
|
---|
869 | }
|
---|
870 |
|
---|
871 | display_svc = argv[i++];
|
---|
872 | } else {
|
---|
873 | printf("Invalid option '%s'.\n", argv[i]);
|
---|
874 | print_syntax();
|
---|
875 | return 1;
|
---|
876 | }
|
---|
877 | }
|
---|
878 |
|
---|
879 | if (i >= argc || str_cmp(argv[i], "display") == 0) {
|
---|
880 | rc = demo_display(display_svc);
|
---|
881 | if (rc != EOK)
|
---|
882 | return 1;
|
---|
883 | } else if (str_cmp(argv[i], "console") == 0) {
|
---|
884 | rc = demo_console();
|
---|
885 | if (rc != EOK)
|
---|
886 | return 1;
|
---|
887 | } else if (str_cmp(argv[i], "ui") == 0) {
|
---|
888 | rc = demo_ui(display_svc);
|
---|
889 | if (rc != EOK)
|
---|
890 | return 1;
|
---|
891 | } else {
|
---|
892 | print_syntax();
|
---|
893 | return 1;
|
---|
894 | }
|
---|
895 | }
|
---|
896 |
|
---|
897 | /** @}
|
---|
898 | */
|
---|