[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>
|
---|
[9259d20] | 44 | #include <io/console.h>
|
---|
[e0545de] | 45 | #include <io/pixelmap.h>
|
---|
[b3c185b6] | 46 | #include <stdbool.h>
|
---|
[9259d20] | 47 | #include <stdlib.h>
|
---|
[00e8290] | 48 | #include <str.h>
|
---|
[5bded44] | 49 | #include <task.h>
|
---|
[00e8290] | 50 | #include <window.h>
|
---|
[045186b] | 51 |
|
---|
[338d0935] | 52 | static void wnd_close_event(void *);
|
---|
[b3c185b6] | 53 | static void wnd_kbd_event(void *, kbd_event_t *);
|
---|
| 54 |
|
---|
| 55 | static display_wnd_cb_t wnd_cb = {
|
---|
[338d0935] | 56 | .close_event = wnd_close_event,
|
---|
[b3c185b6] | 57 | .kbd_event = wnd_kbd_event
|
---|
| 58 | };
|
---|
| 59 |
|
---|
| 60 | static bool quit = false;
|
---|
| 61 |
|
---|
[587b4cb] | 62 | /** Clear screen.
|
---|
| 63 | *
|
---|
| 64 | * @param gc Graphic context
|
---|
| 65 | * @param w Screen width
|
---|
| 66 | * @param h Screen height
|
---|
| 67 | */
|
---|
| 68 | static errno_t clear_scr(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
|
---|
| 69 | {
|
---|
| 70 | gfx_color_t *color = NULL;
|
---|
| 71 | gfx_rect_t rect;
|
---|
| 72 | errno_t rc;
|
---|
| 73 |
|
---|
| 74 | rc = gfx_color_new_rgb_i16(0, 0, 0, &color);
|
---|
| 75 | if (rc != EOK)
|
---|
| 76 | goto error;
|
---|
| 77 |
|
---|
| 78 | rc = gfx_set_color(gc, color);
|
---|
| 79 | if (rc != EOK)
|
---|
| 80 | goto error;
|
---|
| 81 |
|
---|
| 82 | rect.p0.x = 0;
|
---|
| 83 | rect.p0.y = 0;
|
---|
| 84 | rect.p1.x = w;
|
---|
| 85 | rect.p1.y = h;
|
---|
| 86 |
|
---|
| 87 | rc = gfx_fill_rect(gc, &rect);
|
---|
| 88 | if (rc != EOK)
|
---|
| 89 | goto error;
|
---|
| 90 |
|
---|
| 91 | gfx_color_delete(color);
|
---|
| 92 | return EOK;
|
---|
| 93 | error:
|
---|
| 94 | if (color != NULL)
|
---|
| 95 | gfx_color_delete(color);
|
---|
| 96 | return rc;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[00e8290] | 99 | /** Run rectangle demo on a graphic context.
|
---|
| 100 | *
|
---|
| 101 | * @param gc Graphic context
|
---|
[e0545de] | 102 | * @param w Width
|
---|
| 103 | * @param h Height
|
---|
[00e8290] | 104 | */
|
---|
[1822545] | 105 | static errno_t demo_rects(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
|
---|
[045186b] | 106 | {
|
---|
[9259d20] | 107 | gfx_color_t *color = NULL;
|
---|
| 108 | gfx_rect_t rect;
|
---|
[e0545de] | 109 | int i, j;
|
---|
[9259d20] | 110 | errno_t rc;
|
---|
| 111 |
|
---|
[587b4cb] | 112 | rc = clear_scr(gc, w, h);
|
---|
| 113 | if (rc != EOK)
|
---|
| 114 | return rc;
|
---|
| 115 |
|
---|
[e0545de] | 116 | for (j = 0; j < 10; j++) {
|
---|
[9259d20] | 117 | rc = gfx_color_new_rgb_i16(rand() % 0x10000, rand() % 0x10000,
|
---|
| 118 | rand() % 0x10000, &color);
|
---|
| 119 | if (rc != EOK)
|
---|
[00e8290] | 120 | return rc;
|
---|
[9259d20] | 121 |
|
---|
| 122 | rc = gfx_set_color(gc, color);
|
---|
| 123 | if (rc != EOK)
|
---|
[00e8290] | 124 | return rc;
|
---|
[9259d20] | 125 |
|
---|
| 126 | for (i = 0; i < 10; i++) {
|
---|
[00e8290] | 127 | rect.p0.x = rand() % (w - 1);
|
---|
| 128 | rect.p0.y = rand() % (h - 1);
|
---|
| 129 | rect.p1.x = rect.p0.x + rand() % (w - 1 - rect.p0.x);
|
---|
| 130 | rect.p1.y = rect.p0.y + rand() % (h - 1 - rect.p0.y);
|
---|
[9259d20] | 131 |
|
---|
| 132 | rc = gfx_fill_rect(gc, &rect);
|
---|
| 133 | if (rc != EOK)
|
---|
[00e8290] | 134 | return rc;
|
---|
[9259d20] | 135 | }
|
---|
| 136 |
|
---|
| 137 | gfx_color_delete(color);
|
---|
| 138 |
|
---|
| 139 | fibril_usleep(500 * 1000);
|
---|
[b3c185b6] | 140 |
|
---|
| 141 | if (quit)
|
---|
| 142 | break;
|
---|
[9259d20] | 143 | }
|
---|
[e0545de] | 144 |
|
---|
| 145 | return EOK;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[587b4cb] | 148 | /** Fill bitmap with tartan pattern.
|
---|
| 149 | *
|
---|
| 150 | * @param bitmap Bitmap
|
---|
| 151 | * @param w Bitmap width
|
---|
| 152 | * @param h Bitmap height
|
---|
| 153 | * @return EOK on success or an error code
|
---|
| 154 | */
|
---|
| 155 | static errno_t bitmap_tartan(gfx_bitmap_t *bitmap, gfx_coord_t w, gfx_coord_t h)
|
---|
| 156 | {
|
---|
| 157 | int i, j;
|
---|
| 158 | pixelmap_t pixelmap;
|
---|
| 159 | gfx_bitmap_alloc_t alloc;
|
---|
| 160 | errno_t rc;
|
---|
| 161 |
|
---|
| 162 | rc = gfx_bitmap_get_alloc(bitmap, &alloc);
|
---|
| 163 | if (rc != EOK)
|
---|
| 164 | return rc;
|
---|
| 165 |
|
---|
| 166 | /* In absence of anything else, use pixelmap */
|
---|
| 167 | pixelmap.width = w;
|
---|
| 168 | pixelmap.height = h;
|
---|
| 169 | pixelmap.data = alloc.pixels;
|
---|
| 170 |
|
---|
| 171 | for (i = 0; i < w; i++) {
|
---|
| 172 | for (j = 0; j < h; j++) {
|
---|
| 173 | pixelmap_put_pixel(&pixelmap, i, j,
|
---|
| 174 | PIXEL(255, (i % 30) < 3 ? 255 : 0,
|
---|
| 175 | (j % 30) < 3 ? 255 : 0, i / 2));
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | return EOK;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /** Fill bitmap with moire pattern.
|
---|
| 183 | *
|
---|
| 184 | * @param bitmap Bitmap
|
---|
| 185 | * @param w Bitmap width
|
---|
| 186 | * @param h Bitmap height
|
---|
| 187 | * @return EOK on success or an error code
|
---|
| 188 | */
|
---|
| 189 | static errno_t bitmap_moire(gfx_bitmap_t *bitmap, gfx_coord_t w, gfx_coord_t h)
|
---|
| 190 | {
|
---|
| 191 | int i, j;
|
---|
| 192 | int k;
|
---|
| 193 | pixelmap_t pixelmap;
|
---|
| 194 | gfx_bitmap_alloc_t alloc;
|
---|
| 195 | errno_t rc;
|
---|
| 196 |
|
---|
| 197 | rc = gfx_bitmap_get_alloc(bitmap, &alloc);
|
---|
| 198 | if (rc != EOK)
|
---|
| 199 | return rc;
|
---|
| 200 |
|
---|
| 201 | /* In absence of anything else, use pixelmap */
|
---|
| 202 | pixelmap.width = w;
|
---|
| 203 | pixelmap.height = h;
|
---|
| 204 | pixelmap.data = alloc.pixels;
|
---|
| 205 |
|
---|
| 206 | for (i = 0; i < w; i++) {
|
---|
| 207 | for (j = 0; j < h; j++) {
|
---|
| 208 | k = i * i + j * j;
|
---|
| 209 | pixelmap_put_pixel(&pixelmap, i, j,
|
---|
| 210 | PIXEL(255, k, k, k));
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | return EOK;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[bea947f] | 217 | /** Render circle to a bitmap.
|
---|
| 218 | *
|
---|
| 219 | * @param bitmap Bitmap
|
---|
| 220 | * @param w Bitmap width
|
---|
| 221 | * @param h Bitmap height
|
---|
| 222 | * @return EOK on success or an error code
|
---|
| 223 | */
|
---|
| 224 | static errno_t bitmap_circle(gfx_bitmap_t *bitmap, gfx_coord_t w, gfx_coord_t h)
|
---|
| 225 | {
|
---|
| 226 | int i, j;
|
---|
| 227 | int k;
|
---|
| 228 | pixelmap_t pixelmap;
|
---|
| 229 | gfx_bitmap_alloc_t alloc;
|
---|
| 230 | errno_t rc;
|
---|
| 231 |
|
---|
| 232 | rc = gfx_bitmap_get_alloc(bitmap, &alloc);
|
---|
| 233 | if (rc != EOK)
|
---|
| 234 | return rc;
|
---|
| 235 |
|
---|
| 236 | /* In absence of anything else, use pixelmap */
|
---|
| 237 | pixelmap.width = w;
|
---|
| 238 | pixelmap.height = h;
|
---|
| 239 | pixelmap.data = alloc.pixels;
|
---|
| 240 |
|
---|
| 241 | for (i = 0; i < w; i++) {
|
---|
| 242 | for (j = 0; j < h; j++) {
|
---|
| 243 | k = i * i + j * j;
|
---|
| 244 | pixelmap_put_pixel(&pixelmap, i, j,
|
---|
| 245 | k < w * w / 2 ? PIXEL(255, 0, 255, 0) :
|
---|
| 246 | PIXEL(0, 255, 0, 255));
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | return EOK;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[e0545de] | 253 | /** Run bitmap demo on a graphic context.
|
---|
| 254 | *
|
---|
| 255 | * @param gc Graphic context
|
---|
| 256 | * @param w Width
|
---|
| 257 | * @param h Height
|
---|
| 258 | */
|
---|
[1822545] | 259 | static errno_t demo_bitmap(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
|
---|
[e0545de] | 260 | {
|
---|
| 261 | gfx_bitmap_t *bitmap;
|
---|
| 262 | gfx_bitmap_params_t params;
|
---|
| 263 | int i, j;
|
---|
| 264 | gfx_coord2_t offs;
|
---|
| 265 | gfx_rect_t srect;
|
---|
| 266 | errno_t rc;
|
---|
[587b4cb] | 267 |
|
---|
| 268 | rc = clear_scr(gc, w, h);
|
---|
| 269 | if (rc != EOK)
|
---|
| 270 | return rc;
|
---|
[e0545de] | 271 |
|
---|
[a8eed5f] | 272 | gfx_bitmap_params_init(¶ms);
|
---|
[e0545de] | 273 | params.rect.p0.x = 0;
|
---|
| 274 | params.rect.p0.y = 0;
|
---|
| 275 | params.rect.p1.x = w;
|
---|
| 276 | params.rect.p1.y = h;
|
---|
| 277 |
|
---|
| 278 | rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
|
---|
| 279 | if (rc != EOK)
|
---|
| 280 | return rc;
|
---|
| 281 |
|
---|
[587b4cb] | 282 | rc = bitmap_tartan(bitmap, w, h);
|
---|
[e0545de] | 283 | if (rc != EOK)
|
---|
[587b4cb] | 284 | goto error;
|
---|
[e0545de] | 285 |
|
---|
| 286 | for (j = 0; j < 10; j++) {
|
---|
| 287 | for (i = 0; i < 5; i++) {
|
---|
| 288 | srect.p0.x = rand() % (w - 40);
|
---|
[d18f3b7] | 289 | srect.p0.y = rand() % (h - 20);
|
---|
[e0545de] | 290 | srect.p1.x = srect.p0.x + rand() % (w - srect.p0.x);
|
---|
| 291 | srect.p1.y = srect.p0.y + rand() % (h - srect.p0.y);
|
---|
| 292 | offs.x = 0;
|
---|
| 293 | offs.y = 0;
|
---|
| 294 |
|
---|
[0008c0f] | 295 | rc = gfx_bitmap_render(bitmap, &srect, &offs);
|
---|
| 296 | if (rc != EOK)
|
---|
| 297 | goto error;
|
---|
[587b4cb] | 298 | fibril_usleep(250 * 1000);
|
---|
[b3c185b6] | 299 |
|
---|
| 300 | if (quit)
|
---|
| 301 | break;
|
---|
[587b4cb] | 302 | }
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | gfx_bitmap_destroy(bitmap);
|
---|
| 306 |
|
---|
| 307 | return EOK;
|
---|
| 308 | error:
|
---|
| 309 | gfx_bitmap_destroy(bitmap);
|
---|
| 310 | return rc;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | /** Run second bitmap demo on a graphic context.
|
---|
| 314 | *
|
---|
| 315 | * @param gc Graphic context
|
---|
| 316 | * @param w Width
|
---|
| 317 | * @param h Height
|
---|
| 318 | */
|
---|
| 319 | static errno_t demo_bitmap2(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
|
---|
| 320 | {
|
---|
| 321 | gfx_bitmap_t *bitmap;
|
---|
| 322 | gfx_bitmap_params_t params;
|
---|
| 323 | int i, j;
|
---|
| 324 | gfx_coord2_t offs;
|
---|
| 325 | errno_t rc;
|
---|
| 326 |
|
---|
| 327 | rc = clear_scr(gc, w, h);
|
---|
| 328 | if (rc != EOK)
|
---|
| 329 | return rc;
|
---|
| 330 |
|
---|
[b3b00b6] | 331 | gfx_bitmap_params_init(¶ms);
|
---|
[587b4cb] | 332 | params.rect.p0.x = 0;
|
---|
| 333 | params.rect.p0.y = 0;
|
---|
| 334 | params.rect.p1.x = 40;
|
---|
| 335 | params.rect.p1.y = 20;
|
---|
| 336 |
|
---|
| 337 | rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
|
---|
| 338 | if (rc != EOK)
|
---|
| 339 | return rc;
|
---|
| 340 |
|
---|
| 341 | rc = bitmap_moire(bitmap, 40, 20);
|
---|
| 342 | if (rc != EOK)
|
---|
| 343 | goto error;
|
---|
| 344 |
|
---|
| 345 | for (j = 0; j < 10; j++) {
|
---|
| 346 | for (i = 0; i < 10; i++) {
|
---|
| 347 | offs.x = rand() % (w - 40);
|
---|
| 348 | offs.y = rand() % (h - 20);
|
---|
| 349 |
|
---|
| 350 | rc = gfx_bitmap_render(bitmap, NULL, &offs);
|
---|
| 351 | if (rc != EOK)
|
---|
| 352 | goto error;
|
---|
[e0545de] | 353 | }
|
---|
[587b4cb] | 354 |
|
---|
| 355 | fibril_usleep(500 * 1000);
|
---|
[b3c185b6] | 356 |
|
---|
| 357 | if (quit)
|
---|
| 358 | break;
|
---|
[e0545de] | 359 | }
|
---|
| 360 |
|
---|
| 361 | gfx_bitmap_destroy(bitmap);
|
---|
| 362 |
|
---|
| 363 | return EOK;
|
---|
[0008c0f] | 364 | error:
|
---|
| 365 | gfx_bitmap_destroy(bitmap);
|
---|
| 366 | return rc;
|
---|
[e0545de] | 367 | }
|
---|
[bea947f] | 368 | /** Run bitmap color key demo on a graphic context.
|
---|
| 369 | *
|
---|
| 370 | * @param gc Graphic context
|
---|
| 371 | * @param w Width
|
---|
| 372 | * @param h Height
|
---|
| 373 | */
|
---|
| 374 | static errno_t demo_bitmap_kc(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
|
---|
| 375 | {
|
---|
| 376 | gfx_bitmap_t *bitmap;
|
---|
| 377 | gfx_bitmap_params_t params;
|
---|
| 378 | int i, j;
|
---|
| 379 | gfx_coord2_t offs;
|
---|
| 380 | errno_t rc;
|
---|
| 381 |
|
---|
| 382 | rc = clear_scr(gc, w, h);
|
---|
| 383 | if (rc != EOK)
|
---|
| 384 | return rc;
|
---|
| 385 |
|
---|
[b3b00b6] | 386 | gfx_bitmap_params_init(¶ms);
|
---|
[bea947f] | 387 | params.rect.p0.x = 0;
|
---|
| 388 | params.rect.p0.y = 0;
|
---|
| 389 | params.rect.p1.x = 40;
|
---|
| 390 | params.rect.p1.y = 40;
|
---|
| 391 | params.flags = bmpf_color_key;
|
---|
| 392 | params.key_color = PIXEL(0, 255, 0, 255);
|
---|
| 393 |
|
---|
| 394 | rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
|
---|
| 395 | if (rc != EOK)
|
---|
| 396 | return rc;
|
---|
| 397 |
|
---|
| 398 | rc = bitmap_circle(bitmap, 40, 40);
|
---|
| 399 | if (rc != EOK)
|
---|
| 400 | goto error;
|
---|
| 401 |
|
---|
| 402 | for (j = 0; j < 10; j++) {
|
---|
| 403 | for (i = 0; i < 10; i++) {
|
---|
| 404 | offs.x = j * 20 + i * 20;
|
---|
| 405 | offs.y = i * 20;
|
---|
| 406 |
|
---|
| 407 | rc = gfx_bitmap_render(bitmap, NULL, &offs);
|
---|
| 408 | if (rc != EOK)
|
---|
| 409 | goto error;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | fibril_usleep(500 * 1000);
|
---|
| 413 |
|
---|
| 414 | if (quit)
|
---|
| 415 | break;
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | gfx_bitmap_destroy(bitmap);
|
---|
| 419 |
|
---|
| 420 | return EOK;
|
---|
| 421 | error:
|
---|
| 422 | gfx_bitmap_destroy(bitmap);
|
---|
| 423 | return rc;
|
---|
| 424 | }
|
---|
[e0545de] | 425 |
|
---|
| 426 | /** Run demo loop on a graphic context.
|
---|
| 427 | *
|
---|
| 428 | * @param gc Graphic context
|
---|
| 429 | * @param w Width
|
---|
| 430 | * @param h Height
|
---|
| 431 | */
|
---|
[1822545] | 432 | static errno_t demo_loop(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
|
---|
[e0545de] | 433 | {
|
---|
| 434 | errno_t rc;
|
---|
| 435 |
|
---|
[b3c185b6] | 436 | while (!quit) {
|
---|
[e0545de] | 437 | rc = demo_rects(gc, w, h);
|
---|
| 438 | if (rc != EOK)
|
---|
| 439 | return rc;
|
---|
| 440 |
|
---|
| 441 | rc = demo_bitmap(gc, w, h);
|
---|
| 442 | if (rc != EOK)
|
---|
| 443 | return rc;
|
---|
[587b4cb] | 444 |
|
---|
| 445 | rc = demo_bitmap2(gc, w, h);
|
---|
| 446 | if (rc != EOK)
|
---|
| 447 | return rc;
|
---|
[bea947f] | 448 |
|
---|
| 449 | rc = demo_bitmap_kc(gc, w, h);
|
---|
| 450 | if (rc != EOK)
|
---|
| 451 | return rc;
|
---|
[e0545de] | 452 | }
|
---|
[b3c185b6] | 453 |
|
---|
| 454 | return EOK;
|
---|
[00e8290] | 455 | }
|
---|
| 456 |
|
---|
| 457 | /** Run demo on console. */
|
---|
| 458 | static errno_t demo_console(void)
|
---|
| 459 | {
|
---|
| 460 | console_ctrl_t *con = NULL;
|
---|
| 461 | console_gc_t *cgc = NULL;
|
---|
| 462 | gfx_context_t *gc;
|
---|
| 463 | errno_t rc;
|
---|
| 464 |
|
---|
| 465 | printf("Init console..\n");
|
---|
| 466 | con = console_init(stdin, stdout);
|
---|
| 467 | if (con == NULL)
|
---|
| 468 | return EIO;
|
---|
| 469 |
|
---|
| 470 | printf("Create console GC\n");
|
---|
| 471 | rc = console_gc_create(con, stdout, &cgc);
|
---|
| 472 | if (rc != EOK)
|
---|
| 473 | return rc;
|
---|
| 474 |
|
---|
| 475 | gc = console_gc_get_ctx(cgc);
|
---|
| 476 |
|
---|
[e0545de] | 477 | rc = demo_loop(gc, 80, 25);
|
---|
[00e8290] | 478 | if (rc != EOK)
|
---|
| 479 | return rc;
|
---|
[9259d20] | 480 |
|
---|
[9be2358] | 481 | rc = console_gc_delete(cgc);
|
---|
| 482 | if (rc != EOK)
|
---|
[00e8290] | 483 | return rc;
|
---|
| 484 |
|
---|
| 485 | return EOK;
|
---|
| 486 | }
|
---|
| 487 |
|
---|
| 488 | /** Run demo on canvas. */
|
---|
[fd11144] | 489 | static errno_t demo_canvas(const char *display_svc)
|
---|
[00e8290] | 490 | {
|
---|
| 491 | canvas_gc_t *cgc = NULL;
|
---|
| 492 | gfx_context_t *gc;
|
---|
| 493 | window_t *window = NULL;
|
---|
| 494 | pixel_t *pixbuf = NULL;
|
---|
| 495 | surface_t *surface = NULL;
|
---|
| 496 | canvas_t *canvas = NULL;
|
---|
[1822545] | 497 | gfx_coord_t vw, vh;
|
---|
[00e8290] | 498 | errno_t rc;
|
---|
| 499 |
|
---|
| 500 | printf("Init canvas..\n");
|
---|
| 501 |
|
---|
[fd11144] | 502 | window = window_open(display_svc, NULL,
|
---|
[00e8290] | 503 | WINDOW_MAIN | WINDOW_DECORATED, "GFX Demo");
|
---|
| 504 | if (window == NULL) {
|
---|
| 505 | printf("Error creating window.\n");
|
---|
| 506 | return -1;
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | vw = 400;
|
---|
| 510 | vh = 300;
|
---|
| 511 |
|
---|
| 512 | pixbuf = calloc(vw * vh, sizeof(pixel_t));
|
---|
| 513 | if (pixbuf == NULL) {
|
---|
| 514 | printf("Error allocating memory for pixel buffer.\n");
|
---|
[c8cf261] | 515 | return ENOMEM;
|
---|
[00e8290] | 516 | }
|
---|
| 517 |
|
---|
| 518 | surface = surface_create(vw, vh, pixbuf, 0);
|
---|
| 519 | if (surface == NULL) {
|
---|
| 520 | printf("Error creating surface.\n");
|
---|
[c8cf261] | 521 | return EIO;
|
---|
[00e8290] | 522 | }
|
---|
| 523 |
|
---|
| 524 | canvas = create_canvas(window_root(window), NULL, vw, vh,
|
---|
| 525 | surface);
|
---|
| 526 | if (canvas == NULL) {
|
---|
| 527 | printf("Error creating canvas.\n");
|
---|
[c8cf261] | 528 | return EIO;
|
---|
[00e8290] | 529 | }
|
---|
| 530 |
|
---|
| 531 | window_resize(window, 0, 0, vw + 10, vh + 30, WINDOW_PLACEMENT_ANY);
|
---|
| 532 | window_exec(window);
|
---|
| 533 |
|
---|
| 534 | printf("Create canvas GC\n");
|
---|
| 535 | rc = canvas_gc_create(canvas, surface, &cgc);
|
---|
| 536 | if (rc != EOK)
|
---|
| 537 | return rc;
|
---|
| 538 |
|
---|
| 539 | gc = canvas_gc_get_ctx(cgc);
|
---|
| 540 |
|
---|
[5bded44] | 541 | task_retval(0);
|
---|
| 542 |
|
---|
[e0545de] | 543 | rc = demo_loop(gc, 400, 300);
|
---|
[00e8290] | 544 | if (rc != EOK)
|
---|
| 545 | return rc;
|
---|
| 546 |
|
---|
| 547 | rc = canvas_gc_delete(cgc);
|
---|
| 548 | if (rc != EOK)
|
---|
| 549 | return rc;
|
---|
| 550 |
|
---|
| 551 | return EOK;
|
---|
| 552 | }
|
---|
| 553 |
|
---|
[c8cf261] | 554 | /** Run demo on display server. */
|
---|
[fd11144] | 555 | static errno_t demo_display(const char *display_svc)
|
---|
[c8cf261] | 556 | {
|
---|
| 557 | display_t *display = NULL;
|
---|
| 558 | gfx_context_t *gc;
|
---|
[4d9c807] | 559 | display_wnd_params_t params;
|
---|
[c8cf261] | 560 | display_window_t *window = NULL;
|
---|
| 561 | errno_t rc;
|
---|
| 562 |
|
---|
| 563 | printf("Init display..\n");
|
---|
| 564 |
|
---|
[fd11144] | 565 | rc = display_open(display_svc, &display);
|
---|
[c8cf261] | 566 | if (rc != EOK) {
|
---|
| 567 | printf("Error opening display.\n");
|
---|
| 568 | return rc;
|
---|
| 569 | }
|
---|
| 570 |
|
---|
[4d9c807] | 571 | display_wnd_params_init(¶ms);
|
---|
| 572 | params.rect.p0.x = 0;
|
---|
| 573 | params.rect.p0.y = 0;
|
---|
| 574 | params.rect.p1.x = 400;
|
---|
| 575 | params.rect.p1.y = 300;
|
---|
| 576 |
|
---|
| 577 | rc = display_window_create(display, ¶ms, &wnd_cb, NULL, &window);
|
---|
[c8cf261] | 578 | if (rc != EOK) {
|
---|
| 579 | printf("Error creating window.\n");
|
---|
| 580 | return rc;
|
---|
| 581 | }
|
---|
| 582 |
|
---|
| 583 | rc = display_window_get_gc(window, &gc);
|
---|
| 584 | if (rc != EOK) {
|
---|
| 585 | printf("Error getting graphics context.\n");
|
---|
[32dde7e8] | 586 | return rc;
|
---|
[c8cf261] | 587 | }
|
---|
| 588 |
|
---|
[5bded44] | 589 | task_retval(0);
|
---|
| 590 |
|
---|
[e0545de] | 591 | rc = demo_loop(gc, 400, 300);
|
---|
[c8cf261] | 592 | if (rc != EOK)
|
---|
| 593 | return rc;
|
---|
| 594 |
|
---|
| 595 | rc = gfx_context_delete(gc);
|
---|
| 596 | if (rc != EOK)
|
---|
| 597 | return rc;
|
---|
| 598 |
|
---|
[e49b7997] | 599 | display_window_destroy(window);
|
---|
| 600 | display_close(display);
|
---|
| 601 |
|
---|
[c8cf261] | 602 | return EOK;
|
---|
| 603 | }
|
---|
| 604 |
|
---|
[338d0935] | 605 | static void wnd_close_event(void *arg)
|
---|
| 606 | {
|
---|
| 607 | printf("Close event\n");
|
---|
| 608 | quit = true;
|
---|
| 609 | }
|
---|
| 610 |
|
---|
[b3c185b6] | 611 | static void wnd_kbd_event(void *arg, kbd_event_t *event)
|
---|
| 612 | {
|
---|
| 613 | printf("Keyboard event type=%d key=%d\n", event->type, event->key);
|
---|
[5fae123] | 614 | if (event->type == KEY_PRESS)
|
---|
| 615 | quit = true;
|
---|
[b3c185b6] | 616 | }
|
---|
| 617 |
|
---|
[00e8290] | 618 | static void print_syntax(void)
|
---|
| 619 | {
|
---|
[fd11144] | 620 | printf("Syntax: gfxdemo [-d <display>] {canvas|console|display}\n");
|
---|
[00e8290] | 621 | }
|
---|
| 622 |
|
---|
| 623 | int main(int argc, char *argv[])
|
---|
| 624 | {
|
---|
| 625 | errno_t rc;
|
---|
[fd11144] | 626 | const char *display_svc = DISPLAY_DEFAULT;
|
---|
| 627 | int i;
|
---|
| 628 |
|
---|
| 629 | i = 1;
|
---|
| 630 | while (i < argc && argv[i][0] == '-') {
|
---|
| 631 | if (str_cmp(argv[i], "-d") == 0) {
|
---|
| 632 | ++i;
|
---|
| 633 | if (i >= argc) {
|
---|
| 634 | printf("Argument missing.\n");
|
---|
| 635 | print_syntax();
|
---|
| 636 | return 1;
|
---|
| 637 | }
|
---|
| 638 |
|
---|
| 639 | display_svc = argv[i++];
|
---|
| 640 | } else {
|
---|
| 641 | printf("Invalid option '%s'.\n", argv[i]);
|
---|
| 642 | print_syntax();
|
---|
| 643 | return 1;
|
---|
| 644 | }
|
---|
| 645 | }
|
---|
[00e8290] | 646 |
|
---|
[fd11144] | 647 | if (i >= argc || str_cmp(argv[i], "display") == 0) {
|
---|
| 648 | rc = demo_display(display_svc);
|
---|
[0b63dc2] | 649 | if (rc != EOK)
|
---|
| 650 | return 1;
|
---|
[fd11144] | 651 | } else if (str_cmp(argv[i], "console") == 0) {
|
---|
[00e8290] | 652 | rc = demo_console();
|
---|
| 653 | if (rc != EOK)
|
---|
| 654 | return 1;
|
---|
[fd11144] | 655 | } else if (str_cmp(argv[i], "canvas") == 0) {
|
---|
| 656 | rc = demo_canvas(display_svc);
|
---|
[00e8290] | 657 | if (rc != EOK)
|
---|
| 658 | return 1;
|
---|
| 659 | } else {
|
---|
| 660 | print_syntax();
|
---|
| 661 | return 1;
|
---|
| 662 | }
|
---|
[045186b] | 663 | }
|
---|
| 664 |
|
---|
| 665 | /** @}
|
---|
| 666 | */
|
---|