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