fxengine/src/render/bitmap.c

90 lines
2.1 KiB
C
Raw Normal View History

2019-08-29 14:07:02 +02:00
#include <gint/display.h>
2019-09-07 19:57:20 +02:00
#include <render/bitmap.h>
#include <gint/std/string.h>
2019-08-29 16:38:48 +02:00
#include <gint/std/stdlib.h>
bitmap_rich* bitmap_new_rich(uint32_t size_px_x, uint32_t size_px_y, uint32_t* color, bool copy_color, uint32_t *layout, bool copy_layout)
{
bitmap_rich* bmp = malloc(sizeof(bitmap_rich));
2019-08-29 17:06:19 +02:00
if (!bmp)
return 0;
2019-08-29 16:38:48 +02:00
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;
2019-08-29 16:38:48 +02:00
bmp->layout_dynamic = copy_layout;
uint32_t size_octets = size_px_y * bmp->size_o_y * sizeof(uint32_t);
2019-08-29 16:38:48 +02:00
2019-08-29 16:47:54 +02:00
if (copy_color)
{
2019-08-29 17:06:19 +02:00
bmp->color = malloc(size_octets);
if (!bmp->color)
{
free(bmp);
return 0;
}
2019-08-29 16:47:54 +02:00
memcpy(bmp->color, color, size_octets);
}
else
bmp->color = color;
if (copy_layout && layout)
{
bmp->layout=malloc(size_octets);
2019-08-29 17:06:19 +02:00
if (!bmp->layout)
{
free(bmp);
if (copy_color)
free(bmp->color);
return 0;
}
2019-08-29 16:47:54 +02:00
memcpy(bmp->layout, layout, size_octets);
}
else
bmp->layout = layout;
2019-08-29 19:01:51 +02:00
2019-08-29 17:06:19 +02:00
return bmp;
}
void bitmap_delete_rich(bitmap_rich* bmp)
{
if (!bmp)
return;
if (bmp->layout_dynamic && bmp->layout)
free(bmp->layout);
if (bmp->color)
free(bmp->color);
free(bmp);
2019-08-29 16:38:48 +02:00
}
uint8_t bitmap_get_pixel_r(const bitmap_rich * bmp, uint32_t x, uint32_t y)
{
2019-08-29 14:07:02 +02:00
if (x >= bmp->size_px_x || y >= bmp->size_px_y)
return 0;
2019-08-31 20:21:51 +02:00
const uint32_t indice = y * bmp->size_o_y + (x / 32);
2019-08-31 20:21:51 +02:00
const uint32_t numero_bit = 31 - (x %32);
2019-08-29 19:01:51 +02:00
2019-08-29 16:38:48 +02:00
if (bmp->layout)
2019-08-31 20:21:51 +02:00
return (((bmp->layout[indice] & (1 << numero_bit)) >> numero_bit) + ((bmp->color[indice] & (1 << numero_bit)) >> numero_bit));
2019-08-29 16:38:48 +02:00
else
2019-08-31 20:21:51 +02:00
return (0b10 + ((bmp->color[indice] & (1 << numero_bit)) >> numero_bit));
}
2019-08-29 14:07:02 +02:00
void bitmap_display_pixel_r(const bitmap_rich * bmp, uint32_t bmp_x, uint32_t bmp_y, uint32_t x, uint32_t y)
{
uint8_t color = bitmap_get_pixel_r(bmp, bmp_x, bmp_y);
2019-08-31 20:21:51 +02:00
if (color)
2019-08-29 14:07:02 +02:00
dpixel(x, y, 3 * (color % 2));
}