First real map with real tiles

This commit is contained in:
Pyrex 2024-02-27 15:09:51 -08:00
parent 9ef88fb34d
commit d823220e90
9 changed files with 3756 additions and 2198 deletions

View File

@ -2,23 +2,23 @@ load("@bazel_skylib//rules:run_binary.bzl", "run_binary")
cc_library( cc_library(
name = "game", name = "game",
srcs = glob(["*.c"]) + [":art/game_demo_sprites.c", ":map/game_map.c"], srcs = glob(["*.c"]) + [":art/game_tiles.c", ":map/game_map.c"],
hdrs = glob(["*.h"]) + [":art/game_demo_sprites.h", ":map/game_map.h"], hdrs = glob(["*.h"]) + [":art/game_tiles.h", ":map/game_map.h"],
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
deps = ["//device:device", "//sys:sys"] deps = ["//device:device", "//sys:sys"]
) )
run_binary( run_binary(
name = "game_demo_sprites", name = "game_tiles",
args = [ args = [
"game_demo_sprites", "game_tiles",
"256", # n sprites "120", # n sprites
"0", # key color "0", # key color
"$(location :art/game_demo_sprites.png)", "$(location :art/game_tiles.png)",
"$(location :art/game_demo_sprites.c)" "$(location :art/game_tiles.c)"
], ],
srcs = [":art/game_demo_sprites.png"], srcs = [":art/game_tiles.png"],
outs = [":art/game_demo_sprites.c"], outs = [":art/game_tiles.c"],
tool = "//pytools:spritesheet", tool = "//pytools:spritesheet",
) )
@ -26,10 +26,10 @@ run_binary(
name = "game_map", name = "game_map",
args = [ args = [
"game_map", "game_map",
"$(location :map/crocparty.ldtk)", "$(location :map/game_map.ldtk)",
"$(location :map/game_map.c)", "$(location :map/game_map.c)",
], ],
srcs = [":map/crocparty.ldtk"], srcs = [":map/game_map.ldtk"],
outs = [":map/game_map.c"], outs = [":map/game_map.c"],
tool = "//pytools:mapdata" tool = "//pytools:mapdata"
) )

View File

@ -1,8 +0,0 @@
#ifndef CROCPARTY_GAME_DEMO_SPRITES_H
#define CROCPARTY_GAME_DEMO_SPRITES_H
#include "sys/sys.h"
extern sys_spritesheet game_demo_sprites;
#endif // CROCPARTY_GAME_DEMO_SPRITES_H

Binary file not shown.

8
game/art/game_tiles.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef CROCPARTY_GAME_TILES_H
#define CROCPARTY_GAME_TILES_H
#include "sys/sys.h"
extern sys_spritesheet game_tiles;
#endif // CROCPARTY_GAME_TILES_H

BIN
game/art/game_tiles.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 729 B

View File

@ -1,4 +1,4 @@
#include "art/game_demo_sprites.h" #include "art/game_tiles.h"
#include "device/device.h" #include "device/device.h"
#include "map/game_map.h" #include "map/game_map.h"
#include "game.h" #include "game.h"
@ -40,6 +40,24 @@ void game_init() {
} }
} }
sys_spal_set(0xfe, 0xffffffff); sys_spal_set(0xfe, 0xffffffff);
// pico palette
sys_spal_set(0x0, 0x000000ff);
sys_spal_set(0x1, 0x1d2b53ff);
sys_spal_set(0x2, 0x7e2553ff);
sys_spal_set(0x3, 0x008751ff);
sys_spal_set(0x4, 0xab5236ff);
sys_spal_set(0x5, 0x5f574fff);
sys_spal_set(0x6, 0xc2c3c7ff);
sys_spal_set(0x7, 0xfff1e8ff);
sys_spal_set(0x8, 0xff004dff);
sys_spal_set(0x9, 0xffa300ff);
sys_spal_set(0xa, 0xffec27ff);
sys_spal_set(0xb, 0x00e436ff);
sys_spal_set(0xc, 0x29adffff);
sys_spal_set(0xd, 0x83769cff);
sys_spal_set(0xe, 0xff77a8ff);
sys_spal_set(0xf, 0xffccaaff);
} }
void game_destroy() { void game_destroy() {
@ -49,8 +67,12 @@ void game_destroy() {
void game_update() { void game_update() {
game_frame += 4; game_frame += 4;
/*
map_x += 1; map_x += 1;
map_x %= 160; map_x += 32;
map_x %= 64;
map_x -= 32;
*/
ellipse_x0 += ellipse_dx0; ellipse_x0 += ellipse_dx0;
if (ellipse_x0 < 0 || ellipse_x0 > DEVICE_W * 4) { ellipse_dx0 *= -1; } if (ellipse_x0 < 0 || ellipse_x0 > DEVICE_W * 4) { ellipse_dx0 *= -1; }
@ -100,12 +122,12 @@ void game_draw() {
sys_dpal_reset(); sys_dpal_reset();
sys_sprite_draw_ext( sys_sprite_draw_ext(
game_demo_sprites, game_tiles,
0, 0,
x1, y1-64, x1, y1-64,
16, 8, 16, 8,
false, false false, false
); );
sys_map_draw(game_map, game_demo_sprites, 0, 0, map_x, 8, 32, 18); sys_map_draw(game_map, game_tiles, 0, 0, map_x, 0, 32, 18);
} }

File diff suppressed because one or more lines are too long

3699
game/map/game_map.ldtk Normal file

File diff suppressed because it is too large Load Diff

View File

@ -6,9 +6,6 @@ TEMPLATE = """
// generated code! be nice // generated code! be nice
#include "sys/sys.h" #include "sys/sys.h"
sys_i32 {{map_name}}_player_start_x = {{player_start.0}};
sys_i32 {{map_name}}_player_start_y = {{player_start.1}};
sys_maptile {{map_name}}_data[{{width * height}}] = { {{ tiles|join(",") }} }; sys_maptile {{map_name}}_data[{{width * height}}] = { {{ tiles|join(",") }} };
sys_map {{map_name}} = { sys_map {{map_name}} = {
.tiles={{map_name}}_data, .tiles={{map_name}}_data,
@ -18,13 +15,12 @@ sys_map {{map_name}} = {
""".lstrip() """.lstrip()
def main(map_name, fname_ldtk, fname_c): def main(map_name, fname_ldtk, fname_c):
width, height, tiles, player_start = load_mapdata(fname_ldtk) width, height, tiles = load_mapdata(fname_ldtk)
with open(fname_c, "wt") as output: with open(fname_c, "wt") as output:
output.write( output.write(
shared.templates.from_string(TEMPLATE).render( shared.templates.from_string(TEMPLATE).render(
map_name=map_name, map_name=map_name,
player_start=player_start,
tiles=tiles, tiles=tiles,
width=width, width=width,
height=height, height=height,
@ -34,29 +30,27 @@ def main(map_name, fname_ldtk, fname_c):
def load_mapdata(fname_ldtk): def load_mapdata(fname_ldtk):
sparse_tiles = {} sparse_tiles = {}
player_start_xy = None entities = []
with open(fname_ldtk, "rt") as f: with open(fname_ldtk, "rt") as f:
data = json.load(f) data = json.load(f)
for level in data["levels"]: for level in data["levels"]:
level_x = level["worldX"] // 8 level_x = level["worldX"] // 8
level_y = level["worldY"] // 8 level_y = level["worldY"] // 8
for layer in level["layerInstances"]: for layer in level["layerInstances"]:
w = layer["__cWid"] if layer["__identifier"] == "conceptual":
h = layer["__cHei"]
if layer["__identifier"] == "vague":
# for right now I use the vague layer to assign tiles # for right now I use the vague layer to assign tiles
for x, y, ix in annot_xy(layer["intGridCsv"], w, h): for tile in layer["autoLayerTiles"]:
if ix == 0: x, y = tile["px"]
continue x //= 8
y //= 8
ix = tile["t"]
sparse_tiles[level_x + x, level_y + y] = ix sparse_tiles[level_x + x, level_y + y] = ix
if layer["__identifier"] == "entities": if layer["__identifier"] == "entities":
for e in layer["entityInstances"]: for e in layer["entityInstances"]:
x, y = e["__grid"] raise NotImplementedError()
player_start_xy = (level_x + x, level_y + y)
assert player_start_xy is not None, "player start not found"
x_min = 0 x_min = 0
y_min = 0 y_min = 0
assert not any(x for (x, _) in sparse_tiles if x < x_min), "level can't be left of (0, 0)" assert not any(x for (x, _) in sparse_tiles if x < x_min), "level can't be left of (0, 0)"
@ -70,11 +64,9 @@ def load_mapdata(fname_ldtk):
if k in sparse_tiles: if k in sparse_tiles:
dense_tiles.append(sparse_tiles[k]) dense_tiles.append(sparse_tiles[k])
else: else:
dense_tiles.append(0) dense_tiles.append(255)
return width, height, dense_tiles, player_start_xy
return width, height, dense_tiles
def annot_xy(lst, w, h): def annot_xy(lst, w, h):