home *** CD-ROM | disk | FTP | other *** search
/ WarCraft 2000 - Nuclear Epidemic / W2000.nrg / SOURCE.War2000 / GameSound.cpp < prev    next >
C/C++ Source or Header  |  1998-09-21  |  5KB  |  233 lines

  1. #include "ddini.h"
  2. #include "ResFile.h"
  3. #include "FastDraw.h"
  4. #include "mgraph.h"
  5. #include "mouse.h"
  6. #include "menu.h"
  7. #include "MapDiscr.h"
  8. #include "multipl.h"
  9. #include "fog.h"
  10. #include "walls.h"
  11. #include "Nature.h"
  12. #include <time.h>
  13. #include "Nucl.h"
  14. #include "TopZones.h"
  15. #include "Megapolis.h"
  16. #include "dialogs.h"
  17. #include <assert.h>
  18. #include "MapEdit.h"
  19. #include "3DSurf.h"
  20. #include "CWave.h"
  21. #include "CDirSnd.h"
  22. #define MaxSnd 512
  23. extern int WarSound;
  24. extern int WorkSound;
  25. extern int OrderSound;
  26. extern int MidiSound;
  27. static bool SoundOK;
  28. CDirSound* CDS;
  29. char* SoundID[MaxSnd];
  30. word SndTable[MaxSnd][15];
  31. word NSnd[MaxSnd];
  32. word NSounds;
  33. void Errs(LPCSTR s)
  34. {
  35.     MessageBox(hwnd,s,"Sound loading failed...",MB_ICONWARNING|MB_OK);
  36.     assert(false);
  37. };
  38. extern short randoma[8192];
  39. int srpos;
  40. int srando(){
  41.     srpos++;
  42.     srpos&=8191;
  43.     return randoma[srpos];
  44. };
  45. bool IsSound;
  46. void normstr(char* str);
  47. void NLine(FILE* f);
  48. void LoadSounds(char* fn){
  49.     char sss[128];
  50.     char idn[128];
  51.     int srpos=0;
  52.     int nsn,z;
  53.     NSounds=0;
  54.     SoundOK=CDS->DirectSoundOK();
  55.     FILE* f1=fopen(fn,"rt");
  56.     if(!IOresult()){
  57.       do{
  58. uuux:
  59.         z=fscanf(f1,"%s",idn);
  60.         if(idn[0]=='/'){
  61.             NLine(f1);
  62.             goto uuux;
  63.         };
  64.         z=fscanf(f1,"%d",&nsn);
  65.         NSnd[NSounds]=nsn;
  66.         SoundID[NSounds]=new char[strlen(idn)+1];
  67.         strcpy(SoundID[NSounds],idn);
  68.         if(z!=EOF){
  69.             NLine(f1);
  70.             for(int i=0;i<nsn;i++){
  71. uuu:
  72.                 z=fscanf(f1,"%s",sss);
  73.                 if(sss[0]=='/'){
  74.                     NLine(f1);
  75.                     goto uuu;
  76.                 };
  77.                 normstr(sss);
  78.                 if(SoundOK){
  79.                     CWave CW(sss);
  80.                     int sdid;
  81.                     if(CW.WaveOK()){
  82.                         sdid=CDS->CreateSoundBuffer(&CW);
  83.                         if(CDS->DirectSoundOK()){
  84.                             CDS->CopyWaveToBuffer(&CW,sdid);
  85.                         }else{
  86.                             sprintf(idn,"Could not create sound buffer : %s",sss);
  87.                             Errs(idn);
  88.                         };
  89.                     }else{
  90.                         sprintf(idn,"Could not load sound : %s",sss);
  91.                         Errs(idn);
  92.                     };
  93.                     SndTable[NSounds][i]=sdid;
  94.                 };
  95.             };
  96.         };
  97.         NSounds++;
  98.       }while (z!=EOF);
  99.     }else{
  100.         sprintf(sss,"Could not open sounds list : %s",fn);
  101.         Errs(sss);
  102.     };
  103.     //NSounds=0;
  104.     fclose(f1);
  105.     //if(!CDS->DirectSoundOK()){
  106.     //    NSounds=0;
  107.     //};
  108.     
  109. };
  110. void PlayEffect(int n,int pan,int vol){
  111.     if(!SoundOK)return;
  112.     if(n<NSounds){
  113.         if(NSnd[n]>0){
  114.             int maxsnd=NSnd[n];
  115.             int u=maxsnd;
  116.             int nnn=(srando()*maxsnd)>>15;
  117.             bool sndmade=true;
  118.             do{
  119.                 int sid=SndTable[n][nnn];
  120.                 int poss=CDS->GetPos(sid);
  121.                 if(!poss){
  122.                     CDS->SetVolume(sid,vol);
  123.                     CDS->SetPan(sid,pan);
  124.                     CDS->PlaySound(sid);
  125.                     sndmade=false;
  126.                 }else{
  127.                     u--;
  128.                     nnn++;
  129.                     if(nnn>=maxsnd)nnn=0;
  130.                 };
  131.             }while(sndmade&&u);
  132.             if(sndmade&&srando()<2048){
  133.                 int nnn=(srando()*maxsnd)>>15;
  134.                 CDS->SetVolume(SndTable[n][nnn],vol);
  135.                 CDS->SetPan(SndTable[n][nnn],pan);
  136.                 CDS->PlaySound(SndTable[n][nnn]);
  137.             };
  138.         };
  139.     };
  140. };
  141. void AddEffect(int x,int y,int id){
  142.     if(!SoundOK)return;
  143.     if(id<0)return;
  144.     int mx0=mapx-8;
  145.     int my0=mapy-8;
  146.     int mx1=mapx+smaplx+16;
  147.     int my1=mapy+smaply+16;
  148.     int maxp=div(8000,smaplx).quot;
  149.     int pan=(x-(smaplx>>1)-mapx)*maxp;
  150.     if(pan<-4000)pan=-4000;
  151.     if(pan>4000)pan=4000;
  152.     //int pan=-9999;
  153.     if(x>=mapx&&y>=mapy&&x<mapx+smaplx&&y<mapy+smaply){
  154.         PlayEffect(id,pan,0);    
  155.     }else{
  156.         if(x>mx0&&y>my0&&x<mx1&&y<my1){
  157.             PlayEffect(id,pan,-800);
  158.         };
  159.     };
  160. };
  161. void AddWarEffect(int x,int y,int id){
  162.     if(!SoundOK)return;
  163.     if(fmap[y][x]<2000)return;
  164.     if(id<0)return;
  165.     int mx0=mapx-8;
  166.     int my0=mapy-8;
  167.     int mx1=mapx+smaplx+16;
  168.     int my1=mapy+smaply+16;
  169.     int maxp=div(8000,smaplx).quot;
  170.     int pan=(x-(smaplx>>1)-mapx)*maxp;
  171.     if(pan<-4000)pan=-4000;
  172.     if(pan>4000)pan=4000;
  173.     //int pan=-9999;
  174.     if(x>=mapx&&y>=mapy&&x<mapx+smaplx&&y<mapy+smaply){
  175.         PlayEffect(id,pan,WarSound);    
  176.     }else{
  177.         if(x>mx0&&y>my0&&x<mx1&&y<my1){
  178.             PlayEffect(id,pan,WarSound-400);
  179.         };
  180.     };
  181. };
  182. void AddWorkEffect(int x,int y,int id){
  183.     if(!SoundOK)return;
  184.     if(fmap[y][x]<2000)return;
  185.     if(id<0)return;
  186.     int mx0=mapx-8;
  187.     int my0=mapy-8;
  188.     int mx1=mapx+smaplx+16;
  189.     int my1=mapy+smaply+16;
  190.     int maxp=div(8000,smaplx).quot;
  191.     int pan=(x-(smaplx>>1)-mapx)*maxp;
  192.     if(pan<-4000)pan=-4000;
  193.     if(pan>4000)pan=4000;
  194.     //int pan=-9999;
  195.     if(x>=mapx&&y>=mapy&&x<mapx+smaplx&&y<mapy+smaply){
  196.         PlayEffect(id,-pan,WorkSound-200);    
  197.     }else{
  198.         if(x>mx0&&y>my0&&x<mx1&&y<my1){
  199.             PlayEffect(id,-pan,WorkSound-600);
  200.         };
  201.     };
  202. };
  203. void AddOrderEffect(int x,int y,int id){
  204.     if(!SoundOK)return;
  205.     if(fmap[y][x]<2000)return;
  206.     if(id<0)return;
  207.     int mx0=mapx-8;
  208.     int my0=mapy-8;
  209.     int mx1=mapx+smaplx+16;
  210.     int my1=mapy+smaply+16;
  211.     int maxp=div(8000,smaplx).quot;
  212.     int pan=(x-(smaplx>>1)-mapx)*maxp;
  213.     if(pan<-4000)pan=-4000;
  214.     if(pan>4000)pan=4000;
  215.     //int pan=-9999;
  216.     if(x>=mapx&&y>=mapy&&x<mapx+smaplx&&y<mapy+smaply){
  217.         PlayEffect(id,-pan,OrderSound);    
  218.     }else{
  219.         if(x>mx0&&y>my0&&x<mx1&&y<my1){
  220.             PlayEffect(id,-pan,OrderSound-400);
  221.         };
  222.     };
  223. };
  224. void AddTEffect(int x,int y,int id){
  225.     if(!SoundOK)return;
  226.     int xx=x;
  227.     if(xx<mapx)xx=mapx+1;
  228.     if(xx>=mapx+smaplx)xx=mapx+smaplx-1;
  229.     AddOrderEffect(xx,mapy+1,id);
  230. };
  231. void CloseSoundSystem(){
  232.  
  233. };