summaryrefslogtreecommitdiff
path: root/ROMs/typewriter.c
blob: a63a77d5a7dfc6d59cf74f73e3ad039a30ddcfe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#load "font.bin"
#include "lib.c"

int drawChar(int ch, int x, int y, int color)
{
    // adjust for jmp instruction
    char* fontStart = 0x1FFF + 2;
    int charStart = ch * 8;

    for (int yOff = 0; yOff < 8; yOff++)
    {
        int row = fontStart[charStart + yOff];

        for (int xOff = 0; xOff < 8; xOff++)
        {
            int bit = (row >> (7 - xOff)) & 1;
            int pix = bit ? 8 : color;
            setPixel(x + xOff, y + yOff, pix);
        }
    }
}

int print(int* text, int len, int x, int y, int color)
{
    for (int i = 0; i < len; i++)
    {
        int ch = text[i];
        drawChar(ch, x + (i * 8), y, color);
    }
}

int main() 
{
    int x = 0;
    int y = 0;
    while (true)
    {
        if (x >= SCREEN_WIDTH)
        {
            x = 0;
            y += 8;
        }

        if (y >= SCREEN_HEIGHT)
        {
            x = 0;
            y = 0;
            clear(8);
        }

        int sk = readKey();
        while (getKeyState(sk)) { }

        if (sk == SK_NL)
        {
            y += 8;
            x = 0;
            continue;
        }

        if (sk == SK_BS)
        {
            x -= 8;
            if (x < 0)
                x = 0;
            drawChar(' ', x, y, 1);
            continue;
        }
        
        drawChar(sk, x, y, 1);
        x += 8;
    }
}