Add texture helper functions

This commit is contained in:
Fierelier 2024-09-13 23:25:10 +02:00
parent 327cc506ab
commit ce47547205
2 changed files with 49 additions and 5 deletions

45
main.c
View File

@ -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 <unistd.h>
#include <fcntl.h>
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,

9
main.h
View File

@ -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