summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-04-16 12:38:33 +0200
committerkdx <kikoodx@paranoici.org>2023-04-16 12:38:33 +0200
commitdbe29daa8287a0adf67edc77c42efb6505b2399a (patch)
treea656b8fc03f4f3d42e1f1a15ceef1e9ea4fb8f0a
parent1d8bfedd168742bbef782b8ab453f036d9853006 (diff)
downloadpx-dbe29daa8287a0adf67edc77c42efb6505b2399a.tar.gz
draw sprite
-rw-r--r--src/main.c22
-rw-r--r--src/px.c24
-rw-r--r--src/px.h22
3 files changed, 67 insertions, 1 deletions
diff --git a/src/main.c b/src/main.c
index db6a715..f44e527 100644
--- a/src/main.c
+++ b/src/main.c
@@ -11,8 +11,27 @@ main(void)
return 1;
}
+ PxSpr spr = {
+ 8, 8,
+ (uint8_t[]){
+ 2, 0, 0, 0, 0, 0, 3, 0,
+ 0, 2, 0, 0, 0, 3, 0, 3,
+ 0, 0, 2, 2, 2, 0, 4, 0,
+ 0, 0, 0, 0, 0, 5, 0, 0,
+ 0, 0, 0, 0, 7, 0, 0, 0,
+ 0, 0, 0, 6, 0, 0, 0, 0,
+ 0, 0, 0, 6, 0, 0, 0, 0,
+ 7, 0, 0, 0, 0, 0, 0, 7
+ }
+ };
+
+ int x = 8, y = 8;
while (!TZR_ShouldQuit()) {
TZR_CycleEvents();
+ x -= TZR_IsKeyDown(SDL_SCANCODE_A);
+ x += TZR_IsKeyDown(SDL_SCANCODE_D);
+ y -= TZR_IsKeyDown(SDL_SCANCODE_W);
+ y += TZR_IsKeyDown(SDL_SCANCODE_S);
pxClipReset();
pxCls(1);
@@ -21,6 +40,9 @@ main(void)
pxPset(0, 1, pxPget(0, 0));
pxClip(25, 12, 32, 69);
pxCls(2);
+ pxClipReset();
+ pxSpr(&spr, x, y);
+ pxSpr(&spr, 24, 8, .iy=1, .h=2);
TZR_DrawBegin();
pxRender();
diff --git a/src/px.c b/src/px.c
index 3ac1ab7..7eed959 100644
--- a/src/px.c
+++ b/src/px.c
@@ -81,6 +81,30 @@ pxPget(int x, int y)
return pxbuf[x + y * PX_WIDTH];
}
+void
+_pxSpr(const PxSprArgs *args)
+{
+ int w = (args->w < 0) ? (args->spr->w) : (args->w);
+ w = pxMin(w, args->spr->w - args->ix);
+
+ int h = (args->h < 0) ? (args->spr->h) : (args->h);
+ h = pxMin(h, args->spr->h - args->iy);
+
+ printf("%d %d\n", w, h);
+
+ for (int y = 0; y < h; y++) {
+ for (int x = 0; x < w; x++) {
+ const int dx = x + args->x;
+ const int dy = y + args->y;
+ const int ix = x + args->ix;
+ const int iy = y + args->iy;
+ const PxCol c = args->spr->data[ix + iy * args->spr->w];
+ if (c)
+ pxPset(dx, dy, c);
+ }
+ }
+}
+
int
pxMin(int a, int b)
{
diff --git a/src/px.h b/src/px.h
index 871ff54..47ea705 100644
--- a/src/px.h
+++ b/src/px.h
@@ -12,13 +12,28 @@ typedef struct {
uint8_t r, g, b;
} PxPal;
+typedef struct {
+ unsigned int w, h;
+ PxCol *data;
+} PxSpr;
+
+typedef struct {
+ int _;
+ PxSpr *spr;
+ int x;
+ int y;
+ int ix;
+ int iy;
+ int w;
+ int h;
+} PxSprArgs;
+
extern PxCol pxbuf[PX_WIDTH * PX_HEIGHT];
extern PxPal pxpal[256];
void pxRender(void);
/*** DRAW ***/
-
/* Fill the clipping region with color 'c'. */
void pxCls(PxCol c);
@@ -34,6 +49,11 @@ void pxPset(int x, int y, PxCol c);
/* Get a pixel. */
PxCol pxPget(int x, int y);
+/* Draw sprite. */
+#define pxSpr(...) _pxSpr(&(const PxSprArgs){ \
+ .ix=0, .iy=0, .w=-1, .h=-1, ._=0, __VA_ARGS__})
+void _pxSpr(const PxSprArgs *args);
+
/*** MATH ***/
int pxMin(int a, int b);
int pxMax(int a, int b);