Only render tiles in camera view
This commit is contained in:
@@ -21,10 +21,10 @@ main :: proc() {
|
||||
TILEMAP_TILE_SIZE,
|
||||
TILEMAP_TILE_SIZE,
|
||||
)
|
||||
grid = create_tile_grid(300, 300)
|
||||
grid = create_tile_grid(1500, 1500)
|
||||
|
||||
player = {
|
||||
position = {20, 20},
|
||||
position = {1500, 500},
|
||||
camera = {
|
||||
zoom = 4,
|
||||
offset = {f32(raylib.GetScreenWidth()) / 2, f32(raylib.GetScreenHeight()) / 2},
|
||||
@@ -66,7 +66,7 @@ update :: proc(delta: f32) {
|
||||
|
||||
@(private = "file")
|
||||
draw :: proc() {
|
||||
draw_tile_grid(&tilemap_sheet, grid)
|
||||
draw_tile_grid(&tilemap_sheet, grid, &player.camera)
|
||||
draw_player(&player)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import "vendor:raylib"
|
||||
|
||||
TilemapSpritesheet :: struct {
|
||||
@@ -43,14 +44,37 @@ update_tile_grid :: proc(grid: [][]Tile, delta: f32) {
|
||||
}
|
||||
}
|
||||
|
||||
draw_tile_grid :: proc(sheet: ^TilemapSpritesheet, grid: [][]Tile) {
|
||||
for y := 0; y < len(grid); y += 1 {
|
||||
|
||||
draw_tile_grid :: proc(sheet: ^TilemapSpritesheet, grid: [][]Tile, camera: ^raylib.Camera2D) {
|
||||
screen_w := f32(raylib.GetScreenWidth())
|
||||
screen_h := f32(raylib.GetScreenHeight())
|
||||
|
||||
world_w := screen_w / camera.zoom
|
||||
world_h := screen_h / camera.zoom
|
||||
|
||||
min_x := camera.target.x - world_w * 0.5
|
||||
min_y := camera.target.y - world_h * 0.5
|
||||
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 := max(int(min_x / tile_w), 0)
|
||||
start_y := max(int(min_y / tile_h), 0)
|
||||
|
||||
end_x := min(int(max_x / tile_w), len(grid[0]) - 1)
|
||||
end_y := min(int(max_y / tile_h), len(grid) - 1)
|
||||
|
||||
for y := start_y; y <= end_y; y += 1 {
|
||||
row := grid[y]
|
||||
for x := 0; x < len(row); x += 1 {
|
||||
for x := start_x; x <= end_x; x += 1 {
|
||||
tile := &row[x]
|
||||
if (tile.type == TileType.NOTHING) do continue
|
||||
pos := raylib.Vector2{f32(x * int(sheet.tile_width)), f32(y * int(sheet.tile_height))}
|
||||
draw_tile(sheet, tile, pos, raylib.Color{255, 136, 0, 255})
|
||||
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})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user