Banjo API 1.1.0-alpha
Low-level C99 game development API
Loading...
Searching...
No Matches
random_distribution.c
Go to the documentation of this file.
1
20
21#include <banjo/app.h>
22#include <banjo/main.h>
23#include <banjo/bitmap.h>
24#include <banjo/draw.h>
25#include <banjo/event.h>
26#include <banjo/log.h>
27#include <banjo/memory.h>
28#include <banjo/pixel.h>
29#include <banjo/random.h>
30#include <banjo/system.h>
31#include <banjo/window.h>
32
33#define FB_PIXEL_MODE BJ_PIXEL_MODE_XRGB8888
34
35#define WINDOW_W 800
36#define WINDOW_H 600
37
38#define BORDER_W 25
39#define BORDER_H 15
40
41#define GRAPH_W (WINDOW_W - BORDER_W * 2)
42#define GRAPH_H (WINDOW_H - 100)
43
44#define N_DISTRIBUTIONS 3
45
46typedef struct {
47 const char* name;
48 size_t result[GRAPH_W];
49 uint32_t color;
50 size_t min_y; /* fixed to 0 for histograms */
51 size_t max_y; /* per-distribution */
52 size_t n_steps;
54
55size_t n_steps_base = 524288;
56
59static void init_distributions(void) {
60 distributions[0].name = "uniform: %ld draws in [0;Xmax[ ; y = how many x";
61 distributions[0].color = bj_pack_pixel_rgb(FB_PIXEL_MODE, 110, 231, 183);
62 distributions[0].min_y = 0;
63 distributions[0].max_y = 0;
64
65 distributions[1].name = "bernoulli: %ld draws with a probability p (x) ; y = how many hits";
66 distributions[1].color = bj_pack_pixel_rgb(FB_PIXEL_MODE, 147, 197, 253);
67 distributions[1].min_y = 0;
68 distributions[1].max_y = 0;
69
70 distributions[2].name = "normal: %ld draws with Xmax/2 (mean) and 100 (deviation) ; y = how many x";
71 distributions[2].color = bj_pack_pixel_rgb(FB_PIXEL_MODE, 196, 181, 253);
72 distributions[2].min_y = 0;
73 distributions[2].max_y = 0;
74}
75
76static void run_distributions() {
77
78 for (size_t d = 0; d < N_DISTRIBUTIONS; ++d) {
79 bj_memzero(distributions[d].result, sizeof(size_t) * GRAPH_W);
80 distributions[d].min_y = 0;
81 distributions[d].max_y = 0;
82 }
83
84 distributions[0].n_steps = n_steps_base;
85 distributions[1].n_steps = n_steps_base / 128;
86 distributions[2].n_steps = n_steps_base / 64;
87
88 // Uniform: the draw lands in any of the GRAPH_W buckets, so the
89 // histogram stays flat, however many draws.
90 for (size_t s = 0; s < distributions[0].n_steps; ++s) {
91 uint32_t x = bj_random_uniform_i32(bj_pcg32_generator, 0, 0, GRAPH_W - 1);
92 size_t y = ++distributions[0].result[x];
93 if (y > distributions[0].max_y) distributions[0].max_y = y;
94 }
95
96 // Bernoulli: one draw per column, with p = x / GRAPH_W, so the curve
97 // climbs linearly from never to always.
98 for (size_t px = 0; px < GRAPH_W; ++px) {
99 bj_real p = (bj_real)px / GRAPH_W;
100 for (size_t s = 0; s < distributions[1].n_steps; ++s) {
102 size_t y = ++distributions[1].result[px];
103 if (y > distributions[1].max_y) distributions[1].max_y = y;
104 }
105 }
106 }
107
108 // Normal: the draws gather around the mean, half the graph, with a
109 // deviation of 100; the ones off the graph are dropped.
110 const bj_real mean = BJ_F(0.5) * (bj_real)(GRAPH_W - 1);
111 const bj_real sd = BJ_F(100.);
112 for (size_t s = 0; s < distributions[2].n_steps; ++s) {
114 long x = (long)bj_round(v);
115 if (x < 0 || x >= (long)GRAPH_W) continue; /* avoid OOB */
116 size_t y = ++distributions[2].result[(size_t)x];
117 if (y > distributions[2].max_y) distributions[2].max_y = y;
118 }
119}
120
121static uint32_t darken_color(uint32_t pixel, double factor, bj_bitmap* bmp) {
122 uint8_t r, g, b;
123 bj_unpack_pixel_rgb((bj_pixel_mode)bj_bitmap_mode(bmp), pixel, &r, &g, &b);
124 r = (uint8_t)(r * factor);
125 g = (uint8_t)(g * factor);
126 b = (uint8_t)(b * factor);
127 return bj_pack_bitmap_rgb(bmp, r, g, b);
128}
129
130// The dots are the buckets of the histogram; the curve smooths them with a
131// moving average of 21 buckets.
132void draw(bj_bitmap* bmp) {
133 bj_clear_bitmap(bmp);
134
135 const uint32_t color_box = bj_pack_bitmap_rgb(bmp, 64, 72, 84);
136 const uint32_t white = bj_pack_bitmap_rgb(bmp, 224, 230, 238);
137
139 bj_draw_rectangle(bmp, &graph_box, color_box);
140
141 for (size_t d = 0; d < N_DISTRIBUTIONS; ++d) {
142 size_t min_y = distributions[d].min_y; /* 0 for histograms */
143 size_t max_y = distributions[d].max_y;
144 double scale = (max_y > min_y)
145 ? (double)(GRAPH_H - 1) / (double)(max_y - min_y)
146 : 0.0;
147
148 uint32_t color_curve = distributions[d].color;
149 uint32_t color_dots = darken_color(color_curve, 0.7, bmp);
150
151 /* dots */
152 for (int x = 0; x < GRAPH_W; ++x) {
153 size_t ycount = distributions[d].result[x];
154 int yscaled = (int)((ycount - min_y) * scale + 0.5);
155 if (yscaled < 0) yscaled = 0;
156 if (yscaled > GRAPH_H - 1) yscaled = GRAPH_H - 1;
157 int sx = graph_box.x + x;
158 int sy = WINDOW_H - BORDER_H - 1 - yscaled;
159 bj_put_pixel(bmp, sx, sy, color_dots);
160 }
161
162 /* moving-average curve */
163 const int W = 21;
164 int prev_set = 0, px = 0, py = 0;
165 for (int x = 0; x < GRAPH_W; ++x) {
166 int half = W / 2;
167 int xl = (x - half < 0) ? 0 : x - half;
168 int xr = (x + half >= GRAPH_W) ? GRAPH_W - 1 : x + half;
169 uint64_t sum = 0;
170 for (int i = xl; i <= xr; ++i) sum += distributions[d].result[i];
171 double avg = (double)sum / (double)(xr - xl + 1);
172 int yscaled = (int)((avg - (double)min_y) * scale + 0.5);
173 if (yscaled < 0) yscaled = 0;
174 if (yscaled > GRAPH_H - 1) yscaled = GRAPH_H - 1;
175 int sx = graph_box.x + x;
176 int sy = WINDOW_H - BORDER_H - 1 - yscaled;
177
178 if (prev_set) bj_draw_line(bmp, px, py, sx, sy, color_curve);
179 px = sx; py = sy; prev_set = 1;
180 }
181
182 /* legend */
183 int lx = BORDER_W, ly = 10 + 15 * (int)d;
184 bj_rect legend_rect = {(int16_t)lx, (int16_t)ly, 25, 8};
185 bj_draw_filled_rectangle(bmp, &legend_rect, distributions[d].color);
186 bj_draw_textf(bmp, 8, (struct bj_rect){lx + 30, ly, 0, 0}, BJ_TEXT_HALIGN_LEFT, BJ_TEXT_VALIGN_TOP, white, distributions[d].name, distributions[d].n_steps);
187 }
188 bj_draw_textf(bmp, 8, (struct bj_rect){BORDER_W, 10 + 15 * (N_DISTRIBUTIONS + 1), 0, 0}, BJ_TEXT_HALIGN_LEFT, BJ_TEXT_VALIGN_TOP, white, "Use Left/Right arrow keys to change number of draws.");
189
190}
191
192static void on_draw(
193 struct bj_window* w,
194 struct bj_render_target* target,
195 const struct bj_rect* dirty,
196 void* user_data
197) {
198 (void)w; (void)dirty; (void)user_data;
200 // Force the dark background clear colour every draw. Setting it on
201 // the window's framebuffer is cheap and keeps roll() ignorant of
202 // the framebuffer.
204 draw(fb);
205}
206
207static void roll(void) {
210}
211
212// Few draws are noise; many draws are the shape of the distribution. Left
213// halves them, Right doubles them.
214void key_callback(bj_window* p_window, const bj_key_event* e, void* data) {
215 (void)data;
216 if (e->action != BJ_EVENT_ACTION_RELEASE) return;
217
218 switch (e->key) {
219 case BJ_KEY_RETURN:
220 roll(); break;
221 case BJ_KEY_LEFT:
222 if (n_steps_base > 10) { n_steps_base /= 2; roll(); }
223 break;
224 case BJ_KEY_RIGHT:
225 if (n_steps_base < 0x20000000) { n_steps_base *= 2; roll(); }
226 break;
227 case BJ_KEY_ESCAPE:
229 break;
230 default: break;
231 }
232}
233
234static void* setup(struct bj_app* app, void* init_data) {
235 (void)init_data;
236
237 if (!bj_begin(BJ_VIDEO_SYSTEM, 0)) {
238 bj_quit_app(app, 1);
239 return 0;
240 }
241
242 window = bj_create_window("Random Distribution", 100, 100, WINDOW_W, WINDOW_H, 0, 0);
243
246
248 roll();
249 return 0;
250}
251
252static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
253 (void)tick;
254 (void)user_data;
257 bj_quit_app(app, 0);
258 }
259}
260
261static void teardown(struct bj_app* app, void* user_data) {
262 (void)user_data;
264 bj_end();
265}
266
267int main(int argc, char* argv[]) {
268 (void)argc; (void)argv;
269 return bj_run_app(setup, step, 0, teardown, 0);
270}
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
bj_audio_play_note_data data
Definition audio_pcm.c:104
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
#define WINDOW_W
Definition bitmap_blit.c:21
bj_window * window
Definition bitmap_blit.c:24
#define WINDOW_H
Definition bitmap_blit.c:22
Header file for Bitmap drawing functions.
void draw(bj_bitmap *bmp)
Definition drawing_2d.c:135
Sytem event management API.
void key_callback(bj_window *p_window, const bj_key_event *e, void *data)
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.
int bj_bitmap_mode(const struct bj_bitmap *bitmap)
Get the pixel mode of the given bitmap.
void bj_draw_textf(struct bj_bitmap *dst, unsigned height, struct bj_rect area, bj_text_halign halign, bj_text_valign valign, uint32_t fg_native, const char *fmt,...)
Prints formatted text into a bitmap, similar to printf.
void bj_set_bitmap_clear_color(struct bj_bitmap *bitmap, uint32_t color)
Set the colour bj_clear_bitmap fills with.
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.
void bj_put_pixel(struct bj_bitmap *bitmap, size_t x, size_t y, uint32_t value)
Change the pixel colour at given coordinate.
@ BJ_TEXT_HALIGN_LEFT
Align text to the left edge of the area.
Definition bitmap.h:718
@ BJ_TEXT_VALIGN_TOP
Align text to the top edge of the area.
Definition bitmap.h:727
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_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.
enum bj_event_action action
Action (press/release/repeat)
Definition event.h:456
enum bj_key key
Key identifier.
Definition event.h:454
void bj_dispatch_events(void)
Poll and dispatch all pending events.
bj_key_callback_fn bj_set_key_callback(bj_key_callback_fn callback, void *user_data)
Set the global callback for keyboard key events.
@ BJ_KEY_ESCAPE
Esc key.
Definition event.h:177
@ BJ_KEY_LEFT
Left arrow key.
Definition event.h:187
@ BJ_KEY_RIGHT
Right arrow key.
Definition event.h:189
@ BJ_KEY_RETURN
Enter key.
Definition event.h:164
@ BJ_EVENT_ACTION_RELEASE
The key or button was released.
Definition event.h:394
Represent a keyboard key event.
Definition event.h:453
int16_t x
X coordinate of the top-left corner (pixels, can be negative).
Definition rect.h:34
#define bj_round
Round to nearest integer.
Definition math.h:249
#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
void bj_memzero(void *dest, size_t mem_size)
Zero out mem_size bytes at dest.
void bj_unpack_pixel_rgb(enum bj_pixel_mode mode, uint32_t value, uint8_t *red, uint8_t *green, uint8_t *blue)
Unpacks the red, green, and blue components of a packed pixel value.
uint32_t bj_pack_pixel_rgb(enum bj_pixel_mode mode, uint8_t red, uint8_t green, uint8_t blue)
Packs red, green, and blue components into one opaque pixel value.
bj_pixel_mode
Representation of a pixel encoding.
Definition pixel.h:86
int bj_random_bernoulli(bj_random_u32_fn next, void *state, bj_real probability)
Bernoulli(probability).
#define bj_random_normal
Normal N(mean, stddev^2) for the active bj_real precision.
Definition random.h:419
static uint32_t bj_pcg32_generator(void *state)
Adapter for distribution API (void* state).
Definition random.h:185
int32_t bj_random_uniform_i32(bj_random_u32_fn next, void *state, int32_t low, int32_t high)
Uniform signed 32-bit integer in [low, high].
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.
void bj_set_window_should_close(struct bj_window *window)
Flag a window to be closed.
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.
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
All memory-related functions, including custom allocators.
#define FB_PIXEL_MODE
Header file for general pixel manipulation facilities.
#define W
Definition pong.c:116
Pseudo-random number generation API.
distribution distributions[3]
static uint32_t darken_color(uint32_t pixel, double factor, bj_bitmap *bmp)
#define N_DISTRIBUTIONS
static void roll(void)
#define BORDER_W
#define GRAPH_W
const char * name
#define GRAPH_H
#define BORDER_H
static void run_distributions()
size_t result[(800 - 25 *2)]
size_t n_steps_base
static void init_distributions(void)
Header file for system interactions.
int main(void)
Header file for bj_window type.