zoom slower when texels are larger than pixels

This commit is contained in:
Crispy 2024-11-28 22:19:14 +01:00
parent e1e4dfb650
commit d11d85fe8a

View file

@ -1,6 +1,7 @@
package showimage
import "core:os"
import "core:fmt"
import "core:strings"
import rl "vendor:raylib"
@ -26,10 +27,19 @@ main :: proc() {
for !rl.WindowShouldClose() {
scroll := rl.GetMouseWheelMove()
if scroll > 0 {
// zoom in
if zoom > 1 {
change_zoom(1)
} else {
change_zoom(zoom)
}
} else if scroll < 0 {
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)
}