gba-sprite-engine/engine/src/palette/palette_manager.cpp

102 lines
3.2 KiB
C++
Raw Normal View History

2018-08-01 16:03:16 +02:00
//
// Created by Wouter Groeneveld on 27/07/18.
//
2018-08-04 10:43:27 +02:00
#ifdef CODE_COMPILED_AS_PART_OF_TEST
#include <engine/gba/tonc_core_stub.h>
#else
2018-08-01 16:03:16 +02:00
#include <engine/gba/tonc_core.h>
2018-08-04 10:43:27 +02:00
#endif
#include <engine/background/text_stream.h>
#include <engine/palette/palette_manager.h>
2018-08-01 16:03:16 +02:00
const COLOR defaultPaletteData[PALETTE_MAX_SIZE] __attribute__((aligned(4))) = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
PaletteManager::PaletteManager() : PaletteManager(defaultPaletteData, PALETTE_MAX_SIZE) {
}
2018-08-04 10:43:27 +02:00
int getBits(int number, int k, int p) {
return (((1 << k) - 1) & (number >> p));
}
2018-08-06 09:50:53 +02:00
PaletteManager::PaletteManager(const COLOR *paletteData, int size) : data(paletteData), size(size) {
}
CombinedPalette* PaletteManager::operator+(const PaletteManager &other) {
return new CombinedPalette(*this, const_cast<PaletteManager&>(other));
}
2018-08-01 16:03:16 +02:00
void PaletteManager::persist() {
dma3_cpy(this->paletteAddress(), this->data, this->size);
2018-08-01 16:03:16 +02:00
}
COLOR PaletteManager::change(int bank, int index, COLOR newColor) {
auto palBank = this->paletteBank();
COLOR oldColor = palBank[bank][index];
palBank[bank][index] = newColor;
return oldColor;
}
COLOR PaletteManager::color(u32 red, u32 green, u32 blue) {
2018-08-04 10:43:27 +02:00
if(red > 31) red = 31;
if(green > 31) green = 31;
if(blue > 31) blue = 31;
return red | (green<<5) | (blue<<10);
}
2018-08-04 10:43:27 +02:00
u32 PaletteManager::red(COLOR r) {
return getBits(r, 5, 0);
}
u32 PaletteManager::green(COLOR r) {
return getBits(r, 5, 5);
}
u32 PaletteManager::blue(COLOR r) {
return getBits(r, 5, 10);
}
COLOR PaletteManager::modify(COLOR color, u32 intensity) {
return PaletteManager::color(PaletteManager::red(color) + intensity,
PaletteManager::green(color) + intensity, PaletteManager::blue(color) + intensity);
}
2018-08-04 10:43:27 +02:00
void PaletteManager::increaseBrightness(u32 intensity) {
if(intensity > 31) {
failure(Brightness_Intensity_Too_High);
return;
}
for(int bank = 0; bank < PALETTE_BANK_SIZE; bank++) {
for(int index = 0; index < PALETTE_BANK_SIZE; index++) {
auto current = get(bank, index);
auto next = PaletteManager::modify(current, intensity);
2018-08-04 10:43:27 +02:00
change(bank, index, next);
}
}
}
void PaletteManager::persistToBank(int bank) {
auto palBank = this->paletteBank();
dma3_cpy(palBank[bank], this->data, PALETTE_BANK_SIZE);
}