Compare commits
3 Commits
86e367c721
...
2fd02aea45
Author | SHA1 | Date | |
---|---|---|---|
2fd02aea45 | |||
9415786adc | |||
754454c366 |
@ -2,13 +2,17 @@
|
|||||||
#define DROP_AREA_H
|
#define DROP_AREA_H
|
||||||
|
|
||||||
#include "colour.h"
|
#include "colour.h"
|
||||||
|
#include "image.h"
|
||||||
#include "window.h"
|
#include "window.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
render_rect_t rect;
|
render_rect_t rect;
|
||||||
|
render_rect_t image_rect;
|
||||||
colour_t border_colour;
|
colour_t border_colour;
|
||||||
|
image_t image;
|
||||||
bool mouseover;
|
bool mouseover;
|
||||||
|
bool image_loaded;
|
||||||
} drop_area_t;
|
} drop_area_t;
|
||||||
|
|
||||||
void init_drop_area(window_t *wnd, drop_area_t *area, render_rect_t rect,
|
void init_drop_area(window_t *wnd, drop_area_t *area, render_rect_t rect,
|
||||||
|
24
include/image.h
Normal file
24
include/image.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#ifndef IMAGE_H
|
||||||
|
#define IMAGE_H
|
||||||
|
|
||||||
|
#include "SDL_render.h"
|
||||||
|
#include "aliases/aliases.h"
|
||||||
|
#include "window.h"
|
||||||
|
#include <linux/limits.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char path[PATH_MAX];
|
||||||
|
u64 length;
|
||||||
|
} path_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
i64 width;
|
||||||
|
i64 height;
|
||||||
|
SDL_Texture *texture;
|
||||||
|
path_t filepath;
|
||||||
|
} image_t;
|
||||||
|
|
||||||
|
bool load_image(const window_t *wnd, image_t *img, const char *path);
|
||||||
|
|
||||||
|
#endif // !IMAGE_H
|
@ -47,6 +47,12 @@ bool add_event_listener(window_t *wnd, void *obj, event_listener_t listener);
|
|||||||
bool add_renderer_func(window_t *wnd, void *obj, render_func_t func);
|
bool add_renderer_func(window_t *wnd, void *obj, render_func_t func);
|
||||||
void fill_rect(const window_t *wnd, const render_rect_t *rect, colour_t colour);
|
void fill_rect(const window_t *wnd, const render_rect_t *rect, colour_t colour);
|
||||||
void draw_rect(const window_t *wnd, const render_rect_t *rect, colour_t colour);
|
void draw_rect(const window_t *wnd, const render_rect_t *rect, colour_t colour);
|
||||||
|
SDL_Texture *load_texture(const window_t *wnd, const char *path);
|
||||||
|
void unload_texture(SDL_Texture *texture);
|
||||||
|
void get_texture_size(SDL_Texture *texture, i32 *w, i32 *h);
|
||||||
|
void render_texture(const window_t *wnd, SDL_Texture *texture,
|
||||||
|
const render_rect_t *src_rect,
|
||||||
|
const render_rect_t *dst_rect);
|
||||||
bool aabb(u64 x, u64 y, const render_rect_t *rect);
|
bool aabb(u64 x, u64 y, const render_rect_t *rect);
|
||||||
|
|
||||||
#endif // !WINDOW_H
|
#endif // !WINDOW_H
|
||||||
|
@ -1,18 +1,23 @@
|
|||||||
#include "drop_area.h"
|
#include "drop_area.h"
|
||||||
#include "SDL_events.h"
|
#include "SDL_events.h"
|
||||||
#include "colour.h"
|
#include "colour.h"
|
||||||
|
#include "image.h"
|
||||||
#include "window.h"
|
#include "window.h"
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
void handle_hover(const window_t *wnd, void *obj, const SDL_Event *ev);
|
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 handle_drop_file(const window_t *wnd, void *obj, const SDL_Event *ev);
|
||||||
void render_drop_area(const window_t *wnd, void *obj);
|
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,
|
void init_drop_area(window_t *wnd, drop_area_t *area, render_rect_t rect,
|
||||||
colour_t border_colour) {
|
colour_t border_colour) {
|
||||||
area->rect = rect;
|
area->rect = rect;
|
||||||
|
area->image_rect = (render_rect_t){0};
|
||||||
area->border_colour = border_colour;
|
area->border_colour = border_colour;
|
||||||
area->mouseover = false;
|
area->mouseover = false;
|
||||||
|
area->image_loaded = false;
|
||||||
|
|
||||||
add_event_listener(wnd, area, handle_hover);
|
add_event_listener(wnd, area, handle_hover);
|
||||||
add_event_listener(wnd, area, handle_drop_file);
|
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;
|
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) {
|
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;
|
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);
|
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};
|
||||||
|
}
|
||||||
|
93
src/image.c
Normal file
93
src/image.c
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
#include "image.h"
|
||||||
|
#include "aliases/aliases.h"
|
||||||
|
#include "window.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define STREQ(S1, S2) (strcmp(S1, S2) == 0)
|
||||||
|
|
||||||
|
enum file_formats {
|
||||||
|
EXT_PNG,
|
||||||
|
EXT_PNG_CAPS,
|
||||||
|
EXT_JPG,
|
||||||
|
EXT_JPG_CAPS,
|
||||||
|
EXT_JPEG,
|
||||||
|
EXT_JPEG_CAPS,
|
||||||
|
EXT_TIF,
|
||||||
|
EXT_TIF_CAPS,
|
||||||
|
EXT_TIFF,
|
||||||
|
EXT_TIFF_CAPS,
|
||||||
|
|
||||||
|
COUNT_EXT,
|
||||||
|
};
|
||||||
|
|
||||||
|
INTERNAL const char *extensions[COUNT_EXT] = {
|
||||||
|
// clang-format off
|
||||||
|
[EXT_PNG] = ".png",
|
||||||
|
[EXT_PNG_CAPS] = ".PNG",
|
||||||
|
[EXT_JPG] = ".jpg",
|
||||||
|
[EXT_JPG_CAPS] = ".JPG",
|
||||||
|
[EXT_JPEG] = ".jpeg",
|
||||||
|
[EXT_JPEG_CAPS] = ".JPEG",
|
||||||
|
[EXT_TIF] = ".tif",
|
||||||
|
[EXT_TIF_CAPS] = ".TIF",
|
||||||
|
[EXT_TIFF] = ".tiff",
|
||||||
|
[EXT_TIFF_CAPS] = ".TIFF",
|
||||||
|
// clang-format on
|
||||||
|
};
|
||||||
|
|
||||||
|
bool valid_image(const char *path) {
|
||||||
|
for (u64 i = 0; i < COUNT_EXT; ++i) {
|
||||||
|
char *ext = strrchr(path, '.');
|
||||||
|
if (!ext) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (STREQ(ext, extensions[i])) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool load_image(const window_t *wnd, image_t *img, const char *path) {
|
||||||
|
if (!img || !path) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
img->filepath.length = strlen(path);
|
||||||
|
if (img->filepath.length == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!valid_image(path)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 length = strlen(img->filepath.path);
|
||||||
|
if (length > 0) {
|
||||||
|
memset(img->filepath.path, 0, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
strncpy(img->filepath.path, path, img->filepath.length);
|
||||||
|
|
||||||
|
if (img->texture) {
|
||||||
|
unload_texture(img->texture);
|
||||||
|
img->texture = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
img->texture = load_texture(wnd, path);
|
||||||
|
if (!(img->texture)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
i32 w;
|
||||||
|
i32 h;
|
||||||
|
get_texture_size(img->texture, &w, &h);
|
||||||
|
|
||||||
|
img->width = w;
|
||||||
|
img->height = h;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
38
src/window.c
38
src/window.c
@ -2,6 +2,7 @@
|
|||||||
#include "SDL_rect.h"
|
#include "SDL_rect.h"
|
||||||
#include "SDL_render.h"
|
#include "SDL_render.h"
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
#include <SDL2/SDL_image.h>
|
||||||
#include <SDL_events.h>
|
#include <SDL_events.h>
|
||||||
|
|
||||||
void handle_quit(const window_t *wnd, void *obj, const SDL_Event *ev);
|
void handle_quit(const window_t *wnd, void *obj, const SDL_Event *ev);
|
||||||
@ -11,6 +12,8 @@ bool open_window(window_t *wnd, const char *title, u64 w, u64 h,
|
|||||||
colour_t bg_colour) {
|
colour_t bg_colour) {
|
||||||
SDL_Init(SDL_INIT_EVERYTHING);
|
SDL_Init(SDL_INIT_EVERYTHING);
|
||||||
|
|
||||||
|
IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF);
|
||||||
|
|
||||||
wnd->window =
|
wnd->window =
|
||||||
SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w,
|
SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w,
|
||||||
h, SDL_WINDOW_SHOWN);
|
h, SDL_WINDOW_SHOWN);
|
||||||
@ -86,6 +89,8 @@ void close_window(window_t *wnd) {
|
|||||||
SDL_DestroyWindow(wnd->window);
|
SDL_DestroyWindow(wnd->window);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
IMG_Quit();
|
||||||
|
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,6 +142,35 @@ void draw_rect(const window_t *wnd, const render_rect_t *rect,
|
|||||||
SDL_RenderDrawRect(wnd->renderer, &dest);
|
SDL_RenderDrawRect(wnd->renderer, &dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SDL_Texture *load_texture(const window_t *wnd, const char *path) {
|
||||||
|
return IMG_LoadTexture(wnd->renderer, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
void unload_texture(SDL_Texture *texture) { SDL_DestroyTexture(texture); }
|
||||||
|
|
||||||
|
void get_texture_size(SDL_Texture *texture, i32 *w, i32 *h) {
|
||||||
|
SDL_QueryTexture(texture, NULL, NULL, w, h);
|
||||||
|
}
|
||||||
|
|
||||||
|
void render_texture(const window_t *wnd, SDL_Texture *texture,
|
||||||
|
const render_rect_t *src_rect,
|
||||||
|
const render_rect_t *dst_rect) {
|
||||||
|
SDL_Rect src = {
|
||||||
|
.x = src_rect ? src_rect->x : 0,
|
||||||
|
.y = src_rect ? src_rect->y : 0,
|
||||||
|
.w = src_rect ? src_rect->width : 0,
|
||||||
|
.h = src_rect ? src_rect->height : 0,
|
||||||
|
};
|
||||||
|
SDL_Rect dst = {
|
||||||
|
.x = dst_rect ? dst_rect->x : 0,
|
||||||
|
.y = dst_rect ? dst_rect->y : 0,
|
||||||
|
.w = dst_rect ? dst_rect->width : 0,
|
||||||
|
.h = dst_rect ? dst_rect->height : 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
SDL_RenderCopy(wnd->renderer, texture, src_rect ? &src : NULL, &dst);
|
||||||
|
}
|
||||||
|
|
||||||
bool aabb(u64 x, u64 y, const render_rect_t *rect) {
|
bool aabb(u64 x, u64 y, const render_rect_t *rect) {
|
||||||
return x >= rect->x && y >= rect->y && x < rect->x + rect->width &&
|
return x >= rect->x && y >= rect->y && x < rect->x + rect->width &&
|
||||||
y < rect->y + rect->height;
|
y < rect->y + rect->height;
|
||||||
@ -144,9 +178,9 @@ bool aabb(u64 x, u64 y, const render_rect_t *rect) {
|
|||||||
|
|
||||||
void handle_quit(const window_t *wnd, void *obj, const SDL_Event *ev) {
|
void handle_quit(const window_t *wnd, void *obj, const SDL_Event *ev) {
|
||||||
if (ev->type == SDL_QUIT) {
|
if (ev->type == SDL_QUIT) {
|
||||||
window_t *wnd = obj;
|
window_t *window = (window_t *)obj;
|
||||||
|
|
||||||
wnd->running = false;
|
window->running = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user