From 4e79f7dda8956a80e3d537070f223dffcc0784f7 Mon Sep 17 00:00:00 2001 From: Thomas Perl Date: Tue, 4 Aug 2009 17:03:24 +0200 Subject: [PATCH] Split sound event tracking into RenderState This way, the gamestate does not have to be changed from within render() when playing sounds. --- game.c | 38 +++++++++++++++++++++----------------- game.h | 14 ++++++++++++-- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/game.c b/game.c index 0e24d89..84c3606 100644 --- a/game.c +++ b/game.c @@ -56,7 +56,7 @@ GameState *gamestate_new() { 0, WINNER_NONE, GR_COUNT, - 0, /* sound events */ + SOUND_EVENT_NONE, /* sound events */ 0, 0, false, @@ -180,6 +180,7 @@ void gameloop(GameState *s) { Uint32 accumulator = 0; bool quit = false; int p; + RenderState r = { s->sound_events }; #ifdef ENABLE_FPS_LIMIT Uint32 ft, frames; /* frame timer and frames */ @@ -229,7 +230,7 @@ void gameloop(GameState *s) { frames++; #endif - render(s); + render(s, &r); } for (p=1; p<=MAXPLAYERS; p++) { @@ -258,7 +259,7 @@ bool step( GameState* s) { /* bounce from the ground */ if (fabsf(s->ball.move_z) > 0.3) { - s->sound_events |= SOUND_EVENT_GROUND; + s->sound_events ^= SOUND_EVENT_GROUND; sample_volume_group(SOUND_GROUND_FIRST, SOUND_GROUND_LAST, fmaxf(0.0, fminf(1.0, fabsf(s->ball.move_z)/2))); pan_sample_group(SOUND_GROUND_FIRST, SOUND_GROUND_LAST, fmaxf(0.0, fminf(1.0, (s->ball.x)/WIDTH))); s->ball.move_z *= -s->ball.restitution; @@ -287,24 +288,24 @@ bool step( GameState* s) { if (NET_COLLISION_BALL(s->ball)) { /* the ball "fell" into the net */ s->score_event = SCORE_EVENT_NET; - s->sound_events |= SOUND_EVENT_OUT; + s->sound_events ^= SOUND_EVENT_OUT; s->status = "net!"; } else if (IS_OFFSCREEN(s->ball.x, s->ball.y)) { /* ball flew offscreen */ s->score_event = SCORE_EVENT_OFFSCREEN; - s->sound_events |= SOUND_EVENT_OUT; + s->sound_events ^= SOUND_EVENT_OUT; s->status = "out!"; } else if (ground_event) { /* the ball hit the ground on the screen */ if (IS_OUT(s->ball.x, s->ball.y)) { /* the ball bounced in the OUT area */ s->score_event = SCORE_EVENT_OUT; - s->sound_events |= SOUND_EVENT_OUT; + s->sound_events ^= SOUND_EVENT_OUT; s->status = "out!"; } else if (GROUND_IS_VALID(s->ball.last_hit_by, s->ball.x, s->ball.y)) { if (s->ball.ground_hit) { s->score_event = SCORE_EVENT_GROUND_VALID; - s->sound_events |= SOUND_EVENT_OUT; + s->sound_events ^= SOUND_EVENT_OUT; s->status = "did not catch the ball!"; } else { /* first ground hit in valid area */ @@ -313,7 +314,7 @@ bool step( GameState* s) { } else { /* ball hit somewhere invalid */ s->score_event = SCORE_EVENT_GROUND_INVALID; - s->sound_events |= SOUND_EVENT_OUT; + s->sound_events ^= SOUND_EVENT_OUT; s->status = "fault!"; } } @@ -348,7 +349,7 @@ bool step( GameState* s) { game_setup_serve(s); if (s->location->max_visitors > 100) { - s->sound_events |= SOUND_EVENT_APPLAUSE; + s->sound_events ^= SOUND_EVENT_APPLAUSE; } } } else { @@ -379,7 +380,7 @@ bool step( GameState* s) { break; } s->ball.move_y = get_move_y( s, p); - s->sound_events |= SOUND_EVENT_RACKET; + s->sound_events ^= SOUND_EVENT_RACKET; s->ball.ground_hit = false; s->ball.inhibit_gravity = false; s->ball.last_hit_by = p; @@ -460,28 +461,31 @@ bool step( GameState* s) { return false; } -void render( GameState* s) { +void render(GameState* s, RenderState* r) { int x, y, b; unsigned int i; float zoom; float rotate; int t=1000; + soundevent_t sounds; - if (s->sound_events != 0) { - if (s->sound_events & SOUND_EVENT_GROUND) { + /* The bits in sound_events flip when the sound should play */ + if (sounds = (r->sound_events ^ s->sound_events)) { + if (sounds & SOUND_EVENT_GROUND) { play_sample(SOUND_GROUND); } - if (s->sound_events & SOUND_EVENT_OUT) { + if (sounds & SOUND_EVENT_OUT) { play_sample(SOUND_OUT); } - if (s->sound_events & SOUND_EVENT_APPLAUSE) { + if (sounds & SOUND_EVENT_APPLAUSE) { play_sample(SOUND_APPLAUSE); } - if (s->sound_events & SOUND_EVENT_RACKET) { + if (sounds & SOUND_EVENT_RACKET) { play_sample(SOUND_RACKET); } - s->sound_events = 0; + r->sound_events = s->sound_events; } + if( s->winner != WINNER_NONE) { clear_screen(); rectangle(0, 0, WIDTH, HEIGHT, 80, 80, 80); diff --git a/game.h b/game.h index ae340de..8d582c5 100644 --- a/game.h +++ b/game.h @@ -61,12 +61,18 @@ enum { }; enum { + SOUND_EVENT_NONE = 0<<0, SOUND_EVENT_GROUND = 1<<0, SOUND_EVENT_OUT = 1<<1, SOUND_EVENT_APPLAUSE = 1<<2, SOUND_EVENT_RACKET = 1<<3 }; +#define SOUND_EVENT_COUNT 4 + +typedef unsigned char soundevent_t; + + typedef struct { const char* name; const char* area; @@ -150,7 +156,7 @@ typedef struct { unsigned int current_set; int winner; image_id displayed_court_type; - unsigned int sound_events; + soundevent_t sound_events; unsigned int rain; unsigned int fog; bool night; @@ -161,6 +167,10 @@ typedef struct { unsigned int score_time; } GameState; +typedef struct { + soundevent_t sound_events; +} RenderState; + #define PI 3.1415 #define GAME_TICKS 15 @@ -251,7 +261,7 @@ GameState* gamestate_load(const char* filename); /* Game module functions */ void gameloop(GameState*); -void render( GameState*); +void render( GameState*, RenderState*); bool step( GameState*); void limit_value( float*, float, float); float get_move_y( GameState*, unsigned char); -- 2.11.4.GIT