From 8682decd56b19db48e39341aea97afc75ad65de9 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Wed, 26 Jul 2023 03:44:30 +0000 Subject: [PATCH] main: incomplete map scrolling --- main.cpp | 159 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 136 insertions(+), 23 deletions(-) diff --git a/main.cpp b/main.cpp index 0aa1556..5430169 100644 --- a/main.cpp +++ b/main.cpp @@ -94,7 +94,8 @@ constexpr inline void render_block(const uint32_t base_pattern, const uint32_t cell_y = map_y * 4 + block_y; const uint32_t cell_x = map_x * 4 + block_x; - vdp2.vram.u16[64 * cell_y + cell_x] = (base_pattern & 0xfff) + tile_ix; + // assumes NBG0 map plane_a is at offset 0 + vdp2.vram.u16[64 * (cell_y % 64) + (cell_x % 64)] = (base_pattern & 0xfff) + tile_ix; //vdp2.vram.u32[64 * cell_y + cell_x] = base_pattern + tile_ix; } } @@ -102,16 +103,24 @@ constexpr inline void render_block(const uint32_t base_pattern, constexpr int32_t last_map = map_t::wardens_house; -struct draw_state_t { +struct draw_t { struct { uint16_t tilesets[tilesets_ix_last]; // div 32 uint16_t spritesheets[spritesheets_ix_last]; // div 128 } base_pattern; }; +struct player_t { + struct { + int16_t x; + int16_t y; + }; +}; + struct state_t { int32_t map_ix; - draw_state_t draw; + draw_t draw; + player_t player; }; static state_t state = { 0 }; @@ -144,16 +153,22 @@ void load_vram() for (uint32_t i = 0; i < tilesets_ix_last; i++) vdp2_top = load_tileset(vdp2_top, tilesets_ix[i]); - vdp2.reg.CYCA0 = 0x0fff'ffff; - vdp2.reg.CYCA1 = 0xffff'ffff; - vdp2.reg.CYCB0 = 0xffff'ffff; - vdp2.reg.CYCB1 = 0x4fff'ffff; + vdp2.reg.CYCA0 = 0x0121'ffff; + vdp2.reg.CYCA1 = 0x0121'ffff; + vdp2.reg.CYCB0 = 0x4565'ffff; + vdp2.reg.CYCB1 = 0x4565'ffff; uint32_t vdp1_top = (sizeof (union vdp1_vram)); for (uint32_t i = 0; i < spritesheets_ix_last; i++) vdp1_top = load_sprite(vdp1_top, spritesheets_ix[i]); } +// screen space +constexpr int32_t origin_px_x = ((320 - 160) / 2); +constexpr int32_t origin_px_y = ((240 - 144) / 2); +constexpr int32_t origin_cell_x = origin_px_x / 8; +constexpr int32_t origin_cell_y = origin_px_y / 8; + void render_sprites() { uint32_t ix = 2; @@ -172,30 +187,106 @@ void render_sprites() vdp1.vram.cmd[ix].COLR = color_address; // non-palettized (rgb15) color data vdp1.vram.cmd[ix].SRCA = character_address; vdp1.vram.cmd[ix].SIZE = SIZE__X(16) | SIZE__Y(16); - vdp1.vram.cmd[ix].XA = 5 * 16; - vdp1.vram.cmd[ix].YA = 5 * 16; + vdp1.vram.cmd[ix].XA = origin_px_x + 4 * 16; + vdp1.vram.cmd[ix].YA = origin_px_y + 4 * 16 - 4; ix++; + + constexpr uint16_t top_x = 80 - 1; + constexpr uint16_t top_y = 48 - 1; + constexpr uint16_t bot_x = 239 + 1; + constexpr uint16_t bot_y = 191 + 1; + + vdp1.vram.cmd[ix].CTRL = CTRL__JP__JUMP_NEXT | CTRL__COMM__POLYLINE; + vdp1.vram.cmd[ix].LINK = 0; + // "Set [ECD] to '1' for polygons, polylines, and lines" + // "Be sure to set [SPD] to '1' for polygons, polylines, and lines" + // + // The "user clip mode" bit is not set in PMOD here, so setting "user clip + // coordinates" has no effect on this draw command. However, "system clip + // coordinates" and "local coordinates" are always applied, and must be set to + // reasonable values. + vdp1.vram.cmd[ix].PMOD = PMOD__ECD | PMOD__SPD; + vdp1.vram.cmd[ix].COLR = COLR__RGB | rgb15(255, 0, 255); + vdp1.vram.cmd[ix].XA = top_x; + vdp1.vram.cmd[ix].YA = top_y; + vdp1.vram.cmd[ix].XB = bot_x; + vdp1.vram.cmd[ix].YB = top_y; + vdp1.vram.cmd[ix].XC = bot_x; + vdp1.vram.cmd[ix].YC = bot_y; + vdp1.vram.cmd[ix].XD = top_x; + vdp1.vram.cmd[ix].YD = bot_y; + + ix++; + + vdp1.vram.cmd[ix].CTRL = CTRL__END; } +static uint16_t scroll = 0; + +/* + in block units: + + world screen + -----------|--------- + player 0,0 | map 4,4 + + scrolled 16,16 + player 1,1 | + + world | screen + --------------------------|--------- + top left = player x - 5 | (add 4) + top right = player x - 5 | (add 4) + + ---- + + screen scroll + + 0,0 -> screen 0,0 + 1,1 -> screen 16,16 + + */ + void render_map() { + /* + if (++scroll > 60) { + scroll = 0; + state.player.x += 1; + state.player.y += 1; + } + */ + + vdp2.reg.SCXIN0 = state.player.x * 16; + vdp2.reg.SCYIN0 = state.player.y * 16; + /* + vdp2.reg.WPSX0 = 80 << 1; + vdp2.reg.WPSY0 = 48; + vdp2.reg.WPEX0 = 239 << 1; + vdp2.reg.WPEY0 = 191; + vdp2.reg.WCTLA = WCTLA__N0W0E | WCTLA__N0W0A__OUTSIDE; + */ + const map_t& map = maps[maps_ix[state.map_ix]]; const uint8_t border_block = map_objects[maps_ix[state.map_ix]].border_block; const uint32_t base_pattern = state.draw.base_pattern.tilesets[map.tileset]; vdp2.reg.PNCN0 = PNCN0__N0PNB__1WORD | PNCN0__N0CNSM | PNCN0__N0SCN((base_pattern >> 10) & 0x1f); - for (uint32_t map_y = 0; map_y < 16; map_y++) { - for (uint32_t map_x = 0; map_x < 16; map_x++) { + int32_t origin_x = state.player.x; + int32_t origin_y = state.player.y; + + for (int32_t y = origin_y - 5; y <= origin_y + 6; y++) { + for (int32_t x = origin_x - 5; x <= origin_x + 5; x++) { const uint8_t block = - (map_x < map.width && map_y < map.height) - ? map.blocks.start[map.width * map_y + map_x] + (x < static_cast(map.width) && y < static_cast(map.height) && x > 0 && y > 0) + ? map.blocks.start[map.width * y + x] : border_block; render_block(base_pattern, tilesets[map.tileset], - map_x, - map_y, + x + 4, + y + 3, block); } } @@ -308,13 +399,17 @@ void init_vdp1() void init_vdp2() { vdp2.reg.PRISA = PRISA__S0PRIN(7); // Sprite register 0 PRIority Number + vdp2.reg.PRINA = PRINA__N0PRIN(5) + | PRINA__N1PRIN(6); // DISP: Please make sure to change this bit from 0 to 1 during V blank. vdp2.reg.TVMD = ( TVMD__DISP | TVMD__LSMD__NON_INTERLACE | TVMD__VRESO__240 | TVMD__HRESO__NORMAL_320); + vdp2.reg.EXTEN = 0; + /* set the color mode to 5bits per channel, 1024 colors */ - vdp2.reg.RAMCTL = RAMCTL__CRKTE | RAMCTL__CRMD__RGB_5BIT_1024 | RAMCTL__VRAMD | RAMCTL__VRBMD; + vdp2.reg.RAMCTL = RAMCTL__CRKTE | RAMCTL__CRMD__RGB_5BIT_1024;// | RAMCTL__VRAMD | RAMCTL__VRBMD; /* disable display of NBG0 */ vdp2.reg.BGON = 0; @@ -324,31 +419,49 @@ void init_vdp2() set character size for NBG0 to 1x1 cell */ vdp2.reg.CHCTLA = CHCTLA__N0CHCN__16_COLOR | CHCTLA__N0BMEN__CELL_FORMAT - | CHCTLA__N0CHSZ__1x1_CELL; + | CHCTLA__N0CHSZ__1x1_CELL + | CHCTLA__N1CHCN__16_COLOR + | CHCTLA__N1BMEN__CELL_FORMAT + | CHCTLA__N1CHSZ__1x1_CELL; /* plane size */ - vdp2.reg.PLSZ = PLSZ__N0PLSZ__1x1; + vdp2.reg.PLSZ = PLSZ__N0PLSZ__1x1 + | PLSZ__N1PLSZ__1x1; /* map plane offset 1-word: value of bit 6-0 * 0x2000 2-word: value of bit 5-0 * 0x4000 */ - constexpr int plane_a = 0; + constexpr int nbg0_plane_a = 0; + constexpr int nbg1_plane_a = 1; //constexpr int plane_a_offset = plane_a * 0x2000; //constexpr int page_size = 64 * 64 * 2; // N0PNB__1WORD (16-bit) //constexpr int plane_size = page_size * 1; - vdp2.reg.MPOFN = MPOFN__N0MP(0); // bits 8~6 - vdp2.reg.MPABN0 = MPABN0__N0MPB(0) | MPABN0__N0MPA(plane_a); // bits 5~0 - vdp2.reg.MPCDN0 = MPABN0__N0MPD(0) | MPABN0__N0MPC(0); // bits 5~0 + // bits 8~6 + vdp2.reg.MPOFN = MPOFN__N0MP(0) + | MPOFN__N1MP(0); + vdp2.reg.MPABN0 = MPABN0__N0MPB(0) | MPABN0__N0MPA(nbg0_plane_a); // bits 5~0 + vdp2.reg.MPCDN0 = MPCDN0__N0MPD(0) | MPCDN0__N0MPC(0); // bits 5~0 + + vdp2.reg.MPABN1 = MPABN1__N1MPB(0) | MPABN1__N1MPA(nbg1_plane_a); // bits 5~0 + vdp2.reg.MPCDN1 = MPCDN1__N1MPD(0) | MPCDN1__N1MPC(0); // bits 5~0 + + auto& base_pattern = state.draw.base_pattern.tilesets[tileset_t::overworld]; + + vdp2.reg.PNCN0 = PNCN0__N0PNB__1WORD | PNCN0__N0CNSM | PNCN0__N0SCN((base_pattern >> 10) & 0x1f); + vdp2.reg.PNCN1 = PNCN1__N1PNB__1WORD | PNCN1__N1CNSM | PNCN1__N1SCN((base_pattern >> 10) & 0x1f); + + vdp2.vram.u16[0x2000 / 2 + 0] = (base_pattern & 0xfff) + 1; + vdp2.vram.u16[0x2000 / 2 + 1] = (base_pattern & 0xfff) + 2; palette_data(); } void main() { - state.map_ix = map_t::celadon_city; + state.map_ix = map_t::pallet_town; load_vram();