From 3042fcb8cde6587857bd3bad8120c65cc332a545 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Fri, 25 Oct 2024 17:34:29 +0200 Subject: [PATCH] cleanup --- showimage.odin | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/showimage.odin b/showimage.odin index 4c284b1..5fc4cdd 100644 --- a/showimage.odin +++ b/showimage.odin @@ -1,23 +1,21 @@ package showimage import "core:os" -import s "core:strings" +import "core:strings" import rl "vendor:raylib" -path: string tex: ^rl.Texture zoom: f32 = 1 offset: [2]f32 main :: proc() { - config: rl.ConfigFlags = { .WINDOW_RESIZABLE } rl.InitWindow(800, 600, "showimage") - rl.SetWindowState(config) + rl.SetWindowState({ .WINDOW_RESIZABLE }) + rl.SetTargetFPS(60) if len(os.args) > 1 { - path = os.args[1] - t := rl.LoadTexture(s.clone_to_cstring(path)) - tex = &t + path := strings.clone_to_cstring(os.args[1]) + tex = new_clone(rl.LoadTexture(path)) reset_zoom() } @@ -45,20 +43,17 @@ main :: proc() { 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() { - screen_width := rl.GetRenderWidth() - fit_x: i32 = 1 - for fit_x * tex.width < screen_width { - fit_x *= 2 - } - fit_x /= 2 - - screen_height := rl.GetRenderHeight() - fit_y: i32 = 1 - for fit_y * tex.height < screen_height { - fit_y *= 2 - } - fit_y /= 2 - - zoom = f32(min(fit_x, fit_y)) -} \ No newline at end of file + zoom = f32(min( + zoom_to_power(tex.width, rl.GetRenderWidth()), + zoom_to_power(tex.height, rl.GetRenderHeight()), + )) +}