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 := 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 } reset_zoom() } for !rl.WindowShouldClose() { scroll := rl.GetMouseWheelMove() if scroll > 0 { change_zoom(zoom) } else if scroll < 0 { change_zoom(-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() } change_zoom :: proc(delta: f32) { mouse_pos := rl.GetMousePosition() zoom_pos := (mouse_pos - offset) / zoom zoom += delta offset = mouse_pos - zoom_pos * zoom } zoom_to_power :: proc(size: i32, max: i32) -> i32 { fit: i32 = 2 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()), )) }