Only visible tiles update
This commit is contained in:
BIN
builds/linux/assets/player/player.png
Normal file
BIN
builds/linux/assets/player/player.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
BIN
builds/linux/assets/player/player.pxo
Normal file
BIN
builds/linux/assets/player/player.pxo
Normal file
Binary file not shown.
BIN
builds/linux/assets/tiles/master_tilemap.png
Normal file
BIN
builds/linux/assets/tiles/master_tilemap.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.2 KiB |
BIN
builds/linux/assets/tiles/master_tilemap.pxo
Normal file
BIN
builds/linux/assets/tiles/master_tilemap.pxo
Normal file
Binary file not shown.
9
builds/linux/assets/tiles/master_tilemap_addresses.txt
Normal file
9
builds/linux/assets/tiles/master_tilemap_addresses.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
0 - martian ground
|
||||
1 - martian ground barrier UP
|
||||
2 - martian ground barrier DOWN
|
||||
3 - martian ground barrier LEFT
|
||||
4 - martian ground barrier RIGHT
|
||||
5 - martian ground barrier TOP RIGHT CORNER
|
||||
6 - martian ground barrier TOP LEFT CORNER
|
||||
7 - martian ground barrier BOTTOM RIGHT CORNER
|
||||
8 - martian ground barrier BOTTOM LEFT CORNER
|
||||
BIN
builds/linux/game
Executable file
BIN
builds/linux/game
Executable file
Binary file not shown.
@@ -21,7 +21,7 @@ main :: proc() {
|
||||
TILEMAP_TILE_SIZE,
|
||||
TILEMAP_TILE_SIZE,
|
||||
)
|
||||
grid = create_tile_grid(1500, 1500)
|
||||
grid = create_tile_grid(5000, 5000)
|
||||
|
||||
player = {
|
||||
position = {1500, 500},
|
||||
@@ -61,7 +61,13 @@ main :: proc() {
|
||||
@(private = "file")
|
||||
update :: proc(delta: f32) {
|
||||
update_player(&player, delta)
|
||||
update_tile_grid(grid, delta)
|
||||
update_tile_grid(
|
||||
grid,
|
||||
&player.camera,
|
||||
f32(tilemap_sheet.tile_width),
|
||||
f32(tilemap_sheet.tile_height),
|
||||
delta,
|
||||
)
|
||||
}
|
||||
|
||||
@(private = "file")
|
||||
|
||||
@@ -11,6 +11,10 @@ TilemapSpritesheet :: struct {
|
||||
tiles_y: i32,
|
||||
}
|
||||
|
||||
VisibleTileRange :: struct {
|
||||
start_x, start_y: int,
|
||||
end_x, end_y: int,
|
||||
}
|
||||
|
||||
load_tilemap_sheet :: proc(path: cstring, tile_width, tile_height: i32) -> TilemapSpritesheet {
|
||||
tex := raylib.LoadTexture(path)
|
||||
@@ -35,10 +39,17 @@ create_tile_grid :: proc(width, height: i32) -> [][]Tile {
|
||||
return grid
|
||||
}
|
||||
|
||||
update_tile_grid :: proc(grid: [][]Tile, delta: f32) {
|
||||
for y := 0; y < len(grid); y += 1 {
|
||||
update_tile_grid :: proc(
|
||||
grid: [][]Tile,
|
||||
camera: ^raylib.Camera2D,
|
||||
tile_w, tile_h: f32,
|
||||
delta: f32,
|
||||
) {
|
||||
range := get_visible_tile_range(grid, tile_w, tile_h, camera)
|
||||
|
||||
for y := range.start_y; y <= range.end_y; y += 1 {
|
||||
row := grid[y]
|
||||
for x := 0; x < len(row); x += 1 {
|
||||
for x := range.start_x; x <= range.end_x; x += 1 {
|
||||
update_tile_anim(&row[x], delta)
|
||||
}
|
||||
}
|
||||
@@ -46,6 +57,31 @@ update_tile_grid :: proc(grid: [][]Tile, delta: f32) {
|
||||
|
||||
|
||||
draw_tile_grid :: proc(sheet: ^TilemapSpritesheet, grid: [][]Tile, camera: ^raylib.Camera2D) {
|
||||
tile_w := f32(sheet.tile_width)
|
||||
tile_h := f32(sheet.tile_height)
|
||||
|
||||
range := get_visible_tile_range(grid, tile_w, tile_h, camera)
|
||||
|
||||
for y := range.start_y; y <= range.end_y; y += 1 {
|
||||
row := grid[y]
|
||||
for x := range.start_x; x <= range.end_x; x += 1 {
|
||||
tile := &row[x]
|
||||
if tile.type == TileType.NOTHING do continue
|
||||
|
||||
pos := raylib.Vector2{f32(x) * tile_w, f32(y) * tile_h}
|
||||
|
||||
draw_tile(sheet, tile, pos, raylib.Color{0, 136, 200, 255})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
get_visible_tile_range :: proc(
|
||||
grid: [][]Tile,
|
||||
tile_w, tile_h: f32,
|
||||
camera: ^raylib.Camera2D,
|
||||
padding: int = 1,
|
||||
) -> VisibleTileRange {
|
||||
|
||||
screen_w := f32(raylib.GetScreenWidth())
|
||||
screen_h := f32(raylib.GetScreenHeight())
|
||||
|
||||
@@ -57,25 +93,17 @@ draw_tile_grid :: proc(sheet: ^TilemapSpritesheet, grid: [][]Tile, camera: ^rayl
|
||||
max_x := min_x + world_w
|
||||
max_y := min_y + world_h
|
||||
|
||||
tile_w := f32(sheet.tile_width)
|
||||
tile_h := f32(sheet.tile_height)
|
||||
start_x := int(min_x / tile_w) - padding
|
||||
start_y := int(min_y / tile_h) - padding
|
||||
end_x := int(max_x / tile_w) + padding
|
||||
end_y := int(max_y / tile_h) + padding
|
||||
|
||||
start_x := max(int(min_x / tile_w), 0)
|
||||
start_y := max(int(min_y / tile_h), 0)
|
||||
start_x = max(start_x, 0)
|
||||
start_y = max(start_y, 0)
|
||||
|
||||
end_x := min(int(max_x / tile_w), len(grid[0]) - 1)
|
||||
end_y := min(int(max_y / tile_h), len(grid) - 1)
|
||||
end_x = min(end_x, len(grid[0]) - 1)
|
||||
end_y = min(end_y, len(grid) - 1)
|
||||
|
||||
for y := start_y; y <= end_y; y += 1 {
|
||||
row := grid[y]
|
||||
for x := start_x; x <= end_x; x += 1 {
|
||||
tile := &row[x]
|
||||
if tile.type == TileType.NOTHING do continue
|
||||
|
||||
pos := raylib.Vector2{f32(x) * tile_w, f32(y) * tile_h}
|
||||
|
||||
draw_tile(sheet, tile, pos, raylib.Color{0, 136, 200, 255})
|
||||
}
|
||||
}
|
||||
return VisibleTileRange{start_x = start_x, start_y = start_y, end_x = end_x, end_y = end_y}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user