summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 793baa8c3affca9108ed439e188d8c34277b361e (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <string.h>

#include "common.h"
#include "gba-buttons.c"
#include "background-tiles.c"
#include "sprites.h"

//
// Main functions.
//

// TODO: Cleanup OBJ/OAM memory copying and access.
//
#define CBB_0  0
#define SBB_0 28

#define CROSS_TX 15
#define CROSS_TY 10

u16 *bg0_map = SCREENBLOCK_MEM[SBB_0];

void
init_map() {
    // initialize a background
    BG_CTRL_0 = BG_CHARBLOCK(CBB_0) | BG_SCREENBLOCK(SBB_0) | BG_SIZE(3);
    BG_H_SCROLL_0 = 0;
    BG_V_SCROLL_0 = 0;

    // (1) create the tiles: basic tile and a cross
    const Tile tiles[2] = {
        {{0x11111111, 0x01111111, 0x01111111, 0x01111111,
          0x01111111, 0x01111111, 0x01111111, 0x00000001}},
        {{0x00000000, 0x00100100, 0x01100110, 0x00011000,
          0x00011000, 0x01100110, 0x00100100, 0x00000000}},
    };
    TILE_MEM[CBB_0][0] = tiles[0];
    TILE_MEM[CBB_0][1] = tiles[1];

    // (2) create a palette
    PAL_BANK_BG[0][1] = rgb15(31,  0,  0);
    PAL_BANK_BG[1][1] = rgb15( 0, 31,  0);
    PAL_BANK_BG[2][1] = rgb15( 0,  0, 31);
    PAL_BANK_BG[3][1] = rgb15(16, 16, 16);

    // (3) Create a map: four contingent blocks of
    //   0x0000, 0x1000, 0x2000, 0x3000.
    u16 *pse = bg0_map;
    for(int i = 0; i < 4; i++) {
        for(int j = 0; j < 32 * 32; j++) {
            *pse++ = SCREENBLOCK_ENTRY_PAL(i) | 0;
        }
    }
}

int main(void) {
    init_map();
    // Configure the display in mode 0 to show OBJs, where tile memory is
    // sequential.
    DISP_CTRL = DISP_ENABLE_SPRITES | DISP_MODE_0 | DISP_BG_0;

    u32 tx, ty, se_curr, se_prev= CROSS_TY * 32 + CROSS_TX;

    bg0_map[se_prev]++; // initial position of cross

    // Initialize sprite button overlay.
    init_sprite_pal(0, COLOR_WHITE);
    init_sprites(0);
    init_button_sprites();

    int frame_counter = 0;
    int x = 0;
    int y = 0;
    while(true) {
        wait_vsync();
        poll_keys();

        if (key_hold(KEY_DOWN)) {
            y += 3;
        }
        if (key_hold(KEY_UP)) {
            y -= 3;
        }
        if (key_hold(KEY_LEFT)) {
            x -= 3;
        }
        if (key_hold(KEY_RIGHT)) {
            x += 3;
        }
        tx = ((x >> 3) + CROSS_TX) & 0x3F;
        ty = ((y >> 3) + CROSS_TY) & 0x3F;

        se_curr = se_index(tx, ty, 64);
        if(se_curr != se_prev) {
            bg0_map[se_prev]--;
            bg0_map[se_curr]++;
            se_prev= se_curr;
        }

        frame_counter++;
        BG_H_SCROLL_0 = x;
        BG_V_SCROLL_0 = y;

        update_button_sprites();
    };

    return 0;
}