Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
sprite_animation.c
Go to the documentation of this file.
1
12
13#include <banjo/app.h>
14#include <banjo/main.h>
15#include <banjo/bitmap.h>
16#include <banjo/event.h>
17#include <banjo/log.h>
18#include <banjo/memory.h>
19#include <banjo/system.h>
20#include <banjo/window.h>
21
22// This sprite sheet has 7 frames arranged horizontally, each 24x24 pixels.
23// Frame layout: [0][1][2][3][4][5][6]
24#define SPRITE_W 24
25#define SPRITE_H 24
26#define FRAMES 7
27
28#define WINDOW_W 240
29#define WINDOW_H 240
30
32// Frame counter determines which frame to display (1-7).
33size_t frame_count = 1;
36
37// Animation render: blit the current frame from the sprite sheet
38// (offset by frame_count) into a small bitmap, then stretch-blit that
39// into the window's framebuffer. frame_count advances on a timer in
40// step; we just paint whatever it currently points at.
41static void on_draw(
42 struct bj_window* w,
43 struct bj_render_target* target,
44 const struct bj_rect* dirty,
45 void* user_data
46) {
47 (void)w; (void)dirty; (void)user_data;
48
49 // The animation technique: blit ONE frame from the sprite sheet by
50 // calculating the horizontal offset. Frame 1 starts at x=24, frame 2 at
51 // x=48, etc. This shifts which 24x24 region gets copied from the sheet.
53 .x = frame_count * SPRITE_W, // Horizontal offset into sprite sheet
54 .w = 24, .h = 24 // Size of one frame
55 }, bmp_rendering, & (bj_rect){.x = 0, .y = 0},
57 );
58
59 // Scale the small 24x24 frame to fill the 240x240 window for visibility.
61}
62
63static void* setup(struct bj_app* app, void* init_data) {
64 (void)init_data;
65
66 // Create a small off-screen bitmap to hold one animation frame.
70
71 // Load the sprite sheet once. It stays in memory - we just blit different
72 // regions from it each frame to create animation.
73 bmp_sprite_sheet = bj_create_bitmap_from_file(BANJO_ASSETS_DIR"/bmp/gabe-idle-run.bmp", 0);
74
75 if (!bj_begin(BJ_VIDEO_SYSTEM, 0)) {
76 bj_quit_app(app, 1);
77 return 0;
78 }
79
80 window = bj_bind_window("sprite sheet - Banjo", 0, 0, WINDOW_W, WINDOW_H, 0, 0);
81
85 return 0;
86}
87
88static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
89 (void)user_data;
90
92
93 // Advance one sprite frame every 120 ms (about 8 FPS), timed from the
94 // frame delta so the animation runs at the same speed whatever the
95 // app's render rate is. tick.delta is the seconds elapsed since the
96 // previous step.
97 static double since_frame = 0.0;
98 since_frame += tick.delta;
99 if (since_frame >= 0.120) {
100 since_frame -= 0.120;
101 // Advance to the next frame and wrap around (1->2->...->7->1),
102 // then request a fresh paint so banjo redraws with the new frame.
103 if (++frame_count >= FRAMES) {
104 frame_count = 1;
105 }
107 }
108
110 bj_quit_app(app, 0);
111 }
112}
113
114static void teardown(struct bj_app* app, void* user_data) {
115 (void)user_data;
117 bj_end();
118
119 // Destroy the sprite sheet and off-screen buffer.
122}
123
124int main(int argc, char* argv[]) {
125 (void)argc; (void)argv;
126 return bj_run_app(setup, step, 0, teardown, 0);
127}
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.
bj_real delta
Definition app.h:107
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.
bj_bool bj_blit_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).
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.
bj_bitmap * bmp_sprite_sheet
Definition load_bmp.c:81
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
All memory-related functions, including custom allocators.
#define SPRITE_W
#define FRAMES
size_t frame_count
#define SPRITE_H
Header file for system interactions.
Header file for bj_window type.