// PhilVaz DirectSound Demo // includes dsutil.cpp to load wav file from a resource with LoadSoundBuffer function // June 11, 2003 // INCLUDES /////////////////////////////////////////////// #define WIN32_LEAN_AND_MEAN #include // include all the windows headers #include // include useful macros #include // for DirectSound #include // for rand functions #include #include // SAVE SOUND AND RESOURCES FOR LAST #include // include DirectSound #include "dsutil.h" // for LoadSoundBuffer() #include "DirectSoundDemo.h" // sounds and icon resources // DEFINES //////////////////////////////////////////////// // defines for windows #define WINDOW_CLASS_NAME "WINCLASS1" #define WINDOW_WIDTH 640 // size of game window #define WINDOW_HEIGHT 480 #define GAME_SPEED 30 // speed of game (increase to go slower) // MACROS ///////////////////////////////////////////////// #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) #define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1) // GLOBALS //////////////////////////////////////////////// HWND game_window = NULL; // global game window handle HINSTANCE game_instance = NULL; // global game instance handle HDC game_dc = NULL; // global device context (for GDI) handle LPDIRECTSOUND game_sound_main = NULL; // global direct sound object LPDIRECTSOUNDBUFFER game_sound_name1 = NULL; // sound buffer name1 LPDIRECTSOUNDBUFFER game_sound_name2 = NULL; // sound buffer name2 LPDIRECTSOUNDBUFFER game_sound_name3 = NULL; // sound buffer name3 LPDIRECTSOUNDBUFFER game_sound_name4 = NULL; // sound buffer name4 LPDIRECTSOUNDBUFFER game_sound_name5 = NULL; // sound buffer name5 LPDIRECTSOUNDBUFFER game_sound_name6 = NULL; // sound buffer name6 LPDIRECTSOUNDBUFFER game_sound_name7 = NULL; // sound buffer name7 LPDIRECTSOUNDBUFFER game_sound_name8 = NULL; // sound buffer name8 LPDIRECTSOUNDBUFFER game_sound_name9 = NULL; // sound buffer name9 // global pen and brush HPEN white_pen = CreatePen(PS_SOLID, 1, RGB(255,255,255)); HBRUSH white_brush = CreateSolidBrush(RGB(255,255,255)); HPEN black_pen = CreatePen(PS_SOLID, 1, RGB(0,0,0)); HBRUSH black_brush = CreateSolidBrush(RGB(0,0,0)); bool sound_ok; // for whether sound set okay char text[80]; // for display text output // FUNCTIONS ////////////////////////////////////////////// bool GameInit(); void GameMain(); void GameQuit(); // WINPROC //////////////////////////////////////////////// LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { // this is the main message handler of the system HDC hdc; // handle to a device context PAINTSTRUCT ps; // used in WM_PAINT switch(msg) // what is the message { case WM_CREATE: { // do initialization stuff here return(0); // return success } break; case WM_PAINT: { hdc = BeginPaint(hwnd, &ps); // validate the window EndPaint(hwnd, &ps); return(0); // return success } break; case WM_DESTROY: { PostQuitMessage(0); // kill the application, sends a WM_QUIT message return(0); // return success } break; default:break; } // end switch // process any messages that we didn't take care of return (DefWindowProc(hwnd, msg, wparam, lparam)); } // end WinProc // WINMAIN //////////////////////////////////////////////// int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow) { WNDCLASSEX winclass; // this will hold the class we create HWND hwnd; // generic window handle MSG msg; // generic message // first fill in the window class structure winclass.cbSize = sizeof(WNDCLASSEX); winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; winclass.lpfnWndProc = WinProc; winclass.cbClsExtra = 0; winclass.cbWndExtra = 0; winclass.hInstance = hinstance; winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); winclass.hCursor = LoadCursor(NULL, IDC_ARROW); winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); winclass.lpszMenuName = NULL; winclass.lpszClassName = WINDOW_CLASS_NAME; // save the game instance handle game_instance = hinstance; // register the window class if (!RegisterClassEx(&winclass)) return(0); // create the window if (!(hwnd = CreateWindowEx(NULL, // extended style WINDOW_CLASS_NAME, // class "DirectSound Demo", // title WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0,0, // initial game window x,y WINDOW_WIDTH, // initial game width WINDOW_HEIGHT, // initial game height NULL, // handle to parent NULL, // handle to menu hinstance, // instance of this application NULL))) // extra creation parms return(0); // save the game window handle game_window = hwnd; sound_ok = GameInit(); // sound initialization function called here // enter main event loop using PeekMessage() to retrieve messages while(TRUE) { // get initial tick count to keep game speed constant DWORD start_tick = GetTickCount(); // is there a message in queue, if so get it if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { // test if this is a quit if (msg.message == WM_QUIT) break; // translate any accelerator keys TranslateMessage(&msg); // send the message to WinProc DispatchMessage(&msg); } // end if GameMain(); // game main processing function called here // check for key and send quit game if (KEYDOWN(VK_ESCAPE)) SendMessage (hwnd, WM_CLOSE, 0, 0); // wait until we hit correct game speed frame rate while ((GetTickCount() - start_tick) < GAME_SPEED); } // end while GameQuit(); // game quit function and clean up before exit called here return(msg.wParam); // return to Windows } // end WinMain // BEGIN GAME CODE //////////////////////////////////////// /////////////////////////////////////////////////////////// // // GAME INITIALIZATION // /////////////////////////////////////////////////////////// bool GameInit() { game_dc = GetDC(game_window); // get the GDI device context if (DirectSoundCreate(NULL, &game_sound_main, NULL) != DS_OK) { MessageBox(game_window, "Sound could not be initialized -- Direct Sound Create Error","Sound Error",MB_OK); return FALSE; } // // Direct Sound object succeeded so set coop level // if (IDirectSound_SetCooperativeLevel(game_sound_main, game_window, DSSCL_PRIORITY) != DS_OK) { MessageBox(game_window, "Sound could not be initialized -- Cooperative Level Error","Sound Error",MB_OK); return FALSE; } // // Cooperative Level succeeded so set up // secondary buffers and load wave file sound resources // game_sound_name1 = DSLoadSoundBuffer(game_sound_main, MAKEINTRESOURCE(SOUND_NAME1)); game_sound_name2 = DSLoadSoundBuffer(game_sound_main, MAKEINTRESOURCE(SOUND_NAME2)); game_sound_name3 = DSLoadSoundBuffer(game_sound_main, MAKEINTRESOURCE(SOUND_NAME3)); game_sound_name4 = DSLoadSoundBuffer(game_sound_main, MAKEINTRESOURCE(SOUND_NAME4)); game_sound_name5 = DSLoadSoundBuffer(game_sound_main, MAKEINTRESOURCE(SOUND_NAME5)); game_sound_name6 = DSLoadSoundBuffer(game_sound_main, MAKEINTRESOURCE(SOUND_NAME6)); game_sound_name7 = DSLoadSoundBuffer(game_sound_main, MAKEINTRESOURCE(SOUND_NAME7)); game_sound_name8 = DSLoadSoundBuffer(game_sound_main, MAKEINTRESOURCE(SOUND_NAME8)); game_sound_name9 = DSLoadSoundBuffer(game_sound_main, MAKEINTRESOURCE(SOUND_NAME9)); return TRUE; } // END OF GameInit /////////////////////////////////////////////////////////// // // GAME MAIN LOOP AND PROCESSING // /////////////////////////////////////////////////////////// void GameMain() { SetTextColor(game_dc, RGB(255,255,255)); // white text color SetBkColor(game_dc, RGB(0,0,0)); // black background sprintf(text,"DirectSound Demo by PhilVaz"); TextOut(game_dc, 100, 100, text, strlen(text)); sprintf(text,"Press numbers 1 through 9 to hear different sounds"); TextOut(game_dc, 100, 150, text, strlen(text)); sprintf(text,"Press to exit"); TextOut(game_dc, 100, 200, text, strlen(text)); if (sound_ok) { // ASCII 49 to 57 are the digits 1 to 9 on keyboard if (KEYDOWN(49)) IDirectSoundBuffer_Play (game_sound_name1, 0, 0, NULL); if (KEYDOWN(50)) IDirectSoundBuffer_Play (game_sound_name2, 0, 0, NULL); if (KEYDOWN(51)) IDirectSoundBuffer_Play (game_sound_name3, 0, 0, NULL); if (KEYDOWN(52)) IDirectSoundBuffer_Play (game_sound_name4, 0, 0, NULL); if (KEYDOWN(53)) IDirectSoundBuffer_Play (game_sound_name5, 0, 0, NULL); if (KEYDOWN(54)) IDirectSoundBuffer_Play (game_sound_name6, 0, 0, NULL); if (KEYDOWN(55)) IDirectSoundBuffer_Play (game_sound_name7, 0, 0, NULL); if (KEYDOWN(56)) IDirectSoundBuffer_Play (game_sound_name8, 0, 0, NULL); if (KEYDOWN(57)) IDirectSoundBuffer_Play (game_sound_name9, 0, 0, NULL); } } // END OF GameMain /////////////////////////////////////////////////////////// // // GAME QUIT AND CLEAN UP // /////////////////////////////////////////////////////////// void GameQuit() { // if sound was set okay, release DirectSound objects if (game_sound_main) { // FIRST RELEASE SECONDARY BUFFERS IDirectSound_Release(game_sound_name9); IDirectSound_Release(game_sound_name8); IDirectSound_Release(game_sound_name7); IDirectSound_Release(game_sound_name6); IDirectSound_Release(game_sound_name5); IDirectSound_Release(game_sound_name4); IDirectSound_Release(game_sound_name3); IDirectSound_Release(game_sound_name2); IDirectSound_Release(game_sound_name1); // THEN RELEASE MAIN DIRECT SOUND OBJECT IDirectSound_Release(game_sound_main); } // delete the pens and brushes DeleteObject(white_pen); DeleteObject(white_brush); DeleteObject(black_pen); DeleteObject(black_brush); // release the device context (for GDI) from the game window ReleaseDC(game_window, game_dc); } // END OF GameQuit // END GAME CODE //////////////////////////////////////////