altos/pong: Add initial pong implementation
[fw/altos] / src / pong / ao_pong_text.c
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 #include "ao.h"
16 #include "ao_draw.h"
17 #include "ao_draw_int.h"
18 #include "ao_pong_text.h"
19
20 static const uint16_t numbers[] = {
21 #include "ao_pong_font.h"
22 };
23
24 #define GLYPH_WIDTH     16
25 #define GLYPH_SPACING   20
26 #define GLYPH_HEIGHT    24
27 #define GLYPH_ASCENT    24
28
29 const struct ao_font ao_pong_font = {
30         .width = GLYPH_SPACING,
31         .height = GLYPH_HEIGHT,
32         .ascent = GLYPH_ASCENT,
33         .descent = GLYPH_HEIGHT - GLYPH_ASCENT,
34 };
35
36 void
37 ao_pong_text(const struct ao_bitmap     *dst,
38              int16_t                    x,
39              int16_t                    y,
40              char                       *string)
41 {
42         static uint32_t src[GLYPH_HEIGHT];
43         char            c;
44         int             h;
45
46         struct ao_bitmap        src_bitmap = {
47                 .base = src,
48                 .stride = 1,
49                 .width = GLYPH_WIDTH,
50                 .height = GLYPH_HEIGHT
51         };
52
53         y -= GLYPH_ASCENT;
54
55         while ((c = *string++)) {
56                 if (c == ' ') {
57                         ao_rect(dst, x, y, GLYPH_WIDTH, GLYPH_HEIGHT, 0, AO_COPY);
58                 } else {
59                         const uint16_t  *n = &numbers[(c - '0') * GLYPH_HEIGHT];
60
61                         for (h = 0; h < GLYPH_HEIGHT; h++)
62                                 src[h] = n[h];
63
64                         ao_copy(dst,
65                                 x, y, GLYPH_WIDTH, GLYPH_HEIGHT,
66                                 &src_bitmap,
67                                 0, 0, AO_COPY);
68                 }
69                 x += GLYPH_SPACING;
70         }
71 }