Чтение параметров тайлсета из tiles.json

This commit is contained in:
2026-08-28 18:42:24 +05:00
parent 297d03632d
commit 9627da093e
3 changed files with 80 additions and 2 deletions
+55
View File
@@ -4,6 +4,7 @@ import "base:runtime"
import "core:encoding/json"
import "core:fmt"
import "core:os"
import "core:path/filepath"
Map_File :: struct {
compression_level: int `json:"compressionlevel"`,
@@ -41,6 +42,60 @@ Tileset :: struct {
source: string `json:"source"`,
}
Tileset_File :: struct {
columns: int `json:"columns"`,
image: string `json:"image"`,
image_height: int `json:"imageheight"`,
image_width: int `json:"imagewidth"`,
margin: int `json:"margin"`,
name: string `json:"name"`,
spacing: int `json:"spacing"`,
tile_count: int `json:"tilecount"`,
tile_height: int `json:"tileheight"`,
tile_width: int `json:"tilewidth"`,
}
Tileset_Info :: struct {
texture_path: string,
tile_width: i32,
tile_height: i32,
columns: i32,
}
load_tileset :: proc(
path: string,
allocator: runtime.Allocator,
) -> (
out: Tileset_Info,
err: Map_Load_Error,
) {
context.allocator = allocator
data, os_err := os.read_entire_file(path, context.allocator)
if os_err != os.General_Error.None {
return {}, Map_Load_Error{type = .Err_Loading_File, msg = fmt.aprintf("Error reading file: %v (%v)", path, os_err)}
}
ts: Tileset_File
unm_err := json.unmarshal(data, &ts)
free(raw_data(data), context.allocator)
if unm_err != nil {
return {}, Map_Load_Error{type = .Err_Unmarshal, msg = fmt.aprintf("Error unmarshalling tileset: %v (%v)", path, unm_err)}
}
assert(ts.margin == 0 && ts.spacing == 0, "Tilesets with margin/spacing are not supported")
assert(ts.tile_width == 16 && ts.tile_height == 16, "Only 16x16 tiles are supported")
// image path in tiles.json is relative to the tileset file itself
elem := [2]string{filepath.dir(path), ts.image}
out.texture_path, _ = filepath.join(elem[:], context.allocator)
out.tile_width = i32(ts.tile_width)
out.tile_height = i32(ts.tile_height)
out.columns = i32(ts.columns)
return
}
Map_Load_Error_Type :: enum {
None,
Err_Loading_File,