📄 main.cpp
字号:
//-----------------------------------------------------------------------------
// Name: DSound_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
// MouseZoneClass.h
// MouseZoneClass.cpp
// 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
"DSound_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();
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) );
// End the painting
EndPaint( hWnd, &ps );
break;
case WM_LBUTTONDOWN:
// Play the 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 )
{
// Free the sound
delete g_sndButton;
}
//--------------------------------------------------------------------------------------
//
// 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;
//-------------------------------------------------------
// Initialize the sound system
//-------------------------------------------------------
g_SoundSys.hrInitSoundSystem();
//-------------------------------------------------------
// Load the game sound(s)
//-------------------------------------------------------
hr = g_SoundSys.hrLoadSound( "testsound.wav", g_sndButton );
// Return failure if the sound cannot load
if( hr == SOUNDERROR_LOAD ) {
return( 0 );
}
// Return success
return( 1 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -