Changeset 8dbd13d in mainline


Ignore:
Timestamp:
2021-05-10T18:36:49Z (3 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
da15002
Parents:
ae634f4
Message:

Display status line for each demo screen

Clipping helps to prevent accidentally drawing over the status line.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/gfxdemo/gfxdemo.c

    rae634f4 r8dbd13d  
    6969
    7070static bool quit = false;
     71static gfx_typeface_t *tface;
     72static gfx_font_t *font;
     73static gfx_coord_t vpad;
    7174
    7275/** Clear screen.
     
    107110}
    108111
    109 /** Run rectangle demo on a graphic context.
     112/** Initialize demo font.
    110113 *
    111114 * @param gc Graphic context
    112115 * @param w Width
    113116 * @param h Height
     117 * @return EOK on success or an error code
     118 */
     119static errno_t demo_font_init(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
     120{
     121        gfx_font_info_t *finfo;
     122        errno_t rc;
     123
     124        if (quit)
     125                return EOK;
     126
     127        /* XXX Crude way of detecting text mode */
     128        if (w < 256) {
     129                /* Create dummy font for text mode */
     130                rc = gfx_typeface_create(gc, &tface);
     131                if (rc != EOK) {
     132                        printf("Error creating typeface\n");
     133                        goto error;
     134                }
     135
     136                rc = gfx_font_create_textmode(tface, &font);
     137                if (rc != EOK) {
     138                        printf("Error creating font\n");
     139                        goto error;
     140                }
     141
     142                vpad = 0;
     143        } else {
     144                /* Load font */
     145                rc = gfx_typeface_open(gc, "/data/font/helena.tpf", &tface);
     146                if (rc != EOK) {
     147                        printf("Error opening typeface\n");
     148                        goto error;
     149                }
     150
     151                finfo = gfx_typeface_first_font(tface);
     152                if (finfo == NULL) {
     153                        printf("Typeface contains no font.\n");
     154                        rc = ENOENT;
     155                        goto error;
     156                }
     157
     158                rc = gfx_font_open(finfo, &font);
     159                if (rc != EOK) {
     160                        printf("Error opening font.\n");
     161                        goto error;
     162                }
     163
     164                vpad = 5;
     165        }
     166
     167        return EOK;
     168error:
     169        if (tface != NULL)
     170                gfx_typeface_destroy(tface);
     171        return rc;
     172}
     173
     174/** Finalize demo font. */
     175static void demo_font_fini(void)
     176{
     177        if (font == NULL)
     178                return;
     179
     180        gfx_font_close(font);
     181        font = NULL;
     182
     183        gfx_typeface_destroy(tface);
     184        tface = NULL;
     185}
     186
     187/** Start a new demo screen.
     188 *
     189 * Clear the screen, display a status line and set up clipping.
     190 *
     191 * @param gc Graphic context
     192 * @param w Width
     193 * @param h Height
     194 * @param text Demo screen description
     195 * @return EOK on success or an error code
     196 */
     197static errno_t demo_begin(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h,
     198    const char *text)
     199{
     200        gfx_text_fmt_t fmt;
     201        gfx_font_metrics_t metrics;
     202        gfx_coord2_t pos;
     203        gfx_color_t *color;
     204        gfx_rect_t rect;
     205        gfx_coord_t height;
     206        errno_t rc;
     207
     208        rc = gfx_set_clip_rect(gc, NULL);
     209        if (rc != EOK)
     210                return rc;
     211
     212        rc = clear_scr(gc, w, h);
     213        if (rc != EOK)
     214                return rc;
     215
     216        if (font != NULL) {
     217                rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
     218                if (rc != EOK)
     219                        goto error;
     220
     221                gfx_text_fmt_init(&fmt);
     222                fmt.color = color;
     223                fmt.halign = gfx_halign_center;
     224                fmt.valign = gfx_valign_bottom;
     225
     226                pos.x = w / 2;
     227                pos.y = h - 1;
     228                rc = gfx_puttext(font, &pos, &fmt, text);
     229                if (rc != EOK) {
     230                        printf("Error rendering text.\n");
     231                        gfx_color_delete(color);
     232                        goto error;
     233                }
     234
     235                gfx_color_delete(color);
     236
     237                gfx_font_get_metrics(font, &metrics);
     238                height = metrics.ascent + metrics.descent + 1;
     239        } else {
     240                height = 0;
     241        }
     242
     243        rect.p0.x = 0;
     244        rect.p0.y = 0;
     245        rect.p1.x = w;
     246        rect.p1.y = h - height - vpad;
     247        rc = gfx_set_clip_rect(gc, &rect);
     248        if (rc != EOK)
     249                return rc;
     250
     251        return EOK;
     252error:
     253        return rc;
     254}
     255
     256/** Run rectangle demo on a graphic context.
     257 *
     258 * @param gc Graphic context
     259 * @param w Width
     260 * @param h Height
    114261 */
    115262static errno_t demo_rects(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
     
    123270                return EOK;
    124271
    125         rc = clear_scr(gc, w, h);
     272        rc = demo_begin(gc, w, h, "Rectangle rendering");
    126273        if (rc != EOK)
    127274                return rc;
     
    282429                return EOK;
    283430
    284         rc = clear_scr(gc, w, h);
     431        rc = demo_begin(gc, w, h, "Bitmap rendering without offset");
    285432        if (rc != EOK)
    286433                return rc;
     
    345492                return EOK;
    346493
    347         rc = clear_scr(gc, w, h);
     494        rc = demo_begin(gc, w, h, "Bitmap rendering with offset");
    348495        if (rc != EOK)
    349496                return rc;
     
    404551                return EOK;
    405552
    406         rc = clear_scr(gc, w, h);
     553        rc = demo_begin(gc, w, h, "Bitmap rendering with color key");
    407554        if (rc != EOK)
    408555                return rc;
     
    505652        }
    506653
    507         rc = clear_scr(gc, w, h);
     654        rc = demo_begin(gc, w, h, "Text rendering");
    508655        if (rc != EOK)
    509656                goto error;
     
    542689
    543690        rect.p0.x = w / 20;
    544         rect.p0.y = 2 * h / 15;
     691        rect.p0.y = 1 * h / 15;
    545692        rect.p1.x = w - w / 20;
    546         rect.p1.y = 5 * h / 15;
     693        rect.p1.y = 4 * h / 15;
    547694
    548695        rc = gfx_fill_rect(gc, &rect);
     
    640787
    641788                pos.x = w / 20;
    642                 pos.y = (7 + i) * h / 15;
     789                pos.y = (6 + i) * h / 15;
    643790                rc = gfx_puttext(font, &pos, &fmt, "The quick brown fox jumps over the lazy dog.");
    644791                if (rc != EOK)
     
    684831                return EOK;
    685832
    686         rc = clear_scr(gc, w, h);
     833        rc = demo_begin(gc, w, h, "Clipping demonstration");
    687834        if (rc != EOK)
    688835                return rc;
     
    777924        errno_t rc;
    778925
     926        (void) demo_font_init(gc, w, h);
     927
    779928        while (!quit) {
    780929                rc = demo_rects(gc, w, h);
    781930                if (rc != EOK)
    782                         return rc;
     931                        goto error;
    783932
    784933                rc = demo_bitmap(gc, w, h);
    785934                if (rc != EOK)
    786                         return rc;
     935                        goto error;
    787936
    788937                rc = demo_bitmap2(gc, w, h);
    789938                if (rc != EOK)
    790                         return rc;
     939                        goto error;
    791940
    792941                rc = demo_bitmap_kc(gc, w, h);
    793942                if (rc != EOK)
    794                         return rc;
     943                        goto error;
    795944
    796945                rc = demo_text(gc, w, h);
    797946                if (rc != EOK)
    798                         return rc;
     947                        goto error;
    799948
    800949                rc = demo_clip(gc, w, h);
    801950                if (rc != EOK)
    802                         return rc;
    803         }
    804 
    805         return EOK;
     951                        goto error;
     952        }
     953
     954        demo_font_fini();
     955        return EOK;
     956error:
     957        demo_font_fini();
     958        return rc;
    806959}
    807960
Note: See TracChangeset for help on using the changeset viewer.