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 package showimage
import "core:os" import "core:os"
import "core:fmt"
import "core:strings" import "core:strings"
import rl "vendor:raylib" import rl "vendor:raylib"
@ -26,10 +27,19 @@ main :: proc() {
for !rl.WindowShouldClose() { for !rl.WindowShouldClose() {
scroll := rl.GetMouseWheelMove() scroll := rl.GetMouseWheelMove()
if scroll > 0 { if scroll > 0 {
// zoom in
if zoom > 1 {
change_zoom(1)
} else {
change_zoom(zoom) change_zoom(zoom)
}
} else if scroll < 0 { } else if scroll < 0 {
if zoom > 1 {
change_zoom(-1)
} else {
change_zoom(-zoom / 2) change_zoom(-zoom / 2)
} }
}
if rl.IsMouseButtonDown(rl.MouseButton.LEFT) { if rl.IsMouseButtonDown(rl.MouseButton.LEFT) {
offset += rl.GetMouseDelta() offset += rl.GetMouseDelta()
} }
@ -51,20 +61,21 @@ change_zoom :: proc(delta: f32) {
mouse_pos := rl.GetMousePosition() mouse_pos := rl.GetMousePosition()
zoom_pos := (mouse_pos - offset) / zoom zoom_pos := (mouse_pos - offset) / zoom
zoom += delta zoom += delta
fmt.printf("zoomed to %f\n", zoom)
offset = mouse_pos - zoom_pos * zoom offset = mouse_pos - zoom_pos * zoom
} }
zoom_to_power :: proc(size: i32, max: i32) -> i32 { zoom_to_fit :: proc(size: i32, max: i32) -> f32 {
fit: i32 = 2 if size > max {
for fit * size < max { return 1
fit *= 2
} }
return fit / 2 return f32(max / size)
} }
reset_zoom :: proc() { reset_zoom :: proc() {
zoom = f32(min( zoom = f32(min(
zoom_to_power(tex.width, rl.GetRenderWidth()), zoom_to_fit(tex.width, rl.GetRenderWidth()),
zoom_to_power(tex.height, rl.GetRenderHeight()), zoom_to_fit(tex.height, rl.GetRenderHeight()),
)) ))
fmt.printf("zoomed to %f\n",zoom)
} }