From ae55d0fbca888317d6c3fa276bd80a9d63d7999f Mon Sep 17 00:00:00 2001 From: Nils Faerber Date: Sat, 30 Jul 2011 18:21:10 +0200 Subject: [PATCH] Start utilities for drawing --- Makefile | 2 +- mw_utility.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mw_utility.h | 37 +++++++++++++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 mw_utility.c create mode 100644 mw_utility.h diff --git a/Makefile b/Makefile index bfbb40a..5c36c1c 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ CFLAGS = -Wall -O2 $(CCFLAGS) PRGNAME = metawatch -MEMBERS = metawatch crc16ccitt mw_main +MEMBERS = metawatch crc16ccitt mw_utility mw_main # no need to change anything below this line # ------------------------------------------ diff --git a/mw_utility.c b/mw_utility.c new file mode 100644 index 0000000..119207b --- /dev/null +++ b/mw_utility.c @@ -0,0 +1,90 @@ +/* + * (c) 2011 Siegen, Germany by Nils Faerber + * + * license LGPL + */ + +#include +#include +#include + +#include "mw_utility.h" +#include "metawatch.h" + +/* + * The pixmap buffer has at least one byte per pixel, even for monochrome (bpp=1) + * bitmaps + */ +mw_buffer *mw_alloc_pbuffer(unsigned int res_x, unsigned int res_y, unsigned int bpp) +{ + mw_buffer *nmwbuf; + int pbuf_size; + + nmwbuf = (mw_buffer *) malloc(sizeof(mw_buffer)); + if (!nmwbuf) + return NULL; + + nmwbuf->res_x = res_x; + nmwbuf->res_y = res_y; + nmwbuf->bpp = bpp; + + pbuf_size = nmwbuf->res_x * nmwbuf->res_y * ((nmwbuf->bpp / 8) + 1); + nmwbuf->pbuf = malloc(pbuf_size); + if (!nmwbuf->pbuf) { + free(nmwbuf); + return NULL; + } else { + memset(nmwbuf->pbuf, 0, pbuf_size); + return nmwbuf; + } +} + +void mw_free_pbuffer(mw_buffer *mwbuf) +{ + if (!mwbuf) + return; + + free(mwbuf->pbuf); + free(mwbuf); +} + +void mw_dump_mw_buffer(mw_buffer *mwbuf) +{ + int x, y; + unsigned char clr; + + for (y = 0; y < mwbuf->res_y; y++) { + for (x = 0; x < mwbuf->res_x; x++) { + clr = *(unsigned char *)(mwbuf->pbuf+((y*mwbuf->res_x)+x)); + if (clr) + fprintf(stderr, "."); + else + fprintf(stderr, " "); + }; + fprintf(stderr, "\n"); + }; +} + + +/* clear/fill entire buffer with color */ +void mw_buf_clear(mw_buffer *mwbuf, mw_color clr) +{ + int pbuf_size; + + if (!mwbuf) + return; + + pbuf_size = mwbuf->res_x * mwbuf->res_y * ((mwbuf->bpp / 8) + 1); + memset(mwbuf->pbuf, clr, pbuf_size); +} + +/* draw a single pixel */ +void mw_buf_draw_pixel(mw_buffer *mwbuf, unsigned int x, unsigned int y, mw_color clr) +{ + if (!mwbuf) + return; + + *(unsigned char *)(mwbuf->pbuf+((y*mwbuf->res_x)+x)) = clr; +} + + diff --git a/mw_utility.h b/mw_utility.h new file mode 100644 index 0000000..6537360 --- /dev/null +++ b/mw_utility.h @@ -0,0 +1,37 @@ +/* + * (c) 2011 Siegen, Germany by Nils Faerber + * + * license LGPL + */ + +#ifndef _MW_UTILITY_H +#define _MW_UTILITY_H + +typedef struct { + unsigned int res_x; + unsigned int res_y; + unsigned char bpp; + void *pbuf; +} mw_buffer; + +typedef enum { + MW_BLACK = 0, + MW_WHITE, +} mw_color; + +mw_buffer *mw_alloc_pbuffer(unsigned int res_x, unsigned int res_y, unsigned int bpp); + +void mw_free_pbuffer(mw_buffer *mwbuf); + +void mw_dump_mw_buffer(mw_buffer *mwbuf); + + +/* clear/fill entire buffer with color */ +void mw_buf_clear(mw_buffer *mwbuf, mw_color clr); + +/* draw a single pixel */ +void mw_buf_draw_pixel(mw_buffer *mwbuf, unsigned int x, unsigned int y, mw_color clr); + + +#endif + -- 2.39.2