summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-05-05 19:37:28 +0200
committerkdx <kikoodx@paranoici.org>2023-05-05 19:37:28 +0200
commit760da5a3efd2e6ff166164a97a3b2cf3b6db6787 (patch)
tree481fdc95f4239a68eea48db61ae2f23d96bd52ae
parent398c0bf3fe66da5c050c01580612c20bce03038a (diff)
downloadpx-760da5a3efd2e6ff166164a97a3b2cf3b6db6787.tar.gz
z buffer
-rw-r--r--src/main.c6
-rw-r--r--src/px.c28
-rw-r--r--src/px.h5
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 <assert.h>
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
@@ -105,14 +111,24 @@ pxClipReset(void)
}
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);