#include "../graphics.c" i64 time_0 = 0; i64 audio_samples = 0; f32 frames[SOUND_SAMPLE_RATE * NUM_SOUND_CHANNELS] = {0}; b8 ui_button(f64 x, f64 y, f64 width, f64 height) { b8 has_cursor = g_platform.cursor_x >= x && g_platform.cursor_x < x + width && g_platform.cursor_y >= y && g_platform.cursor_y < y + height; b8 is_pressed = has_cursor && g_platform.key_down[BUTTON_LEFT]; if (is_pressed) fill_rectangle(RGB(1.f, 1.f, 1.f), x, y, width, height); else if (has_cursor) fill_rectangle(RGB(.8f, .8f, 0.f), x, y, width, height); else fill_rectangle(RGB(.8f, .8f, .2f), x, y, width, height); return has_cursor && g_platform.key_pressed[BUTTON_LEFT]; } void update_and_render_frame(void) { i32 num_events = p_handle_events(); if (num_events > 0) { fill_rectangle(RGB(.1f, .1f, .1f), 0, 0, g_platform.frame_width, g_platform.frame_height); if (ui_button(100, 100, 200, 200)) p_queue_sound(0, SOUND_SAMPLE_RATE, frames); } i64 samples_elapsed = ((p_time() - time_0) * SOUND_SAMPLE_RATE) / 1000 - audio_samples; audio_samples += samples_elapsed; p_handle_audio(samples_elapsed); p_render_frame(); p_sleep_for(0); } i32 main(i32 argc, c8 **argv) { (void) argc; (void) argv; g_platform = (Platform) { .title = "Sine Wave", .graceful_exit = 1, }; f64 frequency = 440.; for (i64 i = 0; i < SOUND_SAMPLE_RATE; ++i) { f64 t = ((f64) i) / SOUND_SAMPLE_RATE; f64 x = sin(t * (M_PI * 2.) * frequency); if (t < .005) x *= t / .005; if (t > .1) x *= (1. - t) / .9; frames[i * 2] = (f32) (x * .5); frames[i * 2 + 1] = (f32) (x * .5); } time_0 = p_time(); p_event_loop(); return 0; }