showimage/showimage.odin
2024-10-25 17:34:29 +02:00

59 lines
1.1 KiB
Odin

package showimage
import "core:os"
import "core:strings"
import rl "vendor:raylib"
tex: ^rl.Texture
zoom: f32 = 1
offset: [2]f32
main :: proc() {
rl.InitWindow(800, 600, "showimage")
rl.SetWindowState({ .WINDOW_RESIZABLE })
rl.SetTargetFPS(60)
if len(os.args) > 1 {
path := strings.clone_to_cstring(os.args[1])
tex = new_clone(rl.LoadTexture(path))
reset_zoom()
}
for !rl.WindowShouldClose() {
scroll := rl.GetMouseWheelMove()
if scroll > 0 {
zoom *= 2
} else if scroll < 0 {
zoom /= 2
}
if rl.IsMouseButtonDown(rl.MouseButton.LEFT) {
offset += rl.GetMouseDelta()
}
if rl.IsMouseButtonPressed(rl.MouseButton.RIGHT) {
offset = 0
reset_zoom()
}
rl.BeginDrawing()
rl.ClearBackground({50, 50, 50, 255})
if tex != nil {
rl.DrawTextureEx(tex^, offset, 0, zoom, rl.WHITE)
}
rl.EndDrawing()
}
rl.CloseWindow()
}
zoom_to_power :: proc(size: i32, max: i32) -> i32 {
fit: i32 = 1
for fit * size < max {
fit *= 2
}
return fit / 2
}
reset_zoom :: proc() {
zoom = f32(min(
zoom_to_power(tex.width, rl.GetRenderWidth()),
zoom_to_power(tex.height, rl.GetRenderHeight()),
))
}