gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-lib/examples/gb/sgb_border/sgb_border.c
1 #include "sgb_border.h"
2
3 #include <gb/gb.h>
4 #include <stdint.h>
5 #include <gb/sgb.h>
6 #include <string.h>
7
8 #define SGB_CHR_BLOCK0 0
9 #define SGB_CHR_BLOCK1 1
10
11 #define SGB_SCR_FREEZE 1
12 #define SGB_SCR_UNFREEZE 0
13
14 #define SGB_TRANSFER(A,B) map_buf[0]=(A),map_buf[1]=(B),sgb_transfer(map_buf)
15
16 // The display must be turned on before calling this function
17 // (with @ref DISPLAY_ON).
18 void set_sgb_border(unsigned char * tiledata, size_t tiledata_size,
19 unsigned char * tilemap, size_t tilemap_size,
20 unsigned char * palette, size_t palette_size) {
21 if (sgb_check()) {
22 unsigned char map_buf[20];
23 memset(map_buf, 0, sizeof(map_buf));
24
25 SGB_TRANSFER((SGB_MASK_EN << 3) | 1, SGB_SCR_FREEZE);
26
27 BGP_REG = OBP0_REG = OBP1_REG = 0xE4U;
28 SCX_REG = SCY_REG = 0U;
29
30 uint8_t tmp_lcdc = LCDC_REG;
31
32 HIDE_SPRITES, HIDE_WIN, SHOW_BKG;
33 DISPLAY_ON;
34 // prepare tilemap for SGB_BORDER_CHR_TRN (should display all 256 tiles)
35 uint8_t i = 0U;
36 for (uint8_t y = 0; y != 14U; ++y) {
37 uint8_t * dout = map_buf;
38 for (uint8_t x = 0U; x != 20U; ++x) {
39 *dout++ = i++;
40 }
41 set_bkg_tiles(0, y, 20, 1, map_buf);
42 }
43 memset(map_buf, 0, sizeof(map_buf));
44
45 // transfer tile data
46 uint8_t ntiles = (tiledata_size > 256 * 32) ? 0 : tiledata_size >> 5;
47 if ((!ntiles) || (ntiles > 128U)) {
48 set_bkg_data(0, 0, tiledata);
49 SGB_TRANSFER((SGB_CHR_TRN << 3) | 1, SGB_CHR_BLOCK0);
50 if (ntiles) ntiles -= 128U;
51 tiledata += (128 * 32);
52 set_bkg_data(0, ntiles << 1, tiledata);
53 SGB_TRANSFER((SGB_CHR_TRN << 3) | 1, SGB_CHR_BLOCK1);
54 } else {
55 set_bkg_data(0, ntiles << 1, tiledata);
56 SGB_TRANSFER((SGB_CHR_TRN << 3) | 1, SGB_CHR_BLOCK0);
57 }
58
59 // transfer map and palettes
60 set_bkg_data(0, (uint8_t)(tilemap_size >> 4), tilemap);
61 set_bkg_data(128, (uint8_t)(palette_size >> 4), palette);
62 SGB_TRANSFER((SGB_PCT_TRN << 3) | 1, 0);
63
64 LCDC_REG = tmp_lcdc;
65
66 // clear SCREEN
67 fill_bkg_rect(0, 0, 20, 18, 0);
68
69 SGB_TRANSFER((SGB_MASK_EN << 3) | 1, SGB_SCR_UNFREEZE);
70 }
71 }
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.