33#define FB_PIXEL_MODE BJ_PIXEL_MODE_XRGB8888
41#define GRAPH_W (WINDOW_W - BORDER_W * 2)
42#define GRAPH_H (WINDOW_H - 100)
44#define N_DISTRIBUTIONS 3
60 distributions[0].name =
"uniform: %ld draws in [0;Xmax[ ; y = how many x";
65 distributions[1].name =
"bernoulli: %ld draws with a probability p (x) ; y = how many hits";
70 distributions[2].name =
"normal: %ld draws with Xmax/2 (mean) and 100 (deviation) ; y = how many x";
98 for (
size_t px = 0; px <
GRAPH_W; ++px) {
115 if (x < 0 || x >= (
long)
GRAPH_W)
continue;
124 r = (uint8_t)(r * factor);
125 g = (uint8_t)(g * factor);
126 b = (uint8_t)(b * factor);
144 double scale = (max_y > min_y)
145 ? (
double)(
GRAPH_H - 1) / (
double)(max_y - min_y)
149 uint32_t color_dots =
darken_color(color_curve, 0.7, bmp);
152 for (
int x = 0; x <
GRAPH_W; ++x) {
154 int yscaled = (int)((ycount - min_y) * scale + 0.5);
155 if (yscaled < 0) yscaled = 0;
157 int sx = graph_box.
x + x;
164 int prev_set = 0, px = 0, py = 0;
165 for (
int x = 0; x <
GRAPH_W; ++x) {
167 int xl = (x - half < 0) ? 0 : x - half;
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;
175 int sx = graph_box.
x + x;
178 if (prev_set)
bj_draw_line(bmp, px, py, sx, sy, color_curve);
179 px = sx; py = sy; prev_set = 1;
183 int lx =
BORDER_W, ly = 10 + 15 * (int)d;
184 bj_rect legend_rect = {(int16_t)lx, (int16_t)ly, 25, 8};
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);
198 (void)w; (void)dirty; (void)user_data;
234static void*
setup(
struct bj_app* app,
void* init_data) {
261static void teardown(
struct bj_app* app,
void* user_data) {
267int main(
int argc,
char* argv[]) {
268 (void)argc; (void)argv;
Application lifecycle: callback-driven setup, step, and teardown.
static void step(struct bj_app *app, struct bj_tick_info tick, void *user_data)
static void teardown(struct bj_app *app, void *user_data)
bj_audio_play_note_data data
static void * setup(struct bj_app *app, void *init_data)
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)
Header file for Bitmap drawing functions.
void draw(bj_bitmap *bmp)
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.
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.
@ BJ_TEXT_VALIGN_TOP
Align text to the top edge of the area.
struct bj_render_target bj_render_target
struct bj_bitmap bj_bitmap
struct bj_window bj_window
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)
enum bj_key key
Key identifier.
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_LEFT
Left arrow key.
@ BJ_KEY_RIGHT
Right arrow key.
@ BJ_KEY_RETURN
Enter key.
@ BJ_EVENT_ACTION_RELEASE
The key or button was released.
Represent a keyboard key event.
int16_t x
X coordinate of the top-left corner (pixels, can be negative).
#define bj_round
Round to nearest integer.
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
float bj_real
Selected real type for float configuration.
Axis-aligned rectangle: a top-left corner plus a width and height.
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.
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.
static uint32_t bj_pcg32_generator(void *state)
Adapter for distribution API (void* state).
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.
static void bj_invalidate_window(struct bj_window *window)
Mark the whole window as needing a repaint.
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.
Header file for general pixel manipulation facilities.
Pseudo-random number generation API.
distribution distributions[3]
static uint32_t darken_color(uint32_t pixel, double factor, bj_bitmap *bmp)
static void run_distributions()
size_t result[(800 - 25 *2)]
static void init_distributions(void)
Header file for system interactions.
Header file for bj_window type.