Add texture helper functions
This commit is contained in:
parent
327cc506ab
commit
ce47547205
45
main.c
45
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 <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
9
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
|
||||
|
Loading…
Reference in New Issue
Block a user