Map parsing
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package main
|
||||
|
||||
import "base:runtime"
|
||||
import hm "core:container/handle_map"
|
||||
import "core:fmt"
|
||||
import rl "vendor:raylib"
|
||||
@@ -34,6 +35,8 @@ create_scene :: proc() -> Scene {
|
||||
offset = rl.Vector2{SCREEN_W / 2, SCREEN_H / 2},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
// _ = hm.add(
|
||||
// &scene.enemies,
|
||||
// Enemy {
|
||||
@@ -72,7 +75,7 @@ draw_scene :: proc(scene: ^Scene) {
|
||||
0,
|
||||
0,
|
||||
cast(f32)scene.map_tex.texture.width,
|
||||
cast(f32)scene.map_tex.texture.height,
|
||||
-cast(f32)scene.map_tex.texture.height, // FIX: flip Y axis
|
||||
},
|
||||
rl.Rectangle {
|
||||
0,
|
||||
@@ -107,51 +110,33 @@ draw_scene :: proc(scene: ^Scene) {
|
||||
rl.EndMode2D()
|
||||
}
|
||||
|
||||
render_map :: proc(atlas: ^Atlas) -> rl.RenderTexture2D {
|
||||
map_data := [16][16]i32 {
|
||||
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
|
||||
{8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10},
|
||||
{8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10},
|
||||
{8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10},
|
||||
{8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10},
|
||||
{8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10},
|
||||
{8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10},
|
||||
{8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10},
|
||||
{8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10},
|
||||
{8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10},
|
||||
{8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10},
|
||||
{8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10},
|
||||
{8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10},
|
||||
{8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10},
|
||||
{8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10},
|
||||
{16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18},
|
||||
}
|
||||
width := i32(atlas.tile_size.x) * 16
|
||||
height := i32(atlas.tile_size.y) * 16
|
||||
tex := rl.LoadRenderTexture(width, height)
|
||||
render_map :: proc(atlas: ^Atlas, map_data: Map) -> rl.RenderTexture2D {
|
||||
tex := rl.LoadRenderTexture(map_data.width * 16, map_data.height * 16)
|
||||
rl.BeginTextureMode(tex)
|
||||
for row, i in map_data {
|
||||
for col, j in row {
|
||||
offset := get_offset(atlas, col)
|
||||
fmt.printf("offset: %v, frame: %v\n", offset, col)
|
||||
rl.DrawTexturePro(
|
||||
atlas^.tex,
|
||||
rl.Rectangle {
|
||||
offset.x,
|
||||
offset.y,
|
||||
atlas.tile_size.x,
|
||||
atlas.tile_size.y,
|
||||
},
|
||||
rl.Rectangle {
|
||||
f32(j) * atlas.tile_size.x,
|
||||
f32(i) * atlas.tile_size.y,
|
||||
atlas.tile_size.x,
|
||||
atlas.tile_size.y,
|
||||
},
|
||||
rl.Vector2{0, 0},
|
||||
0,
|
||||
rl.WHITE,
|
||||
)
|
||||
for layer in map_data.layers {
|
||||
for row_data, row in layer.data {
|
||||
for tile_id, col in row_data {
|
||||
offset, empty := get_offset(atlas, tile_id)
|
||||
if empty do continue
|
||||
rl.DrawTexturePro(
|
||||
atlas^.tex,
|
||||
rl.Rectangle {
|
||||
offset.x,
|
||||
offset.y,
|
||||
atlas.tile_size.x,
|
||||
atlas.tile_size.y,
|
||||
},
|
||||
rl.Rectangle {
|
||||
f32(col) * atlas.tile_size.x,
|
||||
f32(row) * atlas.tile_size.y,
|
||||
atlas.tile_size.x,
|
||||
atlas.tile_size.y,
|
||||
},
|
||||
rl.Vector2{0, 0},
|
||||
0,
|
||||
rl.WHITE,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
rl.EndTextureMode()
|
||||
@@ -183,9 +168,21 @@ main :: proc() {
|
||||
defer rl.CloseWindow()
|
||||
rl.SetTargetFPS(60)
|
||||
|
||||
|
||||
arena: runtime.Arena
|
||||
defer runtime.arena_destroy(&arena)
|
||||
|
||||
arena_alloc := runtime.arena_allocator(&arena)
|
||||
|
||||
map_data, err := load_map(
|
||||
"./Tiled/безымянный.json",
|
||||
arena_alloc,
|
||||
)
|
||||
if err.type != .None do fmt.println(err.msg)
|
||||
|
||||
//WARN: Add a destructor to free (handlemap dangling)
|
||||
scene := create_scene()
|
||||
scene.map_tex = render_map(&scene.atlas)
|
||||
scene.map_tex = render_map(&scene.atlas, map_data)
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
dt := rl.GetFrameTime()
|
||||
|
||||
Reference in New Issue
Block a user