From 7b1c8dd5bbdfd62f32156464755ef7bd4bb20b15 Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Fri, 6 Dec 2019 23:26:13 -0700 Subject: [PATCH] implemented frame buffer primitives --- src/Makefile | 2 +- src/fbprimitive.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++ src/fbprimitive.h | 20 ++++++++ src/main.c | 19 +++----- src/wintype.h | 1 + 5 files changed, 142 insertions(+), 13 deletions(-) create mode 100644 src/fbprimitive.c create mode 100644 src/fbprimitive.h diff --git a/src/Makefile b/src/Makefile index 26bfb95..c1558d4 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,4 +1,4 @@ -OBJS=main.o sysinput.o fbinit.o log.o gpio.o msg_queue.o time_func.o config.o splash.o +OBJS=main.o sysinput.o fbinit.o fbprimitive.o log.o gpio.o msg_queue.o time_func.o config.o splash.o LIBS=-lbcm2835 -lpthread upiwin: $(OBJS) diff --git a/src/fbprimitive.c b/src/fbprimitive.c new file mode 100644 index 0000000..2292f48 --- /dev/null +++ b/src/fbprimitive.c @@ -0,0 +1,113 @@ +#include "wintype.h" +#include "fbinit.h" + +inline static PUINT16 loc_from_coords(INT32 x, INT32 y) +{ + return Fb_Ptr + (y * Fb_Info->width) + x; +} + +UINT16 Fb_setpixel(INT32 x, INT32 y, UINT16 color, BOOL xor) +{ + UINT16 rc; + PUINT16 loc = loc_from_coords(x, y); + + rc = *loc; + if (xor) + *loc ^= color; + else + *loc = color; + return rc; +} + +void Fb_line(INT32 x1, INT32 y1, INT32 x2, INT32 y2, UINT16 color, BOOL xor) +{ + INT32 tmp; + INT32 dx = x2 - x1; + INT32 dy = y2 - y1; + + if (ABS(dx) < ABS(dy)) + { + if (y1 > y2) + { + tmp = x1; + x1 = x2; + x2 = tmp; + tmp = y1; + y1 = y2; + y2 = tmp; + dx = -dx; + dy = -dy; + } + x1 <<= 16; + dx = (dx << 16) / dy; + while (y1 <= y2) + { + Fb_setpixel(x1 >> 16, y1, color, xor); + x1 += dx; + y1++; + } + } + else + { + if (x1 > x2) + { + tmp = x1; + x1 = x2; + x2 = tmp; + tmp = y1; + y1 = y2; + y2 = tmp; + dx = -dx; + dy = -dy; + } + y1 <<= 16; + dy = dx ? (dy << 16) / dx : 0; + while (x1 <= x2) + { + Fb_setpixel(x1, y1 >> 16, color, xor); + y1 += dy; + x1++; + } + } +} + +void Fb_rectangle(INT32 x1, INT32 y1, INT32 x2, INT32 y2, UINT16 color, BOOL xor) +{ + FB_line(x1, y1, x2, y1, color, xor); + FB_line(x2, y1 + 1, x2, y2 - 1, color, xor); + FB_line(x1, y2, x2, y2, color, xor); + FB_line(x1, y1 + 1, x1, y2 - 1, color, xor); +} + +void Fb_filled_rectangle(INT32 x1, INT32 y1, INT32 x2, INT32 y2, UINT16 color, BOOL xor) +{ + INT32 tmp; + PUINT16 ps, p; + + if (x1 > x2) + { + tmp = x1; + x1 = x2; + x2 = tmp; + } + if (y1 > y2) + { + tmp = y1; + y1 = y2; + y2 = tmp; + } + + ps = loc_from_coords(x1, y1); + for (; y1 <= y2; y1++) + { + p = ps; + for (tmp = x1; tmp <= x2; tmp++) + { + if (xor) + *p++ ^= color; + else + *p++ = color; + } + ps += Fb_Info->width; + } +} diff --git a/src/fbprimitive.h b/src/fbprimitive.h new file mode 100644 index 0000000..fddd6c8 --- /dev/null +++ b/src/fbprimitive.h @@ -0,0 +1,20 @@ +#ifndef __FBPRIMITIVE_H_INCLUDED +#define __FBPRIMITIVE_H_INCLUDED + +#include "wintype.h" + +#define FBPRIMCLR_BLACK 0x0000 +#define FBPRIMCLR_RED 0xF800 +#define FBPRIMCLR_GREEN 0x07E0 +#define FBPRIMCLR_BLUE 0x001F +#define FBPRIMCLR_YELLOW 0xFFE0 +#define FBPRIMCLR_CYAN 0x07FF +#define FBPRIMCLR_MAGENTA 0xF81F +#define FBPRIMCLR_WHITE 0xFFFF + +extern UINT16 Fb_setpixel(INT32 x, INT32 y, UINT16 color, BOOL xor); +extern void Fb_line(INT32 x1, INT32 y1, INT32 x2, INT32 y2, UINT16 color, BOOL xor); +extern void Fb_rectangle(INT32 x1, INT32 y1, INT32 x2, INT32 y2, UINT16 color, BOOL xor); +extern void Fb_filled_rectangle(INT32 x1, INT32 y1, INT32 x2, INT32 y2, UINT16 color, BOOL xor); + +#endif /* __FBPRIMITIVE_H_INCLUDED */ diff --git a/src/main.c b/src/main.c index 8716e56..bd939e9 100644 --- a/src/main.c +++ b/src/main.c @@ -6,23 +6,18 @@ #include "gpio.h" #include "log.h" #include "fbinit.h" +#include "fbprimitive.h" #include "time_func.h" #include "sysinput.h" static void do_draw(void) { - /* - tmp = (char *)Fb_Ptr; - memset(tmp, 0xFF, Fb_Info->screenbytes / 2); - memset(tmp + (Fb_Info->screenbytes / 2), 0x1B, Fb_Info->screenbytes / 2); - */ - UINT16 pixel = Fb_Info->green_mask; - unsigned npix = Fb_Info->screenbytes / sizeof(UINT16); - UINT16 *p = Fb_Ptr; - unsigned i; - - for (i=0; i (y)) ? (x) : (y)) #endif +#define ABS(v) ((v) < 0 ? -(v) : (v)) #define MAKEWORD(a, b) ((WORD)((((UINT_PTR)(a)) & 0xFF) | ((((UINT_PTR)(b)) & 0xFF) << 8))) #define MAKELONG(a, b) ((LONG)((((UINT_PTR)(a)) & 0xFFFF) | ((((UINT_PTR)(b)) & 0xFFFF) << 16)))