From a65e725d0df94f41fb3030b9d61aa625e6cd5df8 Mon Sep 17 00:00:00 2001 From: kdx Date: Fri, 1 Sep 2023 23:40:43 +0200 Subject: fragment shader --- src/main.c | 2 +- src/px.c | 22 ++++++++++++++++++---- src/px.h | 8 +++++++- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/main.c b/src/main.c index 514db1a..e7a1fc9 100644 --- a/src/main.c +++ b/src/main.c @@ -69,7 +69,7 @@ main(void) pxPset(0, 1, pxPget(0, 0)); TZR_DrawBegin(); - pxFlip(); + pxFlip(NULL); TZR_DrawEnd(); } diff --git a/src/px.c b/src/px.c index d7c8650..56c5706 100644 --- a/src/px.c +++ b/src/px.c @@ -57,7 +57,7 @@ pxDeinit(void) } int -pxFlip(void) +pxFlip(PxRawCol (*fs)(PxRawCol c, int x, int y)) { uint8_t *pixels = NULL; int pitch = 0; @@ -72,9 +72,12 @@ pxFlip(void) PxCol col = pxbuf[x + y * PX_WIDTH]; if (pxpal[col].spal) col = pxpal[col].spal - 1; - pixels[ix + y * pitch + 0] = pxpal[col].b; - pixels[ix + y * pitch + 1] = pxpal[col].g; - pixels[ix + y * pitch + 2] = pxpal[col].r; + PxRawCol raw = pxRawCol(col); + if (fs != NULL) + raw = fs(raw, x, y); + pixels[ix + y * pitch + 0] = raw.a[2]; + pixels[ix + y * pitch + 1] = raw.a[1]; + pixels[ix + y * pitch + 2] = raw.a[0]; pixels[ix + y * pitch + 3] = 255; } } @@ -284,9 +287,20 @@ pxCol(uint8_t r, uint8_t g, uint8_t b) if (pal->r == r && pal->g == g && pal->b == b) return i; } + assert(0); return -1; } +PxRawCol +pxRawCol(PxCol c) +{ + return (PxRawCol){{ + pxpal[c].r, + pxpal[c].g, + pxpal[c].b + }}; +} + int pxMin(int a, int b) { diff --git a/src/px.h b/src/px.h index 3d0db03..2d8f39b 100644 --- a/src/px.h +++ b/src/px.h @@ -12,6 +12,10 @@ typedef uint8_t PxCol; +typedef struct { + uint8_t a[3]; +} PxRawCol; + typedef struct { uint8_t r, g, b; bool t; /* tranparency */ @@ -62,7 +66,7 @@ void pxDeinit(void); /* Return -1 on error. * Should be called between TZR_DrawBegin and TZR_DrawEnd. */ -int pxFlip(void); +int pxFlip(PxRawCol (*fs)(PxRawCol c, int x, int y)); /*** DRAW ***/ /* Fill and reset Z on the clipping region with color 'c'. */ @@ -116,6 +120,8 @@ void _pxPalt(const PxPaltArgs *args); /* Try to find a color in palette, return 255 on error. */ PxCol pxCol(uint8_t r, uint8_t g, uint8_t b); +PxRawCol pxRawCol(PxCol c); + /*** MATH ***/ int pxMin(int a, int b); int pxMax(int a, int b); -- cgit v1.2.3