diff options
author | kdx <kikoodx@paranoici.org> | 2023-04-16 07:41:04 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-04-16 07:41:04 +0200 |
commit | e8c38b4786c9103ed18f4508a0294cb7a555f1da (patch) | |
tree | ba8505150fde7f0a6f2ee3e3ccb84cdcbde00e21 | |
parent | 049269bf70e7143e1932f2f115192dbbc27477f4 (diff) | |
download | px-e8c38b4786c9103ed18f4508a0294cb7a555f1da.tar.gz |
clip region
-rw-r--r-- | src/main.c | 3 | ||||
-rw-r--r-- | src/px.c | 48 | ||||
-rw-r--r-- | src/px.h | 15 |
3 files changed, 62 insertions, 4 deletions
@@ -14,7 +14,10 @@ main(void) while (!TZR_ShouldQuit()) { TZR_CycleEvents(); + pxClipReset(); pxCls(1); + pxClip(25, 12, 32, 69); + pxCls(2); TZR_DrawBegin(); pxRender(); @@ -21,12 +21,18 @@ PxPal pxpal[256] = { {0xff, 0x77, 0xa8}, {0xff, 0xcc, 0xaa}, }; +static struct { + int x; + int y; + int w; + int h; +} pxclip = {0, 0, PX_WIDTH, PX_HEIGHT}; void pxRender(void) { - for (int y = 0; y < PX_HEIGHT; y += 1) { - for (int x = 0; x < PX_WIDTH; x += 1) { + for (int y = 0; y < PX_HEIGHT; y++) { + for (int x = 0; x < PX_WIDTH; x++) { const PxCol col = pxbuf[x + y * PX_WIDTH]; TZR_DrawSetColor8(pxpal[col].r, pxpal[col].g, pxpal[col].b, 255); @@ -38,6 +44,40 @@ pxRender(void) void pxCls(PxCol c) { - for (unsigned i = 0; i < sizeof(pxbuf); i++) - pxbuf[i] = c; + for (int y = pxclip.y; y < pxclip.y + pxclip.h - 1; y += 1) + for (int x = pxclip.x; x < pxclip.x + pxclip.w - 1; x += 1) + pxbuf[x + y * PX_WIDTH] = c; +} + +void +pxClip(int x, int y, int w, int h) +{ + pxclip.x = pxClamp(0, PX_WIDTH - 1, x); + pxclip.y = pxClamp(0, PX_HEIGHT - 1, y); + pxclip.w = pxClamp(0, PX_WIDTH - pxclip.x - 1, w); + pxclip.h = pxClamp(0, PX_HEIGHT - pxclip.y - 1, h); +} + +void +pxClipReset(void) +{ + pxClip(0, 0, PX_WIDTH, PX_HEIGHT); +} + +int +pxMin(int a, int b) +{ + return (a < b) ? a : b; +} + +int +pxMax(int a, int b) +{ + return (a > b) ? a : b; +} + +int +pxClamp(int min, int max, int v) +{ + return pxMax(min, pxMin(max, v)); } @@ -16,4 +16,19 @@ 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); + +/* Set the clipping region. */ +void pxClip(int x, int y, int w, int h); + +/* Reset the clipping region. */ +void pxClipReset(void); + +/*** MATH ***/ +int pxMin(int a, int b); +int pxMax(int a, int b); +int pxClamp(int min, int max, int v); |