home *** CD-ROM | disk | FTP | other *** search
- /*
- Company: Sensaura Ltd
- Copyright: (C) 2000
-
- File Name: zfxcons.cpp
- File Description: Source file for implementation of ZoomFX console application. This app
- plays the supplied WAV file twice, once without ZoomFX and once with ZoomFX
- enabled such that the object is stretched horizontally and rotates about
- the vertical axis.
- Author: Adam Philp
- Last Update: 19-JAN-00
-
- Target Compiler: Microsoft Visual C++ Version 5.0
- */
-
- /////////////////////// Included files ////////////////////////////////////////////////////////////
-
- #define WIN32_LEAN_AND_MEAN
-
- #include <windows.h>
- #include <mmsystem.h>
- #include <dsound.h>
- #include <stdio.h>
- #include <math.h>
-
- #include "buffer.h" // DirectSound buffer with ZoomFX support
- #include "directx.h" // DirectX macros and error handling
- #include "debug.h" // Debugging support
-
- /////////////////////// Local variables ///////////////////////////////////////////////////////////
-
- static LPDIRECTSOUND pDirectSound = NULL;
- static LPDIRECTSOUNDBUFFER pPrimary = NULL;
-
- static Buffer buffer;
-
- static const WAVEFORMATEX wfxPrimary =
- {
- WAVE_FORMAT_PCM,
- 2,
- 44100,
- 44100 * 2 * 2,
- 4,
- 16,
- 0
- };
-
- /////////////////////// Local functions ///////////////////////////////////////////////////////////
-
- void TermDirectSound() // Release all DirectSound objects
- {
- PTRACE("TermDirectSound()");
-
- RELEASE(pPrimary)
- RELEASE(pDirectSound)
- }
-
- BOOL InitDirectSound(HWND hWnd) // Initialize DirectSound and create a primary buffer
- {
- DSBUFFERDESC dsbd;
-
- PTRACE("InitDirectSound(hWnd:%08X)", hWnd);
-
- ASSERT(pDirectSound == NULL);
- ASSERT(pPrimary == NULL);
- ASSERT(hWnd != NULL);
- // Create DirectSound and attach it to the supplied window
- TRY_DS(DirectSoundCreate(NULL, &pDirectSound, NULL))
- ASSERT(pDirectSound != NULL);
- TRY_DS(pDirectSound->SetCooperativeLevel(hWnd, DSSCL_PRIORITY))
-
- dsbd.dwSize = sizeof(DSBUFFERDESC); // Set primary buffer properties
- dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRL3D;
- dsbd.dwBufferBytes = 0;
- dsbd.dwReserved = 0;
- dsbd.lpwfxFormat = NULL;
- // Create the primary buffer and set its format
- TRY_DS(pDirectSound->CreateSoundBuffer(&dsbd, &pPrimary, NULL))
- ASSERT(pPrimary != NULL);
- TRY_DS(pPrimary->SetFormat(&wfxPrimary))
-
- return TRUE;
-
- DS_ERROR:
- TermDirectSound();
- return FALSE;
- }
-
- BOOL ZoomFXPlay()
- {
- ZOOMFX_BOX extent = { { -2, -1, 0 }, { 2, 1, 0 } };
- ZOOMFX_ORIENTATION orientation = { { 0, 0, 1 }, { 0, 1, 0 } };
- DWORD dwStartTime;
- double fTime;
- double fPi = 3.1415926535;
-
- PTRACE("ZoomFXPlay()");
-
- if(!buffer.SetZoomFXExtent(&extent))
- return FALSE;
-
- if(!buffer.SetZoomFXOrientation(&orientation))
- return FALSE;
-
- dwStartTime = timeGetTime(); // Get system time in ms
- if(!buffer.Play()) // Play the buffer
- return FALSE;
-
- while(buffer.IsPlaying()) // Wait for buffer to finish
- {
- fTime = (double)(timeGetTime()-dwStartTime);
- orientation.vFront.x = (float)sin(fTime/4000.0f*fPi);
- orientation.vFront.z = (float)cos(fTime/4000.0f*fPi);
- if(!buffer.SetZoomFXOrientation(&orientation))
- return FALSE;
- fprintf(stdout, "\rZoomFX orientation FRONT(%.1f, %.1f, %.1f)", orientation.vFront.x,
- orientation.vFront.y, orientation.vFront.z);
- }
-
- return TRUE;
- }
-
- /////////////////////// Main function /////////////////////////////////////////////////////////////
-
- int main(int argc, char *argv[])
- {
- HWND hWnd;
- int ret;
-
- PTRACE("main(argc: %d)", argc);
-
- ret = -1;
- if(argc != 2) // The wrong number of command line arguments
- {
- fputs("Usage: zfxplay file.wav\n", stdout);
- goto MAIN_ERROR; // Don't continue if no filename supplied
- }
- ret--;
-
- hWnd = GetForegroundWindow(); // Attach DirectSound to the current foreground window
- if(!InitDirectSound(hWnd))
- {
- fputs("Failed to initialize DirectSound\n", stdout);
- goto MAIN_ERROR;
- }
-
- ret--;
- SetWindowText(hWnd, *++argv); // Set Window text to filename
- // Create the buffer
- if(!buffer.Create(*argv, pDirectSound))
- {
- fputs("Failed to create buffer\n", stdout);
- goto MAIN_ERROR;
- }
-
- ret--;
- if(!buffer.CanDoZoomFX())
- {
- fputs("ZoomFX not supported\n", stdout);
- goto MAIN_ERROR;
- }
-
- ret--;
- fputs("Playing with ZoomFX OFF\n", stdout);
-
- if(!buffer.SetPosition(0, 0, 1, DS3D_IMMEDIATE))
- goto MAIN_ERROR;
-
- if(!buffer.Play()) // Play the buffer
- goto MAIN_ERROR;
-
- while(buffer.IsPlaying()); // Wait for buffer to finish
-
- ret--;
- fputs("Playing with ZoomFX ON\n", stdout);
-
- if(!ZoomFXPlay())
- goto MAIN_ERROR;
-
- ret = 0; // Program execution AOK
-
- MAIN_ERROR: // Execution drops to here if there is an error
- buffer.Destroy(); // Ensure buffer is destroyed
- TermDirectSound();
-
- TRACE(0, "main() returned %d", ret);
- return ret;
- }