Browse Source

add curly braces to bank-switch macros

Thomas B 1 month ago
parent
commit
541666aeb9
9 changed files with 75 additions and 62 deletions
  1. 2
    2
      src/banks.h
  2. 2
    2
      src/input.c
  3. 12
    12
      src/main.c
  4. 17
    8
      src/maps.c
  5. 2
    2
      src/sample.c
  6. 2
    2
      src/score.c
  7. 10
    8
      src/sound.c
  8. 6
    6
      src/sprites.c
  9. 22
    20
      src/window.c

+ 2
- 2
src/banks.h View File

@@ -22,8 +22,8 @@
22 22
 
23 23
 #include <gbdk/platform.h>
24 24
 
25
-#define START_ROM_BANK_2(x) __previous__bank = CURRENT_BANK; SWITCH_ROM(x)
25
+#define START_ROM_BANK_2(x) __xyz_previous__bank = CURRENT_BANK; SWITCH_ROM(x); do
26 26
 #define START_ROM_BANK(x) uint8_t START_ROM_BANK_2(x)
27
-#define END_ROM_BANK() SWITCH_ROM(__previous__bank)
27
+#define END_ROM_BANK while (0); SWITCH_ROM(__xyz_previous__bank);
28 28
 
29 29
 #endif // __BANKS_H__

+ 2
- 2
src/input.c View File

@@ -41,13 +41,13 @@ void key_read(void) NONBANKED {
41 41
     joyp = joypad();
42 42
 
43 43
     if (debug_cnt < DEBUG_SEQUENCE_COUNT) {
44
-        START_ROM_BANK(BANK(input));
44
+        START_ROM_BANK(BANK(input)) {
45 45
             if (key_pressed(key_debug_sequence[debug_cnt])) {
46 46
                 debug_cnt++;
47 47
             } else if (key_pressed(0xFF)) {
48 48
                 debug_cnt = 0;
49 49
             }
50
-        END_ROM_BANK();
50
+        } END_ROM_BANK
51 51
     } else {
52 52
         if (key_pressed(0xFF ^ J_START)) {
53 53
             debug_cnt = 0;

+ 12
- 12
src/main.c View File

@@ -138,24 +138,24 @@ static void conf_screen(void) NONBANKED {
138 138
             }
139 139
             win_conf();
140 140
         } else if (key_pressed(J_LEFT)) {
141
-            START_ROM_BANK(BANK(main));
141
+            START_ROM_BANK(BANK(main)) {
142 142
                 if (*conf_entries[debug_menu_index].var > 0) {
143 143
                     (*conf_entries[debug_menu_index].var)--;
144 144
                 } else {
145 145
                     *conf_entries[debug_menu_index].var = conf_entries[debug_menu_index].max;
146 146
                 }
147 147
                 conf_write_crc();
148
-            END_ROM_BANK();
148
+            } END_ROM_BANK
149 149
             win_conf();
150 150
         } else if (key_pressed(J_RIGHT)) {
151
-            START_ROM_BANK(BANK(main));
151
+            START_ROM_BANK(BANK(main)) {
152 152
                 if (*conf_entries[debug_menu_index].var < conf_entries[debug_menu_index].max) {
153 153
                     (*conf_entries[debug_menu_index].var)++;
154 154
                 } else {
155 155
                     *conf_entries[debug_menu_index].var = 0;
156 156
                 }
157 157
                 conf_write_crc();
158
-            END_ROM_BANK();
158
+            } END_ROM_BANK
159 159
             win_conf();
160 160
         } else if (key_pressed(J_A) || key_pressed(J_B) || key_pressed(J_START)) {
161 161
             break;
@@ -353,7 +353,7 @@ static void splash(void) NONBANKED {
353 353
                     snd_note_off();
354 354
                     splash_win();
355 355
                 } else if (key_pressed(J_LEFT)) {
356
-                    START_ROM_BANK(BANK(main));
356
+                    START_ROM_BANK(BANK(main)) {
357 357
                         if (debug_entries[debug_menu_index].flag != DBG_NONE) {
358 358
                             conf_get()->debug_flags ^= debug_entries[debug_menu_index].flag;
359 359
                             conf_write_crc();
@@ -365,10 +365,10 @@ static void splash(void) NONBANKED {
365 365
                             }
366 366
                             switch_special = 1;
367 367
                         }
368
-                    END_ROM_BANK();
368
+                    } END_ROM_BANK
369 369
                     splash_win();
370 370
                 } else if (key_pressed(J_RIGHT)) {
371
-                    START_ROM_BANK(BANK(main));
371
+                    START_ROM_BANK(BANK(main)) {
372 372
                         if (debug_entries[debug_menu_index].flag != DBG_NONE) {
373 373
                             conf_get()->debug_flags ^= debug_entries[debug_menu_index].flag;
374 374
                             conf_write_crc();
@@ -380,10 +380,10 @@ static void splash(void) NONBANKED {
380 380
                             }
381 381
                             switch_special = 1;
382 382
                         }
383
-                    END_ROM_BANK();
383
+                    } END_ROM_BANK
384 384
                     splash_win();
385 385
                 } else if (key_pressed(J_A)) {
386
-                    START_ROM_BANK(BANK(main));
386
+                    START_ROM_BANK(BANK(main)) {
387 387
                         if (debug_entries[debug_menu_index].flag != DBG_NONE) {
388 388
                             conf_get()->debug_flags ^= debug_entries[debug_menu_index].flag;
389 389
                             conf_write_crc();
@@ -395,7 +395,7 @@ static void splash(void) NONBANKED {
395 395
                             }
396 396
                             switch_special = 1;
397 397
                         }
398
-                    END_ROM_BANK();
398
+                    } END_ROM_BANK
399 399
                     splash_win();
400 400
                 } else if (key_pressed(J_B)) {
401 401
                     conf_get()->debug_flags &= ~DBG_MENU;
@@ -530,11 +530,11 @@ static void sgb_init(void) NONBANKED {
530 530
 
531 531
     DISPLAY_ON;
532 532
 
533
-    START_ROM_BANK(BANK(border_sgb));
533
+    START_ROM_BANK(BANK(border_sgb)) {
534 534
         set_sgb_border((const uint8_t *)border_sgb_tiles, sizeof(border_sgb_tiles),
535 535
                        (const uint8_t *)border_sgb_map, sizeof(border_sgb_map),
536 536
                        (const uint8_t *)border_sgb_palettes, sizeof(border_sgb_palettes));
537
-    END_ROM_BANK();
537
+    } END_ROM_BANK
538 538
 
539 539
     DISPLAY_OFF;
540 540
 }

+ 17
- 8
src/maps.c View File

@@ -4,6 +4,9 @@
4 4
  *
5 5
  * Copyright (C) 2025 Thomas Buck <thomas@xythobuz.de>
6 6
  *
7
+ * Based on examples from gbdk-2020:
8
+ * https://github.com/gbdk-2020/gbdk-2020/blob/develop/gbdk-lib/examples/cross-platform/large_map
9
+ *
7 10
  * This program is free software: you can redistribute it and/or modify
8 11
  * it under the terms of the GNU General Public License as published by
9 12
  * the Free Software Foundation, either version 3 of the License, or
@@ -17,12 +20,18 @@
17 20
  * See <http://www.gnu.org/licenses/>.
18 21
  */
19 22
 
23
+#include <assert.h>
24
+
20 25
 #include "banks.h"
21 26
 #include "title_map.h"
22 27
 #include "bg_map.h"
23 28
 #include "util.h"
24 29
 #include "maps.h"
25 30
 
31
+// currently this assumption is hard-coded
32
+static_assert(bg_map_WIDTH == 256, "bg_map needs to be 256x256");
33
+static_assert(bg_map_HEIGHT == 256, "bg_map needs to be 256x256");
34
+
26 35
 #define POS_SCALE_BG 6
27 36
 
28 37
 // define this to disable mirrored map scaling support
@@ -52,7 +61,7 @@ static uint16_t camera_x, camera_y, old_camera_x, old_camera_y;
52 61
 static uint8_t map_pos_x, map_pos_y, old_map_pos_x, old_map_pos_y;
53 62
 
54 63
 void map_title(void) NONBANKED {
55
-    START_ROM_BANK(BANK(title_map));
64
+    START_ROM_BANK(BANK(title_map)) {
56 65
 
57 66
         set_bkg_palette(OAMF_CGB_PAL0, title_map_PALETTE_COUNT, title_map_palettes);
58 67
         set_bkg_data(0, title_map_TILE_COUNT, title_map_tiles);
@@ -76,13 +85,13 @@ void map_title(void) NONBANKED {
76 85
                       title_map_HEIGHT / title_map_TILE_H,
77 86
                       title_map_map);
78 87
 
79
-    END_ROM_BANK();
88
+    } END_ROM_BANK
80 89
 
81 90
     move_bkg(0, 0);
82 91
 }
83 92
 
84 93
 void map_game(void) NONBANKED {
85
-    START_ROM_BANK(BANK(bg_map));
94
+    START_ROM_BANK(BANK(bg_map)) {
86 95
 
87 96
         set_bkg_palette(OAMF_CGB_PAL0, bg_map_PALETTE_COUNT, bg_map_palettes);
88 97
         set_bkg_data(0, bg_map_TILE_COUNT, bg_map_tiles);
@@ -130,7 +139,7 @@ void map_game(void) NONBANKED {
130 139
 
131 140
 #endif // WRAP_BG
132 141
 
133
-    END_ROM_BANK();
142
+    } END_ROM_BANK
134 143
 }
135 144
 
136 145
 void map_move(int16_t delta_x, int16_t delta_y) NONBANKED {
@@ -148,17 +157,17 @@ void map_move(int16_t delta_x, int16_t delta_y) NONBANKED {
148 157
     map_pos_y = (uint8_t)(camera_y >> 3u);
149 158
     map_pos_x = (uint8_t)(camera_x >> 3u);
150 159
 
151
-    START_ROM_BANK(BANK(bg_map));
160
+    START_ROM_BANK(BANK(bg_map)) {
152 161
 
153 162
         // up or down
154 163
         if (map_pos_y != old_map_pos_y) {
155 164
             if (camera_y < old_camera_y) {
156 165
                 set_bkg_sub(map_pos_x, map_pos_y,
157
-                            MIN(DEVICE_SCREEN_WIDTH + 1, bg_map_mapWidth-map_pos_x), 1,
166
+                            MIN(DEVICE_SCREEN_WIDTH + 1, bg_map_mapWidth - map_pos_x), 1,
158 167
                             bg_map_map, bg_map_MAP_ATTRIBUTES, bg_map_mapWidth);
159 168
             } else if ((bg_map_mapHeight - DEVICE_SCREEN_HEIGHT) > map_pos_y) {
160 169
                 set_bkg_sub(map_pos_x, map_pos_y + DEVICE_SCREEN_HEIGHT,
161
-                            MIN(DEVICE_SCREEN_WIDTH + 1, bg_map_mapWidth-map_pos_x), 1,
170
+                            MIN(DEVICE_SCREEN_WIDTH + 1, bg_map_mapWidth - map_pos_x), 1,
162 171
                             bg_map_map, bg_map_MAP_ATTRIBUTES, bg_map_mapWidth);
163 172
             }
164 173
             old_map_pos_y = map_pos_y;
@@ -178,7 +187,7 @@ void map_move(int16_t delta_x, int16_t delta_y) NONBANKED {
178 187
             old_map_pos_x = map_pos_x;
179 188
         }
180 189
 
181
-    END_ROM_BANK();
190
+    } END_ROM_BANK
182 191
 
183 192
     // set old camera position to current camera position
184 193
     old_camera_x = camera_x;

+ 2
- 2
src/sample.c View File

@@ -80,12 +80,12 @@ void sample_isr(void) NONBANKED {
80 80
     NR51_REG = 0xBB; // turn CH3 off in left and right pan
81 81
     NR30_REG = 0x00; // turn DAC off
82 82
 
83
-    START_ROM_BANK(play_bank);
83
+    START_ROM_BANK(play_bank) {
84 84
         // load waveforms
85 85
         for (uint8_t i = 0; i < 16; i++) {
86 86
             _AUD3WAVERAM[i] = *(play_sample++);
87 87
         }
88
-    END_ROM_BANK();
88
+    } END_ROM_BANK
89 89
 
90 90
     NR30_REG = 0x80; // turn DAC on
91 91
     NR31_REG = 0xFE; // length of wave, 2nd shortest

+ 2
- 2
src/score.c View File

@@ -128,9 +128,9 @@ void score_lowest(uint8_t off, struct scores *t) BANKED {
128 128
 }
129 129
 
130 130
 void score_reset(void) NONBANKED {
131
-    START_ROM_BANK(BANK(score));
131
+    START_ROM_BANK(BANK(score)) {
132 132
         memcpy(conf_scores(), initial_scores, sizeof(struct scores) * SCORE_NUM * 2);
133
-    END_ROM_BANK();
133
+    } END_ROM_BANK
134 134
     conf_write_crc();
135 135
 }
136 136
 

+ 10
- 8
src/sound.c View File

@@ -65,9 +65,10 @@ static const struct snds snds[SND_COUNT] = {
65 65
 
66 66
 static void play_note(enum notes note) NONBANKED {
67 67
     if (note < SILENCE) {
68
-        START_ROM_BANK(BANK(sound));
69
-            uint16_t freq = frequencies[note];
70
-        END_ROM_BANK();
68
+        uint16_t freq;
69
+        START_ROM_BANK(BANK(sound)) {
70
+            freq = frequencies[note];
71
+        } END_ROM_BANK
71 72
 
72 73
         NR11_REG = 0x80 | duration; // 50% duty, higher value is shorter time (up to 0x3F)
73 74
         NR12_REG = (conf_get()->music_vol << 4) | 0x00; // given volume, no change
@@ -78,9 +79,10 @@ static void play_note(enum notes note) NONBANKED {
78 79
 
79 80
 static void play_note2(enum notes note) NONBANKED {
80 81
     if (note < SILENCE) {
81
-        START_ROM_BANK(BANK(sound));
82
-            uint16_t freq = frequencies[note];
83
-        END_ROM_BANK();
82
+        uint16_t freq;
83
+        START_ROM_BANK(BANK(sound)) {
84
+            freq = frequencies[note];
85
+        } END_ROM_BANK
84 86
 
85 87
         NR21_REG = 0x80 | duration; // 50% duty, higher value is shorter time (up to 0x3F)
86 88
         NR22_REG = (conf_get()->music_vol << 4) | 0x00; // given volume, no change
@@ -151,7 +153,7 @@ void snd_play(void) NONBANKED {
151 153
         return;
152 154
     }
153 155
 
154
-    START_ROM_BANK(bank);
156
+    START_ROM_BANK(bank) {
155 157
 
156 158
         uint16_t diff = timer_get() - last_t;
157 159
         if (diff >= music->duration) {
@@ -199,5 +201,5 @@ void snd_play(void) NONBANKED {
199 201
         }
200 202
 
201 203
 end:
202
-    END_ROM_BANK();
204
+    } END_ROM_BANK
203 205
 }

+ 6
- 6
src/sprites.c View File

@@ -26,7 +26,7 @@
26 26
 void spr_init(void) NONBANKED {
27 27
     uint8_t off = TILE_NUM_START;
28 28
     for (uint8_t i = 0; i < SPRITE_COUNT; i++) {
29
-        START_ROM_BANK(metasprites[i].bank);
29
+        START_ROM_BANK(metasprites[i].bank) {
30 30
             if (metasprites[i].off == TILE_NUM_START) {
31 31
                 metasprites[i].off = off;
32 32
                 off += metasprites[i].cnt;
@@ -34,7 +34,7 @@ void spr_init(void) NONBANKED {
34 34
             } else {
35 35
                 metasprites[i].off = metasprites[metasprites[i].off].off;
36 36
             }
37
-        END_ROM_BANK();
37
+        } END_ROM_BANK
38 38
     }
39 39
 }
40 40
 
@@ -45,11 +45,11 @@ void spr_init_pal(void) NONBANKED {
45 45
             bank = BANK(power_palettes);
46 46
         }
47 47
 
48
-        START_ROM_BANK(bank);
48
+        START_ROM_BANK(bank) {
49 49
             if ((metasprites[i].pa != NULL) && ((metasprites[i].pa_i & PALETTE_ALL_FLAGS) == PALETTE_PRELOAD)) {
50 50
                 set_sprite_palette(metasprites[i].pa_i, metasprites[i].pa_n, metasprites[i].pa);
51 51
             }
52
-        END_ROM_BANK();
52
+        } END_ROM_BANK
53 53
     }
54 54
 }
55 55
 
@@ -60,7 +60,7 @@ void spr_draw(enum SPRITES sprite, enum SPRITE_FLIP flip,
60 60
         return;
61 61
     }
62 62
 
63
-    START_ROM_BANK(metasprites[sprite].bank);
63
+    START_ROM_BANK(metasprites[sprite].bank) {
64 64
 
65 65
     if (frame >= metasprites[sprite].ms_n) {
66 66
         frame = 0;
@@ -119,7 +119,7 @@ void spr_draw(enum SPRITES sprite, enum SPRITE_FLIP flip,
119 119
             break;
120 120
     }
121 121
 
122
-    END_ROM_BANK();
122
+    } END_ROM_BANK
123 123
 }
124 124
 
125 125
 void spr_ship(enum SPRITE_ROT rot, uint8_t moving, uint8_t *hiwater) NONBANKED {

+ 22
- 20
src/window.c View File

@@ -49,19 +49,19 @@ static uint8_t fnt_off = 0;
49 49
 void win_init(uint8_t is_splash) NONBANKED {
50 50
     fnt_off = is_splash ? title_map_TILE_COUNT : bg_map_TILE_COUNT;
51 51
 
52
-    START_ROM_BANK(BANK(numbers_fnt));
52
+    START_ROM_BANK(BANK(numbers_fnt)) {
53 53
         set_bkg_palette(OAMF_CGB_PAL0 + bg_map_PALETTE_COUNT, numbers_fnt_PALETTE_COUNT, numbers_fnt_palettes);
54 54
         set_win_data(fnt_off, numbers_fnt_TILE_COUNT, numbers_fnt_tiles);
55
-    END_ROM_BANK();
55
+    } END_ROM_BANK
56 56
 
57
-    START_ROM_BANK_2(BANK(window));
57
+    START_ROM_BANK_2(BANK(window)) {
58 58
         set_bkg_palette(OAMF_CGB_PAL0 + bg_map_PALETTE_COUNT + numbers_fnt_PALETTE_COUNT, numbers_fnt_PALETTE_COUNT, num_pal_inv);
59
-    END_ROM_BANK();
59
+    } END_ROM_BANK
60 60
 
61 61
     if (is_splash) {
62
-        START_ROM_BANK_2(BANK(text_fnt));
62
+        START_ROM_BANK_2(BANK(text_fnt)) {
63 63
             set_win_data(fnt_off + numbers_fnt_TILE_COUNT, text_fnt_TILE_COUNT, text_fnt_tiles);
64
-        END_ROM_BANK();
64
+        } END_ROM_BANK
65 65
     }
66 66
 }
67 67
 
@@ -69,19 +69,19 @@ static void set_win_based(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
69 69
                           const uint8_t *tiles, uint8_t base_tile, uint8_t tile_bank,
70 70
                           const uint8_t *attributes, uint8_t attr_bank) NONBANKED {
71 71
     if (attributes != NULL) {
72
-        START_ROM_BANK(attr_bank);
72
+        START_ROM_BANK(attr_bank) {
73 73
             VBK_REG = VBK_ATTRIBUTES;
74 74
             set_win_tiles(x, y, w, h, attributes);
75
-        END_ROM_BANK();
75
+        } END_ROM_BANK
76 76
     } else {
77 77
         VBK_REG = VBK_ATTRIBUTES;
78 78
         fill_win_rect(x, y, w, h, 0x00);
79 79
     }
80 80
 
81
-    START_ROM_BANK(tile_bank);
81
+    START_ROM_BANK(tile_bank) {
82 82
         VBK_REG = VBK_TILES;
83 83
         set_win_based_tiles(x, y, w, h, tiles, base_tile);
84
-    END_ROM_BANK();
84
+    } END_ROM_BANK
85 85
 }
86 86
 
87 87
 static void set_win_based_attr(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
@@ -90,10 +90,10 @@ static void set_win_based_attr(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
90 90
     VBK_REG = VBK_ATTRIBUTES;
91 91
     fill_win_rect(x, y, w, h, attr);
92 92
 
93
-    START_ROM_BANK(tile_bank);
93
+    START_ROM_BANK(tile_bank) {
94 94
         VBK_REG = VBK_TILES;
95 95
         set_win_based_tiles(x, y, w, h, tiles, base_tile);
96
-    END_ROM_BANK();
96
+    } END_ROM_BANK
97 97
 }
98 98
 
99 99
 static void character(uint8_t c, uint8_t pos, uint8_t x_off, uint8_t y_off, uint8_t is_black) {
@@ -227,9 +227,9 @@ void win_score_draw(struct scores score, uint8_t off, uint8_t is_black) BANKED {
227 227
 }
228 228
 
229 229
 static void get_git(char *line_buff) NONBANKED {
230
-    START_ROM_BANK(BANK(git));
230
+    START_ROM_BANK(BANK(git)) {
231 231
         strncpy(line_buff, git_version, 2 * LINE_WIDTH);
232
-    END_ROM_BANK();
232
+    } END_ROM_BANK
233 233
 }
234 234
 
235 235
 void win_about(void) BANKED {
@@ -253,10 +253,11 @@ void win_about(void) BANKED {
253 253
 }
254 254
 
255 255
 static uint8_t get_debug(char *name_buff, uint8_t i) NONBANKED {
256
-    START_ROM_BANK(BANK(main));
256
+    uint8_t n_len;
257
+    START_ROM_BANK(BANK(main)) {
257 258
         strncpy(name_buff, debug_entries[i].name, ENTRY_NAME_LEN + 1);
258 259
 
259
-        uint8_t n_len = strlen(name_buff);
260
+        n_len = strlen(name_buff);
260 261
         name_buff[n_len] = ' ';
261 262
         if (debug_entries[i].flag == DBG_NONE) {
262 263
             if (debug_menu_index == i) {
@@ -269,7 +270,7 @@ static uint8_t get_debug(char *name_buff, uint8_t i) NONBANKED {
269 270
         }
270 271
         name_buff[n_len + 2] = '\0';
271 272
         n_len += 2;
272
-    END_ROM_BANK();
273
+    } END_ROM_BANK
273 274
     return n_len;
274 275
 }
275 276
 
@@ -292,10 +293,11 @@ void win_debug(void) BANKED {
292 293
 }
293 294
 
294 295
 static uint8_t get_conf(char *name_buff, uint8_t i) NONBANKED {
295
-    START_ROM_BANK(BANK(main));
296
+    uint8_t n_len;
297
+    START_ROM_BANK(BANK(main)) {
296 298
         strncpy(name_buff, conf_entries[i].name, ENTRY_NAME_LEN + 1);
297 299
 
298
-        uint8_t n_len = strlen(name_buff);
300
+        n_len = strlen(name_buff);
299 301
         name_buff[n_len] = ' ';
300 302
         if (*conf_entries[i].var < 10) {
301 303
             name_buff[n_len + 1] = *conf_entries[i].var + '0';
@@ -304,7 +306,7 @@ static uint8_t get_conf(char *name_buff, uint8_t i) NONBANKED {
304 306
         }
305 307
         name_buff[n_len + 2] = '\0';
306 308
         n_len += 2;
307
-    END_ROM_BANK();
309
+    } END_ROM_BANK
308 310
     return n_len;
309 311
 }
310 312
 

Loading…
Cancel
Save