Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
bitmap_blit.c
Go to the documentation of this file.
1
11
12#include <banjo/app.h>
13#include <banjo/main.h>
14#include <banjo/bitmap.h>
15#include <banjo/event.h>
16#include <banjo/log.h>
17#include <banjo/memory.h>
18#include <banjo/system.h>
19#include <banjo/window.h>
20
21#define WINDOW_W 800
22#define WINDOW_H 600
23
26
27// The composed image is static: we just copy it to the framebuffer each
28// time banjo asks us to repaint. dirty is ignored because we always blit
29// the whole composite: drawing a sub-rect of an already-finished image
30// has no perf advantage here, and Cocoa / Win32 clip to the dirty region
31// for us at the OS level.
32static void on_draw(
33 struct bj_window* w,
34 struct bj_render_target* target,
35 const struct bj_rect* dirty,
36 void* user_data
37) {
38 (void)w; (void)dirty; (void)user_data;
40}
41
42static void* setup(struct bj_app* app, void* init_data) {
43 (void)init_data;
44
45 // Create an off-screen bitmap to compose our final image. This is where
46 // we'll blit all the source images before displaying the result.
50
51 // Load source images that we'll composite together.
52 bj_bitmap* bmp_blackbuck_512_512 = bj_create_bitmap_from_file(BANJO_ASSETS_DIR"/bmp/blackbuck.bmp", 0);
53 bj_bitmap* bmp_greenland_grid_velo_762_1309 = bj_create_bitmap_from_file(BANJO_ASSETS_DIR"/bmp/greenland_grid_velo.bmp", 0);
54 bj_bitmap* bmp_lena_512_512 = bj_create_bitmap_from_file(BANJO_ASSETS_DIR"/bmp/lena.bmp", 0);
55 bj_bitmap* bmp_snail_256_256 = bj_create_bitmap_from_file(BANJO_ASSETS_DIR"/bmp/snail.bmp", 0);
56
57 // bj_blit() copies a rectangular region from source to destination.
58 // Parameters: source bitmap, source rect, dest bitmap, dest rect, operation
59 //
60 // Source rect defines what region to copy (x, y, width, height).
61 // Dest rect only needs x, y for position (width/height from source rect).
62 // BJ_BLIT_OP_COPY is a direct pixel copy with no blending.
63 //
64 // This is efficient for sprite rendering - load one sprite sheet and blit
65 // individual frames or tiles as needed.
66 bj_blit(bmp_greenland_grid_velo_762_1309, &(bj_rect){.x = 0, .y = 0, .w = 762, .h = 1309}, bmp_rendering, & (bj_rect){.x = 20, .y = 0}, BJ_BLIT_OP_COPY);
67 bj_blit(bmp_blackbuck_512_512, &(bj_rect){.x = 100, .y = 100, .w = 512, .h = 512}, bmp_rendering, & (bj_rect){.x = 100, .y = 200}, BJ_BLIT_OP_COPY);
68 bj_blit(bmp_snail_256_256, &(bj_rect){.x = 0, .y = 0, .w = 256, .h = 256}, bmp_rendering, & (bj_rect){.x = 500, .y = 130}, BJ_BLIT_OP_COPY);
69
70 // Destroy source bitmaps after blitting. The pixels are copied, so we
71 // don't need to keep the sources in memory.
72 bj_destroy_bitmap(bmp_snail_256_256);
73 bj_destroy_bitmap(bmp_lena_512_512);
74 bj_destroy_bitmap(bmp_greenland_grid_velo_762_1309);
75 bj_destroy_bitmap(bmp_blackbuck_512_512);
76
77 if (!bj_begin(BJ_VIDEO_SYSTEM, 0)) {
78 bj_quit_app(app, 1);
79 return 0;
80 }
81
82 window = bj_bind_window("Blitmap Blit", 0, 0, WINDOW_W, WINDOW_H, 0, 0);
83
86
87 // Ask banjo to fire the first paint. After that, the backend re-fires
88 // the callback on its natural cadence whenever something invalidates.
90 return 0;
91}
92
93static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
94 (void)tick;
95 (void)user_data;
98 bj_quit_app(app, 0);
99 }
100}
101
102static void teardown(struct bj_app* app, void* user_data) {
103 (void)user_data;
106 bj_end();
107}
108
109int main(int argc, char* argv[]) {
110 (void)argc; (void)argv;
111 return bj_run_app(setup, step, 0, teardown, 0);
112}
Application lifecycle: callback-driven setup, step, and teardown.
int main(int argc, char *argv[])
Definition audio_pcm.c:177
static void step(struct bj_app *app, struct bj_tick_info tick, void *user_data)
Definition audio_pcm.c:144
static void teardown(struct bj_app *app, void *user_data)
Definition audio_pcm.c:170
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_bitmap * bmp_rendering
Definition bitmap_blit.c:25
#define WINDOW_W
Definition bitmap_blit.c:21
bj_window * window
Definition bitmap_blit.c:24
#define WINDOW_H
Definition bitmap_blit.c:22
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:106
void bj_clear_bitmap(struct bj_bitmap *bitmap)
Fills the entire bitmap with the clear colour.
struct bj_bitmap * bj_create_bitmap_from_file(const char *path, struct bj_error **error)
Creates a new bitmap by loading from a file.
bj_bool bj_blit(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)
Bitmap blitting operation from a source to a destination bitmap.
uint32_t bj_make_bitmap_pixel(struct bj_bitmap *bitmap, uint8_t red, uint8_t green, uint8_t blue)
Returns an opaque value representing a pixel colour, given its RGB composition.
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_set_bitmap_color(struct bj_bitmap *bitmap, uint32_t color, uint8_t roles)
Sets one or more colour properties of a bitmap.
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:93
@ BJ_BITMAP_CLEAR_COLOR
Clear/fill colour for bj_clear_bitmap()
Definition bitmap.h:118
struct bj_render_target bj_render_target
Definition api.h:346
struct bj_bitmap bj_bitmap
Definition api.h:328
struct bj_window bj_window
Definition api.h:354
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.
Axis-aligned rectangle: a top-left corner plus a width and height.
Definition rect.h:33
@ BJ_PIXEL_MODE_BGR24
24bpp BGR
Definition pixel.h:96
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
void bj_set_draw_callback(struct bj_window *window, bj_window_draw_fn fn, void *user_data)
Register the redraw callback for window.
static void bj_invalidate_window(struct bj_window *window)
Mark the whole window as needing a repaint.
Definition window.h:470
struct bj_window * bj_bind_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 new struct bj_window with the specified attributes.
struct bj_bitmap * bj_render_target_bitmap(struct bj_render_target *target)
Reach the software framebuffer behind a render target.
bj_bool bj_should_close_window(struct bj_window *window)
Get the close flag state of a window.
void bj_unbind_window(struct bj_window *window)
Deletes a struct bj_window object and releases associated memory.
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
All memory-related functions, including custom allocators.
Header file for system interactions.
Header file for bj_window type.