From 2fd02aea45af38ac836fd22c8de6e5e702c31df6 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Wed, 10 Jan 2024 00:00:37 +0000 Subject: [PATCH] Implement loading an image when file is dropped --- include/drop_area.h | 4 ++++ src/drop_area.c | 46 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/include/drop_area.h b/include/drop_area.h index a89d08a..955aa94 100644 --- a/include/drop_area.h +++ b/include/drop_area.h @@ -2,13 +2,17 @@ #define DROP_AREA_H #include "colour.h" +#include "image.h" #include "window.h" #include typedef struct { render_rect_t rect; + render_rect_t image_rect; colour_t border_colour; + image_t image; bool mouseover; + bool image_loaded; } drop_area_t; void init_drop_area(window_t *wnd, drop_area_t *area, render_rect_t rect, diff --git a/src/drop_area.c b/src/drop_area.c index 3737c35..a97ede1 100644 --- a/src/drop_area.c +++ b/src/drop_area.c @@ -1,18 +1,23 @@ #include "drop_area.h" #include "SDL_events.h" #include "colour.h" +#include "image.h" #include "window.h" +#include #include void handle_hover(const window_t *wnd, void *obj, const SDL_Event *ev); void handle_drop_file(const window_t *wnd, void *obj, const SDL_Event *ev); void render_drop_area(const window_t *wnd, void *obj); +render_rect_t image_render_rect(drop_area_t *area); void init_drop_area(window_t *wnd, drop_area_t *area, render_rect_t rect, colour_t border_colour) { area->rect = rect; + area->image_rect = (render_rect_t){0}; area->border_colour = border_colour; area->mouseover = false; + area->image_loaded = false; add_event_listener(wnd, area, handle_hover); add_event_listener(wnd, area, handle_drop_file); @@ -40,7 +45,14 @@ void handle_drop_file(const window_t *wnd, void *obj, const SDL_Event *ev) { return; } - printf("%s\n", ev->drop.file); + printf("Loading: %s\n", ev->drop.file); + + area->image_loaded = load_image(wnd, &(area->image), ev->drop.file); + + if (area->image_loaded) { + printf("Width: %ld, Height: %ld\n", area->image.width, area->image.height); + area->image_rect = image_render_rect(area); + } } void render_drop_area(const window_t *wnd, void *obj) { @@ -50,5 +62,37 @@ void render_drop_area(const window_t *wnd, void *obj) { drop_area_t *area = (drop_area_t *)obj; + if (area->image_loaded) { + render_texture(wnd, area->image.texture, NULL, &(area->image_rect)); + } + draw_rect(wnd, &(area->rect), area->border_colour); } + +render_rect_t image_render_rect(drop_area_t *area) { + if (!area || !(area->image_loaded)) { + return (render_rect_t){0}; + } + + u64 w = 0; + u64 h = 0; + + if (area->image.width < area->rect.width && + area->image.height < area->rect.height) { + w = area->image.width; + h = area->image.height; + } else { + f32 w_factor = (f32)area->rect.width / area->image.width; + f32 h_factor = (f32)area->rect.height / area->image.height; + + f32 factor = w_factor < h_factor ? w_factor : h_factor; + + w = area->image.width * factor; + h = area->image.height * factor; + } + + u64 x = area->rect.x + (area->rect.width - w) / 2; + u64 y = area->rect.y + (area->rect.height - h) / 2; + + return (render_rect_t){x, y, w, h}; +}