Counter Strike : Global Offensive Source Code
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.

2705 lines
77 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #undef fopen
  8. #ifdef IS_WINDOWS_PC
  9. #include <windows.h> // SRC only!!
  10. #endif
  11. #if defined( POSIX ) && !defined( _PS3 )
  12. #include <copyfile.h>
  13. #define DeleteFile unlink
  14. #endif
  15. #include "OptionsSubMultiplayer.h"
  16. #include "MultiplayerAdvancedDialog.h"
  17. #include <stdio.h>
  18. #include <vgui_controls/Button.h>
  19. #include <vgui_controls/CheckButton.h>
  20. #include "tier1/KeyValues.h"
  21. #include <vgui_controls/Label.h>
  22. #include <vgui/ISystem.h>
  23. #include <vgui/ISurface.h>
  24. #include <vgui/Cursor.h>
  25. #include <vgui_controls/RadioButton.h>
  26. #include <vgui_controls/ComboBox.h>
  27. #include <vgui_controls/ImagePanel.h>
  28. #include <vgui_controls/FileOpenDialog.h>
  29. #include <vgui_controls/MessageBox.h>
  30. #include <vgui/IVgui.h>
  31. #include <vgui/ILocalize.h>
  32. #include <vgui/IPanel.h>
  33. #include <vgui_controls/MessageBox.h>
  34. #include "CvarTextEntry.h"
  35. #include "CvarToggleCheckButton.h"
  36. #include "CvarSlider.h"
  37. #include "LabeledCommandComboBox.h"
  38. #include "FileSystem.h"
  39. #include "EngineInterface.h"
  40. #include "BitmapImagePanel.h"
  41. #include "UtlBuffer.h"
  42. #include "ModInfo.h"
  43. #include "tier1/convar.h"
  44. #include "materialsystem/IMaterial.h"
  45. #include "materialsystem/IMesh.h"
  46. #include "materialsystem/imaterialvar.h"
  47. // use the JPEGLIB_USE_STDIO define so that we can read in jpeg's from outside the game directory tree. For Spray Import.
  48. #define JPEGLIB_USE_STDIO
  49. #include "jpeglib/jpeglib.h"
  50. #undef JPEGLIB_USE_STDIO
  51. #include <setjmp.h>
  52. #include "bitmap/tgawriter.h"
  53. #include "ivtex.h"
  54. #ifdef IS_WINDOWS_PC
  55. #include <io.h>
  56. #endif
  57. #if defined( _X360 )
  58. #include "xbox/xbox_win32stubs.h"
  59. #endif
  60. #ifdef _PS3
  61. #include "ps3/ps3_core.h"
  62. #include "ps3/ps3_win32stubs.h"
  63. #endif // _GAMECONSOLE
  64. // memdbgon must be the last include file in a .cpp file!!!
  65. #include <tier0/memdbgon.h>
  66. using namespace vgui;
  67. #define DEFAULT_SUIT_HUE 30
  68. #define DEFAULT_PLATE_HUE 6
  69. void UpdateLogoWAD( void *hdib, int r, int g, int b );
  70. struct ColorItem_t
  71. {
  72. char *name;
  73. int r, g, b;
  74. };
  75. static ColorItem_t itemlist[]=
  76. {
  77. { "#Valve_Orange", 255, 120, 24 },
  78. { "#Valve_Yellow", 225, 180, 24 },
  79. { "#Valve_Blue", 0, 60, 255 },
  80. { "#Valve_Ltblue", 0, 167, 255 },
  81. { "#Valve_Green", 0, 167, 0 },
  82. { "#Valve_Red", 255, 43, 0 },
  83. { "#Valve_Brown", 123, 73, 0 },
  84. { "#Valve_Ltgray", 100, 100, 100 },
  85. { "#Valve_Dkgray", 36, 36, 36 },
  86. };
  87. static ColorItem_t s_crosshairColors[] =
  88. {
  89. { "#Valve_Green", 50, 250, 50 },
  90. { "#Valve_Red", 250, 50, 50 },
  91. { "#Valve_Blue", 50, 50, 250 },
  92. { "#Valve_Yellow", 250, 250, 50 },
  93. { "#Valve_Ltblue", 50, 250, 250 }
  94. };
  95. static const int NumCrosshairColors = sizeof( s_crosshairColors ) / sizeof( s_crosshairColors[0] );
  96. //-----------------------------------------------------------------------------
  97. class CrosshairImagePanel : public ImagePanel
  98. {
  99. typedef ImagePanel BaseClass;
  100. public:
  101. CrosshairImagePanel( Panel *parent, const char *name, CCvarToggleCheckButton *pAdditive );
  102. virtual ~CrosshairImagePanel();
  103. virtual void Paint();
  104. void UpdateCrosshair( int r, int g, int b, int size );
  105. protected:
  106. int m_R, m_G, m_B;
  107. int m_barSize;
  108. int m_barGap;
  109. CCvarToggleCheckButton *m_pAdditive;
  110. int m_iCrosshairTextureID;
  111. };
  112. //-----------------------------------------------------------------------------
  113. CrosshairImagePanel::CrosshairImagePanel( Panel *parent, const char *name, CCvarToggleCheckButton *pAdditive ) : ImagePanel( parent, name )
  114. {
  115. m_pAdditive = pAdditive;
  116. UpdateCrosshair( 50, 250, 50, 0 );
  117. m_iCrosshairTextureID = vgui::surface()->CreateNewTextureID();
  118. vgui::surface()->DrawSetTextureFile( m_iCrosshairTextureID, "vgui/white_additive" , true, false);
  119. }
  120. CrosshairImagePanel::~CrosshairImagePanel()
  121. {
  122. if ( vgui::surface() && m_iCrosshairTextureID != -1 )
  123. {
  124. vgui::surface()->DestroyTextureID( m_iCrosshairTextureID );
  125. m_iCrosshairTextureID = -1;
  126. }
  127. }
  128. //-----------------------------------------------------------------------------
  129. void CrosshairImagePanel::UpdateCrosshair( int r, int g, int b, int size )
  130. {
  131. m_R = r;
  132. m_G = g;
  133. m_B = b;
  134. int screenWide, screenTall;
  135. surface()->GetScreenSize( screenWide, screenTall );
  136. if ( size == 0 )
  137. {
  138. if (screenWide <= 640)
  139. {
  140. // if the screen width is 640 or less, set the crosshair num to 3 (large)
  141. size = 1;
  142. }
  143. else if (screenWide < 1024)
  144. {
  145. // if the screen width is between 640 and 1024, set the crosshair num to 2 (medium)
  146. size = 2;
  147. }
  148. else
  149. {
  150. // if the screen width is 1024 or greater, set the crosshair num to 1 (small)
  151. size = 3;
  152. }
  153. }
  154. int scaleBase = 1024;
  155. switch( size )
  156. {
  157. case 3:
  158. scaleBase = 640;
  159. break;
  160. case 2:
  161. scaleBase = 800;
  162. break;
  163. default:
  164. scaleBase = 1024;
  165. break;
  166. }
  167. m_barSize = (int) 9 * screenWide / scaleBase;
  168. m_barGap = (int) 5 * screenWide / scaleBase;
  169. }
  170. //-----------------------------------------------------------------------------
  171. static void DrawCrosshairRect( int x, int y, int w, int h, bool additive )
  172. {
  173. if ( additive )
  174. {
  175. vgui::surface()->DrawTexturedRect( x, y, x+w, y+h );
  176. }
  177. else
  178. {
  179. vgui::surface()->DrawFilledRect( x, y, x+w, y+h );
  180. }
  181. }
  182. //-----------------------------------------------------------------------------
  183. void CrosshairImagePanel::Paint()
  184. {
  185. BaseClass::Paint();
  186. if ( !m_pAdditive )
  187. return;
  188. int wide, tall;
  189. GetSize( wide, tall );
  190. bool additive = m_pAdditive->IsSelected();
  191. int a = 200;
  192. if ( !additive )
  193. {
  194. ConVarRef cl_crosshairalpha( "cl_crosshairalpha" );
  195. if ( cl_crosshairalpha.IsValid() )
  196. {
  197. a = clamp( cl_crosshairalpha.GetInt(), 0, 255 );
  198. }
  199. }
  200. vgui::surface()->DrawSetColor( m_R, m_G, m_B, a );
  201. if ( additive )
  202. {
  203. vgui::surface()->DrawSetTexture( m_iCrosshairTextureID );
  204. }
  205. DrawCrosshairRect( (wide / 2 - m_barGap - m_barSize) + 1, tall / 2, m_barSize, 1, additive );
  206. DrawCrosshairRect( wide / 2 + m_barGap, tall / 2, m_barSize, 1, additive );
  207. DrawCrosshairRect( wide / 2, tall / 2 - m_barGap - m_barSize, 1, m_barSize, additive );
  208. DrawCrosshairRect( wide / 2, tall / 2 + m_barGap, 1, m_barSize, additive );
  209. }
  210. //-----------------------------------------------------------------------------
  211. class AdvancedCrosshairImagePanel : public ImagePanel
  212. {
  213. typedef ImagePanel BaseClass;
  214. public:
  215. AdvancedCrosshairImagePanel( Panel *parent, const char *name );
  216. virtual ~AdvancedCrosshairImagePanel();
  217. virtual void Paint();
  218. void UpdateCrosshair( int r, int g, int b, float scale, const char *crosshairname );
  219. protected:
  220. int m_R, m_G, m_B;
  221. float m_flScale;
  222. // material
  223. int m_iCrosshairTextureID;
  224. IVguiMatInfo *m_pAdvCrosshair;
  225. // animation
  226. IVguiMatInfoVar *m_pFrameVar;
  227. float m_flNextFrameChange;
  228. int m_nNumFrames;
  229. bool m_bAscending; // animating forward or in reverse?
  230. };
  231. //-----------------------------------------------------------------------------
  232. AdvancedCrosshairImagePanel::AdvancedCrosshairImagePanel( Panel *parent, const char *name ) : ImagePanel( parent, name )
  233. {
  234. m_pAdvCrosshair = NULL;
  235. m_pFrameVar = NULL;
  236. if ( ModInfo().AdvCrosshair() )
  237. {
  238. m_iCrosshairTextureID = vgui::surface()->CreateNewTextureID();
  239. UpdateCrosshair( 50, 250, 50, 32.0, "vgui/crosshairs/crosshair1" );
  240. }
  241. }
  242. AdvancedCrosshairImagePanel::~AdvancedCrosshairImagePanel()
  243. {
  244. if ( m_pFrameVar )
  245. {
  246. delete m_pFrameVar;
  247. m_pFrameVar = NULL;
  248. }
  249. if ( m_pAdvCrosshair )
  250. {
  251. delete m_pAdvCrosshair;
  252. m_pAdvCrosshair = NULL;
  253. }
  254. if ( vgui::surface() && m_iCrosshairTextureID != -1 )
  255. {
  256. vgui::surface()->DestroyTextureID( m_iCrosshairTextureID );
  257. m_iCrosshairTextureID = -1;
  258. }
  259. }
  260. //-----------------------------------------------------------------------------
  261. void AdvancedCrosshairImagePanel::UpdateCrosshair( int r, int g, int b, float scale, const char *crosshairname )
  262. {
  263. m_R = r;
  264. m_G = g;
  265. m_B = b;
  266. m_flScale = scale;
  267. vgui::surface()->DrawSetTextureFile( m_iCrosshairTextureID, crosshairname, true, false );
  268. if ( m_pAdvCrosshair )
  269. {
  270. delete m_pAdvCrosshair;
  271. }
  272. m_pAdvCrosshair = vgui::surface()->DrawGetTextureMatInfoFactory( m_iCrosshairTextureID );
  273. Assert(m_pAdvCrosshair);
  274. m_pFrameVar = m_pAdvCrosshair->FindVarFactory( "$frame", NULL );
  275. m_nNumFrames = m_pAdvCrosshair->GetNumAnimationFrames();
  276. m_flNextFrameChange = system()->GetFrameTime() + 0.2;
  277. m_bAscending = true;
  278. }
  279. //-----------------------------------------------------------------------------
  280. void AdvancedCrosshairImagePanel::Paint()
  281. {
  282. BaseClass::Paint();
  283. int wide, tall;
  284. GetSize( wide, tall );
  285. int iClipX0, iClipY0, iClipX1, iClipY1;
  286. ipanel()->GetClipRect(GetVPanel(), iClipX0, iClipY0, iClipX1, iClipY1 );
  287. // scroll through all frames
  288. if ( m_pFrameVar )
  289. {
  290. float curtime = system()->GetFrameTime();
  291. if ( curtime >= m_flNextFrameChange )
  292. {
  293. m_flNextFrameChange = curtime + 0.2;
  294. int frame = m_pFrameVar->GetIntValue();
  295. if ( m_bAscending )
  296. {
  297. frame++;
  298. if ( frame >= m_nNumFrames )
  299. {
  300. m_bAscending = !m_bAscending;
  301. frame--;
  302. }
  303. }
  304. else
  305. {
  306. frame--;
  307. if ( frame < 0 )
  308. {
  309. m_bAscending = !m_bAscending;
  310. frame++;
  311. }
  312. }
  313. m_pFrameVar->SetIntValue(frame);
  314. }
  315. }
  316. float x, y;
  317. // assume square
  318. float flDrawWidth = ( m_flScale/48.0 ) * (float)wide;
  319. int flHalfWidth = (int)( flDrawWidth / 2 );
  320. x = wide/2 - flHalfWidth;
  321. y = tall/2 - flHalfWidth;
  322. vgui::surface()->DrawSetColor( m_R, m_G, m_B, 255 );
  323. vgui::surface()->DrawSetTexture( m_iCrosshairTextureID );
  324. vgui::surface()->DrawTexturedRect( x, y, x+flDrawWidth, y+flDrawWidth );
  325. vgui::surface()->DrawSetTexture(0);
  326. }
  327. //-----------------------------------------------------------------------------
  328. // Purpose: Basic help dialog
  329. //-----------------------------------------------------------------------------
  330. COptionsSubMultiplayer::COptionsSubMultiplayer(vgui::Panel *parent) : vgui::PropertyPage(parent, "OptionsSubMultiplayer")
  331. {
  332. Button *cancel = new Button( this, "Cancel", "#GameUI_Cancel" );
  333. cancel->SetCommand( "Close" );
  334. Button *ok = new Button( this, "OK", "#GameUI_OK" );
  335. ok->SetCommand( "Ok" );
  336. Button *apply = new Button( this, "Apply", "#GameUI_Apply" );
  337. apply->SetCommand( "Apply" );
  338. Button *advanced = new Button( this, "Advanced", "#GameUI_AdvancedEllipsis" );
  339. advanced->SetCommand( "Advanced" );
  340. Button *importSprayImage = new Button( this, "ImportSprayImage", "#GameUI_ImportSprayEllipsis" );
  341. importSprayImage->SetCommand("ImportSprayImage");
  342. m_hImportSprayDialog = NULL;
  343. m_pPrimaryColorSlider = new CCvarSlider( this, "Primary Color Slider", "#GameUI_PrimaryColor",
  344. 0.0f, 255.0f, "topcolor" );
  345. m_pSecondaryColorSlider = new CCvarSlider( this, "Secondary Color Slider", "#GameUI_SecondaryColor",
  346. 0.0f, 255.0f, "bottomcolor" );
  347. m_pHighQualityModelCheckBox = new CCvarToggleCheckButton( this, "High Quality Models", "#GameUI_HighModels", "cl_himodels" );
  348. m_pModelList = new CLabeledCommandComboBox( this, "Player model" );
  349. m_ModelName[0] = 0;
  350. InitModelList( m_pModelList );
  351. m_pLogoList = new CLabeledCommandComboBox( this, "SpraypaintList" );
  352. m_LogoName[0] = 0;
  353. InitLogoList( m_pLogoList );
  354. m_pModelImage = new CBitmapImagePanel( this, "ModelImage", NULL );
  355. m_pModelImage->AddActionSignalTarget( this );
  356. m_pLogoImage = new ImagePanel( this, "LogoImage" );
  357. m_pLogoImage->AddActionSignalTarget( this );
  358. m_nTopColor = DEFAULT_SUIT_HUE;
  359. m_nBottomColor = DEFAULT_PLATE_HUE;
  360. m_nLogoR = 255;
  361. m_nLogoG = 255;
  362. m_nLogoB = 255;
  363. // crosshair controls
  364. m_pCrosshairColorComboBox = new ComboBox(this, "CrosshairColorComboBox", 6, false);
  365. m_pCrosshairSize = new CLabeledCommandComboBox(this, "CrosshairSizeComboBox");
  366. m_pCrosshairTranslucencyCheckbox = new CCvarToggleCheckButton(this, "CrosshairTranslucencyCheckbox", "#GameUI_Translucent", "cl_crosshairusealpha");
  367. m_pCrosshairImage = new CrosshairImagePanel( this, "CrosshairImage", m_pCrosshairTranslucencyCheckbox );
  368. // advanced crosshair controls
  369. //==========
  370. m_pAdvCrosshairRedSlider = new CCvarSlider( this, "Red Color Slider", "#GameUI_CrosshairColor_Red",
  371. 0.0f, 255.0f, "cl_crosshair_red" );
  372. m_pAdvCrosshairGreenSlider = new CCvarSlider( this, "Green Color Slider", "#GameUI_CrosshairColor_Green",
  373. 0.0f, 255.0f, "cl_crosshair_green" );
  374. m_pAdvCrosshairBlueSlider = new CCvarSlider( this, "Blue Color Slider", "#GameUI_CrosshairColor_Blue",
  375. 0.0f, 255.0f, "cl_crosshair_blue" );
  376. m_pAdvCrosshairScaleSlider = new CCvarSlider( this, "Scale Slider", "#GameUI_CrosshairScale",
  377. 16.0f, 48.0f, "cl_crosshair_scale" );
  378. m_pAdvCrosshairRedSlider->AddActionSignalTarget( this );
  379. m_pAdvCrosshairGreenSlider->AddActionSignalTarget( this );
  380. m_pAdvCrosshairBlueSlider->AddActionSignalTarget( this );
  381. m_pAdvCrosshairScaleSlider->AddActionSignalTarget( this );
  382. m_pAdvCrosshairStyle = new CLabeledCommandComboBox( this, "AdvCrosshairList" );
  383. m_pAdvCrosshairImage = new AdvancedCrosshairImagePanel( this, "AdvCrosshairImage" );
  384. InitAdvCrosshairStyleList(m_pAdvCrosshairStyle);
  385. RedrawAdvCrosshairImage();
  386. //=========
  387. m_pDownloadFilterCombo = new ComboBox( this, "DownloadFilterCheck", 3, false );
  388. m_pDownloadFilterCombo->AddItem( "#GameUI_DownloadFilter_ALL", NULL );
  389. m_pDownloadFilterCombo->AddItem( "#GameUI_DownloadFilter_NoSounds", NULL );
  390. m_pDownloadFilterCombo->AddItem( "#GameUI_DownloadFilter_None", NULL );
  391. //=========
  392. LoadControlSettings("Resource/OptionsSubMultiplayer.res");
  393. InitCrosshairColorEntries();
  394. InitCrosshairSizeList(m_pCrosshairSize);
  395. RedrawCrosshairImage();
  396. // turn off the crosshair stuff if the mod specifies "nocrosshair" in the gameinfo.txt file
  397. if ( ModInfo().NoCrosshair() )
  398. {
  399. m_pCrosshairColorComboBox->SetVisible( false );
  400. m_pCrosshairSize->SetVisible( false );
  401. m_pCrosshairTranslucencyCheckbox->SetVisible( false );
  402. m_pCrosshairImage->SetVisible( false );
  403. Panel *pTempPanel = NULL;
  404. // #GameUI_CrosshairDescription (from "Resource/OptionsSubMultiplayer.res")
  405. pTempPanel = FindChildByName( "CrosshairLabel" );
  406. if ( pTempPanel )
  407. {
  408. pTempPanel->SetVisible( false );
  409. }
  410. }
  411. // turn off model selection stuff if the mod specifies "nomodels" in the gameinfo.txt file
  412. if ( ModInfo().NoModels() )
  413. {
  414. Panel *pTempPanel = NULL;
  415. if ( m_pModelImage )
  416. {
  417. m_pModelImage->SetVisible( false );
  418. }
  419. if ( m_pModelList )
  420. {
  421. m_pModelList->SetVisible( false );
  422. }
  423. if ( m_pPrimaryColorSlider )
  424. {
  425. m_pPrimaryColorSlider->SetVisible( false );
  426. }
  427. if ( m_pSecondaryColorSlider )
  428. {
  429. m_pSecondaryColorSlider->SetVisible( false );
  430. }
  431. // #GameUI_PlayerModel (from "Resource/OptionsSubMultiplayer.res")
  432. pTempPanel = FindChildByName( "Label1" );
  433. if ( pTempPanel )
  434. {
  435. pTempPanel->SetVisible( false );
  436. }
  437. // #GameUI_ColorSliders (from "Resource/OptionsSubMultiplayer.res")
  438. pTempPanel = FindChildByName( "Colors" );
  439. if ( pTempPanel )
  440. {
  441. pTempPanel->SetVisible( false );
  442. }
  443. }
  444. // turn off the himodel stuff if the mod specifies "nohimodel" in the gameinfo.txt file
  445. if ( ModInfo().NoHiModel() )
  446. {
  447. if ( m_pHighQualityModelCheckBox )
  448. {
  449. m_pHighQualityModelCheckBox->SetVisible( false );
  450. }
  451. }
  452. // Advanced crosshair selection
  453. if ( !ModInfo().AdvCrosshair() )
  454. {
  455. m_pAdvCrosshairImage->SetVisible( false );
  456. m_pAdvCrosshairRedSlider->SetVisible( false );
  457. m_pAdvCrosshairBlueSlider->SetVisible( false );
  458. m_pAdvCrosshairGreenSlider->SetVisible( false );
  459. m_pAdvCrosshairScaleSlider->SetVisible( false );
  460. m_pAdvCrosshairStyle->SetVisible( false );
  461. Panel *pTempPanel = NULL;
  462. // #GameUI_AdvCrosshairDescription (from "Resource/OptionsSubMultiplayer.res")
  463. pTempPanel = FindChildByName( "AdvCrosshairLabel" );
  464. if ( pTempPanel )
  465. {
  466. pTempPanel->SetVisible( false );
  467. }
  468. }
  469. }
  470. //-----------------------------------------------------------------------------
  471. // Purpose:
  472. //-----------------------------------------------------------------------------
  473. COptionsSubMultiplayer::~COptionsSubMultiplayer()
  474. {
  475. }
  476. //-----------------------------------------------------------------------------
  477. // Purpose:
  478. //-----------------------------------------------------------------------------
  479. void COptionsSubMultiplayer::OnCommand( const char *command )
  480. {
  481. if ( !stricmp( command, "Advanced" ) )
  482. {
  483. if (!m_hMultiplayerAdvancedDialog.Get())
  484. {
  485. m_hMultiplayerAdvancedDialog = new CMultiplayerAdvancedDialog( this );
  486. }
  487. m_hMultiplayerAdvancedDialog->Activate();
  488. }
  489. else if (!stricmp( command, "ImportSprayImage" ) )
  490. {
  491. if (m_hImportSprayDialog == NULL)
  492. {
  493. m_hImportSprayDialog = new FileOpenDialog(NULL, "#GameUI_ImportSprayImage", true);
  494. m_hImportSprayDialog->AddFilter("*.tga,*.jpg,*.bmp,*.vtf", "#GameUI_All_Images", true);
  495. m_hImportSprayDialog->AddFilter("*.tga", "#GameUI_TGA_Images", false);
  496. m_hImportSprayDialog->AddFilter("*.jpg", "#GameUI_JPEG_Images", false);
  497. m_hImportSprayDialog->AddFilter("*.bmp", "#GameUI_BMP_Images", false);
  498. m_hImportSprayDialog->AddFilter("*.vtf", "#GameUI_VTF_Images", false);
  499. m_hImportSprayDialog->AddActionSignalTarget(this);
  500. }
  501. m_hImportSprayDialog->DoModal(false);
  502. m_hImportSprayDialog->Activate();
  503. }
  504. BaseClass::OnCommand( command );
  505. }
  506. // file selected. This can only happen when someone selects an image to be imported as a spray logo.
  507. void COptionsSubMultiplayer::OnFileSelected(const char *fullpath)
  508. {
  509. #ifndef _GAMECONSOLE
  510. if ((fullpath == NULL) || (fullpath[0] == 0))
  511. {
  512. return;
  513. }
  514. ConversionErrorType errcode;
  515. // this can take a while, put up a waiting cursor
  516. surface()->SetCursor(dc_hourglass);
  517. // get the extension of the file we're to convert
  518. char extension[MAX_PATH];
  519. const char *constchar = fullpath + strlen(fullpath);
  520. while ((constchar > fullpath) && (*(constchar-1) != '.'))
  521. {
  522. --constchar;
  523. }
  524. Q_strncpy(extension, constchar, MAX_PATH);
  525. bool deleteIntermediateTGA = false;
  526. bool deleteIntermediateVTF = false;
  527. bool convertTGAToVTF = true;
  528. char tgaPath[MAX_PATH*2];
  529. char *c;
  530. bool failed = false;
  531. Q_strncpy(tgaPath, fullpath, sizeof(tgaPath));
  532. if (stricmp(extension, "tga"))
  533. {
  534. // construct a .tga version of this file path.
  535. c = tgaPath + strlen(tgaPath);
  536. while ((c > tgaPath) && (*(c-1) != '\\') && (*(c-1) != '/'))
  537. {
  538. --c;
  539. }
  540. *c = 0;
  541. char origpath[MAX_PATH*2];
  542. Q_strncpy(origpath, tgaPath, sizeof(origpath));
  543. int index = 0;
  544. do {
  545. Q_snprintf(tgaPath, sizeof(tgaPath), "%stemp%d.tga", origpath, index);
  546. ++index;
  547. } while (_access(tgaPath, 0) != -1);
  548. if (!stricmp(extension, "jpg") || !stricmp(extension, "jpeg"))
  549. {
  550. // convert from the jpeg file format to the TGA file format
  551. errcode = ConvertJPEGToTGA(fullpath, tgaPath);
  552. if (errcode == CE_SUCCESS)
  553. {
  554. deleteIntermediateTGA = true;
  555. }
  556. else
  557. {
  558. failed = true;
  559. vgui::MessageBox *errorDialog = NULL;
  560. if (errcode == CE_MEMORY_ERROR)
  561. {
  562. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_Memory");
  563. }
  564. else if (errcode == CE_CANT_OPEN_SOURCE_FILE)
  565. {
  566. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_Reading_Image");
  567. }
  568. else if (errcode == CE_ERROR_PARSING_SOURCE)
  569. {
  570. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_Image_File_Corrupt");
  571. }
  572. else if (errcode == CE_ERROR_WRITING_OUTPUT_FILE)
  573. {
  574. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_Writing_Temp_Output");
  575. }
  576. else if (errcode == CE_SOURCE_FILE_FORMAT_NOT_SUPPORTED )
  577. {
  578. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Image_Wrong_Size");
  579. }
  580. if (errorDialog != NULL)
  581. {
  582. errorDialog->DoModal();
  583. }
  584. }
  585. }
  586. else if (!stricmp(extension, "bmp"))
  587. {
  588. // convert from the bmp file format to the TGA file format
  589. errcode = ConvertBMPToTGA(fullpath, tgaPath);
  590. if (errcode == CE_SUCCESS)
  591. {
  592. deleteIntermediateTGA = true;
  593. }
  594. else
  595. {
  596. failed = true;
  597. vgui::MessageBox *errorDialog = NULL;
  598. if (errcode == CE_MEMORY_ERROR)
  599. {
  600. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_Memory");
  601. }
  602. else if (errcode == CE_CANT_OPEN_SOURCE_FILE)
  603. {
  604. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_Reading_Image");
  605. }
  606. else if (errcode == CE_ERROR_PARSING_SOURCE)
  607. {
  608. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_Image_File_Corrupt");
  609. }
  610. else if (errcode == CE_SOURCE_FILE_FORMAT_NOT_SUPPORTED)
  611. {
  612. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_BMP_Format_Not_Supported");
  613. }
  614. else if (errcode == CE_ERROR_WRITING_OUTPUT_FILE)
  615. {
  616. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_Writing_Temp_Output");
  617. }
  618. if (errorDialog != NULL)
  619. {
  620. errorDialog->DoModal();
  621. }
  622. }
  623. }
  624. else if (!stricmp(extension, "vtf"))
  625. {
  626. // if the file is already in the vtf format there's no need to convert it.
  627. convertTGAToVTF = false;
  628. }
  629. }
  630. if (convertTGAToVTF && !failed)
  631. {
  632. // convert the TGA file to the VTF format.
  633. errcode = ConvertTGA(tgaPath); // resize TGA so that it has power-of-two dimensions with a max size of 256x256.
  634. if (errcode != CE_SUCCESS)
  635. {
  636. failed = true;
  637. vgui::MessageBox *errorDialog = NULL;
  638. if (errcode == CE_MEMORY_ERROR)
  639. {
  640. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_Memory");
  641. }
  642. else if (errcode == CE_CANT_OPEN_SOURCE_FILE)
  643. {
  644. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_Reading_Image");
  645. }
  646. else if (errcode == CE_ERROR_PARSING_SOURCE)
  647. {
  648. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_Image_File_Corrupt");
  649. }
  650. else if (errcode == CE_SOURCE_FILE_FORMAT_NOT_SUPPORTED)
  651. {
  652. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_TGA_Format_Not_Supported");
  653. }
  654. else if (errcode == CE_ERROR_WRITING_OUTPUT_FILE)
  655. {
  656. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_Writing_Temp_Output");
  657. }
  658. if (errorDialog != NULL)
  659. {
  660. errorDialog->DoModal();
  661. }
  662. }
  663. if (!failed)
  664. {
  665. char tempPath[MAX_PATH*2];
  666. Q_strncpy(tempPath, tgaPath, sizeof(tempPath));
  667. errcode = ConvertTGAToVTF(tempPath);
  668. if (errcode == CE_SUCCESS)
  669. {
  670. deleteIntermediateVTF = true;
  671. }
  672. else
  673. {
  674. failed = true;
  675. vgui::MessageBox *errorDialog = NULL;
  676. if (errcode == CE_MEMORY_ERROR)
  677. {
  678. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_Memory");
  679. }
  680. else if (errcode == CE_CANT_OPEN_SOURCE_FILE)
  681. {
  682. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_Reading_Image");
  683. }
  684. else if (errcode == CE_ERROR_PARSING_SOURCE)
  685. {
  686. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_Image_File_Corrupt");
  687. }
  688. else if (errcode == CE_SOURCE_FILE_FORMAT_NOT_SUPPORTED)
  689. {
  690. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_TGA_Format_Not_Supported");
  691. }
  692. else if (errcode == CE_ERROR_WRITING_OUTPUT_FILE)
  693. {
  694. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_Writing_Temp_Output");
  695. }
  696. else if (errcode == CE_ERROR_LOADING_DLL)
  697. {
  698. errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_Cant_Load_VTEX_DLL");
  699. }
  700. if (errorDialog != NULL)
  701. {
  702. errorDialog->DoModal();
  703. }
  704. }
  705. }
  706. }
  707. char finalPath[MAX_PATH*2];
  708. finalPath[0] = 0;
  709. char vtfPath[MAX_PATH*2];
  710. vtfPath[0] = 0;
  711. if (!failed)
  712. {
  713. Q_strncpy(vtfPath, tgaPath, sizeof(vtfPath));
  714. // rename the tga file to be a vtf file.
  715. c = vtfPath + strlen(vtfPath);
  716. while ((c > vtfPath) && (*(c-1) != '.'))
  717. {
  718. --c;
  719. }
  720. *c = 0;
  721. Q_strncat(vtfPath, "vtf", sizeof(vtfPath), COPY_ALL_CHARACTERS);
  722. // get the vtfFilename from the path.
  723. const char *vtfFilename = fullpath + strlen(fullpath);
  724. while ((vtfFilename > fullpath) && (*(vtfFilename-1) != '\\') && (*(vtfFilename-1) != '/'))
  725. {
  726. --vtfFilename;
  727. }
  728. Q_strncpy(finalPath, engine->GetGameDirectory(), sizeof(finalPath));
  729. Q_strncat(finalPath, "\\materials\\VGUI\\logos\\", sizeof(finalPath), COPY_ALL_CHARACTERS);
  730. Q_strncat(finalPath, vtfFilename, sizeof(finalPath), COPY_ALL_CHARACTERS);
  731. c = finalPath + strlen(finalPath);
  732. while ((c > finalPath) && (*(c-1) != '.'))
  733. {
  734. --c;
  735. }
  736. *c = 0;
  737. Q_strncat(finalPath,"vtf", sizeof(finalPath), COPY_ALL_CHARACTERS);
  738. // make sure the directory exists before we try to copy the file.
  739. g_pFullFileSystem->CreateDirHierarchy("materials/VGUI/logos/", "GAME");
  740. // write out the spray VMT file.
  741. errcode = WriteSprayVMT(finalPath);
  742. if (errcode != CE_SUCCESS)
  743. {
  744. failed = true;
  745. vgui::MessageBox *errorDialog = new vgui::MessageBox("#GameUI_Spray_Import_Error_Title", "#GameUI_Spray_Import_Error_Writing_Output");
  746. errorDialog->DoModal();
  747. }
  748. if (!failed)
  749. {
  750. // copy vtf file to the final location.
  751. #ifdef OSX
  752. copyfile( vtfPath, finalPath, 0, 0 );
  753. #else
  754. CopyFile(vtfPath, finalPath, true);
  755. #endif
  756. // refresh the logo list so the new spray shows up.
  757. InitLogoList(m_pLogoList);
  758. char rootFilename[MAX_PATH];
  759. Q_strncpy(rootFilename, vtfFilename, MAX_PATH);
  760. // get the root filename so we can select in the spray list.
  761. rootFilename[strlen(rootFilename) - 4] = 0;
  762. // automatically select the logo that was just imported.
  763. SelectLogo(rootFilename);
  764. }
  765. }
  766. // delete the intermediate VTF file if one was made.
  767. if (deleteIntermediateVTF)
  768. {
  769. DeleteFile(vtfPath);
  770. // the TGA->VTF conversion process generates a .txt file if one wasn't already there.
  771. // in this case, delete the .txt file.
  772. c = vtfPath + strlen(vtfPath);
  773. while ((c > vtfPath) && (*(c-1) != '.'))
  774. {
  775. --c;
  776. }
  777. Q_strncpy(c, "txt", sizeof(vtfPath)-(c-vtfPath));
  778. DeleteFile(vtfPath);
  779. }
  780. // delete the intermediate TGA file if one was made.
  781. if (deleteIntermediateTGA)
  782. {
  783. DeleteFile(tgaPath);
  784. }
  785. // change the cursor back to normal
  786. surface()->SetCursor(dc_user);
  787. #endif
  788. }
  789. struct ValveJpegErrorHandler_t
  790. {
  791. // The default manager
  792. struct jpeg_error_mgr m_Base;
  793. // For handling any errors
  794. jmp_buf m_ErrorContext;
  795. };
  796. //-----------------------------------------------------------------------------
  797. // Purpose: We'll override the default error handler so we can deal with errors without having to exit the engine
  798. //-----------------------------------------------------------------------------
  799. static void ValveJpegErrorHandler( j_common_ptr cinfo )
  800. {
  801. ValveJpegErrorHandler_t *pError = reinterpret_cast< ValveJpegErrorHandler_t * >( cinfo->err );
  802. char buffer[ JMSG_LENGTH_MAX ];
  803. /* Create the message */
  804. ( *cinfo->err->format_message )( cinfo, buffer );
  805. Warning( "%s\n", buffer );
  806. // Bail
  807. longjmp( pError->m_ErrorContext, 1 );
  808. }
  809. // convert the JPEG file given to a TGA file at the given output path.
  810. ConversionErrorType COptionsSubMultiplayer::ConvertJPEGToTGA(const char *jpegpath, const char *tgaPath)
  811. {
  812. #if !defined( _GAMECONSOLE )
  813. struct jpeg_decompress_struct jpegInfo;
  814. struct ValveJpegErrorHandler_t jerr;
  815. JSAMPROW row_pointer[1];
  816. int row_stride;
  817. int cur_row = 0;
  818. // image attributes
  819. int image_height;
  820. int image_width;
  821. // open the jpeg image file.
  822. FILE *infile = fopen(jpegpath, "rb");
  823. if (infile == NULL)
  824. {
  825. return CE_CANT_OPEN_SOURCE_FILE;
  826. }
  827. // setup error to print to stderr.
  828. jpegInfo.err = jpeg_std_error(&jerr.m_Base);
  829. jpegInfo.err->error_exit = &ValveJpegErrorHandler;
  830. // create the decompress struct.
  831. jpeg_create_decompress(&jpegInfo);
  832. if ( setjmp( jerr.m_ErrorContext ) )
  833. {
  834. // Get here if there is any error
  835. jpeg_destroy_decompress( &jpegInfo );
  836. fclose(infile);
  837. return CE_ERROR_PARSING_SOURCE;
  838. }
  839. jpeg_stdio_src(&jpegInfo, infile);
  840. // read in the jpeg header and make sure that's all good.
  841. if (jpeg_read_header(&jpegInfo, TRUE) != JPEG_HEADER_OK)
  842. {
  843. fclose(infile);
  844. return CE_ERROR_PARSING_SOURCE;
  845. }
  846. // start the decompress with the jpeg engine.
  847. if (jpeg_start_decompress(&jpegInfo) != TRUE)
  848. {
  849. jpeg_destroy_decompress(&jpegInfo);
  850. fclose(infile);
  851. return CE_ERROR_PARSING_SOURCE;
  852. }
  853. // Check for valid width and height (ie. power of 2 and print out an error and exit if not).
  854. if ( !IsPowerOfTwo( jpegInfo.image_height ) || !IsPowerOfTwo( jpegInfo.image_width ) )
  855. {
  856. jpeg_destroy_decompress(&jpegInfo);
  857. fclose( infile );
  858. return CE_SOURCE_FILE_FORMAT_NOT_SUPPORTED;
  859. }
  860. // now that we've started the decompress with the jpeg lib, we have the attributes of the
  861. // image ready to be read out of the decompress struct.
  862. row_stride = jpegInfo.output_width * jpegInfo.output_components;
  863. image_height = jpegInfo.image_height;
  864. image_width = jpegInfo.image_width;
  865. int mem_required = jpegInfo.image_height * jpegInfo.image_width * jpegInfo.output_components;
  866. // allocate the memory to read the image data into.
  867. unsigned char *buf = (unsigned char *)malloc(mem_required);
  868. if (buf == NULL)
  869. {
  870. jpeg_destroy_decompress(&jpegInfo);
  871. fclose(infile);
  872. return CE_MEMORY_ERROR;
  873. }
  874. // read in all the scan lines of the image into our image data buffer.
  875. bool working = true;
  876. while (working && (jpegInfo.output_scanline < jpegInfo.output_height))
  877. {
  878. row_pointer[0] = &(buf[cur_row * row_stride]);
  879. if (jpeg_read_scanlines(&jpegInfo, row_pointer, 1) != TRUE)
  880. {
  881. working = false;
  882. }
  883. ++cur_row;
  884. }
  885. if (!working)
  886. {
  887. free(buf);
  888. jpeg_destroy_decompress(&jpegInfo);
  889. fclose(infile);
  890. return CE_ERROR_PARSING_SOURCE;
  891. }
  892. jpeg_finish_decompress(&jpegInfo);
  893. fclose(infile);
  894. // ok, at this point we have read in the JPEG image to our buffer, now we need to write it out as a TGA file.
  895. CUtlBuffer outBuf;
  896. bool bRetVal = TGAWriter::WriteToBuffer( buf, outBuf, image_width, image_height, IMAGE_FORMAT_RGB888, IMAGE_FORMAT_RGB888 );
  897. if ( bRetVal )
  898. {
  899. if ( !g_pFullFileSystem->WriteFile( tgaPath, NULL, outBuf ) )
  900. {
  901. bRetVal = false;
  902. }
  903. }
  904. free(buf);
  905. return bRetVal ? CE_SUCCESS : CE_ERROR_WRITING_OUTPUT_FILE;
  906. #else
  907. return CE_SOURCE_FILE_FORMAT_NOT_SUPPORTED;
  908. #endif
  909. }
  910. // convert the bmp file given to a TGA file at the given destination path.
  911. ConversionErrorType COptionsSubMultiplayer::ConvertBMPToTGA(const char *bmpPath, const char *tgaPath)
  912. {
  913. #if !defined( _WIN32 )
  914. return CE_SOURCE_FILE_FORMAT_NOT_SUPPORTED;
  915. #else
  916. if ( !IsPC() )
  917. return CE_SOURCE_FILE_FORMAT_NOT_SUPPORTED;
  918. HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, bmpPath, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE | LR_DEFAULTSIZE);
  919. BITMAP bitmap;
  920. GetObject(hBitmap, sizeof(bitmap), &bitmap);
  921. BITMAPINFO *bitmapInfo;
  922. if (bitmap.bmBitsPixel == 24)
  923. {
  924. bitmapInfo = (BITMAPINFO *)malloc(sizeof(BITMAPINFO));
  925. }
  926. else
  927. {
  928. int colorsUsed = 1 << bitmap.bmBitsPixel;
  929. bitmapInfo = (BITMAPINFO *)malloc(colorsUsed * sizeof(RGBQUAD) + sizeof(BITMAPINFO));
  930. }
  931. memset(bitmapInfo, 0, sizeof(BITMAPINFO));
  932. bitmapInfo->bmiHeader.biSize = sizeof(bitmapInfo->bmiHeader);
  933. if (bitmap.bmBitsPixel != 24)
  934. {
  935. bitmapInfo->bmiHeader.biBitCount = bitmap.bmBitsPixel; // need to specify the bits per pixel so GDI will generate a color table for us.
  936. }
  937. HDC dc = CreateCompatibleDC(NULL);
  938. int retcode = GetDIBits(dc, hBitmap, 0, bitmap.bmHeight, NULL, bitmapInfo, DIB_RGB_COLORS);
  939. DeleteDC(dc);
  940. if (retcode == 0)
  941. {
  942. // error getting the bitmap info for some reason.
  943. free(bitmapInfo);
  944. return CE_ERROR_PARSING_SOURCE;
  945. }
  946. int mem_required = 3 * bitmap.bmWidth * bitmap.bmHeight; // mem required for copying the data out into RGB format.
  947. unsigned char *buf = (unsigned char *)malloc(mem_required);
  948. if (buf == NULL)
  949. {
  950. free(bitmapInfo);
  951. return CE_MEMORY_ERROR;
  952. }
  953. if (bitmapInfo->bmiHeader.biBitCount == 24)
  954. {
  955. if ((bitmap.bmWidth * 3) == bitmap.bmWidthBytes)
  956. {
  957. // 24-bit BGR color bitmap that is word aligned.
  958. memcpy(buf, bitmap.bmBits, mem_required);
  959. }
  960. else
  961. {
  962. // 24-bit BGR color bitmap that is not word aligned.
  963. // need to read it in one row at a time since it isn't word aligned.
  964. int row;
  965. for (row = 0; row < bitmap.bmHeight; ++row)
  966. {
  967. memcpy(buf + (row * 3 * bitmap.bmWidth), (unsigned char *)(bitmap.bmBits) + (row * bitmap.bmWidthBytes), 3 * bitmap.bmWidth);
  968. }
  969. }
  970. // bitmaps are loaded as BGR, need to convert it to RGB.
  971. int numPixels = bitmap.bmHeight * bitmap.bmWidth;
  972. // convert to RGB
  973. int index;
  974. for (index = 0; index < numPixels; ++index)
  975. {
  976. unsigned char blue = buf[3 * index]; // save blue value
  977. buf[3 * index] = buf[3 * index + 2]; // copy red value.
  978. buf[3 * index + 2] = blue; // copy blue value.
  979. }
  980. }
  981. else if (bitmapInfo->bmiHeader.biBitCount == 8)
  982. {
  983. // 8-bit 256 color bitmap.
  984. int y, x, index, colorTableEntry;
  985. for (y = 0; y < bitmap.bmHeight; ++y)
  986. {
  987. for (x = 0; x < bitmap.bmWidth; ++x)
  988. {
  989. // compute the color map entry for this pixel
  990. index = y * bitmap.bmWidthBytes + x;
  991. colorTableEntry = ((unsigned char *)bitmap.bmBits)[index];
  992. // get the color for this color map entry.
  993. RGBQUAD *rgbQuad = &(bitmapInfo->bmiColors[colorTableEntry]);
  994. // copy the color values for this pixel to the destination buffer.
  995. buf[(y * bitmap.bmWidth * 3) + (x * 3)] = rgbQuad->rgbRed;
  996. buf[(y * bitmap.bmWidth * 3) + (x * 3) + 1] = rgbQuad->rgbGreen;
  997. buf[(y * bitmap.bmWidth * 3) + (x * 3) + 2] = rgbQuad->rgbBlue;
  998. }
  999. }
  1000. }
  1001. else if (bitmapInfo->bmiHeader.biBitCount == 4)
  1002. {
  1003. // 4-bit 16 color bitmap.
  1004. int y, x, index, colorTableEntry;
  1005. for (y = 0; y < bitmap.bmHeight; ++y)
  1006. {
  1007. for (x = 0; x < bitmap.bmWidth; x += 2)
  1008. {
  1009. // two color table entries per byte
  1010. index = y * bitmap.bmWidthBytes + x / 2;
  1011. // get the color table entry for this pixel
  1012. colorTableEntry = (0xf0 & ((unsigned char *)bitmap.bmBits)[index]) >> 4;
  1013. // get the color values for this pixel's color table entry.
  1014. RGBQUAD *rgbQuad = &(bitmapInfo->bmiColors[colorTableEntry]);
  1015. // copy the pixel's color values to the destination buffer.
  1016. buf[(y * bitmap.bmWidth * 3) + (x * 3)] = rgbQuad->rgbRed;
  1017. buf[(y * bitmap.bmWidth * 3) + (x * 3) + 1] = rgbQuad->rgbGreen;
  1018. buf[(y * bitmap.bmWidth * 3) + (x * 3) + 2] = rgbQuad->rgbBlue;
  1019. // make sure we haven't reached the end of the row.
  1020. if ((x + 1) > bitmap.bmWidth)
  1021. {
  1022. break;
  1023. }
  1024. // get the color table entry for this pixel.
  1025. colorTableEntry = 0x0f & ((unsigned char *)bitmap.bmBits)[index];
  1026. // get the color values for this pixel's color table entry.
  1027. rgbQuad = &(bitmapInfo->bmiColors[colorTableEntry]);
  1028. // copy the pixel's color values to the destination buffer.
  1029. buf[(y * bitmap.bmWidth * 3) + ((x+1) * 3)] = rgbQuad->rgbRed;
  1030. buf[(y * bitmap.bmWidth * 3) + ((x+1) * 3) + 1] = rgbQuad->rgbGreen;
  1031. buf[(y * bitmap.bmWidth * 3) + ((x+1) * 3) + 2] = rgbQuad->rgbBlue;
  1032. }
  1033. }
  1034. }
  1035. else if (bitmapInfo->bmiHeader.biBitCount == 1)
  1036. {
  1037. // 1-bit monochrome bitmap.
  1038. int y, x, index, bit, bitMask;
  1039. for (y = 0; y < bitmap.bmHeight; ++y)
  1040. {
  1041. x = 0;
  1042. while (x < bitmap.bmWidth)
  1043. {
  1044. RGBQUAD *rgbQuad = NULL;
  1045. bitMask = 0x80;
  1046. // get the index into the bitmap data for the next 8 pixels.
  1047. index = y * bitmap.bmWidthBytes + x / 8;
  1048. // go through all 8 bits in this byte to get all 8 pixel colors.
  1049. do
  1050. {
  1051. // get the value of the bit for this pixel.
  1052. bit = ((unsigned char *)bitmap.bmBits)[index] & bitMask;
  1053. // bit will either be 0 or non-zero since there are only two colors.
  1054. if (bit == 0)
  1055. {
  1056. rgbQuad = &(bitmapInfo->bmiColors[0]);
  1057. }
  1058. else
  1059. {
  1060. rgbQuad = &(bitmapInfo->bmiColors[1]);
  1061. }
  1062. // copy this pixel's color values into the destination buffer.
  1063. buf[(y * bitmap.bmWidth * 3) + (x * 3)] = rgbQuad->rgbRed;
  1064. buf[(y * bitmap.bmWidth * 3) + (x * 3) + 1] = rgbQuad->rgbGreen;
  1065. buf[(y * bitmap.bmWidth * 3) + (x * 3) + 2] = rgbQuad->rgbBlue;
  1066. // go to the next pixel.
  1067. ++x;
  1068. bitMask = bitMask >> 1;
  1069. } while ((x < bitmap.bmWidth) && (bitMask > 0));
  1070. }
  1071. }
  1072. }
  1073. else
  1074. {
  1075. free(bitmapInfo);
  1076. free(buf);
  1077. DeleteObject(hBitmap);
  1078. return CE_SOURCE_FILE_FORMAT_NOT_SUPPORTED;
  1079. }
  1080. free(bitmapInfo);
  1081. // bitmaps are stored upside down, so flip the image vertically
  1082. int index;
  1083. int rowWidth = 3 * bitmap.bmWidth;
  1084. unsigned char *row = (unsigned char *)malloc(rowWidth);
  1085. for (index = 0; index < (bitmap.bmHeight / 2); ++index)
  1086. {
  1087. memcpy(row, buf + (index * rowWidth), rowWidth);
  1088. memcpy(buf + (index * rowWidth), buf + (((bitmap.bmHeight - 1) - index) * rowWidth), rowWidth);
  1089. memcpy(buf + (((bitmap.bmHeight - 1) - index) * rowWidth), row, rowWidth);
  1090. }
  1091. // write out the TGA file using the RGB data buffer.
  1092. CUtlBuffer outBuf;
  1093. bool retval = TGAWriter::WriteToBuffer(buf, outBuf, bitmap.bmWidth, bitmap.bmHeight, IMAGE_FORMAT_RGB888, IMAGE_FORMAT_RGB888);
  1094. if ( retval )
  1095. {
  1096. if ( !g_pFullFileSystem->WriteFile( tgaPath, NULL, outBuf ) )
  1097. {
  1098. retval = false;
  1099. }
  1100. }
  1101. DeleteObject(hBitmap);
  1102. return retval ? CE_SUCCESS : CE_ERROR_WRITING_OUTPUT_FILE;
  1103. #endif
  1104. }
  1105. // read a TGA header from the current point in the file stream.
  1106. static void ReadTGAHeader(FILE *infile, TGAHeader &header)
  1107. {
  1108. if (infile == NULL)
  1109. {
  1110. return;
  1111. }
  1112. fread(&header.identsize, sizeof(header.identsize), 1, infile);
  1113. fread(&header.colourmaptype, sizeof(header.colourmaptype), 1, infile);
  1114. fread(&header.imagetype, sizeof(header.imagetype), 1, infile);
  1115. fread(&header.colourmapstart, sizeof(header.colourmapstart), 1, infile);
  1116. fread(&header.colourmaplength, sizeof(header.colourmaplength), 1, infile);
  1117. fread(&header.colourmapbits, sizeof(header.colourmapbits), 1, infile);
  1118. fread(&header.xstart, sizeof(header.xstart), 1, infile);
  1119. fread(&header.ystart, sizeof(header.ystart), 1, infile);
  1120. fread(&header.width, sizeof(header.width), 1, infile);
  1121. fread(&header.height, sizeof(header.height), 1, infile);
  1122. fread(&header.bits, sizeof(header.bits), 1, infile);
  1123. fread(&header.descriptor, sizeof(header.descriptor), 1, infile);
  1124. }
  1125. // write a TGA header to the current point in the file stream.
  1126. static void WriteTGAHeader(FILE *outfile, TGAHeader &header)
  1127. {
  1128. if (outfile == NULL)
  1129. {
  1130. return;
  1131. }
  1132. fwrite(&header.identsize, sizeof(header.identsize), 1, outfile);
  1133. fwrite(&header.colourmaptype, sizeof(header.colourmaptype), 1, outfile);
  1134. fwrite(&header.imagetype, sizeof(header.imagetype), 1, outfile);
  1135. fwrite(&header.colourmapstart, sizeof(header.colourmapstart), 1, outfile);
  1136. fwrite(&header.colourmaplength, sizeof(header.colourmaplength), 1, outfile);
  1137. fwrite(&header.colourmapbits, sizeof(header.colourmapbits), 1, outfile);
  1138. fwrite(&header.xstart, sizeof(header.xstart), 1, outfile);
  1139. fwrite(&header.ystart, sizeof(header.ystart), 1, outfile);
  1140. fwrite(&header.width, sizeof(header.width), 1, outfile);
  1141. fwrite(&header.height, sizeof(header.height), 1, outfile);
  1142. fwrite(&header.bits, sizeof(header.bits), 1, outfile);
  1143. fwrite(&header.descriptor, sizeof(header.descriptor), 1, outfile);
  1144. }
  1145. // reads in a TGA file and converts it to 32 bit RGBA color values in a memory buffer.
  1146. unsigned char * COptionsSubMultiplayer::ReadTGAAsRGBA(const char *tgaPath, int &width, int &height, ConversionErrorType &errcode, TGAHeader &tgaHeader )
  1147. {
  1148. FILE *tgaFile = fopen(tgaPath, "rb");
  1149. if (tgaFile == NULL)
  1150. {
  1151. errcode = CE_CANT_OPEN_SOURCE_FILE;
  1152. return NULL;
  1153. }
  1154. // read header for TGA file.
  1155. ReadTGAHeader(tgaFile, tgaHeader);
  1156. // image type 2 is RGB, other types not supported.
  1157. if (tgaHeader.imagetype != 2)
  1158. {
  1159. fclose(tgaFile);
  1160. errcode = CE_SOURCE_FILE_FORMAT_NOT_SUPPORTED;
  1161. return NULL;
  1162. }
  1163. int tgaDataSize = tgaHeader.width * tgaHeader.height * tgaHeader.bits / 8;
  1164. unsigned char *tgaData = (unsigned char *)malloc(tgaDataSize);
  1165. if (tgaData == NULL)
  1166. {
  1167. fclose(tgaFile);
  1168. errcode = CE_MEMORY_ERROR;
  1169. return NULL;
  1170. }
  1171. fread(tgaData, 1, tgaDataSize, tgaFile);
  1172. fclose(tgaFile);
  1173. width = tgaHeader.width;
  1174. height = tgaHeader.height;
  1175. if (tgaHeader.bits == 24)
  1176. {
  1177. // image needs to be converted to a 32-bit image.
  1178. int numPixels = tgaHeader.width * tgaHeader.height;
  1179. unsigned char *retBuf = (unsigned char *)malloc(numPixels * 4);
  1180. if (retBuf == NULL)
  1181. {
  1182. free(tgaData);
  1183. errcode = CE_MEMORY_ERROR;
  1184. return NULL;
  1185. }
  1186. // convert from RGB to RGBA color format.
  1187. int index;
  1188. for (index = 0; index < numPixels; ++index)
  1189. {
  1190. retBuf[index * 4] = tgaData[index * 3];
  1191. retBuf[index * 4 + 1] = tgaData[index * 3 + 1];
  1192. retBuf[index * 4 + 2] = tgaData[index * 3 + 2];
  1193. retBuf[index * 4 + 3] = 0xff;
  1194. }
  1195. free(tgaData);
  1196. tgaHeader.bits = 32;
  1197. errcode = CE_SUCCESS;
  1198. return retBuf;
  1199. }
  1200. else if (tgaHeader.bits == 32)
  1201. {
  1202. // already in RGBA format so just return it.
  1203. errcode = CE_SUCCESS;
  1204. return tgaData;
  1205. }
  1206. // format not supported, fail.
  1207. free(tgaData);
  1208. errcode = CE_SOURCE_FILE_FORMAT_NOT_SUPPORTED;
  1209. return NULL;
  1210. }
  1211. // resizes the file specified by tgaPath so that it has dimensions that are
  1212. // powers-of-two and is equal to or smaller than 256x256.
  1213. // also converts from 24-bit RGB to 32-bit RGB (with 8-bit alpha)
  1214. ConversionErrorType COptionsSubMultiplayer::ConvertTGA(const char *tgaPath)
  1215. {
  1216. int tgaWidth = 0, tgaHeight = 0;
  1217. ConversionErrorType errcode;
  1218. TGAHeader tgaHeader;
  1219. unsigned char *srcBuffer = ReadTGAAsRGBA(tgaPath, tgaWidth, tgaHeight, errcode, tgaHeader);
  1220. if (srcBuffer == NULL)
  1221. {
  1222. return errcode;
  1223. }
  1224. int paddedImageWidth, paddedImageHeight;
  1225. if ((tgaWidth <= 0) || (tgaHeight <= 0))
  1226. {
  1227. free(srcBuffer);
  1228. return CE_ERROR_PARSING_SOURCE;
  1229. }
  1230. // get the nearest power of two that is greater than the width of the image.
  1231. paddedImageWidth = tgaWidth;
  1232. if (!IsPowerOfTwo(paddedImageWidth))
  1233. {
  1234. // width is not a power of two, calculate the next highest power of two value.
  1235. int i = 1;
  1236. while (paddedImageWidth > 1)
  1237. {
  1238. paddedImageWidth = paddedImageWidth >> 1;
  1239. ++i;
  1240. }
  1241. paddedImageWidth = paddedImageWidth << i;
  1242. }
  1243. // make sure the width is less than or equal to 256
  1244. if (paddedImageWidth > 256)
  1245. {
  1246. paddedImageWidth = 256;
  1247. }
  1248. // get the nearest power of two that is greater than the height of the image
  1249. paddedImageHeight = tgaHeight;
  1250. if (!IsPowerOfTwo(paddedImageHeight))
  1251. {
  1252. // height is not a power of two, calculate the next highest power of two value.
  1253. int i = 1;
  1254. while (paddedImageHeight > 1)
  1255. {
  1256. paddedImageHeight = paddedImageHeight >> 1;
  1257. ++i;
  1258. }
  1259. paddedImageHeight = paddedImageHeight << i;
  1260. }
  1261. // make sure the height is less than or equal to 256
  1262. if (paddedImageHeight > 256)
  1263. {
  1264. paddedImageHeight = 256;
  1265. }
  1266. // compute the amount of stretching that needs to be done to both width and height to get the image to fit.
  1267. float widthRatio = (float)paddedImageWidth / tgaWidth;
  1268. float heightRatio = (float)paddedImageHeight / tgaHeight;
  1269. int finalWidth;
  1270. int finalHeight;
  1271. // compute the final dimensions of the stretched image.
  1272. if (widthRatio < heightRatio)
  1273. {
  1274. finalWidth = paddedImageWidth;
  1275. finalHeight = (int)(tgaHeight * widthRatio + 0.5f);
  1276. // i.e. for 1x1 size pixels in the resized image we will take color from sourceRatio x sourceRatio sized pixels in the source image.
  1277. }
  1278. else if (heightRatio < widthRatio)
  1279. {
  1280. finalHeight = paddedImageHeight;
  1281. finalWidth = (int)(tgaWidth * heightRatio + 0.5f);
  1282. }
  1283. else
  1284. {
  1285. finalHeight = paddedImageHeight;
  1286. finalWidth = paddedImageWidth;
  1287. }
  1288. unsigned char *resizeBuffer = (unsigned char *)malloc(finalWidth * finalHeight * 4);
  1289. // do the actual stretching
  1290. StretchRGBAImage(srcBuffer, tgaWidth, tgaHeight, resizeBuffer, finalWidth, finalHeight);
  1291. free(srcBuffer); // don't need this anymore.
  1292. ///////////////////////////////////////////////////////////////////////
  1293. ///// need to pad the image so both dimensions are power of two's /////
  1294. ///////////////////////////////////////////////////////////////////////
  1295. unsigned char *finalBuffer = (unsigned char *)malloc(paddedImageWidth * paddedImageHeight * 4);
  1296. PadRGBAImage(resizeBuffer, finalWidth, finalHeight, finalBuffer, paddedImageWidth, paddedImageHeight);
  1297. FILE *outfile = fopen(tgaPath, "wb");
  1298. if (outfile == NULL)
  1299. {
  1300. free(resizeBuffer);
  1301. free(finalBuffer);
  1302. return CE_ERROR_WRITING_OUTPUT_FILE;
  1303. }
  1304. tgaHeader.width = paddedImageWidth;
  1305. tgaHeader.height = paddedImageHeight;
  1306. WriteTGAHeader(outfile, tgaHeader);
  1307. fwrite(finalBuffer, 1, paddedImageWidth * paddedImageHeight * 4, outfile);
  1308. fclose(outfile);
  1309. free(resizeBuffer);
  1310. free(finalBuffer);
  1311. return CE_SUCCESS;
  1312. }
  1313. // resize by stretching (or compressing) an RGBA image pointed to by srcBuf into the buffer pointed to by destBuf.
  1314. // the buffers are assumed to be sized appropriately to accomidate RGBA images of the given widths and heights.
  1315. ConversionErrorType COptionsSubMultiplayer::StretchRGBAImage(const unsigned char *srcBuf, const int srcWidth, const int srcHeight,
  1316. unsigned char *destBuf, const int destWidth, const int destHeight)
  1317. {
  1318. if ((srcBuf == NULL) || (destBuf == NULL))
  1319. {
  1320. return CE_CANT_OPEN_SOURCE_FILE;
  1321. }
  1322. int destRow,destColumn;
  1323. float ratioX = (float)srcWidth / (float)destWidth;
  1324. float ratioY = (float)srcHeight / (float)destHeight;
  1325. // loop through all the pixels in the destination image.
  1326. for (destRow = 0; destRow < destHeight; ++destRow)
  1327. {
  1328. for (destColumn = 0; destColumn < destWidth; ++destColumn)
  1329. {
  1330. // calculate the center of the pixel in the source image.
  1331. float srcCenterX = ratioX * (destColumn + 0.5f);
  1332. float srcCenterY = ratioY * (destRow + 0.5f);
  1333. // calculate the starting and ending coords for this destination pixel in the source image.
  1334. float srcStartX = srcCenterX - (ratioX / 2.0f);
  1335. if (srcStartX < 0.0f)
  1336. {
  1337. srcStartX = 0.0f; // this should never happen, but just in case.
  1338. }
  1339. float srcStartY = srcCenterY - (ratioY / 2.0f);
  1340. if (srcStartY < 0.0f)
  1341. {
  1342. srcStartY = 0.0f; // this should never happen, but just in case.
  1343. }
  1344. float srcEndX = srcCenterX + (ratioX / 2.0f);
  1345. if (srcEndX > srcWidth)
  1346. {
  1347. srcEndX = srcWidth; // this should never happen, but just in case.
  1348. }
  1349. float srcEndY = srcCenterY + (ratioY / 2.0f);
  1350. if (srcEndY > srcHeight)
  1351. {
  1352. srcEndY = srcHeight; // this should never happen, but just in case.
  1353. }
  1354. // Calculate the percentage of each source pixels' contribution to the destination pixel color.
  1355. float srcCurrentX; // initialized at the start of the y loop.
  1356. float srcCurrentY = srcStartY;
  1357. float destRed = 0.0f;
  1358. float destGreen = 0.0f;
  1359. float destBlue = 0.0f;
  1360. float destAlpha = 0.0f;
  1361. //// loop for the parts of the source image that will contribute color to the destination pixel.
  1362. while (srcCurrentY < srcEndY)
  1363. {
  1364. float srcCurrentEndY = (float)((int)srcCurrentY + 1);
  1365. if (srcCurrentEndY > srcEndY)
  1366. {
  1367. srcCurrentEndY = srcEndY;
  1368. }
  1369. float srcCurrentHeight = srcCurrentEndY - srcCurrentY;
  1370. srcCurrentX = srcStartX;
  1371. while (srcCurrentX < srcEndX)
  1372. {
  1373. float srcCurrentEndX = (float)((int)srcCurrentX + 1);
  1374. if (srcCurrentEndX > srcEndX)
  1375. {
  1376. srcCurrentEndX = srcEndX;
  1377. }
  1378. float srcCurrentWidth = srcCurrentEndX - srcCurrentX;
  1379. // compute the percentage of the destination pixel's color this source pixel will contribute.
  1380. float srcColorPercentage = (srcCurrentWidth / ratioX) * (srcCurrentHeight / ratioY);
  1381. int srcCurrentPixelX = (int)srcCurrentX;
  1382. int srcCurrentPixelY = (int)srcCurrentY;
  1383. // get the color values for this source pixel.
  1384. unsigned char srcCurrentRed = srcBuf[(srcCurrentPixelY * srcWidth * 4) + (srcCurrentPixelX * 4)];
  1385. unsigned char srcCurrentGreen = srcBuf[(srcCurrentPixelY * srcWidth * 4) + (srcCurrentPixelX * 4) + 1];
  1386. unsigned char srcCurrentBlue = srcBuf[(srcCurrentPixelY * srcWidth * 4) + (srcCurrentPixelX * 4) + 2];
  1387. unsigned char srcCurrentAlpha = srcBuf[(srcCurrentPixelY * srcWidth * 4) + (srcCurrentPixelX * 4) + 3];
  1388. // add the color contribution from this source pixel to the destination pixel.
  1389. destRed += srcCurrentRed * srcColorPercentage;
  1390. destGreen += srcCurrentGreen * srcColorPercentage;
  1391. destBlue += srcCurrentBlue * srcColorPercentage;
  1392. destAlpha += srcCurrentAlpha * srcColorPercentage;
  1393. srcCurrentX = srcCurrentEndX;
  1394. }
  1395. srcCurrentY = srcCurrentEndY;
  1396. }
  1397. // assign the computed color to the destination pixel, round to the nearest value. Make sure the value doesn't exceed 255.
  1398. destBuf[(destRow * destWidth * 4) + (destColumn * 4)] = MIN((int)(destRed + 0.5f), 255);
  1399. destBuf[(destRow * destWidth * 4) + (destColumn * 4) + 1] = MIN((int)(destGreen + 0.5f), 255);
  1400. destBuf[(destRow * destWidth * 4) + (destColumn * 4) + 2] = MIN((int)(destBlue + 0.5f), 255);
  1401. destBuf[(destRow * destWidth * 4) + (destColumn * 4) + 3] = MIN((int)(destAlpha + 0.5f), 255);
  1402. } // column loop
  1403. } // row loop
  1404. return CE_SUCCESS;
  1405. }
  1406. ConversionErrorType COptionsSubMultiplayer::PadRGBAImage(const unsigned char *srcBuf, const int srcWidth, const int srcHeight,
  1407. unsigned char *destBuf, const int destWidth, const int destHeight)
  1408. {
  1409. if ((srcBuf == NULL) || (destBuf == NULL))
  1410. {
  1411. return CE_CANT_OPEN_SOURCE_FILE;
  1412. }
  1413. memset(destBuf, 0, destWidth * destHeight * 4);
  1414. if ((destWidth < srcWidth) || (destHeight < srcHeight))
  1415. {
  1416. return CE_ERROR_PARSING_SOURCE;
  1417. }
  1418. if ((srcWidth == destWidth) && (srcHeight == destHeight))
  1419. {
  1420. // no padding is needed, just copy the buffer straight over and call it done.
  1421. memcpy(destBuf, srcBuf, destWidth * destHeight * 4);
  1422. return CE_SUCCESS;
  1423. }
  1424. if (destWidth == srcWidth)
  1425. {
  1426. // only the top and bottom of the image need padding.
  1427. // do this separately since we can do this more efficiently than the other cases.
  1428. int numRowsToPad = (destHeight - srcHeight) / 2;
  1429. memcpy(destBuf + (numRowsToPad * destWidth * 4), srcBuf, srcWidth * srcHeight * 4);
  1430. }
  1431. else
  1432. {
  1433. int numColumnsToPad = (destWidth - srcWidth) / 2;
  1434. int numRowsToPad = (destHeight - srcHeight) / 2;
  1435. int lastRow = numRowsToPad + srcHeight;
  1436. int row;
  1437. for (row = numRowsToPad; row < lastRow; ++row)
  1438. {
  1439. unsigned char * destOffset = destBuf + (row * destWidth * 4) + (numColumnsToPad * 4);
  1440. const unsigned char * srcOffset = srcBuf + ((row - numRowsToPad) * srcWidth * 4);
  1441. memcpy(destOffset, srcOffset, srcWidth * 4);
  1442. }
  1443. }
  1444. return CE_SUCCESS;
  1445. }
  1446. // convert TGA file at the given location to a VTF file of the same root name at the same location.
  1447. ConversionErrorType COptionsSubMultiplayer::ConvertTGAToVTF(const char *tgaPath)
  1448. {
  1449. FILE *infile = fopen(tgaPath, "rb");
  1450. if (infile == NULL)
  1451. {
  1452. return CE_CANT_OPEN_SOURCE_FILE;
  1453. }
  1454. // read out the header of the image.
  1455. TGAHeader header;
  1456. ReadTGAHeader(infile, header);
  1457. // check to make sure that the TGA has the proper dimensions and size.
  1458. if (!IsPowerOfTwo(header.width) || !IsPowerOfTwo(header.height))
  1459. {
  1460. fclose(infile);
  1461. return CE_SOURCE_FILE_FORMAT_NOT_SUPPORTED;
  1462. }
  1463. // check to make sure that the TGA isn't too big.
  1464. if ((header.width > 256) || (header.height > 256))
  1465. {
  1466. fclose(infile);
  1467. return CE_SOURCE_FILE_FORMAT_NOT_SUPPORTED;
  1468. }
  1469. int imageMemoryFootprint = header.width * header.height * header.bits / 8;
  1470. CUtlBuffer inbuf(0, imageMemoryFootprint);
  1471. // read in the image
  1472. int nBytesRead = fread(inbuf.Base(), imageMemoryFootprint, 1, infile);
  1473. fclose(infile);
  1474. inbuf.SeekPut( CUtlBuffer::SEEK_HEAD, nBytesRead );
  1475. // load vtex_dll.dll and get the interface to it.
  1476. CSysModule *vtexmod = Sys_LoadModule("vtex_dll");
  1477. if (vtexmod == NULL)
  1478. {
  1479. return CE_ERROR_LOADING_DLL;
  1480. }
  1481. CreateInterfaceFn factory = Sys_GetFactory(vtexmod);
  1482. if (factory == NULL)
  1483. {
  1484. Sys_UnloadModule(vtexmod);
  1485. return CE_ERROR_LOADING_DLL;
  1486. }
  1487. IVTex *vtex = (IVTex *)factory(IVTEX_VERSION_STRING, NULL);
  1488. if (vtex == NULL)
  1489. {
  1490. Sys_UnloadModule(vtexmod);
  1491. return CE_ERROR_LOADING_DLL;
  1492. }
  1493. char *vtfParams[4];
  1494. // the 0th entry is skipped cause normally thats the program name.
  1495. vtfParams[0] = "";
  1496. vtfParams[1] = "-quiet";
  1497. vtfParams[2] = "-dontusegamedir";
  1498. vtfParams[3] = (char *)tgaPath;
  1499. // call vtex to do the conversion.
  1500. vtex->VTex(4, vtfParams);
  1501. Sys_UnloadModule(vtexmod);
  1502. return CE_SUCCESS;
  1503. }
  1504. // write a VMT file for the spray VTF file at the given path.
  1505. ConversionErrorType COptionsSubMultiplayer::WriteSprayVMT(const char *vtfPath)
  1506. {
  1507. if (vtfPath == NULL)
  1508. {
  1509. return CE_ERROR_WRITING_OUTPUT_FILE;
  1510. }
  1511. // make the vmt filename
  1512. char vmtPath[MAX_PATH*4];
  1513. Q_strncpy(vmtPath, vtfPath, sizeof(vmtPath));
  1514. char *c = vmtPath + strlen(vmtPath);
  1515. while ((c > vmtPath) && (*(c-1) != '.'))
  1516. {
  1517. --c;
  1518. }
  1519. Q_strncpy(c, "vmt", sizeof(vmtPath) - (c - vmtPath));
  1520. // get the root filename for the vtf file
  1521. char filename[MAX_PATH];
  1522. while ((c > vmtPath) && (*(c-1) != '/') && (*(c-1) != '\\'))
  1523. {
  1524. --c;
  1525. }
  1526. int i = 0;
  1527. while ((*c != 0) && (*c != '.'))
  1528. {
  1529. filename[i++] = *(c++);
  1530. }
  1531. filename[i] = 0;
  1532. // create the vmt file.
  1533. FILE *vmtFile = fopen(vmtPath, "w");
  1534. if (vmtFile == NULL)
  1535. {
  1536. return CE_ERROR_WRITING_OUTPUT_FILE;
  1537. }
  1538. // write the contents of the file.
  1539. fprintf(vmtFile, "LightmappedGeneric\n{\n\t\"$basetexture\" \"vgui\\logos\\%s\"\n\t\"$translucent\" \"1\"\n\t\"$decal\" \"1\"\n\t\"$decalscale\" \"0.250\"\n}\n", filename);
  1540. fclose(vmtFile);
  1541. return CE_SUCCESS;
  1542. }
  1543. //-----------------------------------------------------------------------------
  1544. // Purpose: Builds the list of logos
  1545. //-----------------------------------------------------------------------------
  1546. void COptionsSubMultiplayer::InitLogoList( CLabeledCommandComboBox *cb )
  1547. {
  1548. // Find out images
  1549. FileFindHandle_t fh;
  1550. char directory[ 512 ];
  1551. ConVarRef cl_logofile( "cl_logofile" );
  1552. if ( !cl_logofile.IsValid() )
  1553. return;
  1554. cb->DeleteAllItems();
  1555. const char *logofile = cl_logofile.GetString();
  1556. Q_snprintf( directory, sizeof( directory ), "materials/vgui/logos/*.vtf" );
  1557. const char *fn = g_pFullFileSystem->FindFirst( directory, &fh );
  1558. int i = 0, initialItem = 0;
  1559. while (fn)
  1560. {
  1561. char filename[ 512 ];
  1562. Q_snprintf( filename, sizeof(filename), "materials/vgui/logos/%s", fn );
  1563. if ( strlen( filename ) >= 4 )
  1564. {
  1565. filename[ strlen( filename ) - 4 ] = 0;
  1566. Q_strncat( filename, ".vmt", sizeof( filename ), COPY_ALL_CHARACTERS );
  1567. if ( g_pFullFileSystem->FileExists( filename ) )
  1568. {
  1569. // strip off the extension
  1570. Q_strncpy( filename, fn, sizeof( filename ) );
  1571. filename[ strlen( filename ) - 4 ] = 0;
  1572. cb->AddItem( filename, "" );
  1573. // check to see if this is the one we have set
  1574. Q_snprintf( filename, sizeof(filename), "materials/vgui/logos/%s", fn );
  1575. if (!Q_stricmp(filename, logofile))
  1576. {
  1577. initialItem = i;
  1578. }
  1579. ++i;
  1580. }
  1581. }
  1582. fn = g_pFullFileSystem->FindNext( fh );
  1583. }
  1584. g_pFullFileSystem->FindClose( fh );
  1585. cb->SetInitialItem(initialItem);
  1586. }
  1587. //-----------------------------------------------------------------------------
  1588. // Purpose: Selects the given logo in the logo list.
  1589. //-----------------------------------------------------------------------------
  1590. void COptionsSubMultiplayer::SelectLogo(const char *logoName)
  1591. {
  1592. int numEntries = m_pLogoList->GetItemCount();
  1593. int index;
  1594. wchar_t itemText[MAX_PATH];
  1595. wchar_t itemToSelectText[MAX_PATH];
  1596. // convert the logo filename to unicode
  1597. g_pVGuiLocalize->ConvertANSIToUnicode(logoName, itemToSelectText, sizeof(itemToSelectText));
  1598. // find the index of the spray we want.
  1599. for (index = 0; index < numEntries; ++index)
  1600. {
  1601. m_pLogoList->GetItemText(index, itemText, sizeof(itemText));
  1602. if (!wcscmp(itemText, itemToSelectText))
  1603. {
  1604. break;
  1605. }
  1606. }
  1607. if (index < numEntries)
  1608. {
  1609. // select the logo.
  1610. m_pLogoList->ActivateItem(index);
  1611. }
  1612. }
  1613. #define MODEL_MATERIAL_BASE_FOLDER "materials/vgui/playermodels/"
  1614. void StripStringOutOfString( const char *pPattern, const char *pIn, char *pOut )
  1615. {
  1616. int iLengthBase = strlen( pPattern );
  1617. int iLengthString = strlen( pIn );
  1618. int k = 0;
  1619. for ( int j = iLengthBase; j < iLengthString; j++ )
  1620. {
  1621. pOut[k] = pIn[j];
  1622. k++;
  1623. }
  1624. pOut[k] = 0;
  1625. }
  1626. void FindVMTFilesInFolder( const char *pFolder, const char *pFolderName, CLabeledCommandComboBox *cb, int &iCount, int &iInitialItem )
  1627. {
  1628. ConVarRef cl_modelfile( "cl_playermodel" );
  1629. if ( !cl_modelfile.IsValid() )
  1630. return;
  1631. char directory[ 512 ];
  1632. Q_snprintf( directory, sizeof( directory ), "%s/*.*", pFolder );
  1633. FileFindHandle_t fh;
  1634. const char *fn = g_pFullFileSystem->FindFirst( directory, &fh );
  1635. const char *modelfile = cl_modelfile.GetString();
  1636. while ( fn )
  1637. {
  1638. if ( !stricmp( fn, ".") || !stricmp( fn, "..") )
  1639. {
  1640. fn = g_pFullFileSystem->FindNext( fh );
  1641. continue;
  1642. }
  1643. if ( g_pFullFileSystem->FindIsDirectory( fh ) )
  1644. {
  1645. char folderpath[512];
  1646. Q_snprintf( folderpath, sizeof( folderpath ), "%s/%s", pFolder, fn );
  1647. FindVMTFilesInFolder( folderpath, fn, cb, iCount, iInitialItem );
  1648. fn = g_pFullFileSystem->FindNext( fh );
  1649. continue;
  1650. }
  1651. if ( !strstr( fn, ".vmt" ) )
  1652. {
  1653. fn = g_pFullFileSystem->FindNext( fh );
  1654. continue;
  1655. }
  1656. char filename[ 512 ];
  1657. Q_snprintf( filename, sizeof(filename), "%s/%s", pFolder, fn );
  1658. if ( strlen( filename ) >= 4 )
  1659. {
  1660. filename[ strlen( filename ) - 4 ] = 0;
  1661. Q_strncat( filename, ".vmt", sizeof( filename ), COPY_ALL_CHARACTERS );
  1662. if ( g_pFullFileSystem->FileExists( filename ) )
  1663. {
  1664. char displayname[ 512 ];
  1665. char texturepath[ 512 ];
  1666. // strip off the extension
  1667. Q_strncpy( displayname, fn, sizeof( displayname ) );
  1668. StripStringOutOfString( MODEL_MATERIAL_BASE_FOLDER, filename, texturepath );
  1669. displayname[ strlen( displayname ) - 4 ] = 0;
  1670. cb->AddItem( displayname, texturepath + 1 ); // ignore the initial "/" in texture path
  1671. char realname[ 512 ];
  1672. Q_FileBase( modelfile, realname, sizeof( realname ) );
  1673. Q_FileBase( filename, filename, sizeof( filename ) );
  1674. if (!stricmp(filename, realname))
  1675. {
  1676. iInitialItem = iCount;
  1677. }
  1678. ++iCount;
  1679. }
  1680. }
  1681. fn = g_pFullFileSystem->FindNext( fh );
  1682. }
  1683. }
  1684. //-----------------------------------------------------------------------------
  1685. // Purpose: Builds model list
  1686. //-----------------------------------------------------------------------------
  1687. void COptionsSubMultiplayer::InitModelList( CLabeledCommandComboBox *cb )
  1688. {
  1689. // Find out images
  1690. int i = 0, initialItem = 0;
  1691. cb->DeleteAllItems();
  1692. FindVMTFilesInFolder( MODEL_MATERIAL_BASE_FOLDER, "", cb, i, initialItem );
  1693. cb->SetInitialItem( initialItem );
  1694. }
  1695. //-----------------------------------------------------------------------------
  1696. // Purpose:
  1697. //-----------------------------------------------------------------------------
  1698. void COptionsSubMultiplayer::InitCrosshairColorEntries()
  1699. {
  1700. // parse the string for the custom color settings and get the initial settings.
  1701. ConVarRef cl_crosshaircolor( "cl_crosshaircolor" );
  1702. int index = 0;
  1703. if ( cl_crosshaircolor.IsValid() )
  1704. {
  1705. index = clamp( cl_crosshaircolor.GetInt(), 0, NumCrosshairColors );
  1706. }
  1707. if (m_pCrosshairColorComboBox != NULL)
  1708. {
  1709. KeyValues *data = new KeyValues("data");
  1710. // add in the "Default" selection
  1711. data->Clear();
  1712. // add in the colors for the color list
  1713. for ( int i = 0; i < NumCrosshairColors; i++ )
  1714. {
  1715. data->SetInt("color", i);
  1716. m_pCrosshairColorComboBox->AddItem( s_crosshairColors[ i ].name, data);
  1717. }
  1718. m_pCrosshairColorComboBox->ActivateItemByRow(index);
  1719. data->deleteThis();
  1720. }
  1721. // force the crosshair to redraw.
  1722. RedrawCrosshairImage(); // don't need to do this since we're not drawing anything.
  1723. }
  1724. //-----------------------------------------------------------------------------
  1725. // Purpose: takes the settings from the crosshair settings combo boxes and sliders
  1726. // and apply it to the crosshair illustrations.
  1727. //-----------------------------------------------------------------------------
  1728. void COptionsSubMultiplayer::RedrawCrosshairImage()
  1729. {
  1730. if (m_pCrosshairColorComboBox == NULL)
  1731. {
  1732. return;
  1733. }
  1734. bool enableApplyButton = false;
  1735. // get the color selected in the combo box.
  1736. KeyValues *data = m_pCrosshairColorComboBox->GetActiveItemUserData();
  1737. int colorIndex = data->GetInt("color");
  1738. colorIndex = clamp( colorIndex, 0, NumCrosshairColors );
  1739. int selectedVal = 0;
  1740. int actualVal = 0;
  1741. if (m_pCrosshairColorComboBox != NULL)
  1742. {
  1743. selectedVal = m_pCrosshairColorComboBox->GetActiveItem();
  1744. }
  1745. ConVarRef cl_crosshaircolor( "cl_crosshaircolor" );
  1746. if ( cl_crosshaircolor.IsValid() )
  1747. {
  1748. actualVal = clamp( cl_crosshaircolor.GetInt(), 0, NumCrosshairColors );
  1749. }
  1750. if ( selectedVal != actualVal )
  1751. {
  1752. enableApplyButton = true;
  1753. }
  1754. if (enableApplyButton)
  1755. {
  1756. OnApplyButtonEnable();
  1757. }
  1758. if ( m_pCrosshairImage && m_pCrosshairSize )
  1759. {
  1760. int size = m_pCrosshairSize->GetActiveItem();
  1761. m_pCrosshairImage->UpdateCrosshair(
  1762. s_crosshairColors[selectedVal].r,
  1763. s_crosshairColors[selectedVal].g,
  1764. s_crosshairColors[selectedVal].b,
  1765. size );
  1766. }
  1767. }
  1768. //-----------------------------------------------------------------------------
  1769. // Purpose: takes the settings from the crosshair settings combo boxes and sliders
  1770. // and apply it to the crosshair illustrations.
  1771. //-----------------------------------------------------------------------------
  1772. void COptionsSubMultiplayer::RedrawAdvCrosshairImage()
  1773. {
  1774. if ( !ModInfo().AdvCrosshair() )
  1775. {
  1776. return;
  1777. }
  1778. // get the color selected in the combo box.
  1779. int r,g,b;
  1780. r = clamp( m_pAdvCrosshairRedSlider->GetSliderValue(), 0, 255 );
  1781. g = clamp( m_pAdvCrosshairGreenSlider->GetSliderValue(), 0, 255 );
  1782. b = clamp( m_pAdvCrosshairBlueSlider->GetSliderValue(), 0, 255 );
  1783. float scale = m_pAdvCrosshairScaleSlider->GetSliderValue();
  1784. if ( m_pAdvCrosshairImage && m_pAdvCrosshairStyle )
  1785. {
  1786. char crosshairname[256];
  1787. m_pAdvCrosshairStyle->GetText( crosshairname, sizeof(crosshairname) );
  1788. char texture[ 256 ];
  1789. Q_snprintf ( texture, sizeof( texture ), "vgui/crosshairs/%s", crosshairname );
  1790. m_pAdvCrosshairImage->UpdateCrosshair( r, g, b, scale, texture );
  1791. }
  1792. }
  1793. //-----------------------------------------------------------------------------
  1794. // Purpose: initialize the crosshair size list.
  1795. //-----------------------------------------------------------------------------
  1796. void COptionsSubMultiplayer::InitCrosshairSizeList(CLabeledCommandComboBox *cb)
  1797. {
  1798. if (cb == NULL)
  1799. {
  1800. return;
  1801. }
  1802. cb->Reset();
  1803. // add in the auto, small, medium, and large size selections.
  1804. cb->AddItem("#GameUI_Auto", "cl_crosshairscale 0");
  1805. cb->AddItem("#GameUI_Small", "cl_crosshairscale 1200");
  1806. cb->AddItem("#GameUI_Medium", "cl_crosshairscale 768");
  1807. cb->AddItem("#GameUI_Large", "cl_crosshairscale 600");
  1808. // parse out the size value from the cvar and set the initial value.
  1809. int initialScale = 0;
  1810. ConVarRef cl_crosshairscale( "cl_crosshairscale" );
  1811. if ( cl_crosshairscale.IsValid() )
  1812. {
  1813. initialScale = cl_crosshairscale.GetInt();
  1814. if ( initialScale <= 0 )
  1815. {
  1816. initialScale = 0;
  1817. }
  1818. else if ( initialScale <= 600 )
  1819. {
  1820. initialScale = 3;
  1821. }
  1822. else if ( initialScale <= 768 )
  1823. {
  1824. initialScale = 2;
  1825. }
  1826. else
  1827. {
  1828. initialScale = 1;
  1829. }
  1830. }
  1831. cb->SetInitialItem( initialScale );
  1832. }
  1833. //-----------------------------------------------------------------------------
  1834. // Purpose: initialize the crosshair style list
  1835. //-----------------------------------------------------------------------------
  1836. void COptionsSubMultiplayer::InitAdvCrosshairStyleList(CLabeledCommandComboBox *cb)
  1837. {
  1838. // Find out images
  1839. FileFindHandle_t fh;
  1840. char directory[ 512 ];
  1841. ConVarRef cl_crosshair_file( "cl_crosshair_file" );
  1842. if ( !cl_crosshair_file.IsValid() )
  1843. return;
  1844. cb->DeleteAllItems();
  1845. char crosshairfile[256];
  1846. Q_snprintf( crosshairfile, sizeof(crosshairfile), "materials/vgui/crosshairs/%s.vtf", cl_crosshair_file.GetString() );
  1847. Q_snprintf( directory, sizeof( directory ), "materials/vgui/crosshairs/*.vtf" );
  1848. const char *fn = g_pFullFileSystem->FindFirst( directory, &fh );
  1849. int i = 0, initialItem = 0;
  1850. while (fn)
  1851. {
  1852. char filename[ 512 ];
  1853. Q_snprintf( filename, sizeof(filename), "materials/vgui/crosshairs/%s", fn );
  1854. if ( strlen( filename ) >= 4 )
  1855. {
  1856. filename[ strlen( filename ) - 4 ] = 0;
  1857. Q_strncat( filename, ".vmt", sizeof( filename ), COPY_ALL_CHARACTERS );
  1858. if ( g_pFullFileSystem->FileExists( filename ) )
  1859. {
  1860. // strip off the extension
  1861. Q_strncpy( filename, fn, sizeof( filename ) );
  1862. filename[ strlen( filename ) - 4 ] = 0;
  1863. cb->AddItem( filename, "" );
  1864. // check to see if this is the one we have set
  1865. Q_snprintf( filename, sizeof(filename), "materials/vgui/crosshairs/%s", fn );
  1866. if (!stricmp(filename, crosshairfile))
  1867. {
  1868. initialItem = i;
  1869. }
  1870. ++i;
  1871. }
  1872. }
  1873. fn = g_pFullFileSystem->FindNext( fh );
  1874. }
  1875. g_pFullFileSystem->FindClose( fh );
  1876. cb->SetInitialItem(initialItem);
  1877. }
  1878. //-----------------------------------------------------------------------------
  1879. // Purpose:
  1880. //-----------------------------------------------------------------------------
  1881. void COptionsSubMultiplayer::RemapLogo()
  1882. {
  1883. char logoname[256];
  1884. m_pLogoList->GetText( logoname, sizeof( logoname ) );
  1885. if( !logoname[ 0 ] )
  1886. return;
  1887. char fullLogoName[512];
  1888. // make sure there is a version with the proper shader
  1889. g_pFullFileSystem->CreateDirHierarchy( "materials/VGUI/logos/UI", "GAME" );
  1890. Q_snprintf( fullLogoName, sizeof( fullLogoName ), "materials/VGUI/logos/UI/%s.vmt", logoname );
  1891. if ( !g_pFullFileSystem->FileExists( fullLogoName ) )
  1892. {
  1893. FileHandle_t fp = g_pFullFileSystem->Open( fullLogoName, "wb" );
  1894. if ( !fp )
  1895. return;
  1896. char data[1024];
  1897. Q_snprintf( data, sizeof( data ), "\"UnlitGeneric\"\n\
  1898. {\n\
  1899. // Original shader: BaseTimesVertexColorAlphaBlendNoOverbright\n\
  1900. \"$translucent\" 1\n\
  1901. \"$basetexture\" \"VGUI\\logos\\%s\"\n\
  1902. \"$vertexcolor\" 1\n\
  1903. \"$vertexalpha\" 1\n\
  1904. \"$no_fullbright\" 1\n\
  1905. \"$ignorez\" 1\n\
  1906. }\n\
  1907. ", logoname );
  1908. g_pFullFileSystem->Write( data, strlen( data ), fp );
  1909. g_pFullFileSystem->Close( fp );
  1910. }
  1911. Q_snprintf( fullLogoName, sizeof( fullLogoName ), "logos/UI/%s", logoname );
  1912. m_pLogoImage->SetImage( fullLogoName );
  1913. }
  1914. //-----------------------------------------------------------------------------
  1915. // Purpose:
  1916. //-----------------------------------------------------------------------------
  1917. void COptionsSubMultiplayer::RemapModel()
  1918. {
  1919. const char *pModelName = m_pModelList->GetActiveItemCommand();
  1920. if( pModelName == NULL )
  1921. return;
  1922. char texture[ 256 ];
  1923. Q_snprintf ( texture, sizeof( texture ), "vgui/playermodels/%s", pModelName );
  1924. texture[ strlen( texture ) - 4 ] = 0;
  1925. m_pModelImage->setTexture( texture );
  1926. }
  1927. //-----------------------------------------------------------------------------
  1928. // Purpose: Called whenever model name changes
  1929. //-----------------------------------------------------------------------------
  1930. void COptionsSubMultiplayer::OnTextChanged(vgui::Panel *panel)
  1931. {
  1932. RemapModel();
  1933. RemapLogo();
  1934. RedrawCrosshairImage(); // redraw the crosshair.
  1935. RedrawAdvCrosshairImage();
  1936. }
  1937. //-----------------------------------------------------------------------------
  1938. // Purpose:
  1939. //-----------------------------------------------------------------------------
  1940. void COptionsSubMultiplayer::OnSliderMoved(KeyValues *data)
  1941. {
  1942. m_nTopColor = (int) m_pPrimaryColorSlider->GetSliderValue();
  1943. m_nBottomColor = (int) m_pSecondaryColorSlider->GetSliderValue();
  1944. RemapModel();
  1945. RedrawAdvCrosshairImage();
  1946. }
  1947. //-----------------------------------------------------------------------------
  1948. // Purpose:
  1949. //-----------------------------------------------------------------------------
  1950. void COptionsSubMultiplayer::OnApplyButtonEnable()
  1951. {
  1952. PostMessage(GetParent(), new KeyValues("ApplyButtonEnable"));
  1953. InvalidateLayout();
  1954. }
  1955. //#include <windows.h>
  1956. #define DIB_HEADER_MARKER ((WORD) ('M' << 8) | 'B')
  1957. #define SUIT_HUE_START 192
  1958. #define SUIT_HUE_END 223
  1959. #define PLATE_HUE_START 160
  1960. #define PLATE_HUE_END 191
  1961. #if !defined( _WIN32 ) && !defined( _PS3 )
  1962. typedef struct RGBQUAD {
  1963. BYTE rgbBlue;
  1964. BYTE rgbGreen;
  1965. BYTE rgbRed;
  1966. BYTE rgbReserved;
  1967. };
  1968. #endif
  1969. //-----------------------------------------------------------------------------
  1970. // Purpose:
  1971. //-----------------------------------------------------------------------------
  1972. static void PaletteHueReplace( RGBQUAD *palSrc, int newHue, int Start, int end )
  1973. {
  1974. int i;
  1975. float r, b, g;
  1976. float maxcol, mincol;
  1977. float hue, val, sat;
  1978. hue = (float)(newHue * (360.0 / 255));
  1979. for (i = Start; i <= end; i++)
  1980. {
  1981. b = palSrc[ i ].rgbBlue;
  1982. g = palSrc[ i ].rgbGreen;
  1983. r = palSrc[ i ].rgbRed;
  1984. maxcol = MAX( MAX( r, g ), b ) / 255.0f;
  1985. mincol = MIN( MIN( r, g ), b ) / 255.0f;
  1986. val = maxcol;
  1987. sat = (maxcol - mincol) / maxcol;
  1988. mincol = val * (1.0f - sat);
  1989. if (hue <= 120)
  1990. {
  1991. b = mincol;
  1992. if (hue < 60)
  1993. {
  1994. r = val;
  1995. g = mincol + hue * (val - mincol)/(120 - hue);
  1996. }
  1997. else
  1998. {
  1999. g = val;
  2000. r = mincol + (120 - hue)*(val-mincol)/hue;
  2001. }
  2002. }
  2003. else if (hue <= 240)
  2004. {
  2005. r = mincol;
  2006. if (hue < 180)
  2007. {
  2008. g = val;
  2009. b = mincol + (hue - 120)*(val-mincol)/(240 - hue);
  2010. }
  2011. else
  2012. {
  2013. b = val;
  2014. g = mincol + (240 - hue)*(val-mincol)/(hue - 120);
  2015. }
  2016. }
  2017. else
  2018. {
  2019. g = mincol;
  2020. if (hue < 300)
  2021. {
  2022. b = val;
  2023. r = mincol + (hue - 240)*(val-mincol)/(360 - hue);
  2024. }
  2025. else
  2026. {
  2027. r = val;
  2028. b = mincol + (360 - hue)*(val-mincol)/(hue - 240);
  2029. }
  2030. }
  2031. palSrc[ i ].rgbBlue = (unsigned char)(b * 255);
  2032. palSrc[ i ].rgbGreen = (unsigned char)(g * 255);
  2033. palSrc[ i ].rgbRed = (unsigned char)(r * 255);
  2034. }
  2035. }
  2036. //-----------------------------------------------------------------------------
  2037. // Purpose:
  2038. //-----------------------------------------------------------------------------
  2039. void COptionsSubMultiplayer::RemapPalette( char *filename, int topcolor, int bottomcolor )
  2040. {
  2041. #ifdef _WIN32
  2042. char infile[ 256 ];
  2043. char outfile[ 256 ];
  2044. FileHandle_t file;
  2045. CUtlBuffer outbuffer( 16384, 16384 );
  2046. Q_snprintf( infile, sizeof( infile ), "models/player/%s/%s.bmp", filename, filename );
  2047. Q_strncpy( outfile, "models/player/remapped.bmp", sizeof( outfile ) );
  2048. file = g_pFullFileSystem->Open( infile, "rb" );
  2049. if ( file == FILESYSTEM_INVALID_HANDLE )
  2050. return;
  2051. // Parse bitmap
  2052. BITMAPFILEHEADER bmfHeader;
  2053. DWORD dwBitsSize, dwFileSize;
  2054. LPBITMAPINFO lpbmi;
  2055. dwFileSize = g_pFullFileSystem->Size( file );
  2056. g_pFullFileSystem->Read( &bmfHeader, sizeof(bmfHeader), file );
  2057. outbuffer.Put( &bmfHeader, sizeof( bmfHeader ) );
  2058. if (bmfHeader.bfType == DIB_HEADER_MARKER)
  2059. {
  2060. dwBitsSize = dwFileSize - sizeof(bmfHeader);
  2061. HGLOBAL hDIB = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, dwBitsSize );
  2062. char *pDIB = (LPSTR)GlobalLock((HGLOBAL)hDIB);
  2063. {
  2064. g_pFullFileSystem->Read(pDIB, dwBitsSize, file );
  2065. lpbmi = (LPBITMAPINFO)pDIB;
  2066. // Remap palette
  2067. PaletteHueReplace( lpbmi->bmiColors, topcolor, SUIT_HUE_START, SUIT_HUE_END );
  2068. PaletteHueReplace( lpbmi->bmiColors, bottomcolor, PLATE_HUE_START, PLATE_HUE_END );
  2069. outbuffer.Put( pDIB, dwBitsSize );
  2070. }
  2071. GlobalUnlock( hDIB);
  2072. GlobalFree((HGLOBAL) hDIB);
  2073. }
  2074. g_pFullFileSystem->Close(file);
  2075. g_pFullFileSystem->RemoveFile( outfile, NULL );
  2076. g_pFullFileSystem->CreateDirHierarchy("models/player", NULL);
  2077. file = g_pFullFileSystem->Open( outfile, "wb" );
  2078. if ( file != FILESYSTEM_INVALID_HANDLE )
  2079. {
  2080. g_pFullFileSystem->Write( outbuffer.Base(), outbuffer.TellPut(), file );
  2081. g_pFullFileSystem->Close( file );
  2082. }
  2083. #endif
  2084. }
  2085. //-----------------------------------------------------------------------------
  2086. // Purpose:
  2087. //-----------------------------------------------------------------------------
  2088. void COptionsSubMultiplayer::ColorForName( char const *pszColorName, int&r, int&g, int&b )
  2089. {
  2090. r = g = b = 0;
  2091. int count = sizeof( itemlist ) / sizeof( itemlist[0] );
  2092. for ( int i = 0; i < count; i++ )
  2093. {
  2094. if (!Q_strnicmp(pszColorName, itemlist[ i ].name, strlen(itemlist[ i ].name)))
  2095. {
  2096. r = itemlist[ i ].r;
  2097. g = itemlist[ i ].g;
  2098. b = itemlist[ i ].b;
  2099. return;
  2100. }
  2101. }
  2102. }
  2103. //-----------------------------------------------------------------------------
  2104. // Purpose:
  2105. //-----------------------------------------------------------------------------
  2106. void COptionsSubMultiplayer::OnResetData()
  2107. {
  2108. // reset the DownloadFilter combo box
  2109. if ( m_pDownloadFilterCombo )
  2110. {
  2111. // cl_downloadfilter
  2112. ConVarRef cl_downloadfilter( "cl_downloadfilter" );
  2113. if ( Q_stricmp( cl_downloadfilter.GetString(), "none" ) == 0 )
  2114. {
  2115. m_pDownloadFilterCombo->ActivateItem( 2 );
  2116. }
  2117. else if ( Q_stricmp( cl_downloadfilter.GetString(), "nosounds" ) == 0 )
  2118. {
  2119. m_pDownloadFilterCombo->ActivateItem( 1 );
  2120. }
  2121. else
  2122. {
  2123. m_pDownloadFilterCombo->ActivateItem( 0 );
  2124. }
  2125. }
  2126. }
  2127. //-----------------------------------------------------------------------------
  2128. // Purpose:
  2129. //-----------------------------------------------------------------------------
  2130. void COptionsSubMultiplayer::OnApplyChanges()
  2131. {
  2132. m_pPrimaryColorSlider->ApplyChanges();
  2133. m_pSecondaryColorSlider->ApplyChanges();
  2134. // m_pModelList->ApplyChanges();
  2135. m_pLogoList->ApplyChanges();
  2136. m_pLogoList->GetText(m_LogoName, sizeof(m_LogoName));
  2137. m_pHighQualityModelCheckBox->ApplyChanges();
  2138. for ( int i=0; i<m_cvarToggleCheckButtons.GetCount(); ++i )
  2139. {
  2140. CCvarToggleCheckButton *toggleButton = m_cvarToggleCheckButtons[i];
  2141. if( toggleButton->IsVisible() && toggleButton->IsEnabled() )
  2142. {
  2143. toggleButton->ApplyChanges();
  2144. }
  2145. }
  2146. if ( !ModInfo().NoCrosshair() )
  2147. {
  2148. if (m_pCrosshairSize != NULL)
  2149. {
  2150. m_pCrosshairSize->ApplyChanges();
  2151. }
  2152. if (m_pCrosshairTranslucencyCheckbox != NULL)
  2153. {
  2154. m_pCrosshairTranslucencyCheckbox->ApplyChanges();
  2155. }
  2156. ApplyCrosshairColorChanges();
  2157. }
  2158. if ( ModInfo().AdvCrosshair() )
  2159. {
  2160. m_pAdvCrosshairRedSlider->ApplyChanges();
  2161. m_pAdvCrosshairGreenSlider->ApplyChanges();
  2162. m_pAdvCrosshairBlueSlider->ApplyChanges();
  2163. m_pAdvCrosshairScaleSlider->ApplyChanges();
  2164. m_pAdvCrosshairStyle->ApplyChanges();
  2165. // save the crosshair
  2166. char cmd[512];
  2167. char crosshair[256];
  2168. m_pAdvCrosshairStyle->GetText(crosshair, sizeof(crosshair));
  2169. Q_snprintf(cmd, sizeof(cmd), "cl_crosshair_file %s\n", crosshair);
  2170. engine->ClientCmd_Unrestricted(cmd);
  2171. }
  2172. // save the logo name
  2173. char cmd[512];
  2174. if ( m_LogoName[ 0 ] )
  2175. {
  2176. Q_snprintf(cmd, sizeof(cmd), "cl_logofile materials/vgui/logos/%s.vtf\n", m_LogoName);
  2177. }
  2178. else
  2179. {
  2180. Q_strncpy( cmd, "cl_logofile \"\"\n", sizeof( cmd ) );
  2181. }
  2182. engine->ClientCmd_Unrestricted(cmd);
  2183. if ( m_pModelList && m_pModelList->IsVisible() && m_pModelList->GetActiveItemCommand() )
  2184. {
  2185. Q_strncpy( m_ModelName, m_pModelList->GetActiveItemCommand(), sizeof( m_ModelName ) );
  2186. Q_StripExtension( m_ModelName, m_ModelName, sizeof ( m_ModelName ) );
  2187. // save the player model name
  2188. Q_snprintf(cmd, sizeof(cmd), "cl_playermodel models/%s.mdl\n", m_ModelName );
  2189. engine->ClientCmd_Unrestricted(cmd);
  2190. }
  2191. else
  2192. {
  2193. m_ModelName[0] = 0;
  2194. }
  2195. // set the DownloadFilter cvar
  2196. if ( m_pDownloadFilterCombo )
  2197. {
  2198. ConVarRef cl_downloadfilter( "cl_downloadfilter" );
  2199. switch ( m_pDownloadFilterCombo->GetActiveItem() )
  2200. {
  2201. default:
  2202. case 0:
  2203. cl_downloadfilter.SetValue( "all" );
  2204. break;
  2205. case 1:
  2206. cl_downloadfilter.SetValue( "nosounds" );
  2207. break;
  2208. case 2:
  2209. cl_downloadfilter.SetValue( "none" );
  2210. break;
  2211. }
  2212. }
  2213. }
  2214. //-----------------------------------------------------------------------------
  2215. // Purpose: apply the crosshair color values to the cvar.
  2216. // also set the slider values to match the new value.
  2217. //-----------------------------------------------------------------------------
  2218. void COptionsSubMultiplayer::ApplyCrosshairColorChanges()
  2219. {
  2220. char cmd[256];
  2221. cmd[0] = 0;
  2222. if (m_pCrosshairColorComboBox != NULL)
  2223. {
  2224. int val = m_pCrosshairColorComboBox->GetActiveItem();
  2225. Q_snprintf( cmd, sizeof(cmd), "cl_crosshaircolor %d\n", val );
  2226. engine->ClientCmd_Unrestricted( cmd );
  2227. }
  2228. }
  2229. //-----------------------------------------------------------------------------
  2230. // Purpose: Allow the res file to create controls on per-mod basis
  2231. //-----------------------------------------------------------------------------
  2232. Panel *COptionsSubMultiplayer::CreateControlByName( const char *controlName )
  2233. {
  2234. if( !Q_stricmp( "CCvarToggleCheckButton", controlName ) )
  2235. {
  2236. CCvarToggleCheckButton *newButton = new CCvarToggleCheckButton( this, controlName, "", "" );
  2237. m_cvarToggleCheckButtons.AddElement( newButton );
  2238. return newButton;
  2239. }
  2240. else
  2241. {
  2242. return BaseClass::CreateControlByName( controlName );
  2243. }
  2244. }