Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
8.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // The copyright to the contents herein is the property of Valve, L.L.C.
  4. // The contents may be used and/or copied only with the written permission of
  5. // Valve, L.L.C., or in accordance with the terms and conditions stipulated in
  6. // the agreement/contract under which the contents have been supplied.
  7. //
  8. // $Header: $
  9. // $NoKeywords: $
  10. //
  11. // Sound unit test application
  12. //
  13. //=============================================================================
  14. #include <windows.h>
  15. #include "tier0/dbg.h"
  16. #include "tier0/icommandline.h"
  17. #include "filesystem.h"
  18. #include "datacache/idatacache.h"
  19. #include "appframework/appframework.h"
  20. #include "soundsystem/isoundsystem.h"
  21. #include "vstdlib/cvar.h"
  22. #include "filesystem_init.h"
  23. //-----------------------------------------------------------------------------
  24. // Main system interfaces
  25. //-----------------------------------------------------------------------------
  26. IFileSystem *g_pFileSystem;
  27. ISoundSystem *g_pSoundSystem;
  28. //-----------------------------------------------------------------------------
  29. // Standard spew functions
  30. //-----------------------------------------------------------------------------
  31. static SpewRetval_t SoundTestOutputFunc( SpewType_t spewType, char const *pMsg )
  32. {
  33. printf( pMsg );
  34. fflush( stdout );
  35. if (spewType == SPEW_ERROR)
  36. return SPEW_ABORT;
  37. return (spewType == SPEW_ASSERT) ? SPEW_DEBUGGER : SPEW_CONTINUE;
  38. }
  39. //-----------------------------------------------------------------------------
  40. // The application object
  41. //-----------------------------------------------------------------------------
  42. class CSoundTestApp : public CDefaultAppSystemGroup<CSteamAppSystemGroup>
  43. {
  44. public:
  45. // Methods of IApplication
  46. virtual bool Create();
  47. virtual bool PreInit();
  48. virtual int Main();
  49. virtual void PostShutdown();
  50. virtual void Destroy();
  51. private:
  52. bool CreateAppWindow( char const *pTitle, bool bWindowed, int w, int h );
  53. bool SetupSearchPaths();
  54. void AppPumpMessages();
  55. // Windproc
  56. static LONG WINAPI WinAppWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  57. LONG WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  58. HWND m_hWnd;
  59. bool m_bExitMainLoop;
  60. };
  61. static CSoundTestApp s_SoundTestApp;
  62. DEFINE_WINDOWED_STEAM_APPLICATION_OBJECT_GLOBALVAR( CSoundTestApp, s_SoundTestApp );
  63. //-----------------------------------------------------------------------------
  64. // The application object
  65. //-----------------------------------------------------------------------------
  66. bool CSoundTestApp::Create()
  67. {
  68. SpewOutputFunc( SoundTestOutputFunc );
  69. // Add in the cvar factory
  70. AppModule_t cvarModule = LoadModule( VStdLib_GetICVarFactory() );
  71. AddSystem( cvarModule, CVAR_INTERFACE_VERSION );
  72. AppSystemInfo_t appSystems[] =
  73. {
  74. { "datacache.dll", DATACACHE_INTERFACE_VERSION },
  75. { "soundsystem.dll", SOUNDSYSTEM_INTERFACE_VERSION },
  76. { "", "" } // Required to terminate the list
  77. };
  78. if ( !AddSystems( appSystems ) )
  79. return false;
  80. g_pFileSystem = (IFileSystem*)FindSystem( FILESYSTEM_INTERFACE_VERSION );
  81. g_pSoundSystem = (ISoundSystem*)FindSystem( SOUNDSYSTEM_INTERFACE_VERSION );
  82. return ( g_pFileSystem && g_pSoundSystem );
  83. }
  84. void CSoundTestApp::Destroy()
  85. {
  86. g_pFileSystem = NULL;
  87. g_pSoundSystem = NULL;
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Window management
  91. //-----------------------------------------------------------------------------
  92. bool CSoundTestApp::CreateAppWindow( char const *pTitle, bool bWindowed, int w, int h )
  93. {
  94. WNDCLASSEX wc;
  95. memset( &wc, 0, sizeof( wc ) );
  96. wc.cbSize = sizeof( wc );
  97. wc.style = CS_OWNDC | CS_DBLCLKS;
  98. wc.lpfnWndProc = WinAppWindowProc;
  99. wc.hInstance = (HINSTANCE)GetAppInstance();
  100. wc.lpszClassName = "Valve001";
  101. wc.hIcon = NULL; //LoadIcon( s_HInstance, MAKEINTRESOURCE( IDI_LAUNCHER ) );
  102. wc.hIconSm = wc.hIcon;
  103. RegisterClassEx( &wc );
  104. // Note, it's hidden
  105. DWORD style = WS_POPUP | WS_CLIPSIBLINGS;
  106. if ( bWindowed )
  107. {
  108. // Give it a frame
  109. style |= WS_OVERLAPPEDWINDOW;
  110. style &= ~WS_THICKFRAME;
  111. }
  112. // Never a max box
  113. style &= ~WS_MAXIMIZEBOX;
  114. RECT windowRect;
  115. windowRect.top = 0;
  116. windowRect.left = 0;
  117. windowRect.right = w;
  118. windowRect.bottom = h;
  119. // Compute rect needed for that size client area based on window style
  120. AdjustWindowRectEx(&windowRect, style, FALSE, 0);
  121. // Create the window
  122. m_hWnd = CreateWindow( wc.lpszClassName, pTitle, style, 0, 0,
  123. windowRect.right - windowRect.left, windowRect.bottom - windowRect.top,
  124. NULL, NULL, (HINSTANCE)GetAppInstance(), NULL );
  125. if ( m_hWnd == INVALID_HANDLE_VALUE )
  126. return false;
  127. int CenterX, CenterY;
  128. CenterX = (GetSystemMetrics(SM_CXSCREEN) - w) / 2;
  129. CenterY = (GetSystemMetrics(SM_CYSCREEN) - h) / 2;
  130. CenterX = (CenterX < 0) ? 0: CenterX;
  131. CenterY = (CenterY < 0) ? 0: CenterY;
  132. // In VCR modes, keep it in the upper left so mouse coordinates are always relative to the window.
  133. SetWindowPos (m_hWnd, NULL, CenterX, CenterY, 0, 0,
  134. SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
  135. return true;
  136. }
  137. //-----------------------------------------------------------------------------
  138. // Message handler
  139. //-----------------------------------------------------------------------------
  140. LONG CSoundTestApp::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  141. {
  142. if ( uMsg == WM_CLOSE )
  143. {
  144. m_bExitMainLoop = true;
  145. }
  146. return DefWindowProc( hWnd, uMsg, wParam, lParam );
  147. }
  148. LONG WINAPI CSoundTestApp::WinAppWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  149. {
  150. return s_SoundTestApp.WindowProc( hWnd, uMsg, wParam, lParam );
  151. }
  152. //-----------------------------------------------------------------------------
  153. // Sets up the game path
  154. //-----------------------------------------------------------------------------
  155. bool CSoundTestApp::SetupSearchPaths()
  156. {
  157. CFSSteamSetupInfo steamInfo;
  158. steamInfo.m_pDirectoryName = NULL;
  159. steamInfo.m_bOnlyUseDirectoryName = false;
  160. steamInfo.m_bToolsMode = true;
  161. steamInfo.m_bSetSteamDLLPath = true;
  162. steamInfo.m_bSteam = g_pFileSystem->IsSteam();
  163. if ( FileSystem_SetupSteamEnvironment( steamInfo ) != FS_OK )
  164. return false;
  165. CFSMountContentInfo fsInfo;
  166. fsInfo.m_pFileSystem = g_pFileSystem;
  167. fsInfo.m_bToolsMode = true;
  168. fsInfo.m_pDirectoryName = steamInfo.m_GameInfoPath;
  169. if ( FileSystem_MountContent( fsInfo ) != FS_OK )
  170. return false;
  171. // Finally, load the search paths for the "GAME" path.
  172. CFSSearchPathsInit searchPathsInit;
  173. searchPathsInit.m_pDirectoryName = steamInfo.m_GameInfoPath;
  174. searchPathsInit.m_pFileSystem = g_pFileSystem;
  175. if ( FileSystem_LoadSearchPaths( searchPathsInit ) != FS_OK )
  176. return false;
  177. g_pFileSystem->AddSearchPath( steamInfo.m_GameInfoPath, "SKIN", PATH_ADD_TO_HEAD );
  178. FileSystem_AddSearchPath_Platform( g_pFileSystem, steamInfo.m_GameInfoPath );
  179. // and now add episodic to the GAME searchpath
  180. char shorts[MAX_PATH];
  181. Q_strncpy( shorts, steamInfo.m_GameInfoPath, MAX_PATH );
  182. Q_StripTrailingSlash( shorts );
  183. Q_strncat( shorts, "/../episodic", MAX_PATH, MAX_PATH );
  184. g_pFileSystem->AddSearchPath( shorts, "GAME", PATH_ADD_TO_HEAD );
  185. return true;
  186. }
  187. //-----------------------------------------------------------------------------
  188. // PreInit, PostShutdown
  189. //-----------------------------------------------------------------------------
  190. bool CSoundTestApp::PreInit( )
  191. {
  192. // Add paths...
  193. if ( !SetupSearchPaths() )
  194. return false;
  195. const char *pArg;
  196. int iWidth = 1024;
  197. int iHeight = 768;
  198. bool bWindowed = (CommandLine()->CheckParm( "-fullscreen" ) == NULL);
  199. if (CommandLine()->CheckParm( "-width", &pArg ))
  200. {
  201. iWidth = atoi( pArg );
  202. }
  203. if (CommandLine()->CheckParm( "-height", &pArg ))
  204. {
  205. iHeight = atoi( pArg );
  206. }
  207. if ( !CreateAppWindow( "SoundTest", bWindowed, iWidth, iHeight ) )
  208. return false;
  209. return true;
  210. }
  211. void CSoundTestApp::PostShutdown()
  212. {
  213. }
  214. //-----------------------------------------------------------------------------
  215. // Pump messages
  216. //-----------------------------------------------------------------------------
  217. void CSoundTestApp::AppPumpMessages()
  218. {
  219. MSG msg;
  220. while ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  221. {
  222. TranslateMessage( &msg );
  223. DispatchMessage( &msg );
  224. }
  225. }
  226. //-----------------------------------------------------------------------------
  227. // The application object
  228. //-----------------------------------------------------------------------------
  229. int CSoundTestApp::Main()
  230. {
  231. CAudioSource *pSource = g_pSoundSystem->LoadSound( "sound/ambient/alarms/alarm1.wav" );
  232. CAudioMixer *pMixer;
  233. g_pSoundSystem->PlaySound( pSource, 1.0f, &pMixer );
  234. m_bExitMainLoop = false;
  235. while ( !m_bExitMainLoop )
  236. {
  237. AppPumpMessages();
  238. g_pSoundSystem->Update( Plat_FloatTime() );
  239. }
  240. return 1;
  241. }