fxengine/src/texture.c

117 lines
2.8 KiB
C

#include <fxengine/texture.h>
#include <gint/display.h>
#include <gint/std/string.h>
#include <gint/std/stdlib.h>
fe_texture_rich* fe_texture_new_rich(uint32_t size_px_x, uint32_t size_px_y, uint32_t* color, bool copy_color, uint32_t *layout, bool copy_layout)
{
fe_texture_rich* bmp = malloc(sizeof(fe_texture_rich));
if (!bmp)
return 0;
bmp->size_px_x = size_px_x;
bmp->size_px_y = size_px_y;
bmp->size_o_y = 1 + (size_px_x - 1)/(8*sizeof(uint32_t));
bmp->color_dynamic = copy_color;
bmp->layout_dynamic = copy_layout;
uint32_t size_octets = size_px_y * bmp->size_o_y * sizeof(uint32_t);
if (copy_color)
{
bmp->color = malloc(size_octets);
if (!bmp->color)
{
free(bmp);
return 0;
}
memcpy(bmp->color, color, size_octets);
}
else
bmp->color = color;
if (copy_layout && layout)
{
bmp->layout=malloc(size_octets);
if (!bmp->layout)
{
free(bmp);
if (copy_color)
free(bmp->color);
return 0;
}
memcpy(bmp->layout, layout, size_octets);
}
else
bmp->layout = layout;
return bmp;
}
void fe_texture_debug(fe_texture_rich * txtr)
{
dclear(C_WHITE);
if (txtr)
{
char text[21];
sprintf(text, "ADDRESS : %p", txtr);
dtext(1,1, text, C_BLACK, C_NONE);
sprintf(text, "size %d %d", txtr->size_px_x, txtr->size_px_y);
dtext(1,9, text, C_BLACK, C_NONE);
sprintf(text, "color %p %d", txtr->color, txtr->color_dynamic);
dtext(1,17, text, C_BLACK, C_NONE);
sprintf(text, "layout %p %d", txtr->layout, txtr->layout_dynamic);
dtext(1,25, text, C_BLACK, C_NONE);
}
else
{
dtext(1,1, "Texture set as NULL", C_BLACK, C_NONE);
}
dupdate();
getkey();
}
void fe_texture_delete_rich(fe_texture_rich * bmp)
{
if (!bmp)
return;
if (bmp->layout_dynamic && bmp->layout)
free(bmp->layout);
if (bmp->color)
free(bmp->color);
free(bmp);
}
uint8_t fe_texture_get_pixel_r(const fe_texture_rich * bmp, uint32_t x, uint32_t y)
{
if (x >= bmp->size_px_x || y >= bmp->size_px_y)
return 0;
const uint32_t indice = y * bmp->size_o_y + (x / 32);
const uint32_t numero_bit = 31 - (x %32);
if (bmp->layout)
return (((bmp->layout[indice] & (1 << numero_bit)) >> numero_bit) + ((bmp->color[indice] & (1 << numero_bit)) >> numero_bit));
else
return (0b10 + ((bmp->color[indice] & (1 << numero_bit)) >> numero_bit));
}
void fe_texture_display_pixel_r(const fe_texture_rich * bmp, uint32_t bmp_x, uint32_t bmp_y, uint32_t x, uint32_t y)
{
uint8_t color = fe_texture_get_pixel_r(bmp, bmp_x, bmp_y);
if (color)
dpixel(x, y, 3 * (color % 2));
}