home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Game Programming in 21 Days / TYGAMES_R.ISO / source / day_09 / graph9.c < prev    next >
Encoding:
Text File  |  1994-08-27  |  7.8 KB  |  361 lines

  1.  
  2. // I N C L U D E S ///////////////////////////////////////////////////////////
  3.  
  4. #include <io.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <dos.h>
  8. #include <bios.h>
  9. #include <fcntl.h>
  10. #include "graph9.h"
  11.  
  12. // G L O B A L S  ////////////////////////////////////////////////////////////
  13.  
  14. char far *driver_ptr;      // pointer to the sound driver ct-voice.drv
  15. unsigned version;          // holds the version of the driver
  16. char far *data_ptr;        // pointer to sound file
  17. unsigned ct_voice_status;  // global status variable
  18.  
  19. // F U N C T I O N S /////////////////////////////////////////////////////////
  20.  
  21. void Voc_Get_Version(void)
  22. {
  23. // this function prints out the version of the driver
  24.  
  25. _asm
  26.    {
  27.    mov bx,0          ; function 0 get version number
  28.    call driver_ptr   ; call the driver
  29.    mov version,ax    ; store in version variable
  30.  
  31.    } // end inline asm
  32.  
  33. printf("\nVersion of Driver = %X.0%X",((version>>8) & 0x00ff), (version&0x00ff));
  34.  
  35. } // end Voc_Get_Version
  36.  
  37. //////////////////////////////////////////////////////////////////////////////
  38.  
  39. int Voc_Init_Driver(void)
  40. {
  41. // this function intializes the driver and returns the status
  42.  
  43. int status;
  44.  
  45. _asm
  46.    {
  47.    mov bx,3          ; function 3 initialize the driver
  48.    call driver_ptr   ; call the driver
  49.    mov status,ax     ; store in version variable
  50.  
  51.    } // end inline asm
  52.  
  53. // return status
  54.  
  55. printf("\nDriver Initialized");
  56.  
  57. return(status);
  58.  
  59. } // end Voc_Init_Driver
  60.  
  61. //////////////////////////////////////////////////////////////////////////////
  62.  
  63. int Voc_Terminate_Driver(void)
  64. {
  65. // this function terminates the driver and de-installes it from memory
  66.  
  67. _asm
  68.    {
  69.    mov bx,9          ; function 9 terminate the driver
  70.    call driver_ptr   ; call the driver
  71.  
  72.    } // end inline asm
  73.  
  74. // de-allocate memory
  75.  
  76. _dos_freemem(_FP_SEG(driver_ptr));
  77.  
  78. printf("\nDriver Terminated");
  79.  
  80. } // end Voc_Terminate_Driver
  81.  
  82. //////////////////////////////////////////////////////////////////////////////
  83.  
  84. void Voc_Set_Port(unsigned int port)
  85. {
  86.  
  87. // this function sets the I/O port of the sound blaster
  88.  
  89. _asm
  90.    {
  91.    mov bx,1          ; function 1 set port address
  92.    mov ax,port       ; move the port number into ax
  93.    call driver_ptr   ; call the driver
  94.  
  95.    } // end inline asm
  96.  
  97. } // Voc_Set_Port
  98.  
  99. //////////////////////////////////////////////////////////////////////////////
  100.  
  101. void Voc_Set_Speaker(unsigned int on)
  102. {
  103.  
  104. // this function turns the speaker on or off
  105.  
  106. _asm
  107.    {
  108.    mov bx,4          ; function 4 turn speaker on or off
  109.    mov ax,on         ; move the on/off flag into ax
  110.    call driver_ptr   ; call the driver
  111.  
  112.    } // end inline asm
  113.  
  114. } // Voc_Set_Speaker
  115.  
  116. /////////////////////////////////////////////////////////////////////////////
  117.  
  118. int Voc_Play_Sound(unsigned char far *addr,unsigned char header_length)
  119. {
  120. // this function plays a pre-loaded VOC file
  121.  
  122. unsigned segm,offm;
  123.  
  124. segm = _FP_SEG(addr);
  125. offm = _FP_OFF(addr) + header_length;
  126.  
  127. _asm
  128.    {
  129.    mov bx,6          ; function 6 play a VOC file
  130.    mov ax, segm      ; can only mov a register into segment so we need this
  131.    mov es, ax        ; es gets the segment
  132.    mov di, offm      ; di gets offset
  133.    call driver_ptr   ; call the driver
  134.  
  135.    } // end inline asm
  136.  
  137. } // end Voc_Play_Sound
  138.  
  139. /////////////////////////////////////////////////////////////////////////////
  140.  
  141. int Voc_Stop_Sound(void)
  142. {
  143. // this function will stop a currently playing sound
  144.  
  145. _asm
  146.    {
  147.    mov bx,8          ; function 8 stop a sound
  148.    call driver_ptr   ; call the driver
  149.  
  150.    } // end inline asm
  151.  
  152. } // end Voc_Stop_Sound
  153.  
  154. /////////////////////////////////////////////////////////////////////////////
  155.  
  156. int Voc_Pause_Sound(void)
  157. {
  158. // this function pauses a sound that is playing
  159.  
  160. _asm
  161.    {
  162.    mov bx,10         ; function 10 pause a sound
  163.    call driver_ptr   ; call the driver
  164.  
  165.    } // end inline asm
  166.  
  167. } // end Voc_Pause_Sound
  168.  
  169. /////////////////////////////////////////////////////////////////////////////
  170.  
  171. int Voc_Continue_Sound(void)
  172. {
  173. // this function continues a sound that had been paused
  174.  
  175. _asm
  176.    {
  177.    mov bx,11         ; function 11 continue play
  178.    call driver_ptr   ; call the driver
  179.  
  180.    } // end inline asm
  181.  
  182. } // end Voc_Continue_Sound
  183.  
  184. /////////////////////////////////////////////////////////////////////////////
  185.  
  186. int Voc_Break_Sound(void)
  187. {
  188. // this function breaks a sound that is in a loop
  189.  
  190. _asm
  191.    {
  192.    mov bx,12         ; function 12 break loop
  193.    call driver_ptr   ; call the driver
  194.  
  195.    } // end inline asm
  196.  
  197. } // end Voc_Break_Sound
  198.  
  199. /////////////////////////////////////////////////////////////////////////////
  200.  
  201. void Voc_Set_IRQ(unsigned int irq)
  202. {
  203. // this function sets the irq for the sound blaster
  204.  
  205. _asm
  206.    {
  207.    mov bx,2          ; function 2 set irq for DMA transfer
  208.    mov ax,irq        ; move the irq number into ax
  209.    call driver_ptr   ; call the driver
  210.  
  211.    } // end inline asm
  212.  
  213. } // Voc_Set_IRQ
  214.  
  215. //////////////////////////////////////////////////////////////////////////////
  216.  
  217. void Voc_Set_Status_Addr(char far *status)
  218. {
  219.  
  220. // this function sets the address of the global status word in the driver
  221.  
  222. unsigned segm,offm;
  223.  
  224. // exract the segment and offset of status variable
  225.  
  226. segm = _FP_SEG(status);
  227. offm = _FP_OFF(status);
  228.  
  229. _asm
  230.    {
  231.    mov bx,5          ; function 5 set status varible address
  232.    mov es, segm      ; es gets the segment
  233.    mov di, offm      ; di gets offset
  234.    call driver_ptr   ; call the driver
  235.  
  236.    } // end inline asm
  237.  
  238. } // Voc_Set_Status_Addr
  239.  
  240. //////////////////////////////////////////////////////////////////////////////
  241.  
  242. void Voc_Load_Driver(void)
  243. {
  244. // this functions loads the ct-voice.drv which allows digitized effects
  245. // to be played
  246.  
  247. int driver_handle;
  248.  
  249. unsigned segment,num_para,bytes_read;
  250.  
  251. // open the driver file
  252.  
  253. _dos_open("CT-VOICE.DRV", _O_RDONLY, &driver_handle);
  254.  
  255. // allocate the memory
  256.  
  257. num_para = 1 + (filelength(driver_handle))/16;
  258.  
  259. _dos_allocmem(num_para,&segment);
  260.  
  261. // point driver pointer to data area
  262.  
  263. _FP_SEG(driver_ptr) = segment;
  264. _FP_OFF(driver_ptr) = 0;
  265.  
  266. // load in the driver code
  267.  
  268. data_ptr = driver_ptr;
  269.  
  270. do
  271.  {
  272.  _dos_read(driver_handle,data_ptr, 0x4000, &bytes_read);
  273.  data_ptr += bytes_read;
  274.  
  275.  } while(bytes_read==0x4000);
  276.  
  277. // close the file
  278.  
  279. _dos_close(driver_handle);
  280.  
  281. } // end Voc_Load_Driver
  282.  
  283. //////////////////////////////////////////////////////////////////////////////
  284.  
  285. char far *Voc_Load_Sound(char *filename, unsigned char *header_length)
  286. {
  287. // thid function loads a sound off disk into memory and points returns
  288. // a pointer to the data
  289.  
  290. char far *temp_ptr;
  291. char far *data_ptr;
  292.  
  293. unsigned int sum;
  294.  
  295. int sound_handle;
  296.  
  297. unsigned segment,num_para,bytes_read;
  298.  
  299. // open the sound file
  300.  
  301. _dos_open(filename, _O_RDONLY, &sound_handle);
  302.  
  303. // allocate the memory
  304.  
  305. num_para = 1 + (filelength(sound_handle))/16;
  306.  
  307. _dos_allocmem(num_para,&segment);
  308.  
  309. // point data pointer to allocated data area
  310.  
  311. _FP_SEG(data_ptr) = segment;
  312. _FP_OFF(data_ptr) = 0;
  313.  
  314. // load in the sound data
  315.  
  316. temp_ptr = data_ptr;
  317.  
  318. do
  319.  {
  320.  _dos_read(sound_handle,temp_ptr, 0x4000, &bytes_read);
  321.  temp_ptr += bytes_read;
  322.  
  323.  sum+=bytes_read;
  324.  
  325.  } while(bytes_read==0x4000);
  326.  
  327. // make sure it's a voc file
  328.  
  329.    if ((data_ptr[0] != 'C') || (data_ptr[1] != 'r'))
  330.       {
  331.       printf("\n%s is not a voc file!",filename);
  332.       _dos_freemem(_FP_SEG(data_ptr));
  333.       return(0);
  334.  
  335.       } // end if voc file
  336.  
  337.    *header_length = (unsigned char)data_ptr[20];
  338.  
  339. // close the file
  340.  
  341. _dos_close(sound_handle);
  342.  
  343. return(data_ptr);
  344.  
  345. } // end Voc_Load_Sound
  346.  
  347. //////////////////////////////////////////////////////////////////////////////
  348.  
  349. void Voc_Unload_Sound(char far *sound_ptr)
  350. {
  351.  
  352. // this functions deletes the sound from memory
  353.  
  354. _dos_freemem(_FP_SEG(sound_ptr));
  355.  
  356. } // end Voc_Unload_Sound
  357.  
  358.  
  359. //////////////////////////////////////////////////////////////////////////////
  360.  
  361.