Functions to allocate, reallocate, assign palettes

This commit is contained in:
Vftdan 2024-10-03 23:45:25 +02:00
parent 91fc5098eb
commit 26f4511ceb
2 changed files with 50 additions and 0 deletions

View File

@ -1,5 +1,51 @@
#include "cc-common.h" #include "cc-common.h"
#include <stdlib.h>
struct palette *cc_alloc_palette(uint8_t count) {
struct palette *palette = calloc(1, sizeof(struct palette) + count * sizeof(union color));
if (!palette) {
return NULL;
}
uint8_t *count_ptr = (uint8_t*)&palette->count;
*count_ptr = count;
return palette;
}
struct palette *cc_realloc_palette(struct palette* ptr, uint8_t count) {
if (!ptr) {
return cc_alloc_palette(count);
}
struct palette *palette;
if (count > ptr->count) {
palette = realloc(ptr, sizeof(struct palette) + count * sizeof(union color));
if (!palette) {
return NULL;
}
for (int i = palette->count; i < count; i++) {
palette->colors[i] = (union color){ .v = 0 };
}
} else {
palette = ptr;
}
uint8_t *count_ptr = (uint8_t*)&palette->count;
*count_ptr = count;
return palette;
}
void cc_assign_palette(struct palette *dst, const struct palette *src) {
if (!dst || !src) {
return;
}
int count = dst->count;
if (count > src->count) {
count = src->count;
}
for (int i = 0; i < count; ++i) {
dst->colors[i] = src->colors[i];
}
}
const struct palette cc_default_palette = PALETTE( const struct palette cc_default_palette = PALETTE(
{ { 0xf0, 0xf0, 0xf0, 0xff } }, { { 0xf0, 0xf0, 0xf0, 0xff } },
{ { 0xf2, 0xb2, 0x33, 0xff } }, { { 0xf2, 0xb2, 0x33, 0xff } },

View File

@ -19,6 +19,10 @@ struct palette {
#define LENGTHOF(...) (sizeof(__VA_ARGS__) / sizeof(*(__VA_ARGS__))) #define LENGTHOF(...) (sizeof(__VA_ARGS__) / sizeof(*(__VA_ARGS__)))
#define PALETTE(...) { .count = LENGTHOF((union color[]){__VA_ARGS__}), .colors = {__VA_ARGS__} } #define PALETTE(...) { .count = LENGTHOF((union color[]){__VA_ARGS__}), .colors = {__VA_ARGS__} }
struct palette *cc_alloc_palette(uint8_t count);
struct palette *cc_realloc_palette(struct palette* ptr, uint8_t count);
void cc_assign_palette(struct palette *dst, const struct palette *src);
const extern GlyphBitmap cc_font_atlas[256]; const extern GlyphBitmap cc_font_atlas[256];
const extern struct palette cc_default_palette, cc_default_gray_palette; const extern struct palette cc_default_palette, cc_default_gray_palette;