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 {
|
2024-10-25 17:34:29 +02:00
|
|
|
path := strings.clone_to_cstring(os.args[1])
|
|
|
|
tex = new_clone(rl.LoadTexture(path))
|
2024-10-25 17:18:45 +02:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2024-10-25 17:34:29 +02:00
|
|
|
zoom_to_power :: proc(size: i32, max: i32) -> i32 {
|
|
|
|
fit: i32 = 1
|
|
|
|
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()),
|
|
|
|
))
|
|
|
|
}
|