showimage/showimage.odin

71 lines
1.4 KiB
Odin
Raw Normal View History

2024-10-25 17:18:45 +02:00
package showimage
import "core:os"
2024-10-25 17:34:29 +02:00
import "core:strings"
2024-10-25 17:18:45 +02:00
import rl "vendor:raylib"
tex: ^rl.Texture
zoom: f32 = 1
offset: [2]f32
main :: proc() {
rl.InitWindow(800, 600, "showimage")
2024-10-25 17:34:29 +02:00
rl.SetWindowState({ .WINDOW_RESIZABLE })
rl.SetTargetFPS(60)
2024-10-25 17:18:45 +02:00
if len(os.args) > 1 {
path := os.args[1]
if strings.has_prefix(path, "file://") {
path, _ = strings.substring_from(path, 7)
}
tex = new_clone(rl.LoadTexture(strings.clone_to_cstring(path)))
if tex.id <= 0 { return }
2024-10-25 17:18:45 +02:00
reset_zoom()
}
for !rl.WindowShouldClose() {
scroll := rl.GetMouseWheelMove()
if scroll > 0 {
2024-10-25 18:03:44 +02:00
change_zoom(zoom)
2024-10-25 17:18:45 +02:00
} else if scroll < 0 {
2024-10-25 18:03:44 +02:00
change_zoom(-zoom / 2)
2024-10-25 17:18:45 +02:00
}
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()
}
2024-10-25 18:03:44 +02:00
change_zoom :: proc(delta: f32) {
mouse_pos := rl.GetMousePosition()
zoom_pos := (mouse_pos - offset) / zoom
zoom += delta
offset = mouse_pos - zoom_pos * zoom
}
2024-10-25 17:34:29 +02:00
zoom_to_power :: proc(size: i32, max: i32) -> i32 {
fit: i32 = 2
2024-10-25 17:34:29 +02:00
for fit * size < max {
fit *= 2
2024-10-25 17:18:45 +02:00
}
2024-10-25 17:34:29 +02:00
return fit / 2
}
2024-10-25 17:18:45 +02:00
2024-10-25 17:34:29 +02:00
reset_zoom :: proc() {
zoom = f32(min(
zoom_to_power(tex.width, rl.GetRenderWidth()),
zoom_to_power(tex.height, rl.GetRenderHeight()),
))
}