From 984f35cb724b3d298c75a857a4deec4183cd7a72 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Mon, 9 Nov 2015 14:33:39 +0000 Subject: Add 's' hotkey to scale image to fit window --- main.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 13a0334..b24b159 100644 --- a/main.c +++ b/main.c @@ -90,13 +90,30 @@ void move_view(int x, int y) void zoom_view(int amount) { g_view.scale += amount * 0.1; - if(g_view.scale > 10) + if(g_view.scale > 100) g_view.scale = 10; - else if (g_view.scale < 0.1) + else if (g_view.scale < 0.01) g_view.scale = 0.1; g_view.redraw = 1; } +void scale_to_window() +{ + int ww, wh; + SDL_GetWindowSize(g_window, &ww, &wh); + double window_aspect = (double)ww/(double)wh; + double image_aspect = (double)g_img.width/(double)g_img.height; + + if(window_aspect > image_aspect) { + //Image will become too tall before it becomes too wide + g_view.scale = (double)wh/(double)g_img.height; + } else { + //Image will become too wide before it becomes too tall + g_view.scale = (double)ww/(double)g_img.width; + } + g_view.redraw = 1; +} + void add_path(const char* path) { struct loop_item_s *new_path = @@ -362,6 +379,7 @@ int main(int argc, char** argv) case SDLK_f: toggle_fullscreen(); break; case SDLK_PERIOD: next_frame(); break; case SDLK_SPACE: toggle_playing(); break; + case SDLK_s: scale_to_window(); break; } break; case SDL_MOUSEWHEEL: -- cgit v1.2.3