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