main.cpp

来自「<B>很多DirectX 9.0游戏编程源码例子</B>」· C++ 代码 · 共 184 行

CPP
184
字号
//-----------------------------------------------------------------------------
// Name: DSound_SoundSystem
//
// Description: Example code showing how to implement a sound system to handle
//				DirectSound functions.
//
// File Function: Main body of the program (main.cpp)
//
// Code: 
//		Copyright (c) 2002-2003 LostLogic Corporation. All rights reserved.
//
// Libraries Required:
//		dxguid.lib comctl32.lib winmm.lib dsound.lib
//
// Local Files Required:
//		main.cpp
//		main.h
//		SoundSystem.cpp
//		SoundSystem.h
//		DXUtil.cpp
//
// DX Files: 
//		Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include "main.h"

//--------------------------------------------------------------------------------------
//
// Main windows function, ie main()
//
//--------------------------------------------------------------------------------------
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	HWND		hWnd;
	MSG			msg;
	WNDCLASSEX	wndclass;
	bool		bRet;
		
    // Set up window class
	wndclass.cbSize			= sizeof(wndclass);
	wndclass.style			= CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc	= fnMessageProcessor;
	wndclass.cbClsExtra		= 0;
	wndclass.cbWndExtra		= 0;
	wndclass.hInstance		= hInstance;
	wndclass.hIcon			= LoadIcon( NULL, IDI_APPLICATION );
	wndclass.hCursor		= LoadCursor( NULL, IDC_ARROW );
	wndclass.hbrBackground	= (HBRUSH) GetStockObject (WHITE_BRUSH);
	wndclass.lpszMenuName	= NULL;
	wndclass.lpszClassName	= "Sound System Demo";	// Registered Class Name
	wndclass.hIconSm		= LoadIcon( NULL, IDI_APPLICATION );

	// Register the window class
	if( RegisterClassEx( &wndclass ) == NULL ) 
	{
		// The program failed, exit
		exit(1);
	}

	// Create the window
	hWnd = CreateWindowEx(	WS_EX_OVERLAPPEDWINDOW,
							"Sound System Demo",	// Class Name
							"DSound_SoundSystem",	// Name Displayed on Title Bar
							WS_OVERLAPPEDWINDOW,
							0,
							0,
							320,
							200,
							NULL,
							NULL,
							hInstance,
							NULL );

	// Display the window
	ShowWindow( hWnd, iCmdShow );
	
	// Clear out message structure
	ZeroMemory( &msg, sizeof(msg) );
    
	// Initialize Sound System
	bRet = bInitializeSoundSystem();
	
	// Make sure sound system initialized
	if( !bRet ) {
			MessageBox( hWnd, 
				"Sound System Error", 
				"Unable to initialize Sound System.", 
				MB_ICONERROR );
			// Cleanup
			vCleanup();
			// Exit
			exit(1);
	}

    // Enter the message loop
    while( msg.message!=WM_QUIT ) {
        if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
    }

	// Clean up everything and exit the app
    vCleanup();
    UnregisterClass( "Sound System Demo", wndclass.hInstance );
    return 0;
}

//--------------------------------------------------------------------------------------
//
// Windows message processor
//
//--------------------------------------------------------------------------------------
LRESULT WINAPI fnMessageProcessor( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
		case WM_LBUTTONDOWN:
			// Play the sound
			g_SoundSys.hrPlaySound( g_sndButtonOver );
			break;
		case WM_RBUTTONDOWN:
			// Play the "other" sound
			g_SoundSys.hrPlaySound( g_sndButton );
			break;
		case WM_DESTROY:
            PostQuitMessage( 0 );
            return 0;
		default:
			break;
    }
    return DefWindowProc( hWnd, msg, wParam, lParam );
}

//--------------------------------------------------------------------------------------
//
// Function to cleanup allocated objects
//
//--------------------------------------------------------------------------------------
void vCleanup( void )
{
	delete g_sndButton;
	delete g_sndButtonOver;
}

//--------------------------------------------------------------------------------------
//
// Function to initialize the sound system and load the various
// sounds needed for the game.
//
//--------------------------------------------------------------------------------------
bool bInitializeSoundSystem( void ) 
{
	HRESULT	hr;
	
	//-------------------------------------------------------
	// Allocate memory for the game sound(s)
	//-------------------------------------------------------
	g_sndButton = new GameSound;
	g_sndButtonOver = new GameSound;
	
	//-------------------------------------------------------
	// Initialize the sound system
	//-------------------------------------------------------
	g_SoundSys.hrInitSoundSystem();

	//-------------------------------------------------------
	// Load the game sound(s)
	//-------------------------------------------------------
	hr = g_SoundSys.hrLoadSound( "button.wav", g_sndButton );
	if( hr == SOUNDERROR_LOAD ) {
		return( 0 );
	}
	
	hr = g_SoundSys.hrLoadSound( "button_over.wav", g_sndButtonOver );
	if( hr == SOUNDERROR_LOAD ) {
		return( 0 );
	}
	
	// Return success
	return( 1 );
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?