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.

960 lines
20 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. /*
  9. glapp.c - Simple OpenGL shell
  10. There are several options allowed on the command line. They are:
  11. -height : what window/screen height do you want to use?
  12. -width : what window/screen width do you want to use?
  13. -bpp : what color depth do you want to use?
  14. -window : create a rendering window rather than full-screen
  15. -fov : use a field of view other than 90 degrees
  16. */
  17. #include "stdafx.h"
  18. #pragma warning(disable:4305)
  19. #pragma warning(disable:4244)
  20. #include <windows.h>
  21. #include <math.h>
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <mmsystem.h>
  26. #include "matsysapp.h"
  27. #include "cmdlib.h"
  28. #include "mathlib/mathlib.h"
  29. #include "materialsystem/imaterialproxyfactory.h"
  30. #include "filesystem.h"
  31. #include "materialsystem/imaterialproxy.h"
  32. #include "materialsystem/MaterialSystem_Config.h"
  33. #include "tier0/icommandline.h"
  34. #include "filesystem_tools.h"
  35. #include "materialsystem/imesh.h"
  36. #include "vstdlib/cvar.h"
  37. static int g_nCapture = 0;
  38. #define OSR2_BUILD_NUMBER 1111
  39. #define M_PI 3.14159265358979323846 // matches value in gcc v2 math.h
  40. SpewRetval_t MatSysAppSpewFunc( SpewType_t type, char const *pMsg )
  41. {
  42. printf( "%s", pMsg );
  43. OutputDebugString( pMsg );
  44. if( type == SPEW_ASSERT )
  45. return SPEW_DEBUGGER;
  46. else if( type == SPEW_ERROR )
  47. return SPEW_ABORT;
  48. else
  49. return SPEW_CONTINUE;
  50. }
  51. class MatSysAppMaterialProxyFactory : public IMaterialProxyFactory
  52. {
  53. public:
  54. virtual IMaterialProxy *CreateProxy( const char *proxyName )
  55. {
  56. CreateInterfaceFn clientFactory = Sys_GetFactoryThis();
  57. if( !clientFactory )
  58. {
  59. return NULL;
  60. }
  61. // allocate exactly enough memory for the versioned name on the stack.
  62. char proxyVersionedName[1024];
  63. strcpy( proxyVersionedName, proxyName );
  64. strcat( proxyVersionedName, IMATERIAL_PROXY_INTERFACE_VERSION );
  65. IMaterialProxy *materialProxy;
  66. materialProxy = ( IMaterialProxy * )clientFactory( proxyVersionedName, NULL );
  67. if( !materialProxy )
  68. {
  69. return NULL;
  70. }
  71. return materialProxy;
  72. }
  73. virtual void DeleteProxy( IMaterialProxy *pProxy )
  74. {
  75. if( pProxy )
  76. {
  77. pProxy->Release();
  78. }
  79. }
  80. };
  81. MatSysAppMaterialProxyFactory g_MatSysAppMaterialProxyFactory;
  82. MaterialSystemApp g_MaterialSystemApp;
  83. float fAngle = 0.0f;
  84. char *szFSDesc[] = { "Windowed", "Full Screen" };
  85. extern "C" unsigned int g_Time;
  86. unsigned int g_Time = 0;
  87. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
  88. {
  89. return g_MaterialSystemApp.WinMain(hInstance, hPrevInstance, szCmdLine, iCmdShow);
  90. }
  91. BOOL isdigits( char *s )
  92. {
  93. int i;
  94. for (i = 0; s[i]; i++)
  95. {
  96. if ((s[i] > '9') || (s[i] < '0'))
  97. {
  98. return FALSE;
  99. }
  100. }
  101. return TRUE;
  102. }
  103. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  104. {
  105. return g_MaterialSystemApp.WndProc(hwnd, iMsg, wParam, lParam);
  106. }
  107. // This function builds a list the screen resolutions supported by the display driver
  108. static void BuildModeList(screen_res_t* &pResolutions, int &iResCount)
  109. {
  110. DEVMODE dm;
  111. int mode;
  112. mode = 0;
  113. while(EnumDisplaySettings(NULL, mode, &dm))
  114. {
  115. mode++;
  116. }
  117. pResolutions = (screen_res_t *)malloc(sizeof(screen_res_t)*mode);
  118. mode = 0;
  119. while(EnumDisplaySettings(NULL, mode, &dm))
  120. {
  121. pResolutions[mode].width = dm.dmPelsWidth;
  122. pResolutions[mode].height = dm.dmPelsHeight;
  123. pResolutions[mode].bpp = dm.dmBitsPerPel;
  124. pResolutions[mode].flags = dm.dmDisplayFlags;
  125. pResolutions[mode].frequency = dm.dmDisplayFrequency;
  126. mode++;
  127. }
  128. iResCount = mode;
  129. }
  130. bool Sys_Error(const char *pMsg, ...)
  131. {
  132. va_list marker;
  133. char msg[4096];
  134. va_start(marker, pMsg);
  135. vsprintf(msg, pMsg, marker);
  136. va_end(marker);
  137. MessageBox(NULL, msg, "FATAL ERROR", MB_OK);
  138. g_MaterialSystemApp.Term();
  139. exit(1);
  140. return false;
  141. }
  142. void con_Printf(const char *pMsg, ...)
  143. {
  144. char msg[2048];
  145. va_list marker;
  146. va_start(marker, pMsg);
  147. vsprintf(msg, pMsg, marker);
  148. va_end(marker);
  149. OutputDebugString(msg);
  150. }
  151. bool MSA_IsKeyDown(char key)
  152. {
  153. return !!(GetAsyncKeyState(key) & 0x8000);
  154. }
  155. bool MSA_IsMouseButtonDown( int button )
  156. {
  157. if( button == MSA_BUTTON_LEFT )
  158. return !!(GetAsyncKeyState(VK_LBUTTON) & 0x8000);
  159. else
  160. return !!(GetAsyncKeyState(VK_RBUTTON) & 0x8000);
  161. }
  162. void MSA_Sleep(unsigned long count)
  163. {
  164. if(count > 0)
  165. Sleep(count);
  166. }
  167. static void MaterialSystem_Error( char *fmt, ... )
  168. {
  169. char str[4096];
  170. va_list marker;
  171. va_start(marker, fmt);
  172. vsprintf(str, fmt, marker);
  173. va_end(marker);
  174. Sys_Error(str);
  175. }
  176. static void MaterialSystem_Warning( char *fmt, ... )
  177. {
  178. }
  179. void InitMaterialSystemConfig(MaterialSystem_Config_t *pConfig)
  180. {
  181. memset( pConfig, 0, sizeof(*pConfig) );
  182. // pConfig->screenGamma = 2.2f;
  183. // pConfig->texGamma = 2.2;
  184. // pConfig->overbright = 2;
  185. pConfig->bAllowCheats = false;
  186. // pConfig->bLinearFrameBuffer = false;
  187. pConfig->skipMipLevels = 0;
  188. // pConfig->lightScale = 1.0f;
  189. pConfig->bFilterLightmaps = true;
  190. pConfig->bFilterTextures = true;
  191. pConfig->bMipMapTextures = true;
  192. pConfig->nShowMipLevels = 0;
  193. pConfig->bReverseDepth = false;
  194. pConfig->bCompressedTextures = true;
  195. // pConfig->bBumpmap = true;
  196. pConfig->bShowSpecular = true;
  197. pConfig->bShowDiffuse = true;
  198. // pConfig->maxFrameLatency = 1;
  199. pConfig->bDrawFlat = false;
  200. // pConfig->bLightingOnly = false;
  201. pConfig->bSoftwareLighting = false;
  202. pConfig->bEditMode = false; // No, we're not in WorldCraft.
  203. // pConfig->m_bForceTrilinear = false;
  204. pConfig->m_nForceAnisotropicLevel = 0;
  205. // pConfig->m_bForceBilinear = false;
  206. }
  207. /*
  208. ====================
  209. CalcFov
  210. ====================
  211. */
  212. float CalcFov (float fov_x, float width, float height)
  213. {
  214. float a;
  215. float x;
  216. if (fov_x < 1 || fov_x > 179)
  217. fov_x = 90; // error, set to 90
  218. x = width/tan(fov_x/360*M_PI);
  219. a = atan (height/x);
  220. a = a*360/M_PI;
  221. return a;
  222. }
  223. MaterialSystemApp::MaterialSystemApp()
  224. {
  225. Clear();
  226. }
  227. MaterialSystemApp::~MaterialSystemApp()
  228. {
  229. Term();
  230. }
  231. void MaterialSystemApp::Term()
  232. {
  233. int i;
  234. // Free the command line holder memory
  235. if (m_argc > 0)
  236. {
  237. // Free in reverse order of allocation
  238. for (i = (m_argc-1); i >= 0; i--)
  239. {
  240. free(m_argv[i]);
  241. }
  242. // Free the parameter "pockets"
  243. free(m_argv);
  244. }
  245. // Free the memory that holds the video resolution list
  246. if (m_pResolutions)
  247. free(m_pResolutions);
  248. if (m_hDC)
  249. {
  250. if (!ReleaseDC((HWND)m_hWnd, (HDC)m_hDC))
  251. {
  252. MessageBox(NULL, "ShutdownOpenGL - ReleaseDC failed\n", "ERROR", MB_OK);
  253. }
  254. m_hDC = NULL;
  255. }
  256. if (m_bFullScreen)
  257. {
  258. ChangeDisplaySettings( 0, 0 );
  259. }
  260. Clear();
  261. }
  262. void MaterialSystemApp::Clear()
  263. {
  264. m_pMaterialSystem = NULL;
  265. m_hMaterialSystemInst = 0;
  266. m_hInstance = 0;
  267. m_iCmdShow = 0;
  268. m_hWnd = 0;
  269. m_hDC = 0;
  270. m_bActive = false;
  271. m_bFullScreen = false;
  272. m_width = m_height = 0;
  273. m_centerx = m_centery = 0;
  274. m_bpp = 0;
  275. m_bChangeBPP = false;
  276. m_bAllowSoft = 0;
  277. g_nCapture = 0;
  278. m_szCmdLine = 0;
  279. m_argc = 0;
  280. m_argv = 0;
  281. m_glnWidth = 0;
  282. m_glnHeight = 0;
  283. m_gldAspect = 0;
  284. m_NearClip = m_FarClip = 0;
  285. m_fov = 90;
  286. m_pResolutions = 0;
  287. m_iResCount = 0;
  288. m_iVidMode = 0;
  289. }
  290. int MaterialSystemApp::WinMain(void *hInstance, void *hPrevInstance, char *szCmdLine, int iCmdShow)
  291. {
  292. MSG msg;
  293. HDC hdc;
  294. memset(&msg,0,sizeof(msg));
  295. CommandLine()->CreateCmdLine( Plat_GetCommandLine() );
  296. // Not changable by user
  297. m_hInstance = hInstance;
  298. m_iCmdShow = iCmdShow;
  299. m_pResolutions = 0;
  300. m_NearClip = 8.0f;
  301. m_FarClip = 28400.0f;
  302. // User definable
  303. m_fov = 90.0f;
  304. m_bAllowSoft = FALSE;
  305. m_bFullScreen = TRUE;
  306. // Get the current display device info
  307. hdc = GetDC( NULL );
  308. m_DevInfo.bpp = GetDeviceCaps(hdc, BITSPIXEL);
  309. m_DevInfo.width = GetSystemMetrics(SM_CXSCREEN);
  310. m_DevInfo.height = GetSystemMetrics(SM_CYSCREEN);
  311. ReleaseDC(NULL, hdc);
  312. // Parse the command line if there is one
  313. m_argc = 0;
  314. if (strlen(szCmdLine) > 0)
  315. {
  316. m_szCmdLine = szCmdLine;
  317. GetParameters();
  318. }
  319. // Default to 640 pixels wide
  320. m_width = FindNumParameter("-width", 640);
  321. m_height = FindNumParameter("-height", 480);
  322. m_bpp = FindNumParameter("-bpp", 32);
  323. m_fov = FindNumParameter("-fov", 90);
  324. // Check for windowed rendering
  325. m_bFullScreen = FALSE;
  326. if (FindParameter("-fullscreen"))
  327. {
  328. m_bFullScreen = TRUE;
  329. }
  330. // Build up the video mode list
  331. BuildModeList(m_pResolutions, m_iResCount);
  332. // Create the main program window, start up OpenGL and create our viewport
  333. if (CreateMainWindow( m_width, m_height, m_bpp, m_bFullScreen) != TRUE)
  334. {
  335. ChangeDisplaySettings(0, 0);
  336. MessageBox(NULL, "Unable to create main window.\nProgram will now end.", "FATAL ERROR", MB_OK);
  337. Term();
  338. return 0;
  339. }
  340. // Turn the cursor off for full-screen mode
  341. if (m_bFullScreen == TRUE)
  342. {
  343. // Probably want to do this all the time anyway
  344. ShowCursor(FALSE);
  345. }
  346. // We're live now
  347. m_bActive = TRUE;
  348. // Define this funciton to init your app
  349. AppInit();
  350. RECT rect;
  351. GetWindowRect( (HWND)m_hWnd, &rect );
  352. m_centerx = ( rect.left + rect.right ) / 2;
  353. m_centery = ( rect.top + rect.bottom ) / 2;
  354. // Begin the main program loop
  355. while (m_bActive == TRUE)
  356. {
  357. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  358. {
  359. TranslateMessage (&msg);
  360. DispatchMessage (&msg);
  361. }
  362. if (m_pMaterialSystem)
  363. {
  364. RenderScene();
  365. }
  366. }
  367. if (m_bFullScreen == TRUE)
  368. {
  369. ShowCursor(TRUE);
  370. }
  371. // Release the parameter and video resolution lists
  372. Term();
  373. // Tell the app to cleanup.
  374. AppExit();
  375. return msg.wParam;
  376. }
  377. long MaterialSystemApp::WndProc(void *inhwnd, long iMsg, long wParam, long lParam)
  378. {
  379. if(inhwnd != m_hWnd)
  380. {
  381. return DefWindowProc((HWND)inhwnd, iMsg, wParam, lParam);
  382. }
  383. HWND hwnd = (HWND)inhwnd;
  384. switch (iMsg)
  385. {
  386. case WM_CHAR:
  387. switch(wParam)
  388. {
  389. case VK_ESCAPE:
  390. SendMessage(hwnd, WM_CLOSE, 0, 0);
  391. break;
  392. }
  393. AppChar( wParam );
  394. break;
  395. case WM_KEYDOWN:
  396. AppKey( wParam, true );
  397. break;
  398. case WM_KEYUP:
  399. AppKey( wParam, false );
  400. break;
  401. case WM_ACTIVATE:
  402. if ((LOWORD(wParam) != WA_INACTIVE) && ((HWND)lParam == NULL))
  403. {
  404. ShowWindow(hwnd, SW_RESTORE);
  405. SetForegroundWindow(hwnd);
  406. }
  407. else
  408. {
  409. if (m_bFullScreen)
  410. {
  411. ShowWindow(hwnd, SW_MINIMIZE);
  412. }
  413. }
  414. return 0;
  415. case WM_SETFOCUS:
  416. if(g_bCaptureOnFocus)
  417. {
  418. MouseCapture();
  419. }
  420. break;
  421. case WM_KILLFOCUS:
  422. if(g_bCaptureOnFocus)
  423. {
  424. MouseRelease();
  425. }
  426. break;
  427. case WM_LBUTTONDOWN:
  428. case WM_RBUTTONDOWN:
  429. {
  430. if(!g_bCaptureOnFocus)
  431. {
  432. g_nCapture++;
  433. MouseCapture();
  434. }
  435. }
  436. break;
  437. case WM_LBUTTONUP:
  438. case WM_RBUTTONUP:
  439. {
  440. if(!g_bCaptureOnFocus)
  441. {
  442. g_nCapture--;
  443. MouseRelease();
  444. }
  445. }
  446. break;
  447. case WM_CLOSE:
  448. Term();
  449. m_bActive = FALSE;
  450. break;
  451. case WM_DESTROY:
  452. PostQuitMessage (0);
  453. return 0;
  454. }
  455. return DefWindowProc (hwnd, iMsg, wParam, lParam);
  456. }
  457. bool MaterialSystemApp::InitMaterialSystem()
  458. {
  459. RECT rect;
  460. // Init libraries.
  461. MathLib_Init( true, true, true, 2.2f, 2.2f, 0.0f, 2.0f );
  462. SpewOutputFunc( MatSysAppSpewFunc );
  463. if ((m_hDC = GetDC((HWND)m_hWnd)) == NULL)
  464. {
  465. ChangeDisplaySettings(0, 0);
  466. MessageBox(NULL, "GetDC on main window failed", "FATAL ERROR", MB_OK);
  467. return FALSE;
  468. }
  469. // Load the material system DLL and get its interface.
  470. char *pDLLName = "MaterialSystem.dll";
  471. m_hMaterialSystemInst = LoadLibrary( pDLLName );
  472. if( !m_hMaterialSystemInst )
  473. {
  474. return Sys_Error( "Can't load MaterialSystem.dll\n" );
  475. }
  476. CreateInterfaceFn clientFactory = Sys_GetFactory( pDLLName );
  477. if ( clientFactory )
  478. {
  479. m_pMaterialSystem = (IMaterialSystem *)clientFactory( MATERIAL_SYSTEM_INTERFACE_VERSION, NULL );
  480. if ( !m_pMaterialSystem )
  481. {
  482. return Sys_Error( "Could not get the material system interface from materialsystem.dll" );
  483. }
  484. }
  485. else
  486. {
  487. return Sys_Error( "Could not find factory interface in library MaterialSystem.dll" );
  488. }
  489. const char *pPath = CommandLine()->ParmValue("-game");
  490. if(!pPath)
  491. {
  492. // If they didn't specify -game on the command line, use VPROJECT.
  493. CmdLib_InitFileSystem( "." );
  494. pPath = ".";
  495. }
  496. else
  497. {
  498. CmdLib_InitFileSystem( pPath );
  499. }
  500. // BUGBUG: Can't figure out why this is broken. EXECUTABLE_PATH should already be there
  501. // but it isn't so the shader system is computing texture memory on each run...
  502. g_pFullFileSystem->AddSearchPath( "U:\\main\\game\\bin", "EXECUTABLE_PATH", PATH_ADD_TO_TAIL );
  503. const char *pShaderDLL = FindParameterArg("-shaderdll");
  504. char defaultShaderDLL[256];
  505. if(!pShaderDLL)
  506. {
  507. strcpy(defaultShaderDLL, "shaderapidx9.dll");
  508. pShaderDLL = defaultShaderDLL;
  509. }
  510. if(!m_pMaterialSystem->Init(pShaderDLL, &g_MatSysAppMaterialProxyFactory, CmdLib_GetFileSystemFactory()))
  511. return Sys_Error("IMaterialSystem::Init failed");
  512. MaterialSystem_Config_t config;
  513. InitMaterialSystemConfig(&config);
  514. config.SetFlag( MATSYS_VIDCFG_FLAGS_WINDOWED, true );
  515. config.SetFlag( MATSYS_VIDCFG_FLAGS_RESIZING, true );
  516. if(!m_pMaterialSystem->SetMode(m_hWnd, config))
  517. return Sys_Error("IMaterialSystem::SetMode failed");
  518. m_pMaterialSystem->OverrideConfig(config, false);
  519. GetClientRect((HWND)m_hWnd, &rect);
  520. m_glnWidth= rect.right;
  521. m_glnHeight = rect.bottom;
  522. m_gldAspect = (float)m_glnWidth / m_glnHeight;
  523. GetWindowRect( (HWND)m_hWnd, &rect );
  524. m_centerx = (rect.left + rect.right) / 2;
  525. m_centery = (rect.top + rect.bottom) / 2;
  526. return true;
  527. }
  528. bool MaterialSystemApp::CreateMainWindow(int width, int height, int bpp, bool fullscreen)
  529. {
  530. HWND hwnd;
  531. WNDCLASSEX wndclass;
  532. DWORD dwStyle, dwExStyle;
  533. int x, y, sx, sy, ex, ey, ty;
  534. if ((hwnd = FindWindow(g_szAppName, g_szAppName)) != NULL)
  535. {
  536. SetForegroundWindow(hwnd);
  537. return 0;
  538. }
  539. wndclass.cbSize = sizeof (wndclass);
  540. wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  541. wndclass.lpfnWndProc = ::WndProc;
  542. wndclass.cbClsExtra = 0;
  543. wndclass.cbWndExtra = 0;
  544. wndclass.hInstance = (HINSTANCE)m_hInstance;
  545. wndclass.hIcon = 0;
  546. wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  547. wndclass.hbrBackground = (HBRUSH)COLOR_GRAYTEXT;
  548. wndclass.lpszMenuName = NULL;
  549. wndclass.lpszClassName = g_szAppName;
  550. wndclass.hIconSm = 0;
  551. if (!RegisterClassEx (&wndclass))
  552. {
  553. MessageBox(NULL, "Window class registration failed.", "FATAL ERROR", MB_OK);
  554. return FALSE;
  555. }
  556. if (fullscreen)
  557. {
  558. dwExStyle = WS_EX_TOPMOST;
  559. dwStyle = WS_POPUP | WS_VISIBLE;
  560. x = y = 0;
  561. sx = m_width;
  562. sy = m_height;
  563. }
  564. else
  565. {
  566. dwExStyle = 0;
  567. //dwStyle = WS_CAPTION | WS_SYSMENU | WS_THICKFRAME; // Use this if you want a "normal" window
  568. dwStyle = WS_CAPTION;
  569. ex = GetSystemMetrics(SM_CXEDGE);
  570. ey = GetSystemMetrics(SM_CYEDGE);
  571. ty = GetSystemMetrics(SM_CYSIZE);
  572. // Center the window on the screen
  573. x = (m_DevInfo.width / 2) - ((m_width+(2*ex)) / 2);
  574. y = (m_DevInfo.height / 2) - ((m_height+(2*ey)+ty) / 2);
  575. sx = m_width+(2*ex);
  576. sy = m_height+(2*ey)+ty;
  577. /*
  578. Check to be sure the requested window size fits on the screen and
  579. adjust each dimension to fit if the requested size does not fit.
  580. */
  581. if (sx >= m_DevInfo.width)
  582. {
  583. x = 0;
  584. sx = m_DevInfo.width-(2*ex);
  585. }
  586. if (sy >= m_DevInfo.height)
  587. {
  588. y = 0;
  589. sy = m_DevInfo.height-((2*ey)+ty);
  590. }
  591. }
  592. if ((hwnd = CreateWindowEx (dwExStyle,
  593. g_szAppName, // window class name
  594. g_szAppName, // window caption
  595. dwStyle | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, // window style
  596. x, // initial x position
  597. y, // initial y position
  598. sx, // initial x size
  599. sy, // initial y size
  600. NULL, // parent window handle
  601. NULL, // window menu handle
  602. (HINSTANCE)m_hInstance, // program instance handle
  603. NULL)) // creation parameters
  604. == NULL)
  605. {
  606. ChangeDisplaySettings(0, 0);
  607. MessageBox(NULL, "Window creation failed.", "FATAL ERROR", MB_OK);
  608. return FALSE;
  609. }
  610. m_hWnd = hwnd;
  611. if (!InitMaterialSystem())
  612. {
  613. m_hWnd = NULL;
  614. return FALSE;
  615. }
  616. ShowWindow((HWND)m_hWnd, m_iCmdShow);
  617. UpdateWindow((HWND)m_hWnd);
  618. SetForegroundWindow((HWND)m_hWnd);
  619. SetFocus((HWND)m_hWnd);
  620. return TRUE;
  621. }
  622. void MaterialSystemApp::RenderScene()
  623. {
  624. if(!m_pMaterialSystem)
  625. return;
  626. static DWORD lastTime = 0;
  627. POINT cursorPoint;
  628. float deltax = 0, deltay = 0, frametime;
  629. DWORD newTime = GetTickCount();
  630. DWORD deltaTime = newTime - lastTime;
  631. if ( deltaTime > 1000 )
  632. deltaTime = 0;
  633. lastTime = newTime;
  634. frametime = (float) ((double)deltaTime * 0.001);
  635. g_Time = newTime;
  636. if ( g_nCapture )
  637. {
  638. GetCursorPos( &cursorPoint );
  639. SetCursorPos( m_centerx, m_centery );
  640. deltax = (cursorPoint.x - m_centerx) * 0.1f;
  641. deltay = (cursorPoint.y - m_centery) * -0.1f;
  642. }
  643. else
  644. {
  645. deltax = deltay = 0;
  646. }
  647. CMatRenderContextPtr pRenderContext(m_pMaterialSystem);
  648. m_pMaterialSystem->BeginFrame(deltaTime);
  649. pRenderContext->ClearBuffers(true, true);
  650. pRenderContext->Viewport( 0, 0, m_width, m_height );
  651. pRenderContext->MatrixMode(MATERIAL_PROJECTION);
  652. pRenderContext->LoadIdentity();
  653. pRenderContext->PerspectiveX(m_fov, m_gldAspect, m_NearClip, m_FarClip);
  654. pRenderContext->MatrixMode(MATERIAL_VIEW);
  655. pRenderContext->LoadIdentity();
  656. pRenderContext->MatrixMode(MATERIAL_MODEL);
  657. pRenderContext->LoadIdentity();
  658. AppRender( frametime, deltax, deltay );
  659. m_pMaterialSystem->SwapBuffers();
  660. m_pMaterialSystem->EndFrame();
  661. }
  662. void MaterialSystemApp::MouseCapture()
  663. {
  664. SetCapture( (HWND)m_hWnd );
  665. ShowCursor(FALSE);
  666. SetCursorPos( m_centerx, m_centery );
  667. }
  668. void MaterialSystemApp::MouseRelease()
  669. {
  670. ShowCursor(TRUE);
  671. ReleaseCapture();
  672. SetCursorPos( m_centerx, m_centery );
  673. }
  674. void MaterialSystemApp::GetParameters()
  675. {
  676. int count;
  677. char *s, *tstring;
  678. // Make a copy of the command line to count the parameters - strtok is destructive
  679. tstring = (char *)malloc(sizeof(char)*(strlen(m_szCmdLine)+1));
  680. strcpy(tstring, m_szCmdLine);
  681. // Count the parameters
  682. s = strtok(tstring, " ");
  683. count = 1;
  684. while (strtok(NULL, " ") != NULL)
  685. {
  686. count++;
  687. }
  688. free(tstring);
  689. // Allocate "pockets" for the parameters
  690. m_argv = (char **)malloc(sizeof(char*)*(count+1));
  691. // Copy first parameter into the "pockets"
  692. m_argc = 0;
  693. s = strtok(m_szCmdLine, " ");
  694. m_argv[m_argc] = (char *)malloc(sizeof(char)*(strlen(s)+1));
  695. strcpy(m_argv[m_argc], s);
  696. m_argc++;
  697. // Copy the rest of the parameters
  698. do
  699. {
  700. // get the next token
  701. s = strtok(NULL, " ");
  702. if (s != NULL)
  703. {
  704. // add it to the list
  705. m_argv[m_argc] = (char *)malloc(sizeof(char)*(strlen(s)+1));
  706. strcpy(m_argv[m_argc], s);
  707. m_argc++;
  708. }
  709. }
  710. while (s != NULL);
  711. }
  712. int MaterialSystemApp::FindNumParameter(const char *s, int defaultVal)
  713. {
  714. int i;
  715. for (i = 0; i < (m_argc-1); i++)
  716. {
  717. if (stricmp(m_argv[i], s) == 0)
  718. {
  719. if (isdigits(m_argv[i+1]))
  720. {
  721. return(atoi(m_argv[i+1]));
  722. }
  723. else
  724. {
  725. return defaultVal;
  726. }
  727. }
  728. }
  729. return defaultVal;
  730. }
  731. bool MaterialSystemApp::FindParameter(const char *s)
  732. {
  733. int i;
  734. for (i = 0; i < m_argc; i++)
  735. {
  736. if (stricmp(m_argv[i], s) == 0)
  737. {
  738. return true;
  739. }
  740. }
  741. return false;
  742. }
  743. const char *MaterialSystemApp::FindParameterArg( const char *s )
  744. {
  745. int i;
  746. for (i = 0; i < m_argc; i++)
  747. {
  748. if (stricmp(m_argv[i], s) == 0)
  749. {
  750. if( (i+1) < m_argc )
  751. return m_argv[i+1];
  752. else
  753. return "";
  754. }
  755. }
  756. return NULL;
  757. }
  758. void MaterialSystemApp::SetTitleText(const char *fmt, ...)
  759. {
  760. char str[4096];
  761. va_list marker;
  762. va_start(marker, fmt);
  763. vsprintf(str, fmt, marker);
  764. va_end(marker);
  765. ::SetWindowText((HWND)m_hWnd, str);
  766. }
  767. void MaterialSystemApp::MakeWindowTopmost()
  768. {
  769. ::SetWindowPos((HWND)m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
  770. }
  771. void MaterialSystemApp::AppShutdown()
  772. {
  773. SendMessage( (HWND)m_hWnd, WM_CLOSE, 0, 0 );
  774. }
  775. void MaterialSystemApp::QuitNextFrame()
  776. {
  777. PostMessage( (HWND)m_hWnd, WM_CLOSE, 0, 0 );
  778. }