3. Printing Text

Monolith has (will have) an internal font used to print bitmap fonts.

3.1. Bitmap font data

To make things easy, a default 8x8 bitmap font is baked into the source code.

<<norns_default_font>>=
#include "font.xbm.c"

3.2. Writing a glyph to a video buffer

This takes an 8x8 glyph via XY coordinate and copies it to a location in the video buffer.

gx and gy are the XY coordinates of the glyph.

ox and oy are offset coordinates in the video buffer.

<<norns_funcdefs>>=
void norns_draw_glyph(norns_videobuf *buf,
                      int gx, int gy,
                      int ox, int oy,
                      unsigned char fg, unsigned char bg);
<<norns_functions>>=
void norns_draw_glyph(norns_videobuf *buf,
                      int gx, int gy,
                      int ox, int oy,
                      unsigned char fg, unsigned char bg)
{
    int x, y;
    unsigned char v;
    char b;
    int stride;

    stride = font_width / 8; /* divide by 8 */

    for (y = 0; y < 8; y++) {
        for (x = 0; x < 8; x++) {
            b = font_bits[(8 * gy + y) * stride + gx];
            v = ((b & (1 << x)) != 0) ? fg : bg;
            norns_videobuf_write(buf,
                                 x + ox, y + oy,
                                 v);
        }
    }
}

3.3. Writing a letter to a video buffer

This takes a 'letter', converts it to an XY location, and writes it to a video buffer.

<<norns_funcdefs>>=
void norns_draw_letter(norns_videobuf *buf,
                       char c,
                       int ox, int oy,
                       unsigned char fg, unsigned char bg);
<<norns_functions>>=
void norns_draw_letter(norns_videobuf *buf,
                       char c,
                       int ox, int oy,
                       unsigned char fg, unsigned char bg)
{
    int gx, gy;
    char o;

    o = c - ' '; /* start at 0 */

    gx = o % (font_width >> 3);
    gy = o / (font_width >> 3);

    norns_draw_glyph(buf, gx, gy, ox, oy, fg, bg);
}

3.4. Writing a string to a video buffer

Takes in a string, and writes it to a location in a video buffer.

<<norns_funcdefs>>=
void norns_draw_string(norns_videobuf *buf,
                       int x, int y,
                       unsigned char fg, unsigned char bg,
                       const char *str);
<<norns_functions>>=
void norns_draw_string(norns_videobuf *buf,
                       int x, int y,
                       unsigned char fg, unsigned char bg,
                       const char *str)
{
    int len;
    int n;
    len = strlen(str);

    for (n = 0; n < len; n++) {
        norns_draw_letter(buf, str[n], x + 8*n, y, fg, bg);
    }
}



prev | home | next