28#define FB_PIXEL_MODE BJ_PIXEL_MODE_XRGB8888
36#define GRAPH_W (WINDOW_W - BORDER_W * 2)
37#define GRAPH_H (WINDOW_H - 100)
39#define N_DISTRIBUTIONS 3
55 distributions[0].name =
"uniform: %ld draws in [0;Xmax[ ; y = how many x";
60 distributions[1].name =
"bernoulli: %ld draws with a probability p (x) ; y = how many hits";
65 distributions[2].name =
"normal: %ld draws with Xmax/2 (mean) and 100 (deviation) ; y = how many x";
100 for (
size_t px = 0; px <
GRAPH_W; ++px) {
122 if (x < 0 || x >= (
long)
GRAPH_W)
continue;
131 r = (uint8_t)(r * factor);
132 g = (uint8_t)(g * factor);
133 b = (uint8_t)(b * factor);
153 double scale = (max_y > min_y)
154 ? (
double)(
GRAPH_H - 1) / (
double)(max_y - min_y)
158 uint32_t color_dots =
darken_color(color_curve, 0.7, bmp);
161 for (
int x = 0; x <
GRAPH_W; ++x) {
163 int yscaled = (int)((ycount - min_y) * scale + 0.5);
164 if (yscaled < 0) yscaled = 0;
166 int sx = graph_box.
x + x;
173 int prev_set = 0, px = 0, py = 0;
174 for (
int x = 0; x <
GRAPH_W; ++x) {
176 int xl = (x - half < 0) ? 0 : x - half;
179 for (
int i = xl; i <= xr; ++i) sum +=
distributions[d].result[i];
180 double avg = (double)sum / (
double)(xr - xl + 1);
181 int yscaled = (int)((avg - (
double)min_y) * scale + 0.5);
182 if (yscaled < 0) yscaled = 0;
184 int sx = graph_box.
x + x;
187 if (prev_set)
bj_draw_line(bmp, px, py, sx, sy, color_curve);
188 px = sx; py = sy; prev_set = 1;
192 int lx =
BORDER_W, ly = 10 + 15 * (int)d;
193 bj_rect legend_rect = {(int16_t)lx, (int16_t)ly, 25, 8};
195 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);
207 (void)w; (void)dirty; (void)user_data;
244static void*
setup(
struct bj_app* app,
void* init_data) {
271static void teardown(
struct bj_app* app,
void* user_data) {
277int main(
int argc,
char* argv[]) {
278 (void)argc; (void)argv;
Application lifecycle: callback-driven setup, step, and teardown.
int main(int argc, char *argv[])
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.
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.
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.
int bj_bitmap_mode(struct bj_bitmap *bitmap)
Get the pixel mode of the given bitmap.
void bj_put_pixel(struct bj_bitmap *bitmap, size_t x, size_t y, uint32_t value)
Change the pixel colour at given coordinate.
void bj_set_bitmap_color(struct bj_bitmap *bitmap, uint32_t color, uint8_t roles)
Sets one or more colour properties of a bitmap.
@ BJ_BITMAP_CLEAR_COLOR
Clear/fill colour for bj_clear_bitmap()
@ 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_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_make_pixel_rgb(enum bj_pixel_mode mode, uint32_t value, uint8_t *red, uint8_t *green, uint8_t *blue)
Gets the RGB value of a pixel given its 32-bits representation.
uint32_t bj_get_pixel_value(enum bj_pixel_mode mode, uint8_t red, uint8_t green, uint8_t blue)
Returns an opaque value representing a pixel colour, given its RGB composition.
bj_pixel_mode
Representation of a pixel encoding.
static uint32_t bj_pcg32_generator(void *state)
Adapter for distribution API (void* state).
int32_t bj_uniform_int32_distribution(bj_random_u32_fn next, void *state, int32_t low, int32_t high)
Uniform 32-bit integer in [low, high].
int bj_bernoulli_distribution(bj_random_u32_fn next, void *state, bj_real probability)
Bernoulli(probability).
#define bj_normal_real_distribution
Alias to the real-typed normal distribution for the active precision.
bj_bool bj_begin(int systems, struct bj_error **error)
Initialises the system.
void bj_end(void)
De-initialises the system.
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.
void bj_set_window_should_close(struct bj_window *window)
Flag a given window to be closed.
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 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.