diff options
author | kdx <kikoodx@paranoici.org> | 2023-04-17 19:09:31 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-04-17 19:09:31 +0200 |
commit | 4f98ee228648cc122cd8b883f8480f967ca92c8c (patch) | |
tree | 777d91cf9e542698e289511ae942052583459276 | |
parent | 8164d9623eae77040a1fbafa41acc891f0d33aa1 (diff) | |
download | px-4f98ee228648cc122cd8b883f8480f967ca92c8c.tar.gz |
pal spal and shit
-rw-r--r-- | src/main.c | 14 | ||||
-rw-r--r-- | src/px.c | 68 | ||||
-rw-r--r-- | src/px.h | 18 |
3 files changed, 81 insertions, 19 deletions
@@ -33,21 +33,31 @@ main(void) y -= TZR_IsKeyDown(SDL_SCANCODE_W); y += TZR_IsKeyDown(SDL_SCANCODE_S); + pxPal(); + pxSpal(); + pxPalt(); pxCls(1); pxRectfill(112, 112, x, y, 10); pxRect(16, 16, x, y, 11); - pxPalt(); pxSpr(&spr, x, y); - pxPalt(0); + pxPal(3, 11); + pxPalt(3, true); pxSpr(&spr, x, y + 8); pxPset(0, 0, 10); pxPset(1, 0, 10); pxPset(0, 1, pxPget(0, 0)); + if (TZR_GetTick() / 60 % 2) + pxSpal(1, 0); + else { + pxSpal(10, 11); + pxSpal(11, 10); + } + TZR_DrawBegin(); pxFlip(); TZR_DrawEnd(); @@ -1,25 +1,26 @@ #include "px.h" #include "TZR.h" +#include <assert.h> PxCol pxbuf[PX_WIDTH * PX_HEIGHT] = {0}; PxPal pxpal[256] = { /* PICO-8 palette */ - {0x00, 0x00, 0x00, false}, - {0x1d, 0x2b, 0x53, false}, - {0x7e, 0x25, 0x53, false}, - {0x00, 0x87, 0x51, false}, - {0xab, 0x52, 0x36, false}, - {0x5f, 0x57, 0x4f, false}, - {0xc2, 0xc3, 0xc7, false}, - {0xff, 0xf1, 0xe8, false}, - {0xff, 0x00, 0x4d, false}, - {0xff, 0xa3, 0x00, false}, - {0xff, 0xec, 0x27, false}, - {0x00, 0xe4, 0x36, false}, - {0x29, 0xad, 0xff, false}, - {0x83, 0x76, 0x9c, false}, - {0xff, 0x77, 0xa8, false}, - {0xff, 0xcc, 0xaa, false}, + {0x00, 0x00, 0x00, false, -1, -1}, + {0x1d, 0x2b, 0x53, false, -1, -1}, + {0x7e, 0x25, 0x53, false, -1, -1}, + {0x00, 0x87, 0x51, false, -1, -1}, + {0xab, 0x52, 0x36, false, -1, -1}, + {0x5f, 0x57, 0x4f, false, -1, -1}, + {0xc2, 0xc3, 0xc7, false, -1, -1}, + {0xff, 0xf1, 0xe8, false, -1, -1}, + {0xff, 0x00, 0x4d, false, -1, -1}, + {0xff, 0xa3, 0x00, false, -1, -1}, + {0xff, 0xec, 0x27, false, -1, -1}, + {0x00, 0xe4, 0x36, false, -1, -1}, + {0x29, 0xad, 0xff, false, -1, -1}, + {0x83, 0x76, 0x9c, false, -1, -1}, + {0xff, 0x77, 0xa8, false, -1, -1}, + {0xff, 0xcc, 0xaa, false, -1, -1}, }; static struct { int x; @@ -33,7 +34,9 @@ pxFlip(void) { for (int y = 0; y < PX_HEIGHT; y++) { for (int x = 0; x < PX_WIDTH; x++) { - const PxCol col = pxbuf[x + y * PX_WIDTH]; + PxCol col = pxbuf[x + y * PX_WIDTH]; + if (pxpal[col].spal > -1) + col = pxpal[col].spal; TZR_DrawSetColor8(pxpal[col].r, pxpal[col].g, pxpal[col].b, 255); TZR_DrawPoint(x, y); @@ -70,6 +73,8 @@ pxPset(int x, int y, PxCol c) if (x < pxclip.x || x >= pxclip.x + pxclip.w || y < pxclip.y || y >= pxclip.y + pxclip.h) return; + if (pxpal[c].pal > -1) + c = pxpal[c].pal; pxbuf[x + y * PX_WIDTH] = c; } @@ -145,6 +150,35 @@ _pxSpr(const PxSprArgs *args) } void +_pxPal(const PxPalArgs *args) +{ + assert((args->c0 == -1) ^ (args->c1 != -1)); + if (args->c0 == -1) + for (int i = 0; i < 256; i++) + pxpal[i].pal = -1; + else + pxpal[args->c0].pal = args->c1; +} + +void +_pxSpal(const PxSpalArgs *args) +{ + assert((args->c0 == -1) ^ (args->c1 != -1)); + if (args->c0 == -1) + for (int i = 0; i < 256; i++) + pxpal[i].spal = -1; + else + pxpal[args->c0].spal = args->c1; +} + +void +pxSpalReset(void) +{ + for (int i = 0; i < 256; i++) + pxpal[i].spal = -1; +} + +void _pxPalt(const PxPaltArgs *args) { if (args->col < 0 || args->col > 255) @@ -12,6 +12,7 @@ typedef uint8_t PxCol; typedef struct { uint8_t r, g, b; bool t; /* tranparency */ + int pal, spal; } PxPal; typedef struct { @@ -32,6 +33,13 @@ typedef struct { typedef struct { int _; + int c0; + int c1; +} PxPalArgs; +typedef PxPalArgs PxSpalArgs; + +typedef struct { + int _; int col; bool t; } PxPaltArgs; @@ -68,6 +76,16 @@ void pxRectfill(int x0, int y0, int x1, int y1, PxCol col); .ix=0, .iy=0, .w=-1, .h=-1, ._=0, __VA_ARGS__}) void _pxSpr(const PxSprArgs *args); +/* Bind palette index to new color. */ +#define pxPal(...) _pxPal(&(const PxPalArgs){ \ + .c0=-1, .c1=-1, ._=0, __VA_ARGS__}) +void _pxPal(const PxPalArgs *args); + +/* Set screen palette. */ +#define pxSpal(...) _pxSpal(&(const PxSpalArgs){ \ + .c0=-1, .c1=-1, ._=0, __VA_ARGS__}) +void _pxSpal(const PxSpalArgs *args); + /* Set palette tranparency. */ #define pxPalt(...) _pxPalt(&(const PxPaltArgs){ \ .col=-1, .t=true, ._=0, __VA_ARGS__}) |