summaryrefslogtreecommitdiff
path: root/ROMs/life.c
blob: f35f9d0ad77dfd4e3b2f26ea94d14d17ec40f515 (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
#include "std.c"
#include "hid.c"
#include "screen.c"

#define GRID_SIZE   64
#define GRID_BYTES  8
#define GRID_WIDTH  8
#define GRID_HEIGHT 8

char getState(char* grid, int index)
{
    int byteIndex = index / 8;
    int bitIndex  = index % 8;
    int byte      = grid[byteIndex];

    return ((byte >> bitIndex) & 1) != 0;
}

int setState(char* grid, int index, int state)
{
    int byteIndex = index / 8;
    int bitIndex  = index % 8;
    int byte      = grid[byteIndex];
    
    if (state)
        byte |= (1 << bitIndex);
    else
        byte &= ~(1 << bitIndex);
    
    grid[byteIndex] = byte;
}

int drawGrid(char* grid)
{
    screen_clear(COLOR_BLACK);

    for (int y = 0; y < GRID_HEIGHT; y++)
    {
        for (int x = 0; x < GRID_WIDTH; x++)
        {
            int index = (y * GRID_WIDTH) + x;
            if (getState(grid, index))
                screen_setPixel(x, y, COLOR_WHITE);
        }
    }
}

int main()
{
    char* grid = new char[GRID_BYTES];

    // clear memory
    for (int i = 0; i < GRID_BYTES; i++)
        grid[i] = 0;

    // turn on pixel at (0,0)
    setState(grid, 0, 1);

    drawGrid(grid);

    while (1) { }
}