Skip to content

Commit

Permalink
Hoist PICO-8 palette code out
Browse files Browse the repository at this point in the history
  • Loading branch information
dpt committed Feb 10, 2024
1 parent 6d6b6d9 commit e2caf95
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 97 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ set(PUBLIC_HEADERS
include/framebuf/colour.h
include/framebuf/composite.h
include/framebuf/curve.h
include/framebuf/palettes.h
include/framebuf/pixelfmt.h
include/framebuf/screen.h
include/framebuf/span-bgrx8888.h
Expand Down Expand Up @@ -221,6 +222,7 @@ set(FRAMEBUF_SOURCES
libraries/framebuf/colour/colour.c
libraries/framebuf/composite/composite.c
libraries/framebuf/curve/curve.c
libraries/framebuf/palettes/palettes.c
libraries/framebuf/pixelfmt/log2bpp.c
libraries/framebuf/screen/screen.c
libraries/framebuf/span-registry/get.c
Expand Down
29 changes: 29 additions & 0 deletions include/framebuf/palettes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* palettes.h */

#ifndef PALETTES_H
#define PALETTES_H

#include "framebuf/colour.h"

/* PICO-8 palette */

#define palette_PICO8_BLACK (0)
#define palette_PICO8_DARK_BLUE (1)
#define palette_PICO8_DARK_PURPLE (2)
#define palette_PICO8_DARK_GREEN (3)
#define palette_PICO8_BROWN (4)
#define palette_PICO8_DARK_GREY (5)
#define palette_PICO8_LIGHT_GREY (6)
#define palette_PICO8_WHITE (7)
#define palette_PICO8_RED (8)
#define palette_PICO8_ORANGE (9)
#define palette_PICO8_YELLOW (10)
#define palette_PICO8_GREEN (11)
#define palette_PICO8_BLUE (12)
#define palette_PICO8_LAVENDER (13)
#define palette_PICO8_PINK (14)
#define palette_PICO8_LIGHT_PEACH (15)

void define_pico8_palette(colour_t palette[16]);

#endif /* PALETTES_H */
61 changes: 10 additions & 51 deletions libraries/framebuf/bmfont/test/bmfont-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,13 @@
#include "base/utils.h"
#include "io/path.h"
#include "framebuf/colour.h"
#include "framebuf/palettes.h"
#include "framebuf/pixelfmt.h"

#include "framebuf/bmfont.h"

/* ----------------------------------------------------------------------- */

/* PICO-8 palette */

#define palette_BLACK (0)
#define palette_DARK_BLUE (1)
#define palette_DARK_PURPLE (2)
#define palette_DARK_GREEN (3)
#define palette_BROWN (4)
#define palette_DARK_GREY (5)
#define palette_LIGHT_GREY (6)
#define palette_WHITE (7)
#define palette_RED (8)
#define palette_ORANGE (9)
#define palette_YELLOW (10)
#define palette_GREEN (11)
#define palette_BLUE (12)
#define palette_LAVENDER (13)
#define palette_PINK (14)
#define palette_LIGHT_PEACH (15)

/* ----------------------------------------------------------------------- */

#ifdef USE_SDL

#include <SDL.h>
Expand Down Expand Up @@ -312,7 +292,7 @@ static result_t bmfont_clipping_test(bmfontteststate_t *state)
int fontwidth, fontheight;
int i;

bitmap_clear(&state->bm, state->palette[palette_DARK_GREEN]);
bitmap_clear(&state->bm, state->palette[palette_PICO8_DARK_GREEN]);

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

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

bg = transparent ? state->transparent : state->palette[palette_GREEN];
bg = transparent ? state->transparent : state->palette[palette_PICO8_GREEN];

if (transparent)
{
rc = bmfont_draw(bmfonts[font].bmfont,
&state->scr,
lorem_ipsum,
nchars,
state->palette[palette_BLACK],
state->palette[palette_PICO8_BLACK],
bg,
&pos,
NULL /*endpos*/);
Expand All @@ -358,7 +338,7 @@ static result_t bmfont_clipping_test(bmfontteststate_t *state)
&state->scr,
lorem_ipsum,
nchars,
state->palette[palette_WHITE],
state->palette[palette_PICO8_WHITE],
bg,
&pos,
NULL /*endpos*/);
Expand Down Expand Up @@ -395,7 +375,7 @@ static result_t bmfont_layout_test(bmfontteststate_t *state)
point_t origin = {0,0};
char leafname[256];

bitmap_clear(&state->bm, state->palette[palette_DARK_GREEN]);
bitmap_clear(&state->bm, state->palette[palette_PICO8_DARK_GREEN]);

bmfont_get_info(bmfont, &glyphwidth, &glyphheight);

Expand Down Expand Up @@ -443,13 +423,13 @@ static result_t bmfont_layout_test(bmfontteststate_t *state)
newline = 1;
}

bg = transparent ? state->transparent : state->palette[palette_GREEN];
bg = transparent ? state->transparent : state->palette[palette_PICO8_GREEN];

rc = bmfont_draw(bmfont,
&state->scr,
string,
friendly_break,
state->palette[palette_WHITE],
state->palette[palette_PICO8_WHITE],
bg,
&origin,
&endpos);
Expand Down Expand Up @@ -579,8 +559,8 @@ static result_t bmfont_interactive_test(bmfontteststate_t *state)
quit = 1;
#endif

colour_t fg = state->palette[palette_DARK_BLUE];
colour_t bg = transparency ? state->transparent : state->palette[palette_LIGHT_PEACH];
colour_t fg = state->palette[palette_PICO8_DARK_BLUE];
colour_t bg = transparency ? state->transparent : state->palette[palette_PICO8_LIGHT_PEACH];

if (!dontclear)
{
Expand Down Expand Up @@ -698,26 +678,6 @@ static result_t bmfont_interactive_test(bmfontteststate_t *state)
return result_TEST_PASSED;
}

static void define_pico8_palette(colour_t palette[16])
{
palette[palette_BLACK ] = colour_rgb(0x00, 0x00, 0x00);
palette[palette_DARK_BLUE ] = colour_rgb(0x1D, 0x2B, 0x53);
palette[palette_DARK_PURPLE] = colour_rgb(0x7E, 0x25, 0x53);
palette[palette_DARK_GREEN ] = colour_rgb(0x00, 0x87, 0x51);
palette[palette_BROWN ] = colour_rgb(0xAB, 0x52, 0x36);
palette[palette_DARK_GREY ] = colour_rgb(0x5F, 0x57, 0x4F);
palette[palette_LIGHT_GREY ] = colour_rgb(0xC2, 0xC3, 0xC7);
palette[palette_WHITE ] = colour_rgb(0xFF, 0xF1, 0xE8);
palette[palette_RED ] = colour_rgb(0xFF, 0x00, 0x4D);
palette[palette_ORANGE ] = colour_rgb(0xFF, 0xA3, 0x00);
palette[palette_YELLOW ] = colour_rgb(0xFF, 0xEC, 0x27);
palette[palette_GREEN ] = colour_rgb(0x00, 0xE4, 0x36);
palette[palette_BLUE ] = colour_rgb(0x29, 0xAD, 0xFF);
palette[palette_LAVENDER ] = colour_rgb(0x83, 0x76, 0x9C);
palette[palette_PINK ] = colour_rgb(0xFF, 0x77, 0xA8);
palette[palette_LIGHT_PEACH] = colour_rgb(0xFF, 0xCC, 0xAA);
}

result_t bmfont_test_one_format(const char *resources,
int scr_width,
int scr_height,
Expand All @@ -728,7 +688,6 @@ result_t bmfont_test_one_format(const char *resources,
state.scr_width = scr_width;
state.scr_height = scr_height;


const int scr_rowbytes = (state.scr_width << pixelfmt_log2bpp(scr_fmt)) / 8;

define_pico8_palette(&state.palette[0]);
Expand Down
52 changes: 6 additions & 46 deletions libraries/framebuf/curve/test/curve-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,13 @@
#include "base/utils.h"
#include "io/path.h"
#include "framebuf/colour.h"
#include "framebuf/palettes.h"
#include "framebuf/pixelfmt.h"

#include "framebuf/curve.h"

/* ----------------------------------------------------------------------- */

/* PICO-8 palette */

#define palette_BLACK (0)
#define palette_DARK_BLUE (1)
#define palette_DARK_PURPLE (2)
#define palette_DARK_GREEN (3)
#define palette_BROWN (4)
#define palette_DARK_GREY (5)
#define palette_LIGHT_GREY (6)
#define palette_WHITE (7)
#define palette_RED (8)
#define palette_ORANGE (9)
#define palette_YELLOW (10)
#define palette_GREEN (11)
#define palette_BLUE (12)
#define palette_LAVENDER (13)
#define palette_PINK (14)
#define palette_LIGHT_PEACH (15)

/* ----------------------------------------------------------------------- */

#ifdef USE_SDL

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

if (state->opt.checker)
colour = state->palette[(i & 1) ? palette_DARK_PURPLE : palette_LIGHT_PEACH];
colour = state->palette[(i & 1) ? palette_PICO8_DARK_PURPLE : palette_PICO8_LIGHT_PEACH];
else
colour = state->palette[palette_BLACK];
colour = state->palette[palette_PICO8_BLACK];

if (state->opt.draw_endpoints)
{
screen_draw_pixel(&state->scr, b.x0, b.y0, state->palette[palette_GREEN]);
screen_draw_pixel(&state->scr, b.x1, b.y1, state->palette[palette_RED]);
screen_draw_pixel(&state->scr, b.x0, b.y0, state->palette[palette_PICO8_GREEN]);
screen_draw_pixel(&state->scr, b.x1, b.y1, state->palette[palette_PICO8_RED]);
}

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

draw_all_control_points(state);
calc_all_curves(state);
rotate_lines(state, mx, state->opt.checker ? palette_DARK_GREEN : palette_BLACK);
rotate_lines(state, mx, state->opt.checker ? palette_PICO8_DARK_GREEN : palette_PICO8_BLACK);

#ifdef USE_SDL
if (!box_is_empty(&state->overalldirty))
Expand Down Expand Up @@ -686,26 +666,6 @@ static result_t curve_interactive_test(curveteststate_t *state)
return result_TEST_PASSED;
}

static void define_pico8_palette(colour_t palette[16])
{
palette[palette_BLACK ] = colour_rgb(0x00, 0x00, 0x00);
palette[palette_DARK_BLUE ] = colour_rgb(0x1D, 0x2B, 0x53);
palette[palette_DARK_PURPLE] = colour_rgb(0x7E, 0x25, 0x53);
palette[palette_DARK_GREEN ] = colour_rgb(0x00, 0x87, 0x51);
palette[palette_BROWN ] = colour_rgb(0xAB, 0x52, 0x36);
palette[palette_DARK_GREY ] = colour_rgb(0x5F, 0x57, 0x4F);
palette[palette_LIGHT_GREY ] = colour_rgb(0xC2, 0xC3, 0xC7);
palette[palette_WHITE ] = colour_rgb(0xFF, 0xF1, 0xE8);
palette[palette_RED ] = colour_rgb(0xFF, 0x00, 0x4D);
palette[palette_ORANGE ] = colour_rgb(0xFF, 0xA3, 0x00);
palette[palette_YELLOW ] = colour_rgb(0xFF, 0xEC, 0x27);
palette[palette_GREEN ] = colour_rgb(0x00, 0xE4, 0x36);
palette[palette_BLUE ] = colour_rgb(0x29, 0xAD, 0xFF);
palette[palette_LAVENDER ] = colour_rgb(0x83, 0x76, 0x9C);
palette[palette_PINK ] = colour_rgb(0xFF, 0x77, 0xA8);
palette[palette_LIGHT_PEACH] = colour_rgb(0xFF, 0xCC, 0xAA);
}

result_t curve_test_one_format(const char *resources,
int scr_width,
int scr_height,
Expand Down
25 changes: 25 additions & 0 deletions libraries/framebuf/palettes/palettes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* palettes.c */

#include "framebuf/colour.h"

#include "framebuf/palettes.h"

void define_pico8_palette(colour_t palette[16])
{
palette[palette_PICO8_BLACK ] = colour_rgb(0x00, 0x00, 0x00);
palette[palette_PICO8_DARK_BLUE ] = colour_rgb(0x1D, 0x2B, 0x53);
palette[palette_PICO8_DARK_PURPLE] = colour_rgb(0x7E, 0x25, 0x53);
palette[palette_PICO8_DARK_GREEN ] = colour_rgb(0x00, 0x87, 0x51);
palette[palette_PICO8_BROWN ] = colour_rgb(0xAB, 0x52, 0x36);
palette[palette_PICO8_DARK_GREY ] = colour_rgb(0x5F, 0x57, 0x4F);
palette[palette_PICO8_LIGHT_GREY ] = colour_rgb(0xC2, 0xC3, 0xC7);
palette[palette_PICO8_WHITE ] = colour_rgb(0xFF, 0xF1, 0xE8);
palette[palette_PICO8_RED ] = colour_rgb(0xFF, 0x00, 0x4D);
palette[palette_PICO8_ORANGE ] = colour_rgb(0xFF, 0xA3, 0x00);
palette[palette_PICO8_YELLOW ] = colour_rgb(0xFF, 0xEC, 0x27);
palette[palette_PICO8_GREEN ] = colour_rgb(0x00, 0xE4, 0x36);
palette[palette_PICO8_BLUE ] = colour_rgb(0x29, 0xAD, 0xFF);
palette[palette_PICO8_LAVENDER ] = colour_rgb(0x83, 0x76, 0x9C);
palette[palette_PICO8_PINK ] = colour_rgb(0xFF, 0x77, 0xA8);
palette[palette_PICO8_LIGHT_PEACH] = colour_rgb(0xFF, 0xCC, 0xAA);
}

0 comments on commit e2caf95

Please sign in to comment.