Skip to content

Commit b4ff514

Browse files
committed
Add music to vgc
1 parent bf1bc64 commit b4ff514

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

src/programs/vgc/vgc.c

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "timer.h"
2121
#include "io.h"
2222
#include "beep.h"
23+
#include "random.h"
2324

2425
static void draw_circle(unsigned char* buffer, int cx, int cy, int radius, int color, int width) {
2526
int x = 0;
@@ -35,6 +36,7 @@ static void draw_circle(unsigned char* buffer, int cx, int cy, int radius, int c
3536
buffer[(cy - y) * width + i] = color;
3637
}
3738
}
39+
3840
for (int i = cx - y; i <= cx + y; i++) {
3941
if (i >= 0 && i < width && cy + x >= 0 && cy + x < 200) {
4042
buffer[(cy + x) * width + i] = color;
@@ -64,8 +66,8 @@ void vgc(const char** unused) {
6466
int radius = 30;
6567
int color = 1;
6668

67-
int dx = 2;
68-
int dy = 2;
69+
int dx = 2;
70+
int dy = 2;
6971

7072
int screen_width = 320;
7173
int screen_height = 200;
@@ -95,21 +97,45 @@ void vgc(const char** unused) {
9597
intro_radius -= 5;
9698

9799
if (intro_radius <= target_radius) {
98-
intro_radius = target_radius;
99-
shrink_complete = 1;
100+
intro_radius = target_radius;
101+
shrink_complete = 1;
100102
}
101103

102104
int hz = 2000 - (250 - intro_radius) * 5;
103105
if (hz < 100) hz = 100;
104106
beep(hz, 100);
105107
}
106108

109+
int notes[] = {
110+
293, 329, 349, 55, 329, 220, 261, 55, 220, 329, 329, 55,
111+
};
112+
113+
int num_notes = sizeof(notes) / sizeof(notes[0]);
114+
int note_index = 0;
115+
int note_progress = 0;
116+
117+
int shake_x = 0;
118+
int shake_y = 0;
119+
int allow_shake = 1;
120+
107121
while (1) {
108122
for (int i = 0; i < screen_width * screen_height; i++) {
109123
back_buffer[i] = 0;
110124
}
111125

112-
draw_circle(back_buffer, cx, cy, radius, color, screen_width);
126+
int current_note = notes[note_index];
127+
128+
if (current_note == 55 && allow_shake) {
129+
random();
130+
shake_x = (int)(random_get() % 11) - 5;
131+
random();
132+
shake_y = (int)(random_get() % 11) - 5;
133+
} else {
134+
shake_x = 0;
135+
shake_y = 0;
136+
}
137+
138+
draw_circle(back_buffer, cx + shake_x, cy + shake_y, radius, color, screen_width);
113139

114140
unsigned char* vga = (unsigned char*)0xA0000;
115141
for (int i = 0; i < screen_width * screen_height; i++) {
@@ -125,31 +151,38 @@ void vgc(const char** unused) {
125151
cx = radius;
126152
dx = -dx;
127153
color_change = 1;
128-
beep(500, 20);
154+
allow_shake = 0;
129155
} else if (cx + radius >= screen_width) {
130156
cx = screen_width - radius;
131157
dx = -dx;
132158
color_change = 1;
133-
beep(500, 20);
134-
}
135-
136-
if (cy - radius <= 0) {
159+
allow_shake = 0;
160+
} else if (cy - radius <= 0) {
137161
cy = radius;
138162
dy = -dy;
139163
color_change = 1;
140-
beep(500, 20);
164+
allow_shake = 0;
141165
} else if (cy + radius >= screen_height) {
142166
cy = screen_height - radius;
143167
dy = -dy;
144168
color_change = 1;
145-
beep(500, 20);
169+
allow_shake = 0;
146170
}
147171

148172
if (color_change) {
149-
color = (color % 15) + 1;
173+
color = (color % 15) + 1;
174+
}
175+
176+
beep(current_note, 20);
177+
note_progress++;
178+
179+
if (note_progress >= 10) {
180+
note_progress = 0;
181+
note_index = (note_index + 1) % num_notes;
182+
183+
if (notes[note_index] == 55) {
184+
allow_shake = 1;
185+
}
150186
}
151-
152-
delay(20);
153187
}
154188
}
155-

0 commit comments

Comments
 (0)