summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-04-26 12:14:45 +0200
committerBad Diode <bd@badd10de.dev>2021-04-26 12:14:45 +0200
commit4e51d019fd0c1d21aa8e965fd68dbacec39a2576 (patch)
tree34408809929e21973746dc93eeaf48ca24fef964
parent24887f28c3eb6d67a7c1a0803520b3fb728ee4f3 (diff)
downloadgba-experiments-4e51d019fd0c1d21aa8e965fd68dbacec39a2576.tar.gz
gba-experiments-4e51d019fd0c1d21aa8e965fd68dbacec39a2576.zip
Test screenblock entry demo
-rw-r--r--src/common.h23
-rw-r--r--src/main.c62
-rw-r--r--src/sprites.h1
3 files changed, 73 insertions, 13 deletions
diff --git a/src/common.h b/src/common.h
index 35b4619..6047e5f 100644
--- a/src/common.h
+++ b/src/common.h
@@ -76,6 +76,20 @@
76#define BG_V_SCROLL_2 *((vu16*)(0x04000012 + 0x0004 * 2)) 76#define BG_V_SCROLL_2 *((vu16*)(0x04000012 + 0x0004 * 2))
77#define BG_V_SCROLL_3 *((vu16*)(0x04000012 + 0x0004 * 3)) 77#define BG_V_SCROLL_3 *((vu16*)(0x04000012 + 0x0004 * 3))
78 78
79// Screenblocks for BGs.
80typedef u16 ScreenBlock[1024];
81#define SCREENBLOCK_MEM ((ScreenBlock*)MEM_VRAM)
82
83// Screenblock entry bits.
84#define SCREENBLOCK_ENTRY_H_FLIP (1 << 0xA)
85#define SCREENBLOCK_ENTRY_V_FLIP (1 << 0xB)
86#define SCREENBLOCK_ENTRY_PAL(N) ((N) << 0xC)
87
88size_t se_index(size_t tile_x, size_t tile_y, size_t map_width) {
89 size_t sbb = ((tile_x >> 5) + (tile_y >> 5) * (map_width >> 5));
90 return sbb * 1024 + ((tile_x & 31) + (tile_y & 31) * 32);
91}
92
79// Screen settings. 93// Screen settings.
80#define SCREEN_WIDTH 240 94#define SCREEN_WIDTH 240
81#define SCREEN_HEIGHT 160 95#define SCREEN_HEIGHT 160
@@ -84,6 +98,9 @@
84// (RGB) have a 0--31 range. For example, pure red would be rgb15(31, 0, 0). 98// (RGB) have a 0--31 range. For example, pure red would be rgb15(31, 0, 0).
85typedef u16 Color; 99typedef u16 Color;
86 100
101// A palette is composed of 16 colors, with color at index 0 being transparent.
102typedef Color Palette[16];
103
87// 104//
88// Tile memory access. 105// Tile memory access.
89// 106//
@@ -96,9 +113,6 @@ typedef struct Tile {
96typedef Tile TileBlock[512]; 113typedef Tile TileBlock[512];
97#define TILE_MEM ((TileBlock*) MEM_VRAM) 114#define TILE_MEM ((TileBlock*) MEM_VRAM)
98 115
99typedef u16 ScreenBlock[1024];
100#define SCREENBLOCK_MEM ((ScreenBlock*)MEM_VRAM)
101
102// We can treat the screen as a HxW matrix. With the following macro we can 116// We can treat the screen as a HxW matrix. With the following macro we can
103// write a pixel to the screen at the (x, y) position using: 117// write a pixel to the screen at the (x, y) position using:
104// 118//
@@ -109,6 +123,8 @@ typedef Color Scanline[SCREEN_WIDTH];
109#define SCREEN_BUFFER ((u16*) MEM_VRAM) 123#define SCREEN_BUFFER ((u16*) MEM_VRAM)
110#define PAL_BUFFER_BG ((u16*) MEM_PAL) 124#define PAL_BUFFER_BG ((u16*) MEM_PAL)
111#define PAL_BUFFER_SPRITES ((u16*) 0x05000200) 125#define PAL_BUFFER_SPRITES ((u16*) 0x05000200)
126#define PAL_BANK_BG ((Palette*) MEM_PAL)
127#define PAL_BANK_SPRITES ((Palette*) 0x05000200)
112 128
113// 129//
114// Colors. 130// Colors.
@@ -277,5 +293,4 @@ key_hold(u32 key) {
277// Check if the given key/button is currently pressed. 293// Check if the given key/button is currently pressed.
278#define KEY_PRESSED(key) (~(KEY_INPUTS) & key) 294#define KEY_PRESSED(key) (~(KEY_INPUTS) & key)
279 295
280
281#endif // GBAEXP_COMMON_H 296#endif // GBAEXP_COMMON_H
diff --git a/src/main.c b/src/main.c
index b8f2334..793baa8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -10,23 +10,58 @@
10// 10//
11 11
12// TODO: Cleanup OBJ/OAM memory copying and access. 12// TODO: Cleanup OBJ/OAM memory copying and access.
13//
14#define CBB_0 0
15#define SBB_0 28
13 16
14int main(void) { 17#define CROSS_TX 15
15 // Load background palette. 18#define CROSS_TY 10
16 memcpy(&PAL_BUFFER_BG[0], &bg_palette, 512); 19
17 memcpy(&TILE_MEM[0][0], bg_data, 3168); 20u16 *bg0_map = SCREENBLOCK_MEM[SBB_0];
18 memcpy(&SCREENBLOCK_MEM[30][0], bg_map, 2048); 21
19 22void
20 // Configure BG0 to use 4bpp, 64x32 tile map in charblock 0 and screenblock 23init_map() {
21 // 31. 24 // initialize a background
22 BG_CTRL_0 = BG_CHARBLOCK(0) | BG_SCREENBLOCK(30) | BG_SIZE(0); 25 BG_CTRL_0 = BG_CHARBLOCK(CBB_0) | BG_SCREENBLOCK(SBB_0) | BG_SIZE(3);
23 BG_H_SCROLL_0 = 0; 26 BG_H_SCROLL_0 = 0;
24 BG_V_SCROLL_0 = 0; 27 BG_V_SCROLL_0 = 0;
25 28
29 // (1) create the tiles: basic tile and a cross
30 const Tile tiles[2] = {
31 {{0x11111111, 0x01111111, 0x01111111, 0x01111111,
32 0x01111111, 0x01111111, 0x01111111, 0x00000001}},
33 {{0x00000000, 0x00100100, 0x01100110, 0x00011000,
34 0x00011000, 0x01100110, 0x00100100, 0x00000000}},
35 };
36 TILE_MEM[CBB_0][0] = tiles[0];
37 TILE_MEM[CBB_0][1] = tiles[1];
38
39 // (2) create a palette
40 PAL_BANK_BG[0][1] = rgb15(31, 0, 0);
41 PAL_BANK_BG[1][1] = rgb15( 0, 31, 0);
42 PAL_BANK_BG[2][1] = rgb15( 0, 0, 31);
43 PAL_BANK_BG[3][1] = rgb15(16, 16, 16);
44
45 // (3) Create a map: four contingent blocks of
46 // 0x0000, 0x1000, 0x2000, 0x3000.
47 u16 *pse = bg0_map;
48 for(int i = 0; i < 4; i++) {
49 for(int j = 0; j < 32 * 32; j++) {
50 *pse++ = SCREENBLOCK_ENTRY_PAL(i) | 0;
51 }
52 }
53}
54
55int main(void) {
56 init_map();
26 // Configure the display in mode 0 to show OBJs, where tile memory is 57 // Configure the display in mode 0 to show OBJs, where tile memory is
27 // sequential. 58 // sequential.
28 DISP_CTRL = DISP_ENABLE_SPRITES | DISP_MODE_0 | DISP_BG_0; 59 DISP_CTRL = DISP_ENABLE_SPRITES | DISP_MODE_0 | DISP_BG_0;
29 60
61 u32 tx, ty, se_curr, se_prev= CROSS_TY * 32 + CROSS_TX;
62
63 bg0_map[se_prev]++; // initial position of cross
64
30 // Initialize sprite button overlay. 65 // Initialize sprite button overlay.
31 init_sprite_pal(0, COLOR_WHITE); 66 init_sprite_pal(0, COLOR_WHITE);
32 init_sprites(0); 67 init_sprites(0);
@@ -51,6 +86,15 @@ int main(void) {
51 if (key_hold(KEY_RIGHT)) { 86 if (key_hold(KEY_RIGHT)) {
52 x += 3; 87 x += 3;
53 } 88 }
89 tx = ((x >> 3) + CROSS_TX) & 0x3F;
90 ty = ((y >> 3) + CROSS_TY) & 0x3F;
91
92 se_curr = se_index(tx, ty, 64);
93 if(se_curr != se_prev) {
94 bg0_map[se_prev]--;
95 bg0_map[se_curr]++;
96 se_prev= se_curr;
97 }
54 98
55 frame_counter++; 99 frame_counter++;
56 BG_H_SCROLL_0 = x; 100 BG_H_SCROLL_0 = x;
diff --git a/src/sprites.h b/src/sprites.h
index e7439f1..e249e95 100644
--- a/src/sprites.h
+++ b/src/sprites.h
@@ -95,4 +95,5 @@ init_sprite_pal(size_t starting_index, Color col) {
95 } 95 }
96 96
97} 97}
98
98#endif // GBAEXP_SPRITES_H 99#endif // GBAEXP_SPRITES_H