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.

154 lines
3.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. //
  9. // CGamePalette implementation
  10. //
  11. #include "stdafx.h"
  12. #include "GamePalette.h"
  13. #include "Hammer.h"
  14. #include "tier1/strtools.h"
  15. #pragma warning(push, 1)
  16. #pragma warning(disable:4701 4702 4530)
  17. #include <fstream>
  18. // memdbgon must be the last include file in a .cpp file!!!
  19. #include "tier0/memdbgon.h"
  20. #pragma warning(pop)
  21. #pragma warning(disable:4244)
  22. CGamePalette::CGamePalette()
  23. {
  24. fBrightness = 1.0;
  25. uPaletteBytes = sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * 256;
  26. // allocate memory
  27. pPalette = (LOGPALETTE*) malloc(uPaletteBytes);
  28. pOriginalPalette = (LOGPALETTE*) malloc(uPaletteBytes);
  29. memset(pPalette, 0, uPaletteBytes);
  30. memset(pOriginalPalette, 0, uPaletteBytes);
  31. if(!pPalette || !pOriginalPalette)
  32. {
  33. AfxMessageBox("I couldn't allocate memory for the palette.");
  34. PostQuitMessage(-1);
  35. return;
  36. }
  37. pPalette->palVersion = 0x300;
  38. pPalette->palNumEntries = 256;
  39. pOriginalPalette->palVersion = 0x300;
  40. pOriginalPalette->palNumEntries = 256;
  41. GDIPalette.CreatePalette(pPalette);
  42. }
  43. CGamePalette::~CGamePalette()
  44. {
  45. if(pPalette && pOriginalPalette)
  46. {
  47. // free memory
  48. free(pPalette);
  49. free(pOriginalPalette);
  50. }
  51. }
  52. BOOL CGamePalette::Create(LPCTSTR pszFile)
  53. {
  54. char szRootDir[MAX_PATH];
  55. char szFullPath[MAX_PATH];
  56. APP()->GetDirectory(DIR_PROGRAM, szRootDir);
  57. Q_MakeAbsolutePath( szFullPath, MAX_PATH, pszFile, szRootDir );
  58. strFile = szFullPath;
  59. if( GetFileAttributes(strFile) == 0xffffffff )
  60. return FALSE; // not exist
  61. // open file & read palette
  62. std::ifstream file(strFile, std::ios::binary);
  63. if( !file.is_open() )
  64. return FALSE;
  65. int i;
  66. for(i = 0; i < 256; i++)
  67. {
  68. if(file.eof())
  69. break;
  70. pOriginalPalette->palPalEntry[i].peRed = file.get();
  71. pOriginalPalette->palPalEntry[i].peGreen = file.get();
  72. pOriginalPalette->palPalEntry[i].peBlue = file.get();
  73. pOriginalPalette->palPalEntry[i].peFlags = D3DRMPALETTE_READONLY |
  74. PC_NOCOLLAPSE;
  75. }
  76. file.close();
  77. if(i < 256)
  78. return FALSE;
  79. // copy into working palette
  80. memcpy((void*) pPalette, (void*) pOriginalPalette, uPaletteBytes);
  81. GDIPalette.SetPaletteEntries(0, 256, pPalette->palPalEntry);
  82. return TRUE;
  83. }
  84. static BYTE fixbyte(float fValue)
  85. {
  86. if(fValue > 255.0)
  87. fValue = 255.0;
  88. if(fValue < 0)
  89. fValue = 0;
  90. return BYTE(fValue);
  91. }
  92. void CGamePalette::SetBrightness(float fValue)
  93. {
  94. if(fValue <= 0)
  95. return;
  96. fBrightness = fValue;
  97. // if fValue is 1.0, memcpy
  98. if(fValue == 1.0)
  99. {
  100. memcpy((void*) pPalette, (void*) pOriginalPalette, uPaletteBytes);
  101. GDIPalette.SetPaletteEntries(0, 256, pPalette->palPalEntry);
  102. return;
  103. }
  104. // copy original palette to new palette, scaling by fValue
  105. PALETTEENTRY * pOriginalEntry;
  106. PALETTEENTRY * pNewEntry;
  107. for(int i = 0; i < 256; i++)
  108. {
  109. pOriginalEntry = &pOriginalPalette->palPalEntry[i];
  110. pNewEntry = &pPalette->palPalEntry[i];
  111. pNewEntry->peRed = fixbyte(pOriginalEntry->peRed * fBrightness);
  112. pNewEntry->peGreen = fixbyte(pOriginalEntry->peGreen * fBrightness);
  113. pNewEntry->peBlue = fixbyte(pOriginalEntry->peBlue * fBrightness);
  114. }
  115. GDIPalette.SetPaletteEntries(0, 256, pPalette->palPalEntry);
  116. }
  117. float CGamePalette::GetBrightness()
  118. {
  119. return fBrightness;
  120. }