home *** CD-ROM | disk | FTP | other *** search
/ Game Audio Programming / GameAudioProgramming.iso / Extras / Sensaura / SDK1.0 / data1.cab / Example_Files / ZFXCons / zfxcons.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-13  |  4.8 KB  |  188 lines

  1. /*
  2.     Company:            Sensaura Ltd
  3.     Copyright:          (C) 2000
  4.  
  5.     File Name:            zfxcons.cpp
  6.     File Description:    Source file for implementation of ZoomFX console application.  This app 
  7.                         plays the supplied WAV file twice, once without ZoomFX and once with ZoomFX
  8.                         enabled such that the object is stretched horizontally and rotates about
  9.                         the vertical axis.
  10.     Author:                Adam Philp
  11.     Last Update:        19-JAN-00
  12.  
  13.     Target Compiler:    Microsoft Visual C++ Version 5.0
  14. */
  15.  
  16. ///////////////////////    Included files ////////////////////////////////////////////////////////////
  17.  
  18. #define    WIN32_LEAN_AND_MEAN
  19.  
  20. #include    <windows.h>
  21. #include    <mmsystem.h>
  22. #include    <dsound.h>
  23. #include    <stdio.h>
  24. #include    <math.h>
  25.  
  26. #include    "buffer.h"                    // DirectSound buffer with ZoomFX support
  27. #include    "directx.h"                    // DirectX macros and error handling
  28. #include    "debug.h"                    // Debugging support
  29.  
  30. /////////////////////// Local variables ///////////////////////////////////////////////////////////
  31.  
  32. static LPDIRECTSOUND        pDirectSound = NULL;
  33. static LPDIRECTSOUNDBUFFER    pPrimary = NULL;
  34.  
  35. static Buffer                buffer;
  36.  
  37. static const WAVEFORMATEX    wfxPrimary =
  38. {
  39.     WAVE_FORMAT_PCM,
  40.     2,
  41.     44100,
  42.     44100 * 2 * 2,
  43.     4,
  44.     16,
  45.     0
  46. };
  47.  
  48. /////////////////////// Local functions ///////////////////////////////////////////////////////////
  49.  
  50. void TermDirectSound()                    // Release all DirectSound objects
  51. {
  52.     PTRACE("TermDirectSound()");
  53.  
  54.     RELEASE(pPrimary)
  55.     RELEASE(pDirectSound)
  56. }
  57.  
  58. BOOL InitDirectSound(HWND hWnd)            // Initialize DirectSound and create a primary buffer
  59. {
  60.     DSBUFFERDESC dsbd;
  61.  
  62.     PTRACE("InitDirectSound(hWnd:%08X)", hWnd);
  63.  
  64.     ASSERT(pDirectSound == NULL);
  65.     ASSERT(pPrimary == NULL);
  66.     ASSERT(hWnd != NULL);
  67.                                         // Create DirectSound and attach it to the supplied window
  68.     TRY_DS(DirectSoundCreate(NULL, &pDirectSound, NULL))
  69.     ASSERT(pDirectSound != NULL);
  70.     TRY_DS(pDirectSound->SetCooperativeLevel(hWnd, DSSCL_PRIORITY))
  71.  
  72.     dsbd.dwSize = sizeof(DSBUFFERDESC);    // Set primary buffer properties
  73.     dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRL3D;        
  74.     dsbd.dwBufferBytes = 0;                  
  75.     dsbd.dwReserved = 0;                
  76.     dsbd.lpwfxFormat = NULL;
  77.                                         // Create the primary buffer and set its format
  78.     TRY_DS(pDirectSound->CreateSoundBuffer(&dsbd, &pPrimary, NULL))
  79.     ASSERT(pPrimary != NULL);
  80.     TRY_DS(pPrimary->SetFormat(&wfxPrimary))
  81.  
  82.     return TRUE;
  83.  
  84. DS_ERROR:
  85.     TermDirectSound();
  86.     return FALSE;
  87. }
  88.  
  89. BOOL ZoomFXPlay()
  90. {
  91.     ZOOMFX_BOX        extent = { { -2, -1, 0 }, { 2, 1, 0 } };
  92.     ZOOMFX_ORIENTATION    orientation = { { 0, 0, 1 }, { 0, 1, 0 } };
  93.     DWORD    dwStartTime;
  94.     double    fTime;
  95.     double    fPi = 3.1415926535;
  96.  
  97.     PTRACE("ZoomFXPlay()");
  98.  
  99.     if(!buffer.SetZoomFXExtent(&extent))
  100.         return FALSE;
  101.  
  102.     if(!buffer.SetZoomFXOrientation(&orientation))
  103.         return FALSE;
  104.  
  105.     dwStartTime = timeGetTime();        // Get system time in ms
  106.     if(!buffer.Play())                    // Play the buffer
  107.         return FALSE;
  108.  
  109.     while(buffer.IsPlaying())            // Wait for buffer to finish
  110.     {
  111.         fTime = (double)(timeGetTime()-dwStartTime);
  112.         orientation.vFront.x = (float)sin(fTime/4000.0f*fPi);
  113.         orientation.vFront.z = (float)cos(fTime/4000.0f*fPi);
  114.         if(!buffer.SetZoomFXOrientation(&orientation))
  115.             return FALSE;
  116.         fprintf(stdout, "\rZoomFX orientation FRONT(%.1f, %.1f, %.1f)", orientation.vFront.x,
  117.                 orientation.vFront.y, orientation.vFront.z);
  118.     }
  119.  
  120.     return TRUE;
  121. }
  122.  
  123. ///////////////////////    Main function /////////////////////////////////////////////////////////////
  124.  
  125. int main(int argc, char *argv[])
  126. {
  127.     HWND    hWnd;
  128.     int        ret;
  129.  
  130.     PTRACE("main(argc: %d)", argc);
  131.  
  132.     ret = -1;
  133.     if(argc != 2)                        // The wrong number of command line arguments
  134.     {
  135.         fputs("Usage: zfxplay file.wav\n", stdout);
  136.         goto MAIN_ERROR;                // Don't continue if no filename supplied
  137.     }
  138.     ret--;
  139.                                         
  140.     hWnd = GetForegroundWindow();        // Attach DirectSound to the current foreground window
  141.     if(!InitDirectSound(hWnd))
  142.     {
  143.         fputs("Failed to initialize DirectSound\n", stdout);
  144.         goto MAIN_ERROR;
  145.     }
  146.  
  147.     ret--;
  148.     SetWindowText(hWnd, *++argv);        // Set Window text to filename
  149.                                         // Create the buffer
  150.     if(!buffer.Create(*argv, pDirectSound))
  151.     {
  152.         fputs("Failed to create buffer\n", stdout);
  153.         goto MAIN_ERROR;
  154.     }
  155.  
  156.     ret--;
  157.     if(!buffer.CanDoZoomFX())
  158.     {
  159.         fputs("ZoomFX not supported\n", stdout);
  160.         goto MAIN_ERROR;
  161.     }
  162.  
  163.     ret--;
  164.     fputs("Playing with ZoomFX OFF\n", stdout);
  165.  
  166.     if(!buffer.SetPosition(0, 0, 1, DS3D_IMMEDIATE))
  167.         goto MAIN_ERROR;
  168.  
  169.     if(!buffer.Play())                    // Play the buffer
  170.         goto MAIN_ERROR;
  171.  
  172.     while(buffer.IsPlaying());            // Wait for buffer to finish
  173.  
  174.     ret--;
  175.     fputs("Playing with ZoomFX ON\n", stdout);
  176.     
  177.     if(!ZoomFXPlay())
  178.         goto MAIN_ERROR;
  179.  
  180.     ret = 0;                            // Program execution AOK
  181.  
  182. MAIN_ERROR:                                // Execution drops to here if there is an error
  183.     buffer.Destroy();                    // Ensure buffer is destroyed
  184.     TermDirectSound();
  185.  
  186.     TRACE(0, "main() returned %d", ret);
  187.     return ret;
  188. }