summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-04-16 07:41:04 +0200
committerkdx <kikoodx@paranoici.org>2023-04-16 07:41:04 +0200
commite8c38b4786c9103ed18f4508a0294cb7a555f1da (patch)
treeba8505150fde7f0a6f2ee3e3ccb84cdcbde00e21
parent049269bf70e7143e1932f2f115192dbbc27477f4 (diff)
downloadpx-e8c38b4786c9103ed18f4508a0294cb7a555f1da.tar.gz
clip region
-rw-r--r--src/main.c3
-rw-r--r--src/px.c48
-rw-r--r--src/px.h15
3 files changed, 62 insertions, 4 deletions
diff --git a/src/main.c b/src/main.c
index b542d80..29f8a56 100644
--- a/src/main.c
+++ b/src/main.c
@@ -14,7 +14,10 @@ main(void)
while (!TZR_ShouldQuit()) {
TZR_CycleEvents();
+ pxClipReset();
pxCls(1);
+ pxClip(25, 12, 32, 69);
+ pxCls(2);
TZR_DrawBegin();
pxRender();
diff --git a/src/px.c b/src/px.c
index f7a89cd..769cd33 100644
--- a/src/px.c
+++ b/src/px.c
@@ -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));
}
diff --git a/src/px.h b/src/px.h
index 4429182..cd2df57 100644
--- a/src/px.h
+++ b/src/px.h
@@ -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);