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.

575 lines
16 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // SHOW_MATERIALS.CPP
  4. //
  5. // Show Materials Display.
  6. //=====================================================================================//
  7. #include "vxconsole.h"
  8. #define ID_SHOWMATERIALS_LISTVIEW 100
  9. // column id
  10. #define ID_SM_NAME 0
  11. #define ID_SM_SHADER 1
  12. #define ID_SM_REFCOUNT 2
  13. typedef struct
  14. {
  15. int listIndex;
  16. char *pName;
  17. char *pShaderName;
  18. int refCount;
  19. char refCountBuff[16];
  20. } material_t;
  21. typedef struct
  22. { const CHAR* name;
  23. int width;
  24. int subItemIndex;
  25. CHAR nameBuff[32];
  26. } label_t;
  27. HWND g_showMaterials_hWnd;
  28. HWND g_showMaterials_hWndListView;
  29. RECT g_showMaterials_windowRect;
  30. int g_showMaterials_sortColumn;
  31. int g_showMaterials_sortDescending;
  32. material_t *g_showMaterials_pMaterials;
  33. int g_showMaterials_numMaterials;
  34. int g_showMaterials_currentFrame;
  35. label_t g_showMaterials_Labels[] =
  36. {
  37. {"Name", 300, ID_SM_NAME},
  38. {"Shader", 150, ID_SM_SHADER},
  39. {"RefCount", 80, ID_SM_REFCOUNT},
  40. };
  41. //-----------------------------------------------------------------------------
  42. // ShowMaterials_SaveConfig
  43. //
  44. //-----------------------------------------------------------------------------
  45. void ShowMaterials_SaveConfig()
  46. {
  47. char buff[256];
  48. Sys_SetRegistryInteger( "showMaterialsCurrentFrame", g_showMaterials_currentFrame );
  49. Sys_SetRegistryInteger( "showMaterialsSortColumn", g_showMaterials_sortColumn );
  50. Sys_SetRegistryInteger( "showMaterialsSortDescending", g_showMaterials_sortDescending );
  51. WINDOWPLACEMENT wp;
  52. memset( &wp, 0, sizeof( wp ) );
  53. wp.length = sizeof( WINDOWPLACEMENT );
  54. GetWindowPlacement( g_showMaterials_hWnd, &wp );
  55. g_showMaterials_windowRect = wp.rcNormalPosition;
  56. sprintf( buff, "%d %d %d %d", g_showMaterials_windowRect.left, g_showMaterials_windowRect.top, g_showMaterials_windowRect.right, g_showMaterials_windowRect.bottom );
  57. Sys_SetRegistryString( "showMaterialsWindowRect", buff );
  58. }
  59. //-----------------------------------------------------------------------------
  60. // ShowMaterials_LoadConfig
  61. //
  62. //-----------------------------------------------------------------------------
  63. void ShowMaterials_LoadConfig()
  64. {
  65. int numArgs;
  66. char buff[256];
  67. Sys_GetRegistryInteger( "showMaterialsCurrentFrame", false, g_showMaterials_currentFrame );
  68. Sys_GetRegistryInteger( "showMaterialsSortColumn", ID_SM_NAME, g_showMaterials_sortColumn );
  69. Sys_GetRegistryInteger( "showMaterialsSortDescending", false, g_showMaterials_sortDescending );
  70. Sys_GetRegistryString( "showMaterialsWindowRect", buff, "", sizeof( buff ) );
  71. numArgs = sscanf( buff, "%d %d %d %d", &g_showMaterials_windowRect.left, &g_showMaterials_windowRect.top, &g_showMaterials_windowRect.right, &g_showMaterials_windowRect.bottom );
  72. if ( numArgs != 4 || g_showMaterials_windowRect.left < 0 || g_showMaterials_windowRect.top < 0 || g_showMaterials_windowRect.right < 0 || g_showMaterials_windowRect.bottom < 0 )
  73. memset( &g_showMaterials_windowRect, 0, sizeof( g_showMaterials_windowRect ) );
  74. }
  75. //-----------------------------------------------------------------------------
  76. // ShowMaterials_Clear
  77. //
  78. //-----------------------------------------------------------------------------
  79. void ShowMaterials_Clear()
  80. {
  81. // delete all the list view entries
  82. if ( g_showMaterials_hWnd )
  83. ListView_DeleteAllItems( g_showMaterials_hWndListView );
  84. if ( !g_showMaterials_pMaterials )
  85. return;
  86. for ( int i=0; i<g_showMaterials_numMaterials; i++ )
  87. {
  88. free( g_showMaterials_pMaterials[i].pName );
  89. free( g_showMaterials_pMaterials[i].pShaderName );
  90. }
  91. g_showMaterials_pMaterials = NULL;
  92. g_showMaterials_numMaterials = 0;
  93. }
  94. //-----------------------------------------------------------------------------
  95. // ShowMaterials_Export
  96. //
  97. //-----------------------------------------------------------------------------
  98. void ShowMaterials_Export()
  99. {
  100. }
  101. //-----------------------------------------------------------------------------
  102. // ShowMaterials_SetTitle
  103. //
  104. //-----------------------------------------------------------------------------
  105. void ShowMaterials_SetTitle()
  106. {
  107. char titleBuff[128];
  108. if ( g_showMaterials_hWnd )
  109. {
  110. strcpy( titleBuff, "Materials " );
  111. if ( g_showMaterials_currentFrame )
  112. strcat( titleBuff, " [FRAME]" );
  113. SetWindowText( g_showMaterials_hWnd, titleBuff );
  114. }
  115. }
  116. //-----------------------------------------------------------------------------
  117. // ShowMaterials_CompareFunc
  118. //
  119. //-----------------------------------------------------------------------------
  120. int CALLBACK ShowMaterials_CompareFunc( LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort )
  121. {
  122. material_t* pMaterialA = ( material_t* )lParam1;
  123. material_t* pMaterialB = ( material_t* )lParam2;
  124. int sort = 0;
  125. switch ( g_showMaterials_sortColumn )
  126. {
  127. case ID_SM_NAME:
  128. sort = stricmp( pMaterialA->pName, pMaterialB->pName );
  129. break;
  130. case ID_SM_SHADER:
  131. sort = stricmp( pMaterialA->pShaderName, pMaterialB->pShaderName );
  132. break;
  133. case ID_SM_REFCOUNT:
  134. sort = pMaterialA->refCount - pMaterialB->refCount;
  135. break;
  136. }
  137. // flip the sort order
  138. if ( g_showMaterials_sortDescending )
  139. sort *= -1;
  140. return ( sort );
  141. }
  142. //-----------------------------------------------------------------------------
  143. // ShowMaterials_SortItems
  144. //
  145. //-----------------------------------------------------------------------------
  146. void ShowMaterials_SortItems()
  147. {
  148. LVITEM lvitem;
  149. material_t *pMaterial;
  150. int i;
  151. if ( !g_showMaterials_hWnd )
  152. {
  153. // only sort if window is visible
  154. return;
  155. }
  156. ListView_SortItems( g_showMaterials_hWndListView, ShowMaterials_CompareFunc, 0 );
  157. memset( &lvitem, 0, sizeof( lvitem ) );
  158. lvitem.mask = LVIF_PARAM;
  159. // get each item and reset its list index
  160. int itemCount = ListView_GetItemCount( g_showMaterials_hWndListView );
  161. for ( i=0; i<itemCount; i++ )
  162. {
  163. lvitem.iItem = i;
  164. ListView_GetItem( g_showMaterials_hWndListView, &lvitem );
  165. pMaterial = ( material_t* )lvitem.lParam;
  166. pMaterial->listIndex = i;
  167. }
  168. // update list view columns with sort key
  169. for ( i=0; i<sizeof( g_showMaterials_Labels )/sizeof( g_showMaterials_Labels[0] ); i++ )
  170. {
  171. char symbol;
  172. LVCOLUMN lvc;
  173. if ( i == g_showMaterials_sortColumn )
  174. symbol = g_showMaterials_sortDescending ? '<' : '>';
  175. else
  176. symbol = ' ';
  177. sprintf( g_showMaterials_Labels[i].nameBuff, "%s %c", g_showMaterials_Labels[i].name, symbol );
  178. memset( &lvc, 0, sizeof( lvc ) );
  179. lvc.mask = LVCF_TEXT;
  180. lvc.pszText = ( LPSTR )g_showMaterials_Labels[i].nameBuff;
  181. ListView_SetColumn( g_showMaterials_hWndListView, i, &lvc );
  182. }
  183. }
  184. //-----------------------------------------------------------------------------
  185. // ShowMaterials_AddViewItem
  186. //
  187. //-----------------------------------------------------------------------------
  188. void ShowMaterials_AddViewItem( material_t* pMaterial )
  189. {
  190. LVITEM lvi;
  191. if ( !g_showMaterials_hWnd )
  192. {
  193. // only valid if log window is visible
  194. return;
  195. }
  196. // update the text callback buffers
  197. sprintf( pMaterial->refCountBuff, "%d", pMaterial->refCount );
  198. int itemCount = ListView_GetItemCount( g_showMaterials_hWndListView );
  199. // setup and insert at end of list
  200. memset( &lvi, 0, sizeof( lvi ) );
  201. lvi.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE;
  202. lvi.iItem = itemCount;
  203. lvi.iSubItem = 0;
  204. lvi.state = 0;
  205. lvi.stateMask = 0;
  206. lvi.pszText = LPSTR_TEXTCALLBACK;
  207. lvi.lParam = ( LPARAM )pMaterial;
  208. // insert and set the real index
  209. pMaterial->listIndex = ListView_InsertItem( g_showMaterials_hWndListView, &lvi );
  210. }
  211. //-----------------------------------------------------------------------------
  212. // ShowMaterials_Refresh
  213. //
  214. //-----------------------------------------------------------------------------
  215. void ShowMaterials_Refresh()
  216. {
  217. char command[256];
  218. strcpy( command, "mat_material_list" );
  219. // if ( !g_showMaterials_currentFrame )
  220. // strcat( command, " all" );
  221. // send the command to application which replies with list data
  222. if ( g_connectedToApp )
  223. ProcessCommand( command );
  224. }
  225. //-----------------------------------------------------------------------------
  226. // ShowMaterials_SizeWindow
  227. //
  228. //-----------------------------------------------------------------------------
  229. void ShowMaterials_SizeWindow( HWND hwnd, int cx, int cy )
  230. {
  231. if ( cx==0 || cy==0 )
  232. {
  233. RECT rcClient;
  234. GetClientRect( hwnd, &rcClient );
  235. cx = rcClient.right;
  236. cy = rcClient.bottom;
  237. }
  238. // position the ListView
  239. SetWindowPos( g_showMaterials_hWndListView, NULL, 0, 0, cx, cy, SWP_NOZORDER );
  240. }
  241. //-----------------------------------------------------------------------------
  242. // ShowMaterials_WndProc
  243. //
  244. //-----------------------------------------------------------------------------
  245. LRESULT CALLBACK ShowMaterials_WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
  246. {
  247. WORD wID = LOWORD( wParam );
  248. material_t* pMaterial;
  249. switch ( message )
  250. {
  251. case WM_CREATE:
  252. return 0L;
  253. case WM_DESTROY:
  254. ShowMaterials_SaveConfig();
  255. g_showMaterials_hWnd = NULL;
  256. return 0L;
  257. case WM_INITMENU:
  258. CheckMenuItem( ( HMENU )wParam, IDM_OPTIONS_CURRENTFRAME, MF_BYCOMMAND | ( g_showMaterials_currentFrame ? MF_CHECKED : MF_UNCHECKED ) );
  259. return 0L;
  260. case WM_SIZE:
  261. ShowMaterials_SizeWindow( hwnd, LOWORD( lParam ), HIWORD( lParam ) );
  262. return 0L;
  263. case WM_NOTIFY:
  264. switch ( ( ( LPNMHDR )lParam )->code )
  265. {
  266. case LVN_COLUMNCLICK:
  267. NMLISTVIEW* pnmlv;
  268. pnmlv = ( NMLISTVIEW* )lParam;
  269. if ( g_showMaterials_sortColumn == pnmlv->iSubItem )
  270. {
  271. // user has clicked on same column - flip the sort
  272. g_showMaterials_sortDescending ^= 1;
  273. }
  274. else
  275. {
  276. // sort by new column
  277. g_showMaterials_sortColumn = pnmlv->iSubItem;
  278. }
  279. ShowMaterials_SortItems();
  280. return 0L;
  281. case LVN_GETDISPINFO:
  282. NMLVDISPINFO* plvdi;
  283. plvdi = ( NMLVDISPINFO* )lParam;
  284. pMaterial = ( material_t* )plvdi->item.lParam;
  285. switch ( plvdi->item.iSubItem )
  286. {
  287. case ID_SM_NAME:
  288. plvdi->item.pszText = pMaterial->pName;
  289. return 0L;
  290. case ID_SM_SHADER:
  291. plvdi->item.pszText = pMaterial->pShaderName;
  292. return 0L;
  293. case ID_SM_REFCOUNT:
  294. plvdi->item.pszText = pMaterial->refCountBuff;
  295. return 0L;
  296. default:
  297. break;
  298. }
  299. break;
  300. }
  301. break;
  302. case WM_COMMAND:
  303. switch ( wID )
  304. {
  305. case IDM_OPTIONS_REFRESH:
  306. ShowMaterials_Refresh();
  307. return 0L;
  308. case IDM_OPTIONS_EXPORT:
  309. ShowMaterials_Export();
  310. return 0L;
  311. case IDM_OPTIONS_CURRENTFRAME:
  312. g_showMaterials_currentFrame ^= 1;
  313. ShowMaterials_SetTitle();
  314. ShowMaterials_Refresh();
  315. return 0L;
  316. }
  317. break;
  318. }
  319. return ( DefWindowProc( hwnd, message, wParam, lParam ) );
  320. }
  321. //-----------------------------------------------------------------------------
  322. // ShowMaterials_Init
  323. //
  324. //-----------------------------------------------------------------------------
  325. bool ShowMaterials_Init()
  326. {
  327. // set up our window class
  328. WNDCLASS wndclass;
  329. memset( &wndclass, 0, sizeof( wndclass ) );
  330. wndclass.style = 0;
  331. wndclass.lpfnWndProc = ShowMaterials_WndProc;
  332. wndclass.cbClsExtra = 0;
  333. wndclass.cbWndExtra = 0;
  334. wndclass.hInstance = g_hInstance;
  335. wndclass.hIcon = g_hIcons[ICON_APPLICATION];
  336. wndclass.hCursor = LoadCursor( NULL, IDC_ARROW );
  337. wndclass.hbrBackground = g_hBackgroundBrush;
  338. wndclass.lpszMenuName = MAKEINTRESOURCE( MENU_SHOWMATERIALS );
  339. wndclass.lpszClassName = "SHOWMATERIALSCLASS";
  340. if ( !RegisterClass( &wndclass ) )
  341. return false;
  342. ShowMaterials_LoadConfig();
  343. return true;
  344. }
  345. //-----------------------------------------------------------------------------
  346. // ShowMaterials_Open
  347. //
  348. //-----------------------------------------------------------------------------
  349. void ShowMaterials_Open()
  350. {
  351. RECT clientRect;
  352. HWND hWnd;
  353. int i;
  354. if ( g_showMaterials_hWnd )
  355. {
  356. // only one instance
  357. if ( IsIconic( g_showMaterials_hWnd ) )
  358. ShowWindow( g_showMaterials_hWnd, SW_RESTORE );
  359. SetForegroundWindow( g_showMaterials_hWnd );
  360. return;
  361. }
  362. hWnd = CreateWindowEx(
  363. WS_EX_CLIENTEDGE,
  364. "SHOWMATERIALSCLASS",
  365. "",
  366. WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_SIZEBOX|WS_MINIMIZEBOX|WS_MAXIMIZEBOX,
  367. 0,
  368. 0,
  369. 700,
  370. 400,
  371. g_hDlgMain,
  372. NULL,
  373. g_hInstance,
  374. NULL );
  375. g_showMaterials_hWnd = hWnd;
  376. GetClientRect( g_showMaterials_hWnd, &clientRect );
  377. hWnd = CreateWindow(
  378. WC_LISTVIEW,
  379. "",
  380. WS_VISIBLE|WS_CHILD|LVS_REPORT,
  381. 0,
  382. 0,
  383. clientRect.right-clientRect.left,
  384. clientRect.bottom-clientRect.top,
  385. g_showMaterials_hWnd,
  386. ( HMENU )ID_SHOWMATERIALS_LISTVIEW,
  387. g_hInstance,
  388. NULL );
  389. g_showMaterials_hWndListView = hWnd;
  390. // init list view columns
  391. for ( i=0; i<sizeof( g_showMaterials_Labels )/sizeof( g_showMaterials_Labels[0] ); i++ )
  392. {
  393. LVCOLUMN lvc;
  394. memset( &lvc, 0, sizeof( lvc ) );
  395. lvc.mask = LVCF_FMT|LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
  396. lvc.iSubItem = 0;
  397. lvc.cx = g_showMaterials_Labels[i].width;
  398. lvc.fmt = LVCFMT_LEFT;
  399. lvc.pszText = ( LPSTR )g_showMaterials_Labels[i].name;
  400. ListView_InsertColumn( g_showMaterials_hWndListView, i, &lvc );
  401. }
  402. ListView_SetBkColor( g_showMaterials_hWndListView, g_backgroundColor );
  403. ListView_SetTextBkColor( g_showMaterials_hWndListView, g_backgroundColor );
  404. DWORD style = LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_HEADERDRAGDROP;
  405. ListView_SetExtendedListViewStyleEx( g_showMaterials_hWndListView, style, style );
  406. // populate list view
  407. for ( i=0; i<g_showMaterials_numMaterials; i++ )
  408. ShowMaterials_AddViewItem( &g_showMaterials_pMaterials[i] );
  409. ShowMaterials_SortItems();
  410. ShowMaterials_SetTitle();
  411. if ( g_showMaterials_windowRect.right && g_showMaterials_windowRect.bottom )
  412. MoveWindow( g_showMaterials_hWnd, g_showMaterials_windowRect.left, g_showMaterials_windowRect.top, g_showMaterials_windowRect.right-g_showMaterials_windowRect.left, g_showMaterials_windowRect.bottom-g_showMaterials_windowRect.top, FALSE );
  413. ShowWindow( g_showMaterials_hWnd, SHOW_OPENWINDOW );
  414. // get data from application
  415. ShowMaterials_Refresh();
  416. }
  417. //-----------------------------------------------------------------------------
  418. // rc_MaterialList
  419. //
  420. // Sent from application with material list
  421. //-----------------------------------------------------------------------------
  422. int rc_MaterialList( char* commandPtr )
  423. {
  424. char* cmdToken;
  425. int numMaterials;
  426. int materialList;
  427. int retAddr;
  428. int retVal;
  429. int errCode = -1;
  430. xrMaterial_t* pLocalList;
  431. // remove old entries
  432. ShowMaterials_Clear();
  433. // get number of materials
  434. cmdToken = GetToken( &commandPtr );
  435. if ( !cmdToken[0] )
  436. goto cleanUp;
  437. sscanf( cmdToken, "%x", &numMaterials );
  438. // get material list
  439. cmdToken = GetToken( &commandPtr );
  440. if ( !cmdToken[0] )
  441. goto cleanUp;
  442. sscanf( cmdToken, "%x", &materialList );
  443. // get retAddr
  444. cmdToken = GetToken( &commandPtr );
  445. if ( !cmdToken[0] )
  446. goto cleanUp;
  447. sscanf( cmdToken, "%x", &retAddr );
  448. pLocalList = new xrMaterial_t[numMaterials];
  449. memset( pLocalList, 0, numMaterials*sizeof( xrMaterial_t ) );
  450. g_showMaterials_numMaterials = numMaterials;
  451. g_showMaterials_pMaterials = new material_t[numMaterials];
  452. memset( g_showMaterials_pMaterials, 0, numMaterials*sizeof( material_t ) );
  453. // get the caller's command list
  454. DmGetMemory( ( void* )materialList, numMaterials*sizeof( xrMaterial_t ), pLocalList, NULL );
  455. // build out the resident list
  456. for ( int i=0; i<numMaterials; i++ )
  457. {
  458. // swap the structure
  459. pLocalList[i].refCount = BigDWord( pLocalList[i].refCount );
  460. g_showMaterials_pMaterials[i].pName = strdup( pLocalList[i].nameString );
  461. g_showMaterials_pMaterials[i].pShaderName = strdup( pLocalList[i].shaderString );
  462. g_showMaterials_pMaterials[i].refCount = pLocalList[i].refCount;
  463. // add to list view
  464. ShowMaterials_AddViewItem( &g_showMaterials_pMaterials[i] );
  465. }
  466. // return the result
  467. retVal = numMaterials;
  468. int xboxRetVal = BigDWord( retVal );
  469. DmSetMemory( ( void* )retAddr, sizeof( int ), &xboxRetVal, NULL );
  470. DebugCommand( "0x%8.8x = MaterialList( 0x%8.8x, 0x%8.8x )\n", retVal, numMaterials, materialList );
  471. delete [] pLocalList;
  472. // update
  473. ShowMaterials_SortItems();
  474. // success
  475. errCode = 0;
  476. cleanUp:
  477. return ( errCode );
  478. }