From 373c27572ec6eba668630517db63703305c225e5 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 26 May 2024 15:40:06 +0100 Subject: [PATCH] Add viewing area and ensure all images are displayed within it --- src/main.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main.c b/src/main.c index f625743..5b2e9ff 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include "tiffread.h" #include #include +#include #include #include #include @@ -18,6 +19,8 @@ #define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 +#define VIEW_AREA_WIDTH 700 +#define VIEW_AREA_HEIGHT 500 #define AMASK 0xff000000 #define BMASK 0x00ff0000 @@ -34,6 +37,7 @@ struct point { internal void write_debug_ppm6(const Image *img); internal void *load_tiff_image(void *args); +internal SDL_Rect get_dest_rect(SDL_Surface *surface); typedef struct img_thread_args ImgThreadArgs; struct img_thread_args { @@ -90,12 +94,7 @@ int main(int argc, char *argv[]) { goto MAIN_DESTROY_SURFACE; } - SDL_Rect dest = { - .h = surface->h, - .w = surface->w, - .x = (WINDOW_WIDTH - surface->w) / 2, - .y = (WINDOW_HEIGHT - surface->h) / 2, - }; + SDL_Rect dest = get_dest_rect(surface); bool running = true; @@ -188,3 +187,16 @@ internal void *load_tiff_image(void *args) { return surface; } + +internal SDL_Rect get_dest_rect(SDL_Surface *surface) { + f64 ratio = (f64)(surface->h) / (f64)(surface->w); + u64 width = surface->w <= VIEW_AREA_WIDTH ? surface->w : VIEW_AREA_WIDTH; + u64 height = width * ratio; + + return (SDL_Rect){ + .w = width, + .h = height, + .x = (WINDOW_WIDTH - width) / 2, + .y = (WINDOW_HEIGHT - height) / 2, + }; +}