Store glyph pixels as chars, not floats

Smaller memory footprint, and probably-insignificant performance
impact.
This commit is contained in:
Clownacy 2020-02-09 13:56:45 +00:00
parent bb11cd567d
commit d4004fe99a

View file

@ -23,7 +23,7 @@ typedef struct Backend_Surface
typedef struct Backend_Glyph typedef struct Backend_Glyph
{ {
float *pixels; unsigned char *pixels;
unsigned int width; unsigned int width;
unsigned int height; unsigned int height;
} Backend_Glyph; } Backend_Glyph;
@ -304,7 +304,7 @@ Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width
if (glyph == NULL) if (glyph == NULL)
return NULL; return NULL;
glyph->pixels = (float*)malloc(width * height * sizeof(float)); glyph->pixels = (unsigned char*)malloc(width * height);
if (glyph->pixels == NULL) if (glyph->pixels == NULL)
{ {
@ -312,15 +312,10 @@ Backend_Glyph* Backend_LoadGlyph(const unsigned char *pixels, unsigned int width
return NULL; return NULL;
} }
float *destination_pointer = glyph->pixels; unsigned char *destination_pointer = glyph->pixels;
for (unsigned int y = 0; y < height; ++y) for (unsigned int y = 0; y < height; ++y)
{ memcpy(&glyph->pixels[y * width], &pixels[y * pitch], width);
const unsigned char *source_pointer = &pixels[y * pitch];
for (unsigned int x = 0; x < width; ++x)
*destination_pointer++ = *source_pointer++ / 255.0f;
}
glyph->width = width; glyph->width = width;
glyph->height = height; glyph->height = height;
@ -356,10 +351,12 @@ void Backend_DrawGlyph(Backend_Glyph *glyph, long x, long y)
{ {
for (unsigned int ix = MAX(-x, 0); x + ix < MIN(x + glyph->width, glyph_destination_surface->width); ++ix) for (unsigned int ix = MAX(-x, 0); x + ix < MIN(x + glyph->width, glyph_destination_surface->width); ++ix)
{ {
const float alpha = glyph->pixels[iy * glyph->width + ix]; unsigned char alpha_int = glyph->pixels[iy * glyph->width + ix];
if (alpha != 0.0f) if (alpha_int != 0)
{ {
float alpha = alpha_int / 255.0f;
unsigned char *bitmap_pixel = glyph_destination_surface->pixels + (y + iy) * glyph_destination_surface->pitch + (x + ix) * 3; unsigned char *bitmap_pixel = glyph_destination_surface->pixels + (y + iy) * glyph_destination_surface->pitch + (x + ix) * 3;
for (unsigned int j = 0; j < 3; ++j) for (unsigned int j = 0; j < 3; ++j)