diff --git a/audio.h b/audio.h index 4ef3828..7dbb13a 100644 --- a/audio.h +++ b/audio.h @@ -18,24 +18,32 @@ struct { uint32_t frame; uint8_t soundPlayed; - uint16_t soundPlayedSample; + uint16_t soundPlayedFrame; uint32_t noise; +uint8_t crashSample; + int engineIntensity; int engineOsc; int engineInc; + } LCR_audio; + + + void LCR_audioInit(void) { LCR_LOG0("initializing audio"); LCR_audio.frame = 0; LCR_audio.soundPlayed = LCR_SOUND_NONE; - LCR_audio.soundPlayedSample = 0; + LCR_audio.soundPlayedFrame = 0; LCR_audio.noise = 0; +LCR_audio.crashSample = 0; + LCR_audio.engineOsc = 0; LCR_audio.engineInc = 1; @@ -54,7 +62,7 @@ void LCR_audioPlaySound(uint8_t sound) { LCR_LOG2("playing sound"); LCR_audio.soundPlayed = sound; - LCR_audio.soundPlayedSample = 0; + LCR_audio.soundPlayedFrame = 0; } uint8_t _LCR_audioNoise(void) @@ -63,60 +71,60 @@ uint8_t _LCR_audioNoise(void) return LCR_audio.noise >> 16; } - uint8_t LCR_audioGetNextSample(void) { unsigned char result = 128; -switch (LCR_audio.soundPlayed) -{ - case LCR_SOUND_CRASH_SMALL: - result += (LCR_AUDIO_CRASH_LEN - LCR_audio.soundPlayedSample) * - (_LCR_audioNoise() / 16) / LCR_AUDIO_CRASH_LEN; + switch (LCR_audio.soundPlayed) + { + case LCR_SOUND_CRASH_SMALL: + case LCR_SOUND_CRASH_BIG: + { + int limit = (LCR_audio.soundPlayed == LCR_SOUND_CRASH_BIG ? 256 : 128) - + (LCR_audio.soundPlayedFrame * 256) / LCR_AUDIO_CRASH_LEN; - if (LCR_audio.soundPlayedSample >= LCR_AUDIO_CRASH_LEN) - LCR_audio.soundPlayed = LCR_SOUND_NONE; + if (LCR_audio.frame % 2 || // lower frequency + LCR_audio.soundPlayedFrame < LCR_AUDIO_CRASH_LEN / 4) + LCR_audio.crashSample = _LCR_audioNoise(); - break; + if (LCR_audio.crashSample > limit) + LCR_audio.crashSample = (_LCR_audioNoise() % 2) ? 0 : limit; - case LCR_SOUND_CRASH_BIG: - result += ((LCR_AUDIO_CRASH_LEN * 2) - LCR_audio.soundPlayedSample) * - (_LCR_audioNoise() / 8) / (2 * LCR_AUDIO_CRASH_LEN); + result += LCR_audio.crashSample - limit / 2; - if (LCR_audio.soundPlayedSample >= 2 * LCR_AUDIO_CRASH_LEN) - LCR_audio.soundPlayed = LCR_SOUND_NONE; + if (limit == 0) + LCR_audio.soundPlayed = LCR_SOUND_NONE; - break; + break; + } - default: - break; -} + default: + break; + } -if (LCR_audio.soundPlayed != LCR_SOUND_NONE) - LCR_audio.soundPlayedSample++; -else -{ - LCR_audio.engineOsc += - LCR_audio.engineInc ? - (((_LCR_audioNoise() % 256) < - (10 + LCR_audio.engineIntensity))) : -31; + if (LCR_audio.soundPlayed != LCR_SOUND_NONE) + LCR_audio.soundPlayedFrame++; + else + { + LCR_audio.engineOsc += + LCR_audio.engineInc ? + (((_LCR_audioNoise() % 256) < + (10 + LCR_audio.engineIntensity))) : -31; - if (LCR_audio.engineInc && LCR_audio.engineOsc > - (90 + (LCR_audio.engineIntensity / 8))) - LCR_audio.engineInc = 0; - else if ((!LCR_audio.engineInc) && LCR_audio.engineOsc < 10) - LCR_audio.engineInc = 1; + if (LCR_audio.engineInc && LCR_audio.engineOsc > + (90 + (LCR_audio.engineIntensity / 8))) + LCR_audio.engineInc = 0; + else if ((!LCR_audio.engineInc) && LCR_audio.engineOsc < 10) + LCR_audio.engineInc = 1; - result += - LCR_audio.engineIntensity < 20 ? - LCR_audio.engineOsc / 2 : LCR_audio.engineOsc; -} + result += + LCR_audio.engineIntensity < 20 ? + LCR_audio.engineOsc / 2 : LCR_audio.engineOsc; + } LCR_audio.frame++; return result; } - - #endif // guard diff --git a/frontend_sdl.c b/frontend_sdl.c index 4388d3f..3b38ce0 100644 --- a/frontend_sdl.c +++ b/frontend_sdl.c @@ -16,23 +16,14 @@ FILE *musicFile = 0; void audioFillCallback(void *userdata, uint8_t *s, int l) { - for (int i = 0; i < l; ++i) + if (musicFile) { - unsigned char byte = 128; - - if (musicFile) - { - if (!fread(&byte,1,1,musicFile)) - rewind(musicFile); - } - -// s[i] = byte; - -s[i] = byte / 2 + LCR_gameGetNextAudioSample() / 2; -//s[i] = LCR_gameGetNextAudioSample(); - - + if (!fread(s,1,l,musicFile)) + rewind(musicFile); } + + for (int i = 0; i < l; ++i) + s[i] = s[i] / 2 + LCR_gameGetNextAudioSample() / 2; } const uint8_t *keyboardState; @@ -97,7 +88,7 @@ int main(int argc, char *argv[]) audioSpec.freq = 8000; audioSpec.format = AUDIO_U8; audioSpec.channels = 1; - audioSpec.samples = 256; + audioSpec.samples = 64; if (SDL_OpenAudio(&audioSpec,NULL) < 0) fputs("could not initialize audio",stderr); diff --git a/racing.h b/racing.h index ba46ed6..27ca156 100644 --- a/racing.h +++ b/racing.h @@ -45,7 +45,10 @@ typedef int32_t LCR_GameUnit; ///< abstract game unit #define LCR_CAR_DRIFT_THRESHOLD_1 (LCR_GAME_UNIT / 4) #define LCR_CAR_DRIFT_THRESHOLD_0 (LCR_GAME_UNIT / 200) -#define LCR_CAR_CRASH_SPEED_THRESHOLD 25 + +#define LCR_CAR_CRASH_SPEED_DIFF 25 +#define LCR_CAR_CRASH_SPEED_THRESHOLD 70 + // multipliers (in 8ths) of friction and acceleration on concrete: #define LCR_CAR_GRASS_FACTOR 5 @@ -1077,7 +1080,9 @@ uint32_t LCR_racingStep(unsigned int input) TPE_worldStep(&(LCR_racing.physicsWorld)); LCR_LOG2("stepping physics engine done"); - int speedDiff = LCR_racing.carSpeeds[0] - LCR_racing.carSpeeds[1]; + int speedDiff = + TPE_abs(LCR_racing.carSpeeds[0]) - + TPE_abs(LCR_racing.carSpeeds[1]); LCR_racing.carSpeeds[1] = LCR_racing.carSpeeds[0]; @@ -1087,14 +1092,11 @@ uint32_t LCR_racingStep(unsigned int input) if (TPE_vec3Dot(carVel,carForw) < 0) LCR_racing.carSpeeds[0] *= -1; - else if (speedDiff < -1 * LCR_CAR_CRASH_SPEED_THRESHOLD) - { - result |= (speedDiff < -2 * LCR_CAR_CRASH_SPEED_THRESHOLD) ? + if (speedDiff < -1 * LCR_CAR_CRASH_SPEED_DIFF && + TPE_abs(LCR_racing.carSpeeds[0]) <= LCR_CAR_CRASH_SPEED_THRESHOLD) + result |= (speedDiff < -2 * LCR_CAR_CRASH_SPEED_DIFF) ? LCR_RACING_EVENT_CRASH_BIG : LCR_RACING_EVENT_CRASH_SMALL; - LCR_racing.carSpeeds[1] = 0; // prevent several crash events in a row - } - _LCR_racingUpdateCarPosRot(); TPE_Unit angle = TPE_vec3Dot(carUp,TPE_vec3Normalized(TPE_vec3Minus(