Add viewing area and ensure all images are displayed within it

This commit is contained in:
Abdelrahman Said 2024-05-26 15:40:06 +01:00
parent c564cabc66
commit 373c27572e

View File

@ -4,6 +4,7 @@
#include "tiffread.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_video.h>
@ -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,
};
}