From ce475472057daae9fba1dcf64f80f5b6f2116d72 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Fri, 13 Sep 2024 23:25:10 +0200 Subject: [PATCH] Add texture helper functions --- main.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ main.h | 9 ++++----- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index c393cdd..7f8f35b 100644 --- a/main.c +++ b/main.c @@ -48,6 +48,51 @@ void unigi_ext_texture_draw( } } +unigi_ext_type_texture * unigi_ext_texture_create(unigi_ext_type_2d_coord width, unigi_ext_type_2d_coord height) { + unigi_ext_type_texture * texture = malloc(sizeof(unigi_ext_type_texture)); + if (texture == NULL) { return NULL; } + texture->width = width; + texture->height = height; + texture->pixels = malloc(sizeof(unigi_type_color) * width * height); + if (texture->pixels == NULL) { + free(texture); + return NULL; + } + return texture; +} + +void unigi_ext_texture_destroy(unigi_ext_type_texture * texture) { + free(texture->pixels); + free(texture); +} + +#include +#include +unigi_ext_type_texture * unigi_ext_texture_open(char * path) { + int fd = open(path,O_RDONLY); + if (fd == -1) { goto fail; } + unigi_ext_type_2d_coord width; + unigi_ext_type_2d_coord height; + if (read(fd,&width,sizeof(uint16_t)) != sizeof(uint16_t)) { goto fail_file; } + if (read(fd,&height,sizeof(uint16_t)) != sizeof(uint16_t)) { goto fail_file; } + unigi_ext_type_texture * texture = unigi_ext_texture_create(width,height); + if (texture == NULL) { goto fail_file; } + + if (read(fd,texture->pixels,sizeof(uint16_t) * width * height) != sizeof(uint16_t) * width * height) { + goto fail_texture; + } + + close(fd); + return texture; + + fail_texture:; + unigi_ext_texture_destroy(texture); + fail_file:; + close(fd); + fail:; + return NULL; +} + void unigi_ext_rect_dimensions( unigi_ext_type_rect bounds, unigi_ext_type_2d_coord * width, diff --git a/main.h b/main.h index 3eea43f..97f99f3 100644 --- a/main.h +++ b/main.h @@ -10,10 +10,9 @@ void unigi_ext_texture_draw( unigi_ext_type_rect texbounds, unigi_ext_type_rect bufbounds ); +void unigi_ext_rect_dimensions(unigi_ext_type_rect bounds, unigi_ext_type_2d_coord * width, unigi_ext_type_2d_coord * height); +unigi_ext_type_texture * unigi_ext_texture_create(unigi_ext_type_2d_coord width, unigi_ext_type_2d_coord height); +void unigi_ext_texture_destroy(unigi_ext_type_texture * texture); +unigi_ext_type_texture * unigi_ext_texture_open(char * path); -void unigi_ext_rect_dimensions( - unigi_ext_type_rect bounds, - unigi_ext_type_2d_coord * width, - unigi_ext_type_2d_coord * height -); #endif