Skip to content

Commit e2caf95

Browse files
committed
Hoist PICO-8 palette code out
1 parent 6d6b6d9 commit e2caf95

File tree

5 files changed

+72
-97
lines changed

5 files changed

+72
-97
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ set(PUBLIC_HEADERS
8080
include/framebuf/colour.h
8181
include/framebuf/composite.h
8282
include/framebuf/curve.h
83+
include/framebuf/palettes.h
8384
include/framebuf/pixelfmt.h
8485
include/framebuf/screen.h
8586
include/framebuf/span-bgrx8888.h
@@ -221,6 +222,7 @@ set(FRAMEBUF_SOURCES
221222
libraries/framebuf/colour/colour.c
222223
libraries/framebuf/composite/composite.c
223224
libraries/framebuf/curve/curve.c
225+
libraries/framebuf/palettes/palettes.c
224226
libraries/framebuf/pixelfmt/log2bpp.c
225227
libraries/framebuf/screen/screen.c
226228
libraries/framebuf/span-registry/get.c

include/framebuf/palettes.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* palettes.h */
2+
3+
#ifndef PALETTES_H
4+
#define PALETTES_H
5+
6+
#include "framebuf/colour.h"
7+
8+
/* PICO-8 palette */
9+
10+
#define palette_PICO8_BLACK (0)
11+
#define palette_PICO8_DARK_BLUE (1)
12+
#define palette_PICO8_DARK_PURPLE (2)
13+
#define palette_PICO8_DARK_GREEN (3)
14+
#define palette_PICO8_BROWN (4)
15+
#define palette_PICO8_DARK_GREY (5)
16+
#define palette_PICO8_LIGHT_GREY (6)
17+
#define palette_PICO8_WHITE (7)
18+
#define palette_PICO8_RED (8)
19+
#define palette_PICO8_ORANGE (9)
20+
#define palette_PICO8_YELLOW (10)
21+
#define palette_PICO8_GREEN (11)
22+
#define palette_PICO8_BLUE (12)
23+
#define palette_PICO8_LAVENDER (13)
24+
#define palette_PICO8_PINK (14)
25+
#define palette_PICO8_LIGHT_PEACH (15)
26+
27+
void define_pico8_palette(colour_t palette[16]);
28+
29+
#endif /* PALETTES_H */

libraries/framebuf/bmfont/test/bmfont-test.c

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,13 @@
1818
#include "base/utils.h"
1919
#include "io/path.h"
2020
#include "framebuf/colour.h"
21+
#include "framebuf/palettes.h"
2122
#include "framebuf/pixelfmt.h"
2223

2324
#include "framebuf/bmfont.h"
2425

2526
/* ----------------------------------------------------------------------- */
2627

27-
/* PICO-8 palette */
28-
29-
#define palette_BLACK (0)
30-
#define palette_DARK_BLUE (1)
31-
#define palette_DARK_PURPLE (2)
32-
#define palette_DARK_GREEN (3)
33-
#define palette_BROWN (4)
34-
#define palette_DARK_GREY (5)
35-
#define palette_LIGHT_GREY (6)
36-
#define palette_WHITE (7)
37-
#define palette_RED (8)
38-
#define palette_ORANGE (9)
39-
#define palette_YELLOW (10)
40-
#define palette_GREEN (11)
41-
#define palette_BLUE (12)
42-
#define palette_LAVENDER (13)
43-
#define palette_PINK (14)
44-
#define palette_LIGHT_PEACH (15)
45-
46-
/* ----------------------------------------------------------------------- */
47-
4828
#ifdef USE_SDL
4929

5030
#include <SDL.h>
@@ -312,7 +292,7 @@ static result_t bmfont_clipping_test(bmfontteststate_t *state)
312292
int fontwidth, fontheight;
313293
int i;
314294

315-
bitmap_clear(&state->bm, state->palette[palette_DARK_GREEN]);
295+
bitmap_clear(&state->bm, state->palette[palette_PICO8_DARK_GREEN]);
316296

317297
bmfont_get_info(bmfonts[font].bmfont, &fontwidth, &fontheight);
318298

@@ -335,15 +315,15 @@ static result_t bmfont_clipping_test(bmfontteststate_t *state)
335315
pos.x = centres[i].x - stringwidth / 2 + 1;
336316
pos.y = centres[i].y - fontheight / 2 + 1;
337317

338-
bg = transparent ? state->transparent : state->palette[palette_GREEN];
318+
bg = transparent ? state->transparent : state->palette[palette_PICO8_GREEN];
339319

340320
if (transparent)
341321
{
342322
rc = bmfont_draw(bmfonts[font].bmfont,
343323
&state->scr,
344324
lorem_ipsum,
345325
nchars,
346-
state->palette[palette_BLACK],
326+
state->palette[palette_PICO8_BLACK],
347327
bg,
348328
&pos,
349329
NULL /*endpos*/);
@@ -358,7 +338,7 @@ static result_t bmfont_clipping_test(bmfontteststate_t *state)
358338
&state->scr,
359339
lorem_ipsum,
360340
nchars,
361-
state->palette[palette_WHITE],
341+
state->palette[palette_PICO8_WHITE],
362342
bg,
363343
&pos,
364344
NULL /*endpos*/);
@@ -395,7 +375,7 @@ static result_t bmfont_layout_test(bmfontteststate_t *state)
395375
point_t origin = {0,0};
396376
char leafname[256];
397377

398-
bitmap_clear(&state->bm, state->palette[palette_DARK_GREEN]);
378+
bitmap_clear(&state->bm, state->palette[palette_PICO8_DARK_GREEN]);
399379

400380
bmfont_get_info(bmfont, &glyphwidth, &glyphheight);
401381

@@ -443,13 +423,13 @@ static result_t bmfont_layout_test(bmfontteststate_t *state)
443423
newline = 1;
444424
}
445425

446-
bg = transparent ? state->transparent : state->palette[palette_GREEN];
426+
bg = transparent ? state->transparent : state->palette[palette_PICO8_GREEN];
447427

448428
rc = bmfont_draw(bmfont,
449429
&state->scr,
450430
string,
451431
friendly_break,
452-
state->palette[palette_WHITE],
432+
state->palette[palette_PICO8_WHITE],
453433
bg,
454434
&origin,
455435
&endpos);
@@ -579,8 +559,8 @@ static result_t bmfont_interactive_test(bmfontteststate_t *state)
579559
quit = 1;
580560
#endif
581561

582-
colour_t fg = state->palette[palette_DARK_BLUE];
583-
colour_t bg = transparency ? state->transparent : state->palette[palette_LIGHT_PEACH];
562+
colour_t fg = state->palette[palette_PICO8_DARK_BLUE];
563+
colour_t bg = transparency ? state->transparent : state->palette[palette_PICO8_LIGHT_PEACH];
584564

585565
if (!dontclear)
586566
{
@@ -698,26 +678,6 @@ static result_t bmfont_interactive_test(bmfontteststate_t *state)
698678
return result_TEST_PASSED;
699679
}
700680

701-
static void define_pico8_palette(colour_t palette[16])
702-
{
703-
palette[palette_BLACK ] = colour_rgb(0x00, 0x00, 0x00);
704-
palette[palette_DARK_BLUE ] = colour_rgb(0x1D, 0x2B, 0x53);
705-
palette[palette_DARK_PURPLE] = colour_rgb(0x7E, 0x25, 0x53);
706-
palette[palette_DARK_GREEN ] = colour_rgb(0x00, 0x87, 0x51);
707-
palette[palette_BROWN ] = colour_rgb(0xAB, 0x52, 0x36);
708-
palette[palette_DARK_GREY ] = colour_rgb(0x5F, 0x57, 0x4F);
709-
palette[palette_LIGHT_GREY ] = colour_rgb(0xC2, 0xC3, 0xC7);
710-
palette[palette_WHITE ] = colour_rgb(0xFF, 0xF1, 0xE8);
711-
palette[palette_RED ] = colour_rgb(0xFF, 0x00, 0x4D);
712-
palette[palette_ORANGE ] = colour_rgb(0xFF, 0xA3, 0x00);
713-
palette[palette_YELLOW ] = colour_rgb(0xFF, 0xEC, 0x27);
714-
palette[palette_GREEN ] = colour_rgb(0x00, 0xE4, 0x36);
715-
palette[palette_BLUE ] = colour_rgb(0x29, 0xAD, 0xFF);
716-
palette[palette_LAVENDER ] = colour_rgb(0x83, 0x76, 0x9C);
717-
palette[palette_PINK ] = colour_rgb(0xFF, 0x77, 0xA8);
718-
palette[palette_LIGHT_PEACH] = colour_rgb(0xFF, 0xCC, 0xAA);
719-
}
720-
721681
result_t bmfont_test_one_format(const char *resources,
722682
int scr_width,
723683
int scr_height,
@@ -728,7 +688,6 @@ result_t bmfont_test_one_format(const char *resources,
728688
state.scr_width = scr_width;
729689
state.scr_height = scr_height;
730690

731-
732691
const int scr_rowbytes = (state.scr_width << pixelfmt_log2bpp(scr_fmt)) / 8;
733692

734693
define_pico8_palette(&state.palette[0]);

libraries/framebuf/curve/test/curve-test.c

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,13 @@
1818
#include "base/utils.h"
1919
#include "io/path.h"
2020
#include "framebuf/colour.h"
21+
#include "framebuf/palettes.h"
2122
#include "framebuf/pixelfmt.h"
2223

2324
#include "framebuf/curve.h"
2425

2526
/* ----------------------------------------------------------------------- */
2627

27-
/* PICO-8 palette */
28-
29-
#define palette_BLACK (0)
30-
#define palette_DARK_BLUE (1)
31-
#define palette_DARK_PURPLE (2)
32-
#define palette_DARK_GREEN (3)
33-
#define palette_BROWN (4)
34-
#define palette_DARK_GREY (5)
35-
#define palette_LIGHT_GREY (6)
36-
#define palette_WHITE (7)
37-
#define palette_RED (8)
38-
#define palette_ORANGE (9)
39-
#define palette_YELLOW (10)
40-
#define palette_GREEN (11)
41-
#define palette_BLUE (12)
42-
#define palette_LAVENDER (13)
43-
#define palette_PINK (14)
44-
#define palette_LIGHT_PEACH (15)
45-
46-
/* ----------------------------------------------------------------------- */
47-
4828
#ifdef USE_SDL
4929

5030
#include <SDL.h>
@@ -354,14 +334,14 @@ static void draw_a_curve(curveteststate_t *state)
354334
b.y1 = state->draw_points[i + 1].y;
355335

356336
if (state->opt.checker)
357-
colour = state->palette[(i & 1) ? palette_DARK_PURPLE : palette_LIGHT_PEACH];
337+
colour = state->palette[(i & 1) ? palette_PICO8_DARK_PURPLE : palette_PICO8_LIGHT_PEACH];
358338
else
359-
colour = state->palette[palette_BLACK];
339+
colour = state->palette[palette_PICO8_BLACK];
360340

361341
if (state->opt.draw_endpoints)
362342
{
363-
screen_draw_pixel(&state->scr, b.x0, b.y0, state->palette[palette_GREEN]);
364-
screen_draw_pixel(&state->scr, b.x1, b.y1, state->palette[palette_RED]);
343+
screen_draw_pixel(&state->scr, b.x0, b.y0, state->palette[palette_PICO8_GREEN]);
344+
screen_draw_pixel(&state->scr, b.x1, b.y1, state->palette[palette_PICO8_RED]);
365345
}
366346

367347
if (state->opt.use_aa)
@@ -620,7 +600,7 @@ static result_t curve_interactive_test(curveteststate_t *state)
620600

621601
draw_all_control_points(state);
622602
calc_all_curves(state);
623-
rotate_lines(state, mx, state->opt.checker ? palette_DARK_GREEN : palette_BLACK);
603+
rotate_lines(state, mx, state->opt.checker ? palette_PICO8_DARK_GREEN : palette_PICO8_BLACK);
624604

625605
#ifdef USE_SDL
626606
if (!box_is_empty(&state->overalldirty))
@@ -686,26 +666,6 @@ static result_t curve_interactive_test(curveteststate_t *state)
686666
return result_TEST_PASSED;
687667
}
688668

689-
static void define_pico8_palette(colour_t palette[16])
690-
{
691-
palette[palette_BLACK ] = colour_rgb(0x00, 0x00, 0x00);
692-
palette[palette_DARK_BLUE ] = colour_rgb(0x1D, 0x2B, 0x53);
693-
palette[palette_DARK_PURPLE] = colour_rgb(0x7E, 0x25, 0x53);
694-
palette[palette_DARK_GREEN ] = colour_rgb(0x00, 0x87, 0x51);
695-
palette[palette_BROWN ] = colour_rgb(0xAB, 0x52, 0x36);
696-
palette[palette_DARK_GREY ] = colour_rgb(0x5F, 0x57, 0x4F);
697-
palette[palette_LIGHT_GREY ] = colour_rgb(0xC2, 0xC3, 0xC7);
698-
palette[palette_WHITE ] = colour_rgb(0xFF, 0xF1, 0xE8);
699-
palette[palette_RED ] = colour_rgb(0xFF, 0x00, 0x4D);
700-
palette[palette_ORANGE ] = colour_rgb(0xFF, 0xA3, 0x00);
701-
palette[palette_YELLOW ] = colour_rgb(0xFF, 0xEC, 0x27);
702-
palette[palette_GREEN ] = colour_rgb(0x00, 0xE4, 0x36);
703-
palette[palette_BLUE ] = colour_rgb(0x29, 0xAD, 0xFF);
704-
palette[palette_LAVENDER ] = colour_rgb(0x83, 0x76, 0x9C);
705-
palette[palette_PINK ] = colour_rgb(0xFF, 0x77, 0xA8);
706-
palette[palette_LIGHT_PEACH] = colour_rgb(0xFF, 0xCC, 0xAA);
707-
}
708-
709669
result_t curve_test_one_format(const char *resources,
710670
int scr_width,
711671
int scr_height,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* palettes.c */
2+
3+
#include "framebuf/colour.h"
4+
5+
#include "framebuf/palettes.h"
6+
7+
void define_pico8_palette(colour_t palette[16])
8+
{
9+
palette[palette_PICO8_BLACK ] = colour_rgb(0x00, 0x00, 0x00);
10+
palette[palette_PICO8_DARK_BLUE ] = colour_rgb(0x1D, 0x2B, 0x53);
11+
palette[palette_PICO8_DARK_PURPLE] = colour_rgb(0x7E, 0x25, 0x53);
12+
palette[palette_PICO8_DARK_GREEN ] = colour_rgb(0x00, 0x87, 0x51);
13+
palette[palette_PICO8_BROWN ] = colour_rgb(0xAB, 0x52, 0x36);
14+
palette[palette_PICO8_DARK_GREY ] = colour_rgb(0x5F, 0x57, 0x4F);
15+
palette[palette_PICO8_LIGHT_GREY ] = colour_rgb(0xC2, 0xC3, 0xC7);
16+
palette[palette_PICO8_WHITE ] = colour_rgb(0xFF, 0xF1, 0xE8);
17+
palette[palette_PICO8_RED ] = colour_rgb(0xFF, 0x00, 0x4D);
18+
palette[palette_PICO8_ORANGE ] = colour_rgb(0xFF, 0xA3, 0x00);
19+
palette[palette_PICO8_YELLOW ] = colour_rgb(0xFF, 0xEC, 0x27);
20+
palette[palette_PICO8_GREEN ] = colour_rgb(0x00, 0xE4, 0x36);
21+
palette[palette_PICO8_BLUE ] = colour_rgb(0x29, 0xAD, 0xFF);
22+
palette[palette_PICO8_LAVENDER ] = colour_rgb(0x83, 0x76, 0x9C);
23+
palette[palette_PICO8_PINK ] = colour_rgb(0xFF, 0x77, 0xA8);
24+
palette[palette_PICO8_LIGHT_PEACH] = colour_rgb(0xFF, 0xCC, 0xAA);
25+
}

0 commit comments

Comments
 (0)