Start utilities for drawing

This commit is contained in:
Nils Faerber 2011-07-30 18:21:10 +02:00
parent 8f684d091f
commit ae55d0fbca
3 changed files with 128 additions and 1 deletions

View file

@ -6,7 +6,7 @@ CFLAGS = -Wall -O2 $(CCFLAGS)
PRGNAME = metawatch PRGNAME = metawatch
MEMBERS = metawatch crc16ccitt mw_main MEMBERS = metawatch crc16ccitt mw_utility mw_main
# no need to change anything below this line # no need to change anything below this line
# ------------------------------------------ # ------------------------------------------

90
mw_utility.c Normal file
View file

@ -0,0 +1,90 @@
/*
* (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
*
* license LGPL
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#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;
}

37
mw_utility.h Normal file
View file

@ -0,0 +1,37 @@
/*
* (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
*
* 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