cryptic-clock/src/c/bitmaps.cpp
2025-12-09 14:50:06 +01:00

98 lines
2.8 KiB
C++

#include "bitmaps.hpp"
#include <@rebble/sdk-c-utils/macros.h>
using namespace Pebble;
// TODO move this to pebble-cpp
bool operator==(const GColor8& lhs, const GColor8& rhs) { return lhs.argb == rhs.argb; }
bool operator!=(const GColor8& lhs, const GColor8& rhs) { return lhs.argb != rhs.argb; }
namespace {
GOwnedBitmap digitsBitmap;
GOwnedBitmap digitBitmaps[10];
GOwnedBitmap frameBitmap;
GColor* digitColor;
GColor* frameColor;
int get_palette_size(GBitmapBase& bitmap){
switch (bitmap.GetFormat()) {
case GBitmapFormat1BitPalette: return 2;
case GBitmapFormat2BitPalette: return 4;
case GBitmapFormat4BitPalette: return 16;
default: return 0;
}
}
GColor* find_color(GBitmapBase& bitmap, GColor color) {
GColor* palette = bitmap.GetPalette();
if (palette == nullptr) {
ERROR("Palette is null");
return nullptr;
}
int paletteSize = get_palette_size(bitmap);
if (paletteSize == 0) {
ERROR("Expected GBitmapFormat[124]BitPalette, got: %d", digitsBitmap.GetFormat());
return nullptr;
}
for (int i = 0; i < paletteSize; i++) {
if (palette[i] == color) {
DEBUG("Found color 0x%02X at index %d", color, i);
return &palette[i];
}
}
ERROR("Color 0x%02X not found in the palette", color);
return nullptr;
}
}
void init_bitmaps() {
digitsBitmap = GOwnedBitmap::CreateWithResource(RESOURCE_ID_DIGITS);
auto size = digitsBitmap.GetBounds().size;
uint16_t w = size.w / 10;
uint16_t h = size.h;
for (int i = 0; i < 10; i++) {
digitBitmaps[i] = GOwnedBitmap::CreateSubBitmap(digitsBitmap, Pebble::GRect(i * w, 0, w, h));
}
frameBitmap = GOwnedBitmap::CreateWithResource(RESOURCE_ID_FRAME);
digitColor = find_color(digitsBitmap, GColorBlack);
frameColor = find_color(frameBitmap, GColorBlack);
}
void deinit_bitmaps() {
for (int i = 0; i < 10; i++) {
digitBitmaps[i] = GOwnedBitmap();
}
digitsBitmap = GOwnedBitmap();
frameBitmap = GOwnedBitmap();
digitColor = nullptr;
frameColor = nullptr;
}
GBitmapBase& get_digit_bitmap(uint16_t digit) {return digitBitmaps[digit];}
GBitmapBase& get_frame_bitmap() {return frameBitmap;}
void set_digit_color(GColor color) {
if (digitColor != nullptr) *digitColor = color;
}
void set_frame_color(GColor color) {
if (frameColor != nullptr) *frameColor = color;
}
void print_palette(GBitmapBase& bitmap) {
int paletteSize = get_palette_size(bitmap);
if (paletteSize == 0) {
ERROR("Expected GBitmapFormat[124]BitPalette, got: %d", digitsBitmap.GetFormat());
return;
}
DEBUG("%d colors:", paletteSize);
GColor8* palette = bitmap.GetPalette();
for (int i = 0; i < paletteSize; i++) {
DEBUG("* 0x%02X = argb(%d,%d,%d,%d)", palette[i].argb, palette[i].a, palette[i].r, palette[i].g, palette[i].b);
}
}