CHGame 0.3.0
Libraries for the CHGame handheld
Loading...
Searching...
No Matches
Palette.h
Go to the documentation of this file.
1/// @file Palette.h
2/// @brief The house palette: sixteen named colours, and the tricks a palette allows.
3///
4/// CHGfx draws with palette indices (0-15) and applies the palette while it
5/// converts the framebuffer for the panel, so recolouring an index recolours
6/// every pixel that uses it, for free. pal:: keeps a staging copy in RGB444
7/// (the 12 bpp panel's own grid, so output, fades and the simulator agree)
8/// and pal::commit() works out the final colours once a frame: the felt
9/// theme, FX_A/FX_B colour cycling, a flash, desaturation and the fade.
10/// CHGfx stages gfx_setPalette() itself (it takes effect when the next flush
11/// starts), so commit() may run while a frame is still going out; it only
12/// calls it when a colour really changed, because each change costs CHGfx a
13/// lookup-table rebuild.
14///
15/// Every frame: pal::tick() once per logic tick, pal::commit() before the
16/// flush.
17#pragma once
18#include <stdint.h>
19
20/// @defgroup chgame_palette Palette
21/// @ingroup lib_chgame
22/// @brief The sixteen house colours and pal::: felt themes, fades, flashes
23/// and colour cycling.
24///
25/// Draw with the colour names (`gfx_clear(FELT)`, `text35(x, y, s, GOLD)`).
26/// Set the palette up once with pal::init(), then every frame:
27/// @code
28/// pal::tick(); // once per logic tick: FX_A/FX_B cycle, flashes count down
29/// gfx_wait();
30/// pal::commit(); // before the flush: hands the colours to CHGfx if they changed
31/// // ... draw ...
32/// gfx_flushAsync();
33/// @endcode
34/// Colours are RGB444 (`0xRGB`, 4 bits a channel), the 12 bpp panel's own grid.
35/// @{
36
37/// @brief The house colours, by palette index.
38/// @details A game that needs other colours in a slot passes its own table to
39/// pal::init() and names the slot itself (CHDominoes has BONE and SLATE where
40/// SKIN and CYAN are).
41enum : uint8_t {
42 INK = 0, ///< 0: black (0x000), outlines and text.
43 WHITE, ///< 1: white (0xFFF).
44 FELT_DK, ///< 2: dark felt; re-dyed by pal::setTheme().
45 FELT, ///< 3: the table felt; re-dyed by pal::setTheme().
46 FELT_LT, ///< 4: light felt; re-dyed by pal::setTheme().
47 SILVER, ///< 5: light grey (0xBBC).
48 RED, ///< 6: red (0xE12).
49 WINE, ///< 7: dark red (0x702).
50 GOLD, ///< 8: gold (0xFC2).
51 WOOD, ///< 9: brown (0x741), table rails.
52 BLUE, ///< 10: blue (0x26E).
53 NAVY, ///< 11: dark blue (0x125).
54 SKIN, ///< 12: skin tone (0xFB8).
55 CYAN, ///< 13: cyan (0x6EF).
56 FX_A, ///< 14: animated by pal::tick() while cycling is on (see pal::Mode).
57 FX_B, ///< 15: animated by pal::tick() while cycling is on (see pal::Mode).
58};
59
60/// @brief The palette: themes, fades, flashes and colour cycling.
61namespace pal {
62
63/// @brief The house colours above, RGB444, in index order. pal::init()'s default.
64extern const uint16_t HOUSE[16];
65
66/// @brief The felt colours: FELT_DK, FELT and FELT_LT can be re-dyed as a whole.
67enum Theme : uint8_t {
68 GREEN, ///< The classic green felt (the default).
69 BLUE_FELT, ///< Blue felt.
70 RED_FELT, ///< Red felt.
71 PURPLE, ///< Purple felt.
72 THEME_COUNT ///< The number of built-in themes.
73};
74/// @brief The built-in felts: FELT_DK, FELT, FELT_LT in RGB444 for each Theme.
75extern const uint16_t FELTS[THEME_COUNT][3];
76
77/// @brief What FX_A and FX_B do by themselves while cycling is on (setMode()).
78enum Mode : uint8_t {
79 /// FX_A runs through a rainbow (banner outlines, sparkle); FX_B shimmers
80 /// gold - white - gold. The default.
82 /// FX_A shimmers cyan - white (squares a piece can go to); FX_B pulses
83 /// red - gold (pieces it can take).
85 /// FX_A fades black - white - black about once a second (a cursor
86 /// outline); FX_B as CASINO.
88 /// FX_A flickers red - gold - white (a hot hand); FX_B as CASINO.
89 FIRE
90};
91
92/// @brief Load a set of colours and commit them at once.
93/// @param base Sixteen RGB444 colours, in index order (default: HOUSE, the
94/// green felt). FELT_DK..FELT_LT come from here until setTheme().
95/// @details Call once in `setup()`, after gfx_begin().
96void init(const uint16_t *base = HOUSE);
97/// @brief Replace the built-in felt themes with a game's own.
98/// @param felts A table of `count` themes, each FELT_DK, FELT, FELT_LT in RGB444
99/// (kept by pointer: it must stay valid).
100/// @param count The number of themes in the table.
101void setThemes(const uint16_t (*felts)[3], uint8_t count);
102/// @brief Re-dye the felt (FELT_DK, FELT, FELT_LT).
103/// @param theme A Theme, or an index into the table given to setThemes(); an
104/// index past the end selects theme 0.
105void setTheme(uint8_t theme);
106/// @brief The current felt theme.
107/// @return The index last given to setTheme() (0 at start).
108uint8_t theme();
109/// @brief Choose what FX_A and FX_B animate as.
110/// @param mode A Mode (default CASINO).
111void setMode(uint8_t mode);
112/// @brief Fade the whole screen toward black.
113/// @param level 0 = black .. 16 = full colour (the default); larger values are 16.
114/// @details Fade in or out by stepping the level a frame at a time.
115void setFade(uint8_t level);
116/// @brief The current fade level.
117/// @return 0 (black) .. 16 (full colour).
118uint8_t fade();
119/// @brief Wash the colours toward grey (a paused game, a game over).
120/// @param amount 0 = full colour (the default) .. 16 = grey.
121void setDesaturate(uint8_t amount);
122/// @brief Show one palette index in another colour for a few frames (a hit, a win).
123/// @param index The palette index to flash (0-15).
124/// @param rgb444 The colour to show it in.
125/// @param frames How many tick()s the flash lasts. One flash at a time: a new
126/// one replaces the last.
127void flash(uint8_t index, uint16_t rgb444, uint8_t frames);
128/// @brief Set a colour by hand.
129/// @param index The palette index (0-15); for FX_A/FX_B, turn cycling off
130/// first or tick() overwrites it.
131/// @param rgb444 The colour.
132void setFx(uint8_t index, uint16_t rgb444);
133/// @brief Whether FX_A/FX_B animate by themselves.
134/// @param on true (the default) to cycle as setMode() says; false to leave them
135/// as set with setFx().
136void setCycling(bool on);
137/// @brief Advance the colour cycling and any flash by one tick.
138/// @details Call once per logic tick, before commit().
139void tick();
140/// @brief Restart the FX_A/FX_B cycle from its beginning.
141/// @details The debug protocol uses it so scripted runs repeat exactly.
143/// @brief Work out the final colours and hand them to CHGfx if any changed.
144/// @details Call once per frame, before the flush (after gfx_wait() is the
145/// usual place, but any time is safe: CHGfx stages the palette for the next
146/// flush). A frame where nothing changed costs a few comparisons.
147void commit();
148/// @brief A staged colour, before fade, flash and desaturation.
149/// @param index The palette index (0-15).
150/// @return Its colour, RGB444.
151uint16_t rgb444(uint8_t index);
152
153} // namespace pal
154
155/// @}
@ NAVY
11: dark blue (0x125).
Definition Palette.h:53
@ WHITE
1: white (0xFFF).
Definition Palette.h:43
@ BLUE
10: blue (0x26E).
Definition Palette.h:52
@ FELT_LT
4: light felt; re-dyed by pal::setTheme().
Definition Palette.h:46
@ FX_B
15: animated by pal::tick() while cycling is on (see pal::Mode).
Definition Palette.h:57
@ WOOD
9: brown (0x741), table rails.
Definition Palette.h:51
@ SILVER
5: light grey (0xBBC).
Definition Palette.h:47
@ FX_A
14: animated by pal::tick() while cycling is on (see pal::Mode).
Definition Palette.h:56
@ GOLD
8: gold (0xFC2).
Definition Palette.h:50
@ CYAN
13: cyan (0x6EF).
Definition Palette.h:55
@ FELT
3: the table felt; re-dyed by pal::setTheme().
Definition Palette.h:45
@ WINE
7: dark red (0x702).
Definition Palette.h:49
@ SKIN
12: skin tone (0xFB8).
Definition Palette.h:54
@ FELT_DK
2: dark felt; re-dyed by pal::setTheme().
Definition Palette.h:44
@ INK
0: black (0x000), outlines and text.
Definition Palette.h:42
@ RED
6: red (0xE12).
Definition Palette.h:48
The palette: themes, fades, flashes and colour cycling.
Definition Palette.h:61
Mode
What FX_A and FX_B do by themselves while cycling is on (setMode()).
Definition Palette.h:78
@ CASINO
FX_A runs through a rainbow (banner outlines, sparkle); FX_B shimmers gold - white - gold.
Definition Palette.h:81
@ TARGETS
FX_A shimmers cyan - white (squares a piece can go to); FX_B pulses red - gold (pieces it can take).
Definition Palette.h:84
@ HOVER
FX_A fades black - white - black about once a second (a cursor outline); FX_B as CASINO.
Definition Palette.h:87
@ FIRE
FX_A flickers red - gold - white (a hot hand); FX_B as CASINO.
Definition Palette.h:89
void tick()
Advance the colour cycling and any flash by one tick.
void flash(uint8_t index, uint16_t rgb444, uint8_t frames)
Show one palette index in another colour for a few frames (a hit, a win).
uint8_t fade()
The current fade level.
const uint16_t FELTS[THEME_COUNT][3]
The built-in felts: FELT_DK, FELT, FELT_LT in RGB444 for each Theme.
void setDesaturate(uint8_t amount)
Wash the colours toward grey (a paused game, a game over).
void setFx(uint8_t index, uint16_t rgb444)
Set a colour by hand.
uint16_t rgb444(uint8_t index)
A staged colour, before fade, flash and desaturation.
void setCycling(bool on)
Whether FX_A/FX_B animate by themselves.
void setTheme(uint8_t theme)
Re-dye the felt (FELT_DK, FELT, FELT_LT).
const uint16_t HOUSE[16]
The house colours above, RGB444, in index order. pal::init()'s default.
void setThemes(const uint16_t(*felts)[3], uint8_t count)
Replace the built-in felt themes with a game's own.
uint8_t theme()
The current felt theme.
Theme
The felt colours: FELT_DK, FELT and FELT_LT can be re-dyed as a whole.
Definition Palette.h:67
@ BLUE_FELT
Blue felt.
Definition Palette.h:69
@ RED_FELT
Red felt.
Definition Palette.h:70
@ THEME_COUNT
The number of built-in themes.
Definition Palette.h:72
@ GREEN
The classic green felt (the default).
Definition Palette.h:68
@ PURPLE
Purple felt.
Definition Palette.h:71
void commit()
Work out the final colours and hand them to CHGfx if any changed.
void init(const uint16_t *base=HOUSE)
Load a set of colours and commit them at once.
void setFade(uint8_t level)
Fade the whole screen toward black.
void setMode(uint8_t mode)
Choose what FX_A and FX_B animate as.
void resetClock()
Restart the FX_A/FX_B cycle from its beginning.