Banjo API 1.1.0-alpha
Low-level C99 game development API
Loading...
Searching...
No Matches
drawing_2d.c
Go to the documentation of this file.
1
107
108#include <banjo/app.h>
109#include <banjo/bitmap.h>
110#include <banjo/draw.h>
111#include <banjo/event.h>
112#include <banjo/log.h>
113#include <banjo/main.h>
114#include <banjo/pixel.h>
115#include <banjo/system.h>
116#include <banjo/window.h>
117
119
120// The scene is composed at a fixed internal resolution: every draw call below
121// is sized for a 500x500 area. The window itself is resizable, so we render
122// the scene once into this off-screen canvas and scale that onto the
123// framebuffer each frame, rather than drawing straight into a framebuffer
124// whose size changes (which would push the fixed coordinates out of bounds).
125enum { CANVAS_W = 500, CANVAS_H = 500 };
126static bj_bitmap* canvas = 0;
127
128static void on_draw(
129 struct bj_window* w,
130 struct bj_render_target* target,
131 const struct bj_rect* dirty,
132 void* user_data
133);
134
135void draw(bj_bitmap* bmp) {
136 // Clear the bitmap to black before drawing.
137 bj_clear_bitmap(bmp);
138
139 // Create colours in the bitmap's native pixel format. Always use
140 // bj_pack_bitmap_rgb() rather than hardcoding values, as the format
141 // varies by platform and configuration.
142 const uint32_t color_red = bj_pack_bitmap_rgb(bmp, 0xFF, 0x00, 0x00);
143 const uint32_t color_cyan = bj_pack_bitmap_rgb(bmp, 0x7F, 0xFF, 0xD4);
144 const uint32_t color_white = bj_pack_bitmap_rgb(bmp, 0xFF, 0xFF, 0xFF);
145
146 // Draw individual pixels. bj_put_pixel() sets a single pixel at (x, y).
147 // This is the most basic drawing operation.
148 for (size_t x = 10; x < 490; ++x) {
149 if (x % 7 == 0) {
150 bj_put_pixel(bmp, x, 10, color_red);
151 }
152 }
153
154 // Draw a polyline (connected line segments). BJ_POLYLINE_END_CLOSED closes
155 // the shape by connecting the last point back to the first. This draws a
156 // banjo outline.
157 int poly_x[] = { 100, 95, 95, 100, 100, 95, 75, 75, 95, 120, 140, 140, 120, 115, 115, 120, 120, 115, };
158 int poly_y[] = { 20, 25, 50, 55, 100, 100, 120, 145, 165, 165, 145, 120, 100, 100, 55, 50, 25, 20, };
159 bj_draw_polyline(bmp, 18, poly_x, poly_y, BJ_POLYLINE_END_CLOSED, color_cyan);
160
161 // Lines and polylines choose which pixels they claim. BJ_LINE_MODE_STEPPED is
162 // the thin, classic line. BJ_LINE_MODE_WATERTIGHT never takes a diagonal step,
163 // so it leaves no gap at the corners of a slope — draw a shape with it and
164 // a fill cannot leak out through them.
165 //
166 // The two lines below are the same segment, drawn one above the other:
167 // the lower one is visibly the fatter of the two on the slope.
168 bj_draw_line_mode(bmp, 300, 40, 400, 70, BJ_LINE_MODE_STEPPED, color_red);
169 bj_draw_line_mode(bmp, 300, 55, 400, 85, BJ_LINE_MODE_WATERTIGHT, color_red);
170
171 // Draw outlined triangles by indexing into a vertex array. This technique
172 // is common in graphics programming and efficiently reuses vertex data.
173 // Here we draw 13 triangles forming a fox shape.
174 int verts[][2] = {
175 {330, 270}, {270, 210}, {210, 270}, {210, 150}, {390, 210}, {450, 270},
176 {450, 150}, {180, 330}, {270, 390}, {390, 390}, {480, 330}, {330, 450},
177 {300, 480}, {360, 480},
178 };
179
180 size_t tris[13][3] = {
181 {0, 1, 2}, {0, 2, 3}, {0, 4, 5}, {0, 1, 4}, {4, 6, 5}, {2, 8, 7},
182 {0, 8, 2}, {0, 5, 9}, {9, 5, 10}, {8, 9, 11}, {8, 11, 12},
183 {9, 13, 11}, {11, 12, 13},
184 };
185
186 for (size_t t = 0; t < 13; ++t) {
188 verts[tris[t][0]][0], verts[tris[t][0]][1],
189 verts[tris[t][1]][0], verts[tris[t][1]][1],
190 verts[tris[t][2]][0], verts[tris[t][2]][1],
191 color_white
192 );
193 }
194
195 // Draw a checkerboard pattern using filled rectangles. The bj_rect
196 // structure defines a rectangle's position and size.
197 bj_rect board = {.w = 10, .h = 10,};
198 for(size_t y = 0 ; y < 8 ; ++y) {
199 for(size_t x = 0 ; x < 8 ; ++x) {
200 board.x = 200 + x * board.w;
201 board.y = 50 + y * board.h;
202 // XOR determines checkerboard pattern (alternating squares).
203 if((x ^ y) & 1) {
204 bj_draw_filled_rectangle(bmp, &board, color_red);
205 }
206 }
207 }
208 // Draw an outline around the entire checkerboard. Note the difference
209 // between bj_draw_rectangle (outline) and bj_draw_filled_rectangle (solid).
210 {
211 bj_rect outline = {200, 50, 80, 80};
212 bj_draw_rectangle(bmp, &outline, color_cyan);
213 }
214
215 // Draw concentric circles with alternating colours. bj_draw_filled_circle()
216 // takes centre (x, y), radius, and colour.
217 for (int r = 80; r > 0; r -= 20) {
218 bj_draw_filled_circle(bmp, 100, 400, r, (r/20) % 2 ? color_red : color_white);
219 }
220
221 // Draw filled triangles for a simple mountain scene. Filled triangles are
222 // drawn with bj_draw_filled_triangle(), taking three vertices and a colour.
223 const uint32_t color_dark_gray = bj_pack_bitmap_rgb(bmp, 0x50, 0x50, 0x50);
224 const uint32_t color_gray = bj_pack_bitmap_rgb(bmp, 0x80, 0x80, 0x80);
225 const uint32_t color_light_gray = bj_pack_bitmap_rgb(bmp, 0xB0, 0xB0, 0xB0);
226
227 bj_draw_filled_triangle(bmp, 250, 400, 200, 480, 300, 480, color_gray);
228 bj_draw_filled_triangle(bmp, 300, 420, 250, 480, 350, 480, color_dark_gray);
229 bj_draw_filled_triangle(bmp, 350, 390, 300, 480, 400, 480, color_light_gray);
230}
231
232// Static drawing: banjo asks, we present. The scene was rendered into the
233// canvas once in setup; here we just scale that fixed 500x500 image onto the
234// window's framebuffer at whatever size the window currently is. Passing 0
235// for both areas means "whole source -> whole destination", and
236// bj_blit_bitmap_stretched clips to the destination, so this is always in bounds no
237// matter how the window is resized.
238static void on_draw(
239 struct bj_window* w,
240 struct bj_render_target* target,
241 const struct bj_rect* dirty,
242 void* user_data
243) {
244 (void)w; (void)dirty; (void)user_data;
246}
247
248static void* setup(struct bj_app* app, void* init_data) {
249 (void)init_data;
250
251 if (!bj_begin(BJ_VIDEO_SYSTEM, 0)) {
252 bj_quit_app(app, 1);
253 return 0;
254 }
255
256 // Fixed-resolution canvas: the scene is static, so render it once here.
257 // on_draw then only scales it onto the framebuffer.
259 if (canvas == 0) {
260 bj_quit_app(app, 1);
261 return 0;
262 }
263 draw(canvas);
264
265 window = bj_create_window("2D Drawing", 100, 100, CANVAS_W, CANVAS_H,
267
271 return 0;
272}
273
274static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
275 (void)tick;
276 (void)user_data;
278
280 bj_quit_app(app, 0);
281 }
282}
283
284static void teardown(struct bj_app* app, void* user_data) {
285 (void)user_data;
288 bj_end();
289}
290
291int main(int argc, char* argv[]) {
292 (void)argc; (void)argv;
293 return bj_run_app(setup, step, 0, teardown, 0);
294}
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.
static bj_bitmap * canvas
Definition drawing_2d.c:126
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.
bj_bool bj_blit_bitmap_stretched(const struct bj_bitmap *src, const struct bj_rect *src_area, struct bj_bitmap *dst, const struct bj_rect *dst_area, enum bj_blit_op op)
Stretched bitmap blitting (nearest neighbor).
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_bitmap * bj_create_bitmap(size_t width, size_t height, enum bj_pixel_mode mode, size_t stride)
Creates a new struct bj_bitmap with the specified width and height.
void bj_put_pixel(struct bj_bitmap *bitmap, size_t x, size_t y, uint32_t value)
Change the pixel colour at given coordinate.
void bj_destroy_bitmap(struct bj_bitmap *bitmap)
Deletes a struct bj_bitmap object and releases associated memory.
@ BJ_BLIT_OP_COPY
Copy source to destination (fast path when formats match)
Definition bitmap.h:99
struct bj_render_target bj_render_target
Definition api.h:369
struct bj_bitmap bj_bitmap
Definition api.h:340
struct bj_window bj_window
Definition api.h:384
void bj_draw_rectangle(struct bj_bitmap *bitmap, const struct bj_rect *area, uint32_t pixel)
Draws a rectangle in the given bitmap.
void bj_draw_polyline(struct bj_bitmap *bitmap, size_t count, const int *x, const int *y, bj_polyline_end end, uint32_t color)
Draw a polyline from C-style coordinate arrays.
void bj_draw_triangle(struct bj_bitmap *bitmap, int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color)
Draws the edges of a triangle given its 3 corners.
void bj_draw_line_mode(struct bj_bitmap *bitmap, int x0, int y0, int x1, int y1, bj_line_mode mode, uint32_t pixel)
Draws a line of pixels, choosing which ones it claims.
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_filled_circle(struct bj_bitmap *bitmap, int cx, int cy, int radius, uint32_t color)
Draw a filled circle onto a bitmap.
void bj_draw_filled_triangle(struct bj_bitmap *bitmap, int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color)
Draws a filled triangle given its 3 corners.
@ BJ_POLYLINE_END_CLOSED
An extra segment closes the polyline into a loop.
Definition draw.h:109
@ BJ_LINE_MODE_WATERTIGHT
No diagonal steps: consecutive pixels differ along one axis only, so the line has no gap at any corne...
Definition draw.h:93
@ BJ_LINE_MODE_STEPPED
Diagonal steps allowed, one pixel per step along the major axis.
Definition draw.h:87
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.
uint16_t w
Width in pixels.
Definition rect.h:36
uint16_t h
Height in pixels.
Definition rect.h:37
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
Axis-aligned rectangle: a top-left corner plus a width and height.
Definition rect.h:33
@ BJ_PIXEL_MODE_XRGB8888
32bpp RGB
Definition pixel.h:94
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.
@ BJ_WINDOW_FLAG_RESIZABLE
User may resize the window.
Definition window.h:196
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
Header file for general pixel manipulation facilities.
#define CANVAS_W
Definition shaders.c:26
#define CANVAS_H
Definition shaders.c:27
Header file for system interactions.
int main(void)
Header file for bj_window type.