Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
interpolation.c
Go to the documentation of this file.
1
46
47#include <banjo/app.h>
48#include <banjo/main.h>
49#include <banjo/bitmap.h>
50#include <banjo/draw.h>
51#include <banjo/event.h>
52#include <banjo/system.h>
53#include <banjo/window.h>
54
55#define WIDTH 640
56#define HEIGHT 360
57#define BALL_R 24
58#define SPEED 320.0f /* pixels per second */
59#define FIXED_HZ 10 /* deliberately low so the stutter is obvious */
60#define TOP_Y 120 /* smooth (interpolated) ball */
61#define BOT_Y 240 /* raw (un-interpolated) ball */
62
63static bj_window* window = 0;
64
65/* Physics state, advanced only in fixed_step at FIXED_HZ. We keep the
66 previous position alongside the current one so the draw callback can
67 interpolate between them. */
68static float prev_x;
69static float curr_x;
70static float vx = SPEED;
71
72/* The interpolation factor for the current render. The step callback
73 receives it in tick.alpha and stashes it here; on_draw reads it. The
74 draw callback isn't handed a bj_tick_info, so this hand-off is how
75 alpha reaches the place that actually draws. */
76static float render_alpha;
77
78/* Advance the simulation by exactly one fixed tick (1 / FIXED_HZ s).
79 Save the old position first so we can interpolate toward the new one. */
80static void fixed_step(struct bj_app* app, struct bj_tick_info tick, void* ud) {
81 (void)app; (void)ud;
82 prev_x = curr_x;
83 curr_x += vx * tick.delta;
84 if (curr_x < BALL_R) { curr_x = BALL_R; vx = +SPEED; }
85 if (curr_x > WIDTH - BALL_R) { curr_x = WIDTH - BALL_R; vx = -SPEED; }
86}
87
88static void on_draw(
89 struct bj_window* w,
90 struct bj_render_target* target,
91 const struct bj_rect* dirty,
92 void* user_data
93) {
94 (void)w; (void)dirty; (void)user_data;
97
98 const uint32_t green = bj_make_bitmap_pixel(fb, 0x40, 0xE0, 0x60);
99 const uint32_t red = bj_make_bitmap_pixel(fb, 0xE0, 0x50, 0x50);
100
101 /* Smooth: render between the last two physics positions using alpha. */
102 const float smooth_x = prev_x + (curr_x - prev_x) * render_alpha;
103 bj_draw_filled_circle(fb, (int)smooth_x, TOP_Y, BALL_R, green);
104
105 /* Raw: render the current physics position directly. At FIXED_HZ it
106 only changes 10 times a second, so it visibly jumps. */
107 bj_draw_filled_circle(fb, (int)curr_x, BOT_Y, BALL_R, red);
108}
109
110static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
111 (void)user_data;
112 render_alpha = (float)tick.alpha; /* hand alpha to on_draw */
114 bj_invalidate_window(window); /* render every frame at the cap rate */
115
117 bj_quit_app(app, 0);
118 }
119}
120
121static void* setup(struct bj_app* app, void* init_data) {
122 (void)init_data;
123 bj_set_fixed_step_rate(app, FIXED_HZ); /* slow physics on purpose */
124 if (!bj_begin(BJ_VIDEO_SYSTEM, 0)) {
125 bj_quit_app(app, 1);
126 return 0;
127 }
128 window = bj_bind_window("Interpolation: smooth (top) vs raw (bottom), ESC to quit",
129 100, 100, WIDTH, HEIGHT, 0, 0);
132 prev_x = curr_x = BALL_R;
133 return 0;
134}
135
136static void teardown(struct bj_app* app, void* user_data) {
137 (void)app; (void)user_data;
139 bj_end();
140}
141
142int main(int argc, char* argv[]) {
143 (void)argc; (void)argv;
144 /* Frame rate stays at the default 60, so ~6 renders happen between
145 each physics tick. That gap is exactly what alpha interpolates. */
147}
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_window * window
Definition bitmap_blit.c:24
Header file for Bitmap drawing functions.
Sytem event management API.
bj_real delta
Definition app.h:107
bj_real alpha
Definition app.h:108
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_set_fixed_step_rate(struct bj_app *app, int hz)
Configure the fixed-step rate (in Hz) for app.
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.
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_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_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_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_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.
static float render_alpha
#define WIDTH
#define FIXED_HZ
#define BALL_R
static float curr_x
static float vx
#define SPEED
#define BOT_Y
#define TOP_Y
#define HEIGHT
static void fixed_step(struct bj_app *app, struct bj_tick_info tick, void *ud)
static float prev_x
Portable main() replacement with platform-aware entry shim.
Header file for system interactions.
Header file for bj_window type.