main: add connection drawing

This commit is contained in:
Zack Buhman 2023-07-27 05:32:54 +00:00
parent da49dbeb2b
commit 9645b4b414
6 changed files with 102 additions and 60 deletions

View File

@ -147,10 +147,10 @@ void load_vram()
for (uint32_t i = 0; i < tileset_t::count; i++)
vdp2_top = load_tileset(vdp2_top, static_cast<enum tileset_t::tileset>(i));
vdp2.reg.CYCA0 = 0x0121'ffff;
vdp2.reg.CYCA1 = 0x0121'ffff;
vdp2.reg.CYCB0 = 0x4565'ffff;
vdp2.reg.CYCB1 = 0x4565'ffff;
vdp2.reg.CYCA0 = 0x0fff'ffff;
vdp2.reg.CYCA1 = 0x0fff'ffff;
vdp2.reg.CYCB0 = 0x4fff'ffff;
vdp2.reg.CYCB1 = 0x4fff'ffff;
uint32_t vdp1_top = (sizeof (union vdp1_vram));
for (uint32_t i = 0; i < spritesheet_t::count; i++)
@ -244,12 +244,48 @@ static uint16_t scroll = 0;
// there are 16 pixels per block
static inline uint8_t get_block(const map_t& map, int32_t block_x, int32_t block_y)
{
const uint8_t border_block = map_objects[state.map].border_block;
const bool x_lt = block_x < static_cast<int32_t>(map.width);
const bool y_lt = block_y < static_cast<int32_t>(map.height);
const bool x_gt = block_x >= 0;
const bool y_gt = block_y >= 0;
const bool inside_map = x_lt && y_lt && x_gt && y_gt;
const bool north = x_lt && x_gt && !(y_gt);
const bool south = x_lt && x_gt && !(y_lt);
const bool west = y_lt && y_gt && !(x_gt);
const bool east = y_lt && y_gt && !(x_lt);
#define _has(dir) (map.connections[map_t::connection_t::dir].map != map_t::unconnected)
#define _get(dir) (maps[map.connections[map_t::connection_t::dir].map])
#define _offset(dir) (map.connections[map_t::connection_t::dir].offset)
if (inside_map) {
return map.blocks.start[map.width * block_y + block_x];
} else if (north && _has(north)) {
const map_t& north_map = _get(north);
const uint32_t north_y = north_map.height + block_y;
const uint32_t north_x = block_x - _offset(north);
return get_block(north_map, north_x, north_y);
} else {
return border_block;
}
#undef _offset
#undef _get
#undef _has
}
void render_map()
{
if (++scroll > 4) {
state.player.x = 8 * 16;
if (++scroll > 1) {
scroll = 0;
state.player.x += 1;
state.player.y += 1;
//state.player.x += 1;
state.player.y -= 1;
}
vdp2.reg.SCXIN0 = state.player.x - 16;
@ -263,7 +299,6 @@ void render_map()
*/
const map_t& map = maps[state.map];
const uint8_t border_block = map_objects[state.map].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);
@ -274,13 +309,7 @@ void render_map()
for (int32_t y = origin_y - 3; y <= (origin_y + 3); y++) {
for (int32_t x = origin_x - 3; x <= (origin_x + 3); x++) {
const uint8_t block = ( (x < static_cast<int32_t>(map.width))
&& (y < static_cast<int32_t>(map.height))
&& (x >= 0)
&& (y >= 0))
? map.blocks.start[map.width * y + x]
: border_block;
//const uint8_t block = map.blocks.start[map.width * 0 + 0];
const uint8_t block = get_block(map, x, y);
render_block(base_pattern,
tilesets[map.tileset],
@ -447,6 +476,7 @@ void init_vdp2()
void main()
{
state.map = map_t::pallet_town;
state.player.y = 16 * 8;
load_vram();

View File

@ -3,6 +3,7 @@
#include <cstdint>
#include "gen/maps.hpp"
#include "gen/sprites.hpp"
struct position_t {
uint8_t x;
@ -44,12 +45,12 @@ struct object_event_t {
left,
right,
none,
boulder_movement_byte_2,
boulder_movement_byte_2, // fixme
};
enum type type;
position_t position;
uint8_t sprite_id; // fixme
enum spritesheet_t::spritesheet sprite_id; // fixme
enum movement movement;
enum range_or_direction range_or_direction;
uint8_t text_id; // fixme

View File

@ -1,32 +0,0 @@
from itertools import chain
from generate.maps import sorted_map_headers
def includes_header():
yield "#pragma once"
yield ""
yield '#include "maps.hpp"'
yield ""
def directions():
return set(
# fixme: improve the connection parser
# add dataclass fields
connection[0]
for map_header in sorted_map_headers()
for connection in map_header.connections
)
def struct_connection_t():
struct connection_t {
enum direction {
# directions for lulz
};
enum direction direction;
map_t::map map;
uint8_t offset;
};
def ():

View File

@ -2,6 +2,7 @@ from parse import parse
from generate.generate import renderer
from generate.maps import sorted_map_headers
from generate.sprites import sprite_name
def warp_event(ev):
x, y = ev.position
@ -36,7 +37,7 @@ def object_event(ev):
"{",
f".type = object_event_t::type::{ev.type},",
f".position = {{ {x}, {y} }},",
".sprite_id = 0,", # fixme
f".sprite_id = spritesheet_t::{sprite_name(ev.sprite_id)},",
f".movement = object_event_t::movement::{ev.movement.lower()},",
*(range_or_direction(ev)),
".text_id = 0,", # fixme

View File

@ -19,6 +19,10 @@ from generate.sort import default_sort
from generate.binary import binary_res, start_size_value
from generate.generate import renderer
from parse.map_header import Connection
directions = sorted(['north', 'south', 'east', 'west'])
def sorted_map_constants_list():
return sorted(parse.map_constants_list.items(), key=default_sort)
@ -71,7 +75,7 @@ def struct_tileset_t():
*tileset_names,
"};",
"",
f"static constexpr int32_t count = {len(_sorted_tilesets_constants_list)};"
f"static constexpr int32_t count = {len(_sorted_tilesets_constants_list)};",
"};",
]
@ -83,21 +87,37 @@ def struct_map_t():
)
return [
"struct map_t {",
"enum map {",
*map_names,
"last_map, // special map name",
"unused_map_ed, // special silph co elevator map name",
"unconnected, // special unconnected map",
"};",
"",
*struct_connection_t(),
"",
"start_size_t blocks;",
"enum tileset_t::tileset tileset;",
"uint32_t width;",
"uint32_t height;",
"",
"enum map {",
*map_names,
"last_map,", # special map name
"unused_map_ed,", # special silph co elevator map name
"};",
"connection_t connections[4];",
"",
f"static constexpr int32_t count = {len(_sorted_map_headers)};",
"};",
]
def struct_connection_t():
return [
"struct connection_t {",
"enum map_t::map map;",
"int8_t offset;",
"",
"enum direction {",
*(f"{d}," for d in directions),
"};",
"};",
]
def blockset_tileset(name: str):
tileset_index = parse.tileset_constants_list[name]
tileset_header = parse.tileset_headers_list[tileset_index]
@ -125,6 +145,25 @@ def tilesets():
yield from blockset_tileset(name)
yield "};"
def connections(map_header):
cs = dict((c.name, c) for c in map_header.connections)
assert len(cs) == len(map_header.connections), map_header.connections
unconnected = Connection(
name = "__invalid",
map_name1 = "Unconnected",
map_name2 = "unconnected",
offset = 0,
)
for direction in directions: # already sorted
connection = cs.get(direction, unconnected)
yield f"[map_t::connection_t::{direction}] = {{"
yield f".map = map_t::{connection.map_name2.lower()},"
yield f".offset = {connection.offset},"
yield "},"
def map(map_header):
block_path = parse.maps_blocks_list[map_header.blocks()]
map_constant = parse.map_constants_list[map_header.name2]
@ -136,6 +175,9 @@ def map(map_header):
f".tileset = tileset_t::{map_header.tileset.lower()},",
f".width = {map_constant.width},",
f".height = {map_constant.height},",
".connections = {",
*connections(map_header),
"},",
"},",
]

View File

@ -1,5 +1,5 @@
from pprint import pprint
from parse import parse
for i in parse.map_headers:
for i in parse.map_objects_list.items():
pprint(i)