1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
#if 0 /*
#/ ================================================================
#/
#/ sinewave.c
#/
#/ ================================================================
#/
#/ Self-compilation shell script
#/
SRC=${0##*./}
BIN=${SRC%.*}
gcc \
-Wall -Wextra -Werror -pedantic \
-Wno-old-style-declaration \
-Wno-missing-braces \
-Wno-unused-variable \
-Wno-unused-but-set-variable \
-Wno-unused-parameter \
-Wno-overlength-strings \
-O3 \
-fsanitize=undefined,address,leak \
-lX11 -lm -lasound \
-o $BIN $SRC && \
./$BIN $@ && rm $BIN
exit $? # */
#endif
#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) {
p_handle_events();
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 = (Platform) {
.title = "Sine Wave",
.graceful_exit = 1,
};
f64 frequency = 440. * 6;
for (i64 i = 0; i < AUDIO_SAMPLE_RATE; ++i) {
f64 t = ((f64) i) / AUDIO_SAMPLE_RATE;
f64 x = sin(t * 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;
}
|