fix: Флип тайлов перенесен в рендер

This commit is contained in:
2026-08-28 20:17:36 +05:00
parent 35a64fe432
commit cdadd72635
2 changed files with 27 additions and 18 deletions
+11 -8
View File
@@ -76,7 +76,7 @@ draw_scene :: proc(scene: ^Scene) {
0,
0,
cast(f32)scene.map_tex.texture.width,
-cast(f32)scene.map_tex.texture.height, // FIX: flip Y axis
cast(f32)scene.map_tex.texture.height,
},
rl.Rectangle {
0,
@@ -112,10 +112,16 @@ draw_scene :: proc(scene: ^Scene) {
}
render_map :: proc(atlas: ^Atlas, map_data: Map) -> rl.RenderTexture2D {
tex := rl.LoadRenderTexture(map_data.width * 16, map_data.height * 16)
tex := rl.LoadRenderTexture(
map_data.width * i32(atlas.tile_size.x),
map_data.height * i32(atlas.tile_size.y),
)
// RT хранится в GL-координатах (Y вверх), поэтому ряды тайлов пишем
// снизу вверх — тогда текстуру можно выводить без переворота
rl.BeginTextureMode(tex)
for layer in map_data.layers {
for row_data, row in layer.data {
dest_y := f32(i32(map_data.height) - 1 - i32(row)) * atlas.tile_size.y
for tile_id, col in row_data {
offset, empty := get_offset(atlas, tile_id)
if empty do continue
@@ -125,11 +131,11 @@ render_map :: proc(atlas: ^Atlas, map_data: Map) -> rl.RenderTexture2D {
offset.x,
offset.y,
atlas.tile_size.x,
atlas.tile_size.y,
-atlas.tile_size.y, // флип: RT хранит квадры перевёрнутыми
},
rl.Rectangle {
f32(col) * atlas.tile_size.x,
f32(row) * atlas.tile_size.y,
dest_y,
atlas.tile_size.x,
atlas.tile_size.y,
},
@@ -175,10 +181,7 @@ main :: proc() {
arena_alloc := runtime.arena_allocator(&arena)
map_data, err := load_map(
"./Tiled/map_file.json",
arena_alloc,
)
map_data, err := load_map("./Tiled/map_file.json", arena_alloc)
if err.type != .None {
fmt.println(err.msg)
+16 -10
View File
@@ -43,16 +43,16 @@ Tileset :: struct {
}
Tileset_File :: struct {
columns: int `json:"columns"`,
columns: int `json:"columns"`,
image: string `json:"image"`,
image_height: int `json:"imageheight"`,
image_width: int `json:"imagewidth"`,
margin: int `json:"margin"`,
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"`,
spacing: int `json:"spacing"`,
tile_count: int `json:"tilecount"`,
tile_height: int `json:"tileheight"`,
tile_width: int `json:"tilewidth"`,
}
Tileset_Info :: struct {
@@ -83,8 +83,14 @@ load_tileset :: proc(
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")
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}