TEST-2: Implement parser for Tiled tileset files
This commit is contained in:
@@ -41,6 +41,45 @@ 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"`,
|
||||
tiled_version: string `json:"tiledversion"`,
|
||||
tile_height: int `json:"tileheight"`,
|
||||
tile_width: int `json:"tilewidth"`,
|
||||
type: string `json:"type"`,
|
||||
version: string `json:"version"`,
|
||||
}
|
||||
|
||||
Tileset_Load_Error_Type :: enum {
|
||||
None,
|
||||
Err_Loading_File,
|
||||
Err_Unmarshal,
|
||||
}
|
||||
|
||||
Tileset_Load_Error :: struct {
|
||||
type: Tileset_Load_Error_Type,
|
||||
msg: string,
|
||||
}
|
||||
|
||||
Tileset_Data :: struct {
|
||||
name: string,
|
||||
image: string,
|
||||
tile_width: i32,
|
||||
tile_height: i32,
|
||||
columns: i32,
|
||||
rows: i32,
|
||||
margin: i32,
|
||||
spacing: i32,
|
||||
tile_count: i32,
|
||||
}
|
||||
|
||||
Map_Load_Error_Type :: enum {
|
||||
None,
|
||||
Err_Loading_File,
|
||||
@@ -137,3 +176,46 @@ load_map :: proc(
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
load_tileset :: proc(
|
||||
path: string,
|
||||
allocator: runtime.Allocator,
|
||||
) -> (
|
||||
tileset_out: Tileset_Data,
|
||||
err: Tileset_Load_Error,
|
||||
) {
|
||||
|
||||
context.allocator = allocator
|
||||
|
||||
cfg_data, os_err := os.read_entire_file(path, context.allocator)
|
||||
if os_err != os.General_Error.None {
|
||||
return {}, Tileset_Load_Error{type = .Err_Loading_File, msg = fmt.aprintf("Error reading file: %v (%v)", path, os_err)}
|
||||
}
|
||||
|
||||
unm: Tileset_File
|
||||
unm_err := json.unmarshal(cfg_data, &unm)
|
||||
if unm_err != nil {
|
||||
return {}, Tileset_Load_Error{type = .Err_Unmarshal, msg = fmt.aprintf("Error unmarshalling file: %v (%v)\nOnly Tiled version 1.11.2 is supported", path, unm_err)}
|
||||
}
|
||||
|
||||
assert(unm.type == "tileset", "Only files with type 'tileset' are supported")
|
||||
assert(
|
||||
unm.tile_height > 0 && unm.tile_width > 0,
|
||||
"Only positive tile sizes are supported",
|
||||
)
|
||||
assert(unm.columns > 0, "Tileset must have at least one column")
|
||||
|
||||
tileset_out.name = unm.name
|
||||
tileset_out.image = unm.image
|
||||
tileset_out.tile_width = i32(unm.tile_width)
|
||||
tileset_out.tile_height = i32(unm.tile_height)
|
||||
tileset_out.columns = i32(unm.columns)
|
||||
tileset_out.rows = i32(
|
||||
(unm.image_height - 2 * unm.margin + unm.spacing) / (unm.tile_height + unm.spacing),
|
||||
)
|
||||
tileset_out.margin = i32(unm.margin)
|
||||
tileset_out.spacing = i32(unm.spacing)
|
||||
tileset_out.tile_count = i32(unm.tile_count)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user