From 760da5a3efd2e6ff166164a97a3b2cf3b6db6787 Mon Sep 17 00:00:00 2001 From: kdx Date: Fri, 5 May 2023 19:37:28 +0200 Subject: z buffer --- src/main.c | 6 ++++-- src/px.c | 28 ++++++++++++++++++++++------ src/px.h | 5 ++++- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/main.c b/src/main.c index 43c5d09..c3110a4 100644 --- a/src/main.c +++ b/src/main.c @@ -51,11 +51,13 @@ main(void) if (pxbuf[i]) pxbuf[i] -= 1; + pxZ(1); + pxSpr(&spr, x, y); + pxZ(0); + pxRectfill(112, 112, x, y, 10); pxRect(16, 16, x, y, 15); - pxSpr(&spr, x, y); - pxPal(3, 11); pxPalt(3, true); pxSpr(&spr, x, y + 8, .flip_x=true, .flip_y=true); diff --git a/src/px.c b/src/px.c index 694c789..267b2e0 100644 --- a/src/px.c +++ b/src/px.c @@ -4,6 +4,7 @@ #include PxCol pxbuf[PX_WIDTH * PX_HEIGHT] = {0}; +unsigned int pxzbuf[PX_WIDTH * PX_HEIGHT] = {0}; PxPal pxpal[256] = { /* PICO-8 palette */ {0x00, 0x00, 0x00, false, 0, 0}, @@ -30,6 +31,7 @@ static struct { int h; } pxclip = {0, 0, PX_WIDTH, PX_HEIGHT}; static SDL_Texture *pxtexture = NULL; +static unsigned int pxz = 0; int pxInit(void) @@ -79,14 +81,18 @@ pxFlip(void) SDL_UnlockTexture(pxtexture); SDL_RenderCopy(___tzr_renderer, pxtexture, NULL, NULL); + + return 0; } void pxCls(PxCol c) { - for (int y = 0; y < PX_HEIGHT; y++) - for (int x = 0; x < PX_WIDTH; x++) - pxPset(x, y, c); + for (int y = pxclip.y; y < pxclip.y + pxclip.h; y++) + for (int x = pxclip.x; x < pxclip.x + pxclip.w; x++) { + pxbuf[x + y * PX_WIDTH] = c; + pxzbuf[x + y * PX_WIDTH] = 0; + } } void @@ -104,15 +110,25 @@ pxClipReset(void) pxClip(0, 0, PX_WIDTH, PX_HEIGHT); } +void +pxZ(unsigned int z) +{ + pxz = z; +} + void 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) - c = pxpal[c].pal - 1; - pxbuf[x + y * PX_WIDTH] = c; + + /* z sorting */ + if (pxz < pxzbuf[x + y * PX_WIDTH]) + return; + pxzbuf[x + y * PX_WIDTH] = pxz; + + pxbuf[x + y * PX_WIDTH] = pxpal[c].pal ? pxpal[c].pal : c; } uint8_t diff --git a/src/px.h b/src/px.h index cc59c7f..713f245 100644 --- a/src/px.h +++ b/src/px.h @@ -64,7 +64,7 @@ void pxDeinit(void); int pxFlip(void); /*** DRAW ***/ -/* Fill the clipping region with color 'c'. */ +/* Fill and reset Z on the clipping region with color 'c'. */ void pxCls(PxCol c); /* Set the clipping region. */ @@ -73,6 +73,9 @@ void pxClip(int x, int y, int w, int h); /* Reset the clipping region. */ void pxClipReset(void); +/* Set Z layer. */ +void pxZ(unsigned int z); + /* Put a pixel. */ void pxPset(int x, int y, PxCol c); -- cgit v1.2.3