diff options
Diffstat (limited to 'lib/libgfx')
-rw-r--r-- | lib/libgfx/Makefile | 1 | ||||
-rw-r--r-- | lib/libgfx/include/libgfx/draw.h | 24 | ||||
-rw-r--r-- | lib/libgfx/include/libgfx/gfx.h | 11 | ||||
-rw-r--r-- | lib/libgfx/src/draw.c | 74 |
4 files changed, 91 insertions, 19 deletions
diff --git a/lib/libgfx/Makefile b/lib/libgfx/Makefile index 1f866ac..12fdd9a 100644 --- a/lib/libgfx/Makefile +++ b/lib/libgfx/Makefile @@ -4,7 +4,6 @@ CFILES = $(shell find src/ -name "*.c") OBJ = $(CFILES:.c=.o) all: headers $(OBJ) build/libgfx.a - echo "----------------------------------------" echo $(USRDIR) mv build/libgfx.a $(USRDIR)/lib/ cp -r include/ $(USRDIR)/include/ diff --git a/lib/libgfx/include/libgfx/draw.h b/lib/libgfx/include/libgfx/draw.h index 60d2b24..9f56f6c 100644 --- a/lib/libgfx/include/libgfx/draw.h +++ b/lib/libgfx/include/libgfx/draw.h @@ -36,6 +36,17 @@ /* Shape types */ #define SHAPE_SQUARE 0x00000000 +/* Basic color defines */ +#define GFX_BLACK 0x000000 +#define GFX_RED 0xFF0000 +#define GFX_GREEN 0x00FF00 +#define GFX_BLUE 0x0000FF +#define GFX_WHITE 0xFFFFFF +#define GFX_PURPLE 0x800080 +#define GFX_YELLOW 0xFFFF00 +#define GFX_DARK 0x1D2021 +#define GFX_AQUA 0x427B58 + /* * Default shape initializer, something that * works and can be tweaked. The idea of this @@ -78,7 +89,20 @@ struct gfx_shape { dimm_t height; }; +/* + * A point or single pixel that + * may be plotted onto the screen. + * + * @x,y: Position of the point on the screen + * @rgb: Color of the point (RGB) + */ +struct gfx_point { + scrpos_t x, y; + color_t rgb; +}; + int gfx_draw_shape(struct gfx_ctx *ctx, const struct gfx_shape *shape); +int gfx_plot_point(struct gfx_ctx *ctx, const struct gfx_point *point); __always_inline static inline size_t gfx_io_index(struct gfx_ctx *ctx, scrpos_t x, scrpos_t y) diff --git a/lib/libgfx/include/libgfx/gfx.h b/lib/libgfx/include/libgfx/gfx.h index 3468571..67a1006 100644 --- a/lib/libgfx/include/libgfx/gfx.h +++ b/lib/libgfx/include/libgfx/gfx.h @@ -48,17 +48,6 @@ typedef uint32_t pixel_t; typedef pixel_t color_t; /* - * Basic color defines - */ -#define GFX_BLACK 0x000000 -#define GFX_RED 0xFF0000 -#define GFX_GREEN 0x00FF00 -#define GFX_BLUE 0x0000FF -#define GFX_WHITE 0xFFFFFF -#define GFX_PURPLE 0x800080 -#define GFX_YELLOW 0xFFFF00 - -/* * Represents cartesian x/y values */ typedef uint32_t cartpos_t; diff --git a/lib/libgfx/src/draw.c b/lib/libgfx/src/draw.c index 49f2f53..9ef8630 100644 --- a/lib/libgfx/src/draw.c +++ b/lib/libgfx/src/draw.c @@ -33,6 +33,39 @@ #include <libgfx/draw.h> /* + * See if a pixel is within the bounds of + * the screen. + * + * @ctx: Graphics context pointer + * @x: X position to check + * @y: Y position to check + * + * Returns 0 if within bounds, otherwise + * a negative value. + */ +static int +gfx_pixel_bounds(struct gfx_ctx *ctx, uint32_t x, uint32_t y) +{ + scrpos_t scr_width, scr_height; + struct fbattr fbdev; + + if (ctx == NULL) { + return -1; + } + + /* Grab screen dimensions */ + fbdev = ctx->fbdev; + scr_width = fbdev.width; + scr_height = fbdev.height; + + if (x >= scr_height || y >= scr_width) { + return -1; + } + + return 0; +} + +/* * Draw a classic square onto the screen. * * @ctx: Graphics context @@ -43,21 +76,15 @@ gfx_draw_square(struct gfx_ctx *ctx, const struct gfx_shape *shape) { struct fbattr fbdev; off_t idx; - scrpos_t scr_width, scr_height; scrpos_t x, y; if (ctx == NULL || shape == NULL) { return -EINVAL; } - /* Grab screen dimensions */ - fbdev = ctx->fbdev; - scr_width = fbdev.width; - scr_height = fbdev.height; - for (x = shape->x; x < shape->x + shape->width; ++x) { for (y = shape->y; y < shape->y + shape->height; ++y) { - if (x >= scr_width || y >= scr_height) { + if (gfx_pixel_bounds(ctx, x, y) < 0) { break; } @@ -69,6 +96,39 @@ gfx_draw_square(struct gfx_ctx *ctx, const struct gfx_shape *shape) } /* + * Plot a single pixel (aka point) onto + * the screen. + * + * @ctx: The graphics context pointer + * @point: Point to plot + * + * Returns 0 on success, otherwise a less + * than zero value. + */ +int +gfx_plot_point(struct gfx_ctx *ctx, const struct gfx_point *point) +{ + uint32_t index; + + if (ctx == NULL || point == NULL) { + return -EINVAL; + } + + /* + * Is this even a valid point on the screen for + * us to plot on? + */ + if (gfx_pixel_bounds(ctx, point->x, point->y) < 0) { + return -1; + } + + /* Plot it !! */ + index = gfx_io_index(ctx, point->x, point->y); + ctx->io[index] = point->rgb; + return 0; +} + +/* * Draw a shape onto the screen * * @ctx: libgfx graphics context |