Redid the world -> screen funcs.

Had to rework these they were wrong initially.
Added comments to make it clear what I am doing.
This commit is contained in:
2026-07-16 08:52:14 +02:00
parent f2110236e3
commit a726107da7
+40 -12
View File
@@ -55,20 +55,41 @@ map_init :: proc() -> Game_Map {
} }
} }
world_to_grid :: proc(pixel_pos: rl.Vector2, camera: rl.Camera2D) -> Grid_Pos { // requires raw window-space
grid_space := rl.GetScreenToWorld2D(pixel_pos, camera) // i.e mouse position clicked in window get that space in screen
// returns screen-space pos, scaled to game resolution.
// @TODO: I wonder if this floor and casting is expensive, look for a smarter way later window_to_screen :: proc(window_pos: rl.Vector2) -> rl.Vector2 {
pos_x := int(math.floor(grid_space.x)) // window_width / game_width will give the scale back
pos_y := int(math.floor(grid_space.y)) // invert it to scale down back to the render size
screen_x := window_pos.x / (WINDOW_WIDTH / GAME_WIDTH)
return Grid_Pos{pos_x, pos_y} screen_y := window_pos.y / (WINDOW_HEIGHT / GAME_HEIGHT)
return rl.Vector2{screen_x, screen_y}
} }
// requires screen-space pos i.e mouse pos, scaled from window -> render size
// returns world-space position (accounts for camera and its zoom)
// remember uesd with raw input coordinates, not something already in world space (like player.pos).
screen_to_world :: proc(screen_pos: rl.Vector2, camera: rl.Camera2D) -> rl.Vector2 {
return rl.GetScreenToWorld2D(screen_pos, camera)
}
// @TODO: Do I need this?? Maybe for debug view??
world_to_screen :: proc(world_pos: rl.Vector2, camera: rl.Camera2D) -> rl.Vector2 {
return rl.GetWorldToScreen2D(world_pos, camera)
}
// requires world-space position i.e player.pos, or output of screen_to_world
// returns grid col/row not considering camera
world_to_grid :: proc(world_pos: rl.Vector2) -> Grid_Pos {
col := int(math.floor(world_pos.x / TILE_SIZE))
row := int(math.floor(world_pos.y / TILE_SIZE))
return Grid_Pos{col, row}
}
// returns world-space position grid pos top-left corner opposite of world_to_grid.
grid_to_world :: proc(col: int, row: int) -> rl.Vector2 { grid_to_world :: proc(col: int, row: int) -> rl.Vector2 {
world_x := f32(col * TILE_SIZE) world_x := f32(col * TILE_SIZE)
world_y := f32(row * TILE_SIZE) world_y := f32(row * TILE_SIZE)
return rl.Vector2{world_x, world_y} return rl.Vector2{world_x, world_y}
} }
@@ -77,9 +98,16 @@ draw_map :: proc(current_map: Game_Map) {
row := index / current_map.width row := index / current_map.width
column := index % current_map.width column := index % current_map.width
color := rl.LIGHTGRAY tile := Tile_Type(value)
if value == 1 do color = rl.DARKGRAY color: rl.Color
if value == 2 do color = rl.GREEN switch tile {
case .FLOOR:
color = rl.LIGHTGRAY
case .WALL:
color = rl.DARKGRAY
case .PICKUP:
color = rl.GREEN
}
rect := grid_to_world(column, row) rect := grid_to_world(column, row)
rl.DrawRectangle(i32(rect.x), i32(rect.y), TILE_SIZE, TILE_SIZE, color) rl.DrawRectangle(i32(rect.x), i32(rect.y), TILE_SIZE, TILE_SIZE, color)