Compare commits

...

3 Commits

Author SHA1 Message Date
Axel Meyer
e5a350fcb2 Fix load_textures overwriting tile_size with nil for variable-size atlases
When an atlas declares tile_size_px = null (variable-size tiles), the
load_textures call was overwriting the map's tile_size with nil, causing
an arithmetic error in draw_layer on the first render frame.

Guard the tile_size update so it only applies when tile_size_px is
non-nil; maps using variable-size atlases keep their tile_size from
the stub default (32) set at load time.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-21 23:28:33 +02:00
Axel Meyer
99d00bd115 Rewrite draw_layer for atlas-handle + uv rendering; bump to 0.3.0
draw_layer now samples tile sub-rectangles from the atlas diffuse
texture via the ADR-0044 source-rect render primitive (13-arg
draw_sprite_transform). load_textures gains a walkable field in
tile-records (from atlas JSON) so gameplay queries work after the
tiles-dir is removed. load_tilemap falls back to an atlas-bootstrap
stub when the legacy tilemap JSON is absent, allowing maps.load to
succeed before load_textures is called. Texture loading uses pcall
to degrade gracefully in headless environments. Bumps lib version to
0.3.0 to flag the breaking change in tile-record shape (id is now
integer, name is the stable string identifier).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 23:09:02 +02:00
Axel Meyer
8a75aee48a Rewrite load_textures for paired-PNG atlas format
Replaces the per-tile texture-handle pattern with per-atlas handles
plus per-tile UV-rects. Each map-lib atlas record now holds a tiles-by-
id dictionary loaded from tiles.atlas.json and two texture handles
(diffuse + height) loaded from the paired PNGs. Tile-records expose
{id, name, uv, blocks_sight} fields; the breaking-change to numeric id
+ named name flows to all consumers in this slice.

Adds get_layer_diffuse_texture and get_layer_height_texture that
aggregate to a single handle when every cell in a layer references the
same atlas, returning nil for multi-atlas layers so lighting can fall
back to per-cell sampling.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 22:57:12 +02:00
2 changed files with 161 additions and 72 deletions

231
init.lua
View File

@@ -225,28 +225,48 @@ local function load_tilemap(full_id)
error(string.format("maps.load: cross-lib tilemap resolution deferred [DEPRECATED-MVP]; tilemap '%s' not from current module '%s'",
full_id, engine.module.id()))
end
-- Try legacy tilemap JSON first (M.1 path; present in pre-M.2 assets).
-- Fall back to an atlas-bootstrap stub when the file is absent (M.2 path):
-- load_textures will replace this stub with the real atlas data.
local path = "assets/tiles/" .. local_name .. ".tilemap.json"
local raw = engine.asset.load_json(path)
validate_tilemap_table(raw, path, local_name)
-- Cook tiles: copy raw fields and add texture + texture_handle slots.
local cooked = {}
for i, t in ipairs(raw.tiles) do
cooked[i] = {
id = t.id,
walkable = (t.walkable == true),
color = t.color, -- color-mode fallback
texture = t.texture, -- optional atlas-id
texture_handle = nil, -- populated by load_textures
local ok, raw = pcall(engine.asset.load_json, path)
if ok then
validate_tilemap_table(raw, path, local_name)
-- Cook tiles: copy raw fields and add texture + texture_handle slots.
local cooked = {}
for i, t in ipairs(raw.tiles) do
cooked[i] = {
id = t.id,
walkable = (t.walkable == true),
color = t.color, -- color-mode fallback
texture = t.texture, -- optional atlas-id
texture_handle = nil, -- populated by load_textures
}
end
local tilemap = {
id = full_id,
tile_size = raw.tile_size,
asset_pack = raw.asset_pack, -- optional alias-key
tiles = cooked,
}
tilemap_registry[full_id] = tilemap
return tilemap
end
local tilemap = {
id = full_id,
tile_size = raw.tile_size,
asset_pack = raw.asset_pack, -- optional alias-key
tiles = cooked,
-- M.2 atlas-bootstrap stub: placeholder so build_map_v2 can register the
-- map before load_textures replaces this record with real atlas data.
-- GID validation is intentionally relaxed (large stub palette).
engine.print(string.format(
"maps.load: tilemap JSON not found (%s); using atlas-bootstrap stub for '%s' (call load_textures to populate)",
path, full_id))
local stub_tiles = {}
for i = 1, 4096 do stub_tiles[i] = { id = i, name = "", walkable = false } end
local stub = {
id = full_id,
tile_size = 32,
tiles = stub_tiles,
}
tilemap_registry[full_id] = tilemap
return tilemap
tilemap_registry[full_id] = stub
return stub
end
local function build_map(t_map, tilemap)
@@ -317,6 +337,8 @@ end
-- These must be declared before the public draw_map* functions that call them.
-- =====================================================================
-- Internal: draw all cells in one layer using atlas-handle + UV sampling.
-- (M.2: replaces per-tile texture_handle pattern with atlas-level handle.)
local function draw_layer(m, layer_name)
local layer = m.layers[layer_name]
if not layer then return end
@@ -335,20 +357,15 @@ local function draw_layer(m, layer_name)
local px = x * ts
local py = y * ts
local rot_deg = rot_quad * 90.0
if tile.texture_handle then
engine.render.draw_sprite_transform(
tile.texture_handle,
px + ts / 2, py + ts / 2,
math.rad(rot_deg),
1.0, 1.0,
ts / 2, ts / 2,
0xFFFFFFFF
)
else
local c = tile.color or { 100, 100, 100 }
engine.render.draw_rect(px, py, ts, ts,
engine.render.rgb(c[1], c[2], c[3]))
end
engine.render.draw_sprite_transform(
atlas.diffuse_texture_handle,
px + ts / 2, py + ts / 2,
math.rad(rot_deg),
ts / tile.uv.w, ts / tile.uv.h,
ts / 2, ts / 2,
0xFFFFFFFF,
tile.uv.x, tile.uv.y, tile.uv.w, tile.uv.h
)
end
end
end
@@ -609,52 +626,124 @@ function M.is_indoor(x, y, map_id)
return m.roof[y * m.size.w + x + 1] == 1
end
-- ====================================================================
-- Resolve tilemap-tile atlas-ids to texture-handles via asset-lib.
-- Operates on the current map's tilemap; call after maps.set_current.
-- asset_aliases: { [alias-key] = asset-lib-id } from module's manifest.
-- ====================================================================
local function load_textures_for_tilemap(tm, asset_aliases)
if tm.asset_pack == nil then return end -- color-only tilemap, no textures
local lib_id = asset_aliases[tm.asset_pack]
if lib_id == nil then
error("maps.load_textures: asset-pack alias '" .. tm.asset_pack
.. "' not in asset_aliases")
end
local atlas_path = lib_id .. "/assets/atlas.json"
local atlas = engine.asset.load_json(atlas_path)
local pack = atlas[tm.asset_pack]
if pack == nil then
error("maps.load_textures: asset_pack '" .. tm.asset_pack
.. "' not declared in atlas of '" .. lib_id .. "'")
end
for _, tile in ipairs(tm.tiles) do
if tile.texture then
local entry = pack[tile.texture]
if entry == nil then
error("maps.load_textures: atlas-id '" .. tile.texture
.. "' not in asset_pack '" .. tm.asset_pack .. "'")
end
tile.texture_handle = engine.asset.load_texture(lib_id .. "/assets/" .. entry.file)
end
end
end
-- =====================================================================
-- Atlas-Loading (M.2: paired-PNG diffuse + height atlases)
-- Replaces the M.1-era tilemap-indexed loader. Each atlas-set lives at
-- <asset-lib>/assets/atlases/<atlas_id>/ with four files:
-- tiles.diffuse.atlas.png — RGBA8888 packed atlas
-- tiles.height.atlas.png — L8 grayscale heightmap
-- tiles.atlas.json — metadata + per-tile UV-rects
-- tiles.atlas.lock.json — name -> id stable bindings (not loaded
-- at runtime; only used by the baker)
-- =====================================================================
function M.load_textures(asset_aliases)
local map_id = M.current()
if map_id == nil then
error("maps.load_textures: no current map; call maps.set_current first")
end
local m = map_registry[map_id]
if m.schema_version == 2 then
for _, tm in ipairs(m.atlases) do
load_textures_for_tilemap(tm, asset_aliases)
end
return
if m.schema_version ~= 2 then
error("maps.load_textures: only schema-v2 maps supported in v0.3.0+")
end
-- v1 legacy: kept for M.create with manual v1 table
load_textures_for_tilemap(m.tilemap, asset_aliases)
for atlas_idx, atlas_alias in ipairs(m.atlas_aliases) do
local lib_id = asset_aliases[atlas_alias]
if lib_id == nil then
error(string.format(
"maps.load_textures: asset-alias '%s' not in module asset_aliases",
atlas_alias))
end
local base = lib_id .. "/assets/atlases/" .. atlas_alias
local meta = engine.asset.load_json(base .. "/tiles.atlas.json")
if meta.atlas_id ~= atlas_alias then
error(string.format(
"maps.load_textures: atlas_id mismatch in %s (json has '%s')",
base, meta.atlas_id))
end
-- Build tile-record dictionary keyed by integer ID
local tiles_by_id = {}
for _, t in ipairs(meta.tiles) do
tiles_by_id[t.id] = {
id = t.id,
name = t.name,
walkable = (t.walkable == true),
uv = { x = t.uv[1], y = t.uv[2], w = t.uv[3], h = t.uv[4] },
blocks_sight = t.blocks_sight,
}
end
-- Texture loading may fail in headless test environments (no OpenGL
-- context). Use pcall so that tile-record shape + walkability tests
-- still pass; only the handle is nil when loading is unavailable.
local ok_d, diffuse_h = pcall(engine.asset.load_texture, base .. "/tiles.diffuse.atlas.png")
local ok_h, height_h = pcall(engine.asset.load_texture, base .. "/tiles.height.atlas.png")
m.atlases[atlas_idx] = {
id = atlas_alias,
atlas_id = meta.atlas_id,
atlas_size_px = meta.atlas_size_px,
tile_size_px = meta.tile_size_px,
tiles = tiles_by_id,
diffuse_texture_handle = ok_d and diffuse_h or nil,
height_texture_handle = ok_h and height_h or nil,
}
end
-- Update m.tile_size from first atlas only when the atlas declares a uniform
-- tile size. Variable-size atlases (tile_size_px = null) keep the tile_size
-- that was set by the stub or tilemap JSON at load time (typically 32).
if m.atlases[1] and m.atlases[1].tile_size_px ~= nil then
m.tile_size = m.atlases[1].tile_size_px
end
end
-- Returns the diffuse atlas-texture-handle for a layer, IF all cells in
-- the layer reference the same atlas-index. For multi-atlas layers
-- (cells with mixed atlas_idx in their packed-u32 GIDs) returns nil —
-- consumers must fall back to per-cell sampling. (M.2)
function M.get_layer_diffuse_texture(layer_name, map_id)
local id = map_id or current_map_id
if not id then error("maps.get_layer_diffuse_texture: no current map") end
local m = map_registry[id]
if not m.layers or not m.layers[layer_name] then return nil end
local tiles = m.layers[layer_name].tiles
local atlas_idx = nil
for i = 1, #tiles do
local gid = tiles[i] or 0
if gid ~= 0 then
local a = (gid >> 24) & 0xFF
if atlas_idx == nil then
atlas_idx = a
elseif atlas_idx ~= a then
return nil -- mixed atlases
end
end
end
if atlas_idx == nil then return nil end -- empty layer
local atlas = m.atlases[atlas_idx + 1]
return atlas and atlas.diffuse_texture_handle or nil
end
-- Same as above but for the height-channel. (M.2)
function M.get_layer_height_texture(layer_name, map_id)
local id = map_id or current_map_id
if not id then error("maps.get_layer_height_texture: no current map") end
local m = map_registry[id]
if not m.layers or not m.layers[layer_name] then return nil end
local tiles = m.layers[layer_name].tiles
local atlas_idx = nil
for i = 1, #tiles do
local gid = tiles[i] or 0
if gid ~= 0 then
local a = (gid >> 24) & 0xFF
if atlas_idx == nil then
atlas_idx = a
elseif atlas_idx ~= a then
return nil
end
end
end
if atlas_idx == nil then return nil end
local atlas = m.atlases[atlas_idx + 1]
return atlas and atlas.height_texture_handle or nil
end
function M.iterate_layers_pre_entities(fn, map_id)

View File

@@ -1 +1 @@
{"id":"lib-core.maps","version":"0.2.0","api_min":"0.1"}
{"id":"lib-core.maps","version":"0.3.0","api_min":"0.1"}