#include "unigi.ext/main.h" void unigi_ext_texture_draw(unigi_ext_type_texture *sprite, unigi_ext_type_texture *fb, unigi_ext_type_rect texrect, unigi_ext_type_rect outrect) { // Precalc the step, as it's always the same even if we clip the rect unigi_ext_type_2d_coord tex_width, tex_height, out_width, out_height; unigi_ext_rect_dimensions(texrect,&tex_width,&tex_height); unigi_ext_rect_dimensions(outrect,&out_width,&out_height); int32_t stepx = (1 << unigi_ext_texture_fixedpointdepth) * (float)tex_width / out_width; int32_t stepy = (1 << unigi_ext_texture_fixedpointdepth) * (float)tex_height / out_height; int32_t texx_init = (1 << unigi_ext_texture_fixedpointdepth) * texrect.x1; int32_t texy = (1 << unigi_ext_texture_fixedpointdepth) * texrect.y1; // Clip the rect if (outrect.x1 < 0) { texx_init += stepx * -outrect.x1; outrect.x1 = 0; } if (outrect.y1 < 0) { texy += stepy * -outrect.y1; outrect.y1 = 0; } if (outrect.x2 >= fb->width) { outrect.x2 = fb->width - 1; } if (outrect.y2 >= fb->height) { outrect.y2 = fb->height - 1; } for (int y = outrect.y1; y < outrect.y2; y++) { int32_t texx = texx_init; int yiter = y * fb->width; for (int x = outrect.x1; x < outrect.x2; x++) { uint16_t pix = sprite->pixels[((texy >> unigi_ext_texture_fixedpointdepth) * sprite->width) + (texx >> unigi_ext_texture_fixedpointdepth)]; if (pix & 0xF000) { fb->pixels[yiter + x] = pix; } texx += stepx; } texy += stepy; } } 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; } uint16_t width; uint16_t 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, unigi_ext_type_2d_coord * height ) { *width = abs(bounds.x2 - bounds.x1); *height = abs(bounds.y2 - bounds.y1); }