From d11d85fe8a27d753305adc5619ff0747b83bb711 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Thu, 28 Nov 2024 22:19:14 +0100 Subject: [PATCH] zoom slower when texels are larger than pixels --- showimage.odin | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/showimage.odin b/showimage.odin index 16c8c35..647130a 100644 --- a/showimage.odin +++ b/showimage.odin @@ -1,6 +1,7 @@ package showimage import "core:os" +import "core:fmt" import "core:strings" import rl "vendor:raylib" @@ -26,9 +27,18 @@ main :: proc() { for !rl.WindowShouldClose() { scroll := rl.GetMouseWheelMove() if scroll > 0 { - change_zoom(zoom) + // zoom in + if zoom > 1 { + change_zoom(1) + } else { + change_zoom(zoom) + } } else if scroll < 0 { - change_zoom(-zoom / 2) + if zoom > 1 { + change_zoom(-1) + } else { + change_zoom(-zoom / 2) + } } if rl.IsMouseButtonDown(rl.MouseButton.LEFT) { offset += rl.GetMouseDelta() @@ -51,20 +61,21 @@ change_zoom :: proc(delta: f32) { mouse_pos := rl.GetMousePosition() zoom_pos := (mouse_pos - offset) / zoom zoom += delta + fmt.printf("zoomed to %f\n", zoom) offset = mouse_pos - zoom_pos * zoom } -zoom_to_power :: proc(size: i32, max: i32) -> i32 { - fit: i32 = 2 - for fit * size < max { - fit *= 2 +zoom_to_fit :: proc(size: i32, max: i32) -> f32 { + if size > max { + return 1 } - return fit / 2 + return f32(max / size) } reset_zoom :: proc() { zoom = f32(min( - zoom_to_power(tex.width, rl.GetRenderWidth()), - zoom_to_power(tex.height, rl.GetRenderHeight()), + zoom_to_fit(tex.width, rl.GetRenderWidth()), + zoom_to_fit(tex.height, rl.GetRenderHeight()), )) + fmt.printf("zoomed to %f\n",zoom) }