⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 <B>很多DirectX 9.0游戏编程源码例子</B>
💻 CPP
字号:
//-----------------------------------------------------------------------------
// Name: DMusic_PlaySound
//
// Description: Example code showing how to play a wave file
//
// File Function: Main body of the program (main.cpp)
//
// Code: 
//		Copyright (c) 2002 LostLogic Corporation. All rights reserved.
//
// Libraries Required:
//		dxguid.lib winmm.lib dsound.lib
//
// Local Files Required:
//		main.cpp
//		main.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	= "Direct Sound 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,
							"Direct Sound Demo",	// Class Name
							"DMusic_PlaySound",		// 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 Direct Sound
	bRet = bInitializeSoundSystem( hWnd );
	if( bRet == 0 ) {
		MessageBox( hWnd, "Initialization Failure", 
					"Failed to initialize Direct Sound", 
					MB_ICONEXCLAMATION|MB_OK );
		// The program failed, 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( "Direct Sound Demo", wndclass.hInstance );
    return 0;
}

//--------------------------------------------------------------------------------------
//
// Windows message processor
//
//--------------------------------------------------------------------------------------
LRESULT WINAPI fnMessageProcessor( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	HDC			hdc;
	PAINTSTRUCT	ps;
	char		szMessage[256];
	
	// Force a redraw
	InvalidateRect( hWnd, NULL, 0 );

	switch( msg ) 
	{
		// Called when the window is refreshed
		case WM_PAINT:
			// Start painting
			hdc = BeginPaint( hWnd, &ps );
			// Populate the string to output
			sprintf(szMessage,"Press The Left-Mouse Button To Play Sound"); 
			// Draw the text 
			TextOut( hdc, 10, 50, szMessage, strlen(szMessage) );
			// Populate the string to output
			sprintf(szMessage,"** Click In This Window **"); 
			// Draw the text 
			TextOut( hdc, 70, 90, szMessage, strlen(szMessage) );
			// End the painting
			EndPaint( hWnd, &ps );
			break;
		case WM_LBUTTONDOWN:
			// Play the sound
			vPlaySound();
			break;
		case WM_DESTROY:
            PostQuitMessage( 0 );
            return 0;
		default:
			break;
    }
    return DefWindowProc( hWnd, msg, wParam, lParam );
}

//--------------------------------------------------------------------------------------
//
// Function to cleanup allocated objects
//
//--------------------------------------------------------------------------------------
void vCleanup( void )
{
	if( g_pSound ) {
		if( g_pPerformance ) 
			g_pSound->Unload( g_pPerformance );
	}
	SAFE_RELEASE( g_pSound );

	SAFE_RELEASE( g_pLoader );
	SAFE_RELEASE( g_pPerformance );
}

//--------------------------------------------------------------------------------------
//
// Function to initialize the sound system and load the various
// sounds needed for the game.
//
//--------------------------------------------------------------------------------------
bool bInitializeSoundSystem( HWND hWnd ) 
{
	HRESULT					hResult;
	IDirectMusicAudioPath8	*dmAudioPath;

	// Initialize COM
	CoInitialize( NULL );
	
	// Create the loader
	if( FAILED( hResult = CoCreateInstance( CLSID_DirectMusicLoader, NULL, 
		CLSCTX_INPROC, IID_IDirectMusicLoader8,
		(void**) &g_pLoader ) ) ) {
		return( 0 );
	}
	
	// Create the performance
	if( FAILED( hResult = CoCreateInstance( CLSID_DirectMusicPerformance, NULL,
		CLSCTX_INPROC, IID_IDirectMusicPerformance8,
		(void**) &g_pPerformance ) ) ) {
		return( 0 );
	}
	
	// Initialize the audio
	if( FAILED( hResult = g_pPerformance->InitAudio( 
		NULL,
		NULL,
		hWnd,
		DMUS_APATH_DYNAMIC_STEREO,
		4,
		DMUS_AUDIOF_ALL,
		NULL
		))) {
		return( 0 );
	}
	
	// Get the default path
	if( FAILED( g_pPerformance->GetDefaultAudioPath( &dmAudioPath ) ) ) 
		return( 0 );
	
	// Set the default volume
	if( FAILED( dmAudioPath->SetVolume(0,0) ) ) 
		return( 0 );

	// Load the sound from a file
	if ( FAILED(g_pLoader->LoadObjectFromFile (
		CLSID_DirectMusicSegment,
		IID_IDirectMusicSegment8,
		L"testsound.wav",
		( LPVOID* ) &g_pSound
		) ) )
	{
		return( 0 );
	}
	
	// Download the data
	if ( FAILED ( g_pSound->Download( g_pPerformance ) ) ) {
		return( 0 );
	}

	// Return success
	return( 1 );
}

//--------------------------------------------------------------------------------------
//
// Function to play the global sound file
//
//--------------------------------------------------------------------------------------
void vPlaySound( void )
{
	// Play the sound segment
	g_pPerformance->PlaySegmentEx(
		g_pSound,
		NULL,
		NULL,
		DMUS_SEGF_DEFAULT | DMUS_SEGF_SECONDARY,
		0,
		NULL,
		NULL,
		NULL
	);
}








⌨️ 快捷键说明

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