center zooming on mouse

This commit is contained in:
Crispy 2024-10-25 18:03:44 +02:00
parent 7a1de9d871
commit e1e4dfb650

View file

@ -26,9 +26,9 @@ main :: proc() {
for !rl.WindowShouldClose() { for !rl.WindowShouldClose() {
scroll := rl.GetMouseWheelMove() scroll := rl.GetMouseWheelMove()
if scroll > 0 { if scroll > 0 {
zoom *= 2 change_zoom(zoom)
} else if scroll < 0 { } else if scroll < 0 {
zoom /= 2 change_zoom(-zoom / 2)
} }
if rl.IsMouseButtonDown(rl.MouseButton.LEFT) { if rl.IsMouseButtonDown(rl.MouseButton.LEFT) {
offset += rl.GetMouseDelta() offset += rl.GetMouseDelta()
@ -47,6 +47,13 @@ main :: proc() {
rl.CloseWindow() rl.CloseWindow()
} }
change_zoom :: proc(delta: f32) {
mouse_pos := rl.GetMousePosition()
zoom_pos := (mouse_pos - offset) / zoom
zoom += delta
offset = mouse_pos - zoom_pos * zoom
}
zoom_to_power :: proc(size: i32, max: i32) -> i32 { zoom_to_power :: proc(size: i32, max: i32) -> i32 {
fit: i32 = 2 fit: i32 = 2
for fit * size < max { for fit * size < max {