diff options
author | Ian Moffett <ian@osmora.org> | 2025-08-03 03:20:02 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-08-03 03:20:02 -0400 |
commit | 96d4341a01c9b935d309ccc4b6d31d852a76257b (patch) | |
tree | 30945ac49a15b4a62b86087d8cbfcf135c352711 /lib/libgfx/src | |
parent | 0e30d188a7f19a9ea8d24286c16ecdd6599b687c (diff) |
libgfx: draw: Add gfx_pixel_bounds() helper
Implement helper to check if a point on the screen is within bounds of
the screen.
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'lib/libgfx/src')
-rw-r--r-- | lib/libgfx/src/draw.c | 41 |
1 files changed, 34 insertions, 7 deletions
diff --git a/lib/libgfx/src/draw.c b/lib/libgfx/src/draw.c index 49f2f53..8280f8a 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; } |