Skip to content

Commit fa7aaa3

Browse files
committed
Updated pong to use new twr_audio_from functions and increased volume of ball bounce noise
1 parent ccb590a commit fa7aaa3

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

examples/pong/extra.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ float* generate_square_wave(double frequency, double duration, long sample_rate)
1515

1616
long load_square_wave(double frequency, double duration, long sample_rate) {
1717
float* wave = generate_square_wave(frequency, duration, sample_rate);
18-
long node_id = twr_audio_from_samples(1, sample_rate, wave, (long)ceil(duration * sample_rate));
18+
long node_id = twr_audio_from_float_pcm(1, sample_rate, wave, (long)ceil(duration * sample_rate));
1919
free(wave);
2020
return node_id;
2121
}

examples/pong/pong.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ void Pong::tick(long time) {
261261

262262
this->run_time += delta;
263263
}
264+
const double BALL_BOUNCE_VOL = 2.0;
264265
void Pong::tickBall(long delta) {
265266
double prev_y = this->ball_y;
266267
this->ball_x += this->ball_velocity_x * delta;
@@ -270,18 +271,18 @@ void Pong::tickBall(long delta) {
270271
if (this->ball_x <= this->border_width) { //left wall
271272
this->ball_x = this->border_width;
272273
this->ball_velocity_x *= -1;
273-
twr_audio_play(this->bounce_noise);
274+
twr_audio_play_volume(this->bounce_noise, BALL_BOUNCE_VOL, 0.0);
274275
} else if (this->ball_x >= this->width - this->ball_size - this->border_width) { //right wall
275276
this->ball_x = this->width - this->ball_size - this->border_width;
276277
this->ball_velocity_x *= -1;
277-
twr_audio_play(this->bounce_noise);
278+
twr_audio_play_volume(this->bounce_noise, BALL_BOUNCE_VOL, 0.0);
278279
}
279280

280281
//x and y are seperate checks for the corner case
281282
if (this->ball_y <= border_width) { //top wall
282283
this->ball_y = this->border_width;
283284
this->ball_velocity_y *= -1;
284-
twr_audio_play(this->bounce_noise);
285+
twr_audio_play_volume(this->bounce_noise, BALL_BOUNCE_VOL, 0.0);
285286
} else if (this->ball_y >= this->height - this->ball_size - this->border_width) { //bottom wall, lost game
286287
this->ball_y = this->height - this->ball_size - this->border_width - 1.0;
287288
twr_audio_play(this->lose_noise);
@@ -324,7 +325,7 @@ void Pong::tickBall(long delta) {
324325
//set score time
325326
this->score_time = this->last_timestamp;
326327
}
327-
twr_audio_play(this->bounce_noise);
328+
twr_audio_play_volume(this->bounce_noise, BALL_BOUNCE_VOL, 0.0);
328329
}
329330
}
330331
void Pong::tickPaddle(long delta) {

examples/pong/two-player-pong.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ T better_abs(T val) {
336336
}
337337

338338

339+
const double BALL_BOUNCE_VOL = 2.0;
339340
void paddleCollision(Ball& ball, double& n_x, double& n_y, Paddle& paddle, double paddle_x, long bounce_noise){
340341

341342
double paddle_middle = paddle.y + PADDLE_HEIGHT/2.0;
@@ -364,7 +365,7 @@ void paddleCollision(Ball& ball, double& n_x, double& n_y, Paddle& paddle, doubl
364365
ball.v_y = better_abs(ball.v_y);
365366
}
366367

367-
twr_audio_play(bounce_noise);
368+
twr_audio_play_volume(bounce_noise, BALL_BOUNCE_VOL, 0.0);
368369

369370
//add paddle velocity to ball
370371
double paddle_vel = get_paddle_vel(paddle);
@@ -396,15 +397,15 @@ void TwoPlayerPong::updateBall(double delta) {
396397
//interpolate to add the extra y back in th eopposite direction
397398
n_y = 0 - n_y;
398399
this->ball.v_y *= -1;
399-
twr_audio_play(this->bounce_noise);
400+
twr_audio_play_volume(this->bounce_noise, BALL_BOUNCE_VOL, 0.0);
400401
} else if (n_y > max_y) {
401402
//bounce off bottom wall by flipping y direction
402403
//interpolate to add the extra y back in the opposite direction
403404

404405
//max_y - (n_y - max_y) = max_y + max_y - n_y = 2*max_y - n_y
405406
n_y = 2*max_y - n_y;
406407
this->ball.v_y *= -1;
407-
twr_audio_play(this->bounce_noise);
408+
twr_audio_play_volume(this->bounce_noise, BALL_BOUNCE_VOL, 0.0);
408409
}
409410

410411
if (n_x < 0) { //hit left

0 commit comments

Comments
 (0)