From e49504d57745c2c5b1c60369f7730252e41ce90c Mon Sep 17 00:00:00 2001 From: Adam Greenwood-Byrne Date: Tue, 9 Mar 2021 22:37:23 +0000 Subject: [PATCH] Added some block functions - more work to do here --- part12-wgt/wgt.c | 9 ++++++ part12-wgt/wgt.h | 6 ++++ part12-wgt/wmiscb.c | 20 ++++++++++++++ part12-wgt/wnpblock.c | 64 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 part12-wgt/wmiscb.c create mode 100644 part12-wgt/wnpblock.c diff --git a/part12-wgt/wgt.c b/part12-wgt/wgt.c index f22c20b..a9c13e4 100644 --- a/part12-wgt/wgt.c +++ b/part12-wgt/wgt.c @@ -16,6 +16,15 @@ void *memset(void *dest, int val, unsigned len) return dest; } +void *memcpy(void *dest, const void *src, unsigned len) +{ + char *d = dest; + const char *s = src; + while (len--) + *d++ = *s++; + return dest; +} + int abs(int i) { return i < 0 ? -i : i; diff --git a/part12-wgt/wgt.h b/part12-wgt/wgt.h index 6217f30..1b55e4f 100644 --- a/part12-wgt/wgt.h +++ b/part12-wgt/wgt.h @@ -47,6 +47,7 @@ extern short bx,by,tx,ty; // ######## HELPER FUNCTIONS ######## void *memset(void *dest, int val, unsigned len); +void *memcpy(void *dest, const void *src, unsigned len); int abs(int i); int strlen(const char *str); @@ -80,3 +81,8 @@ void wtextbackground (unsigned int col); void wtexttransparent (short transparent); void wtextgrid (short onoff); void wregionfill (short x, short y); +void wfreeblock (block ptr); +short wgetblockwidth (block ptr); +short wgetblockheight (block ptr); +block wnewblock (short x, short y, short x2, short y2); +block wallocblock (short width, short height); diff --git a/part12-wgt/wmiscb.c b/part12-wgt/wmiscb.c new file mode 100644 index 0000000..d27ef8b --- /dev/null +++ b/part12-wgt/wmiscb.c @@ -0,0 +1,20 @@ +#include "wgt.h" +#include "include/mem.h" + +extern short bx,by,tx,ty; + +void wfreeblock (block ptr) +{ + free (ptr); +} + +short wgetblockwidth (block ptr) +{ + return *(short *)ptr; /* Width is first 2 bytes of data */ +} + +short wgetblockheight (block ptr) +{ + ptr += 2; /* Skip width */ + return *(short *)ptr; /* Height is second 2 bytes of data */ +} diff --git a/part12-wgt/wnpblock.c b/part12-wgt/wnpblock.c new file mode 100644 index 0000000..be3cfcc --- /dev/null +++ b/part12-wgt/wnpblock.c @@ -0,0 +1,64 @@ +#include "wgt.h" +#include "include/mem.h" + +block wnewblock (short x, short y, short x2, short y2) +{ + block ptr,orig; + int temp,width,height; + int dispofs; + int size; + int ctr; + + width = abs (x - x2) + 1; + height = abs (y - y2) + 1; + size = (int)width * (int)height + 5; + if (x2 < x) + { + temp = x; x = x2; x2 = temp; + } + if (y2 < y) + { + temp = y; y = y2; y2 = temp; + } + ptr = malloc (size); + if (ptr == NULL) + return NULL; + orig = ptr; + *(short *)ptr = width; /* store the width */ + ptr += 2; /* and height */ + *(short *)ptr = height; + ptr += 2; + + dispofs = y * WGT_SYS.xres + x; + temp = width; + if (temp > WGT_SYS.xres) + temp = WGT_SYS.xres; + for (ctr = y; ctr <= y2; ctr++) + { + memcpy (ptr, &abuf[dispofs], temp); + /* read off the screen */ + ptr += width; + dispofs += WGT_SYS.xres; + /* into the new ptr */ + } + return orig; +} + +block wallocblock (short width, short height) +{ + block ptr,orig; + int size; + + size = (int)width * (int)height + 5; + ptr = malloc (size); + if (ptr == NULL) + return NULL; + orig = ptr; + *(short *)ptr = width; /* store the width */ + ptr += 2; + *(short *)ptr = height; /* and height */ + ptr += 2; + + memset (ptr, 0, size - 4); + return orig; +}