| 1 |
#include <gba/gba.hpp> |
| 2 |
|
| 3 |
namespace GameBoyAdvance { |
| 4 |
|
| 5 |
Video video; |
| 6 |
|
| 7 |
void Video::generate_palette(Emulator::Interface::PaletteMode mode) { |
| 8 |
for(unsigned color = 0; color < (1 << 15); color++) { |
| 9 |
if(mode == Emulator::Interface::PaletteMode::Literal) { |
| 10 |
palette[color] = color; |
| 11 |
continue; |
| 12 |
} |
| 13 |
|
| 14 |
unsigned B = (color >> 10) & 31; |
| 15 |
unsigned G = (color >> 5) & 31; |
| 16 |
unsigned R = (color >> 0) & 31; |
| 17 |
|
| 18 |
if(mode == Emulator::Interface::PaletteMode::Channel) { |
| 19 |
R = image::normalize(R, 5, 16); |
| 20 |
G = image::normalize(G, 5, 16); |
| 21 |
B = image::normalize(B, 5, 16); |
| 22 |
palette[color] = interface->videoColor(color, 0, R, G, B); |
| 23 |
continue; |
| 24 |
} |
| 25 |
|
| 26 |
if(mode == Emulator::Interface::PaletteMode::Standard) { |
| 27 |
R = image::normalize(R, 5, 16); |
| 28 |
G = image::normalize(G, 5, 16); |
| 29 |
B = image::normalize(B, 5, 16); |
| 30 |
palette[color] = interface->videoColor(color, 0, R, G, B); |
| 31 |
continue; |
| 32 |
} |
| 33 |
|
| 34 |
if(mode == Emulator::Interface::PaletteMode::Emulation) { |
| 35 |
R = curve[R]; |
| 36 |
G = curve[G]; |
| 37 |
B = curve[B]; |
| 38 |
|
| 39 |
unsigned Rr = R * 16; |
| 40 |
unsigned Gr = R * 4; |
| 41 |
unsigned Br = R * 4; |
| 42 |
|
| 43 |
unsigned Rg = G * 8; |
| 44 |
unsigned Gg = G * 16; |
| 45 |
unsigned Bg = G * 8; |
| 46 |
|
| 47 |
unsigned Rb = B * 0; //intentionally always zero |
| 48 |
unsigned Gb = B * 8; |
| 49 |
unsigned Bb = B * 16; |
| 50 |
|
| 51 |
if(Rr < Rg) std::swap(Rr, Rg); |
| 52 |
if(Rr < Rb) std::swap(Rr, Rb); |
| 53 |
if(Rg < Rb) std::swap(Rg, Rb); |
| 54 |
|
| 55 |
if(Gr < Gg) std::swap(Gr, Gg); |
| 56 |
if(Gr < Gb) std::swap(Gr, Gb); |
| 57 |
if(Gg < Gb) std::swap(Gg, Gb); |
| 58 |
|
| 59 |
if(Br < Bg) std::swap(Br, Bg); |
| 60 |
if(Br < Bb) std::swap(Br, Bb); |
| 61 |
if(Bg < Bb) std::swap(Bg, Bb); |
| 62 |
|
| 63 |
R = (((4 * Rr + 2 * Rg + Rb) * 160) >> 14) + 32; |
| 64 |
G = (((4 * Gr + 2 * Gg + Gb) * 160) >> 14) + 32; |
| 65 |
B = (((4 * Br + 2 * Bg + Bb) * 160) >> 14) + 32; |
| 66 |
|
| 67 |
R = image::normalize(R, 8, 16); |
| 68 |
G = image::normalize(G, 8, 16); |
| 69 |
B = image::normalize(B, 8, 16); |
| 70 |
palette[color] = interface->videoColor(color, 0, R, G, B); |
| 71 |
continue; |
| 72 |
} |
| 73 |
|
| 74 |
palette[color] = 0; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
Video::Video() { |
| 79 |
palette = new uint32_t[1 << 15](); |
| 80 |
} |
| 81 |
|
| 82 |
Video::~Video() { |
| 83 |
delete[] palette; |
| 84 |
} |
| 85 |
|
| 86 |
const uint8 Video::curve[32] = { |
| 87 |
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0e, 0x10, 0x12, |
| 88 |
0x14, 0x16, 0x18, 0x1c, 0x20, 0x28, 0x38, 0x38, |
| 89 |
0x40, 0x48, 0x50, 0x58, 0x60, 0x68, 0x70, 0x80, |
| 90 |
0x88, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, |
| 91 |
}; |
| 92 |
|
| 93 |
} |