summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/graph.c2
-rwxr-xr-xexamples/sinewave.c67
-rwxr-xr-xexamples/ui.c13
3 files changed, 59 insertions, 23 deletions
diff --git a/examples/graph.c b/examples/graph.c
index dc4b226..6ad5fdc 100755
--- a/examples/graph.c
+++ b/examples/graph.c
@@ -531,7 +531,7 @@ void update_and_render_frame(void) {
// Render
- fill_rectangle(OP_SET, 0xffffff, 0, 0, platform.frame_width, platform.frame_height);
+ fill_rectangle(OP_SET, 0xa0b0c0, 0, 0, platform.frame_width, platform.frame_height);
if (adding_edge) {
f64 x0 = world.nodes[adding_src].x;
diff --git a/examples/sinewave.c b/examples/sinewave.c
index 6beed29..a1f19f8 100755
--- a/examples/sinewave.c
+++ b/examples/sinewave.c
@@ -25,34 +25,71 @@ gcc \
exit $? # */
#endif
-#include "../reduced_system_layer.c"
+#include "../graphics.c"
+
+i64 time_0 = 0;
+i64 audio_samples = 0;
+
+f32 frames[AUDIO_SAMPLE_RATE * AUDIO_NUM_CHANNELS] = {0};
+
+b8 ui_button(f64 x, f64 y, f64 width, f64 height) {
+ b8 has_cursor = platform.cursor_x >= x && platform.cursor_x < x + width &&
+ platform.cursor_y >= y && platform.cursor_y < y + height;
+
+ b8 is_pressed = has_cursor && platform.key_down[BUTTON_LEFT];
+
+ if (is_pressed)
+ fill_rectangle(OP_SET, 0xffffff, x, y, width, height);
+ else if (has_cursor)
+ fill_rectangle(OP_SET, 0xa0a000, x, y, width, height);
+ else
+ fill_rectangle(OP_SET, 0x808030, x, y, width, height);
+
+ return has_cursor && platform.key_pressed[BUTTON_LEFT];
+}
void update_and_render_frame(void) {
- // Nothing
+ i32 num_events = p_handle_events();
+
+ if (num_events > 0) {
+ fill_rectangle(OP_SET, 0x202020, 0, 0, platform.frame_width, platform.frame_height);
+
+ if (ui_button(100, 100, 200, 200))
+ p_queue_sound(0, AUDIO_SAMPLE_RATE, frames);
+ }
+
+ i64 samples_elapsed = ((p_time() - time_0) * AUDIO_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;
- platform.graceful_exit = 1;
+ platform = (Platform) {
+ .title = "Sine Wave",
+ .graceful_exit = 1,
+ };
- f64 frequency = 440. * 4;
+ f64 frequency = 440.;
- f32 frames[AUDIO_SAMPLE_RATE * AUDIO_NUM_CHANNELS];
for (i64 i = 0; i < AUDIO_SAMPLE_RATE; ++i) {
f64 t = ((f64) i) / AUDIO_SAMPLE_RATE;
- f64 x = sin(t * frequency);
- if (t < .1)
- x *= t / .1;
- if (t > .7)
- x *= (1. - t) / .3;
- frames[i * 2] = (f32) x * .5;
- frames[i * 2 + 1] = (f32) x * .5;
+ 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);
}
- p_queue_sound(0, AUDIO_SAMPLE_RATE, frames);
- p_handle_audio(AUDIO_SAMPLE_RATE);
- p_cleanup();
+ time_0 = p_time();
+
+ p_event_loop();
return 0;
}
diff --git a/examples/ui.c b/examples/ui.c
index 1301a6c..79bdd70 100755
--- a/examples/ui.c
+++ b/examples/ui.c
@@ -87,7 +87,7 @@ void update_and_render_frame(void) {
for (i64 i = 0; i < platform.input_size; ++i)
if (platform.input[i].ctrl)
switch (platform.input[i].key) {
- case 'v': {
+ case KEY_V: {
if (selection != 0) {
i64 i0 = selection < 0 ? cursor + selection : cursor;
i64 i1 = selection < 0 ? cursor : cursor + selection;
@@ -112,7 +112,7 @@ void update_and_render_frame(void) {
}
} break;
- case 'x': {
+ case KEY_X: {
i64 len = 0;
static c8 buf[1024];
@@ -131,7 +131,7 @@ void update_and_render_frame(void) {
text_len -= i1 - i0;
} break;
- case 'c': {
+ case KEY_C: {
i64 len = 0;
static c8 buf[1024];
@@ -172,7 +172,7 @@ void update_and_render_frame(void) {
++cursor;
break;
- case '\b':
+ case KEY_BACKSPACE:
if (selection != 0) {
i64 i0 = selection < 0 ? cursor + selection : cursor;
i64 i1 = selection < 0 ? cursor : cursor + selection;
@@ -205,9 +205,8 @@ void update_and_render_frame(void) {
}
break;
- case '\n':
- case '\r':
- case '\t':
+ case KEY_ENTER:
+ case KEY_TAB:
platform.input[i].c = platform.input[i].key;
// fallthrough