Banjo API 1.1.0-alpha
Low-level C99 game development API
Loading...
Searching...
No Matches
pixel_enumeration.c

Walking a shape's pixels without drawing them, and why a line is not a ray.

Walking a shape's pixels without drawing them, and why a line is not a ray.The drawing functions answer "paint this shape". The enumeration functions answer "which pixels does this shape touch" — and hand you the coordinates instead of writing anything. That is what an edge walker, a ray caster or a line-of-sight test needs.

This program puts the two walks side by side on the same ray. On the left, bj_enumerate_line_pixels walks one pixel per step, so a diagonal jumps corner to corner. On the right, bj_enumerate_grid_ray_cells reports every cell the ray crosses.

Watch the gaps appear on the left as the ray turns. Each gap is a corner a projectile would fly through.

#include <banjo/app.h>
#include <banjo/bitmap.h>
#include <banjo/draw.h>
#include <banjo/event.h>
#include <banjo/main.h>
#include <banjo/math.h>
#include <banjo/system.h>
#include <banjo/window.h>
#define CELLS 15
#define CELL_PX 20
#define GRID_PX (CELLS * CELL_PX)
#define LEFT_X 20
#define RIGHT_X (LEFT_X + GRID_PX + 40)
#define GRID_Y 40
static struct bj_window* window = 0;
// Everything a callback needs to paint one cell of one grid.
struct grid_paint {
struct bj_bitmap* bmp;
int origin_x;
uint32_t color;
};
static void fill_cell(struct grid_paint* g, int cx, int cy) {
if (cx < 0 || cy < 0 || cx >= CELLS || cy >= CELLS) {
return;
}
const struct bj_rect r = {
(int16_t)(g->origin_x + cx * CELL_PX + 1),
(int16_t)(GRID_Y + cy * CELL_PX + 1),
(uint16_t)(CELL_PX - 1),
(uint16_t)(CELL_PX - 1)
};
}
// bj_pixel_fn: the walked pixel *is* the cell index here.
static bj_bool paint_pixel(void* user_data, int x, int y) {
fill_cell((struct grid_paint*)user_data, x, y);
return BJ_TRUE;
}
// bj_cell_fn: same job, but the walk also hands us the hit distance.
static bj_bool paint_cell(void* user_data, int x, int y, bj_real t) {
(void)t; // a ray caster would stop here on the first wall
fill_cell((struct grid_paint*)user_data, x, y);
return BJ_TRUE;
}
static void draw_grid_lines(struct bj_bitmap* bmp, int origin_x, uint32_t color) {
for (int i = 0; i <= CELLS; ++i) {
bj_draw_line(bmp, origin_x, GRID_Y + i * CELL_PX,
origin_x + GRID_PX, GRID_Y + i * CELL_PX, color);
bj_draw_line(bmp, origin_x + i * CELL_PX, GRID_Y,
origin_x + i * CELL_PX, GRID_Y + GRID_PX, color);
}
}
static void draw(struct bj_bitmap* bmp) {
const uint32_t grey = bj_pack_bitmap_rgb(bmp, 70, 70, 80);
const uint32_t white = bj_pack_bitmap_rgb(bmp, 235, 235, 235);
const uint32_t orange = bj_pack_bitmap_rgb(bmp, 200, 90, 30);
const uint32_t teal = bj_pack_bitmap_rgb(bmp, 40, 130, 140);
// The ray, in cell units, from the middle of the centre cell.
const bj_real cx = BJ_F(7.5);
const bj_real cy = BJ_F(7.5);
const bj_real ex = cx + bj_cos(angle) * BJ_F(7.0);
const bj_real ey = cy + bj_sin(angle) * BJ_F(7.0);
// Left: Bresenham. One pixel per step along the major axis.
struct grid_paint left = { bmp, LEFT_X, orange };
bj_enumerate_line_pixels((int)cx, (int)cy, (int)ex, (int)ey, paint_pixel, &left);
// Right: supercovering. Every cell the ray actually crosses.
struct grid_paint right = { bmp, RIGHT_X, teal };
bj_enumerate_grid_ray_cells(cx, cy, ex, ey, paint_cell, &right);
// The ray itself, over both grids, so the two walks can be compared.
(int)(LEFT_X + cx * CELL_PX), (int)(GRID_Y + cy * CELL_PX),
(int)(LEFT_X + ex * CELL_PX), (int)(GRID_Y + ey * CELL_PX), white);
(int)(RIGHT_X + cx * CELL_PX), (int)(GRID_Y + cy * CELL_PX),
(int)(RIGHT_X + ex * CELL_PX), (int)(GRID_Y + ey * CELL_PX), white);
}
static void on_draw(
struct bj_window* w,
struct bj_render_target* target,
const struct bj_rect* dirty,
void* user_data
) {
(void)w; (void)dirty; (void)user_data;
}
static void* setup(struct bj_app* app, void* init_data) {
(void)init_data;
bj_quit_app(app, 1);
return 0;
}
window = bj_create_window("Pixel enumeration: line walk vs grid ray",
80, 80, RIGHT_X + GRID_PX + 20, GRID_Y + GRID_PX + 20,
0, 0);
return 0;
}
static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
(void)tick; (void)user_data;
angle += BJ_F(0.004);
if (angle > BJ_TAU) {
}
bj_quit_app(app, 0);
}
}
static void teardown(struct bj_app* app, void* user_data) {
(void)app; (void)user_data;
bj_end();
}
int main(int argc, char* argv[]) {
(void)argc; (void)argv;
return bj_run_app(setup, step, 0, teardown, 0);
}
Application lifecycle: callback-driven setup, step, and teardown.
static void step(struct bj_app *app, struct bj_tick_info tick, void *user_data)
Definition audio_pcm.c:142
static void teardown(struct bj_app *app, void *user_data)
Definition audio_pcm.c:168
static void * setup(struct bj_app *app, void *init_data)
Definition audio_pcm.c:107
Header file for Bitmap type.
static void on_draw(struct bj_window *w, struct bj_render_target *target, const struct bj_rect *dirty, void *user_data)
Definition bitmap_blit.c:32
bj_window * window
Definition bitmap_blit.c:24
Header file for Bitmap drawing functions.
void draw(bj_bitmap *bmp)
Definition drawing_2d.c:135
Sytem event management API.
int bj_run_app(bj_app_setup_fn setup, bj_app_step_fn step, bj_app_fixed_step_fn fixed_step, bj_app_teardown_fn teardown, void *init_data)
Drive the application lifecycle.
void bj_quit_app(struct bj_app *app, int exit_code)
Signal the given application to exit on the next iteration.
Timing snapshot handed to the step and fixed-step callbacks.
Definition app.h:107
void bj_clear_bitmap(struct bj_bitmap *bitmap)
Fills the entire bitmap with the clear colour.
uint32_t bj_pack_bitmap_rgb(struct bj_bitmap *bitmap, uint8_t red, uint8_t green, uint8_t blue)
Pack an (R, G, B) triple into the bitmap's native pixel format.
struct bj_render_target bj_render_target
Definition api.h:369
uint32_t bj_bool
Boolean type used throughout the Banjo API.
Definition api.h:264
struct bj_bitmap bj_bitmap
Definition api.h:340
struct bj_window bj_window
Definition api.h:384
#define BJ_TRUE
Boolean true value (1).
Definition api.h:282
void bj_enumerate_line_pixels(int x0, int y0, int x1, int y1, bj_pixel_fn callback, void *user_data)
Reports every pixel a line touches, without drawing anything.
void bj_draw_filled_rectangle(struct bj_bitmap *bitmap, const struct bj_rect *area, uint32_t pixel)
Draws a filled rectangle in the given bitmap.
void bj_draw_line(struct bj_bitmap *bitmap, int x0, int y0, int x1, int y1, uint32_t pixel)
Draws a line of pixels in the given bitmap.
void bj_enumerate_grid_ray_cells(bj_real x0, bj_real y0, bj_real x1, bj_real y1, bj_cell_fn callback, void *user_data)
Reports every grid cell a ray passes through.
void bj_dispatch_events(void)
Poll and dispatch all pending events.
void bj_close_on_escape(struct bj_window *window, const struct bj_key_event *event, void *user_data)
Handle the ESC key to close a window.
bj_key_callback_fn bj_set_key_callback(bj_key_callback_fn callback, void *user_data)
Set the global callback for keyboard key events.
int16_t y
Y coordinate of the top-left corner (pixels, can be negative).
Definition rect.h:35
int16_t x
X coordinate of the top-left corner (pixels, can be negative).
Definition rect.h:34
#define BJ_TAU
TAU in the selected bj_real precision.
Definition math.h:117
#define bj_sin
Sine.
Definition math.h:250
#define BJ_FZERO
Zero constant in bj_real.
Definition math.h:93
#define bj_cos
Cosine.
Definition math.h:241
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
Definition math.h:82
float bj_real
Selected real type for float configuration.
Definition math.h:80
Axis-aligned rectangle: a top-left corner plus a width and height.
Definition rect.h:33
bj_bool bj_begin(int systems, struct bj_error **error)
Initialises the system.
void bj_end(void)
De-initialises the system.
@ BJ_VIDEO_SYSTEM
Definition system.h:81
static void bj_invalidate_window(struct bj_window *window)
Mark the whole window as needing a repaint.
Definition window.h:513
bj_bool bj_window_should_close(struct bj_window *window)
Report whether a window's close flag is set.
struct bj_window * bj_create_window(const char *title, uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t flags, struct bj_error **error)
Create a window with the given attributes.
void bj_set_window_draw_callback(struct bj_window *window, bj_window_draw_fn fn, void *user_data)
Register the redraw callback for window.
struct bj_bitmap * bj_render_target_bitmap(struct bj_render_target *target)
Reach the software framebuffer behind a render target.
void bj_destroy_window(struct bj_window *window)
Destroy a window and release its resources.
Portable main() replacement with platform-aware entry shim.
C99 math shim with bj_real precision type and scalar utilities.
#define CELLS
static bj_bool paint_pixel(void *user_data, int x, int y)
struct bj_bitmap * bmp
#define LEFT_X
static void fill_cell(struct grid_paint *g, int cx, int cy)
#define CELL_PX
#define GRID_PX
#define RIGHT_X
#define GRID_Y
static bj_real angle
static void draw_grid_lines(struct bj_bitmap *bmp, int origin_x, uint32_t color)
static bj_bool paint_cell(void *user_data, int x, int y, bj_real t)
Header file for system interactions.
int main(void)
Header file for bj_window type.