Mercurial > hg > index.fcgi > lj > lj046-2players
diff src/pcsound.c @ 0:c84446dfb3f5
initial add
author | paulo@localhost |
---|---|
date | Fri, 13 Mar 2009 00:39:12 -0700 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/pcsound.c Fri Mar 13 00:39:12 2009 -0700 1.3 @@ -0,0 +1,108 @@ 1.4 +#include "ljpc.h" 1.5 +extern const DATAFILE *sound_dat; 1.6 +extern char withSound; 1.7 + 1.8 +static const unsigned short shiftPitches[16] = { 1.9 + 630, 667, 707, 749, 794, 841, 891, 944, 1.10 + 1000, 1059, 1122, 1189, 1260, 1335, 1424, 1498 1.11 +}; 1.12 + 1.13 +/** 1.14 + * Plays a sample from datafile by name, with the specified volume 1.15 + * and the specified rate scale factor, panned center, without loop. 1.16 + * @param filename name of object in datafile 1.17 + * @param vol scale factor for volume (0-255); if set to 0, 1.18 + * stops all voices playing the sample 1.19 + * @param pitch scale factor for playback rate (1000 = normal; 2000 = double speed) 1.20 + */ 1.21 +static void playPitchSample(const char *filename, int vol, int pitch) { 1.22 + if (withSound && sound_dat) { 1.23 + const DATAFILE *entry = find_datafile_object(sound_dat, filename); 1.24 + 1.25 + if (entry) { 1.26 + if (vol < 1) { 1.27 + stop_sample(entry->dat); 1.28 + } else { 1.29 + play_sample(entry->dat, vol, 128, pitch, 0); 1.30 + } 1.31 + } 1.32 + } 1.33 +} 1.34 + 1.35 +void ezPlaySample(const char *filename, int vol) { 1.36 + playPitchSample(filename, vol, 1000); 1.37 +} 1.38 + 1.39 +void playSampleForTetromino(int piece) { 1.40 + static const char tetrominoNames[] = "IJLOSTZ22V"; 1.41 + 1.42 + piece &= LJP_MASK; 1.43 + if (piece >= 0 && piece < 10) { 1.44 + char filename[32]; 1.45 + usprintf(filename, "next%c_wav", tetrominoNames[piece]); 1.46 + ezPlaySample(filename, 128); 1.47 + } 1.48 +} 1.49 + 1.50 +void playSoundEffects(LJView *v, LJBits sounds, int countdown) { 1.51 + // Handle sound 1.52 + if ((sounds & LJSND_SPAWN) 1.53 + && !(v->hideNext)) { 1.54 + playSampleForTetromino(v->field->curPiece[1]); 1.55 + } 1.56 + if (sounds & LJSND_HOLD) { 1.57 + ezPlaySample("hold_wav", 128); 1.58 + } 1.59 + if (sounds & LJSND_ROTATE) { 1.60 + ezPlaySample("rotate_wav", 128); 1.61 + } 1.62 + if (sounds & LJSND_IRS) { 1.63 + ezPlaySample("irs_wav", 128); 1.64 + } 1.65 + if (sounds & LJSND_SQUARE) { 1.66 + ezPlaySample("square_wav", 128); 1.67 + } 1.68 + if (sounds & LJSND_SHIFT) { 1.69 + int x = v->plat->skin->shiftScale 1.70 + ? v->field->x + 8 - (LJ_PF_WID - 4) / 2 1.71 + : 8; 1.72 + int pitch = shiftPitches[x]; 1.73 + playPitchSample("shift_wav", 128, pitch); 1.74 + } 1.75 + if (sounds & LJSND_LAND) { 1.76 + ezPlaySample("land_wav", 192); 1.77 + } 1.78 + if (sounds & LJSND_LOCK) { 1.79 + ezPlaySample("lock_wav", 192); 1.80 + } 1.81 + if (sounds & LJSND_LINE) { 1.82 + ezPlaySample("line_wav", 128); 1.83 + } 1.84 + if (sounds & LJSND_SECTIONUP) { 1.85 + ezPlaySample("sectionup_wav", 128); 1.86 + } 1.87 + 1.88 + if (v->plat->b2bcd1 > 0) { 1.89 + if (--v->plat->b2bcd1 == 0) { 1.90 + playPitchSample("line_wav", 148, 1200); 1.91 + } 1.92 + } 1.93 + if (sounds & LJSND_SETB2B) { 1.94 + v->plat->b2bcd1 = 10; 1.95 + } 1.96 + if (v->plat->b2bcd2 > 0) { 1.97 + if (--v->plat->b2bcd2 == 0) { 1.98 + playPitchSample("line_wav", 168, 1440); 1.99 + } 1.100 + } 1.101 + if (sounds & LJSND_B2B) { 1.102 + v->plat->b2bcd2 = 20; 1.103 + } 1.104 + if (countdown < v->control->countdown) { 1.105 + char name[32]; 1.106 + usprintf(name, "count_%d_wav", countdown); 1.107 + ezPlaySample(name, 128); 1.108 + } 1.109 + LJMusic_poll(v->plat->skin->bgm); 1.110 +} 1.111 +