Devilution
Diablo devolved - magic behind the 1996 computer game
palette.cpp
Go to the documentation of this file.
1 #include "all.h"
2 #include "../SourceX/display.h"
3 #include "../3rdParty/Storm/Source/storm.h"
4 
6 
7 SDL_Color logical_palette[256];
8 SDL_Color system_palette[256];
9 SDL_Color orig_palette[256];
10 
11 /* data */
12 
13 int gamma_correction = 100;
15 BOOLEAN sgbFadedIn = TRUE;
16 
18 {
19  assert(palette);
20  if (SDLC_SetSurfaceAndPaletteColors(pal_surface, palette, system_palette, 0, 256) < 0) {
21  ErrSdl();
22  }
23  pal_surface_palette_version++;
24 }
25 
26 void ApplyGamma(SDL_Color *dst, const SDL_Color *src, int n)
27 {
28  int i;
29  double g;
30 
31  g = gamma_correction / 100.0;
32 
33  for (i = 0; i < n; i++) {
34  dst->r = pow(src->r / 256.0, g) * 256.0;
35  dst->g = pow(src->g / 256.0, g) * 256.0;
36  dst->b = pow(src->b / 256.0, g) * 256.0;
37  dst++;
38  src++;
39  }
40  force_redraw = 255;
41 }
42 
43 void SaveGamma()
44 {
45  SRegSaveValue("Diablo", "Gamma Correction", 0, gamma_correction);
46  SRegSaveValue("Diablo", "Color Cycling", FALSE, color_cycling_enabled);
47 }
48 
49 static void LoadGamma()
50 {
51  int gamma_value;
52  int value;
53 
54  value = gamma_correction;
55  if (!SRegLoadValue("Diablo", "Gamma Correction", 0, &value))
56  value = 100;
57  gamma_value = value;
58  if (value < 30) {
59  gamma_value = 30;
60  } else if (value > 100) {
61  gamma_value = 100;
62  }
63  gamma_correction = gamma_value - gamma_value % 5;
64  if (!SRegLoadValue("Diablo", "Color Cycling", 0, &value))
65  value = 1;
66  color_cycling_enabled = value;
67 }
68 
70 {
71  LoadGamma();
72  memcpy(system_palette, orig_palette, sizeof(orig_palette));
73  InitPalette();
74 }
75 
76 void LoadPalette(char *pszFileName)
77 {
78  int i;
79  void *pBuf;
80  BYTE PalData[256][3];
81 
82  assert(pszFileName);
83 
84  WOpenFile(pszFileName, &pBuf, FALSE);
85  WReadFile(pBuf, (char *)PalData, sizeof(PalData), pszFileName);
86  WCloseFile(pBuf);
87 
88  for (i = 0; i < 256; i++) {
89  orig_palette[i].r = PalData[i][0];
90  orig_palette[i].g = PalData[i][1];
91  orig_palette[i].b = PalData[i][2];
92 #ifndef USE_SDL1
93  orig_palette[i].a = SDL_ALPHA_OPAQUE;
94 #endif
95  }
96 }
97 
98 void LoadRndLvlPal(int l)
99 {
100  int rv;
101  char szFileName[MAX_PATH];
102 
103  if (l == DTYPE_TOWN) {
104  LoadPalette("Levels\\TownData\\Town.pal");
105  } else {
106  rv = random_(0, 4) + 1;
107  sprintf(szFileName, "Levels\\L%iData\\L%i_%i.PAL", l, l, rv);
108  LoadPalette(szFileName);
109  }
110 }
111 
112 void ResetPal()
113 {
114 }
115 
117 {
118  if (gamma_correction < 100) {
119  gamma_correction += 5;
120  if (gamma_correction > 100)
121  gamma_correction = 100;
123  palette_update();
124  }
125 }
126 
128 {
129  if (gamma_correction > 30) {
130  gamma_correction -= 5;
131  if (gamma_correction < 30)
132  gamma_correction = 30;
134  palette_update();
135  }
136 }
137 
138 int UpdateGamma(int gamma)
139 {
140  if (gamma) {
141  gamma_correction = 130 - gamma;
143  palette_update();
144  }
145  SaveGamma();
146  return 130 - gamma_correction;
147 }
148 
149 void SetFadeLevel(DWORD fadeval)
150 {
151  int i;
152 
153  for (i = 0; i < 255; i++) {
154  system_palette[i].r = (fadeval * logical_palette[i].r) >> 8;
155  system_palette[i].g = (fadeval * logical_palette[i].g) >> 8;
156  system_palette[i].b = (fadeval * logical_palette[i].b) >> 8;
157  }
158  palette_update();
159 }
160 
162 {
163  SetFadeLevel(0);
164 }
165 
166 void PaletteFadeIn(int fr)
167 {
168  int i;
169 
171  DWORD tc = SDL_GetTicks();
172  for (i = 0; i < 256; i = (SDL_GetTicks() - tc) / 2.083) { // 32 frames @ 60hz
173  SetFadeLevel(i);
174  SDL_Rect SrcRect = { SCREEN_X, SCREEN_Y, SCREEN_WIDTH, SCREEN_HEIGHT };
175  BltFast(&SrcRect, NULL);
176  RenderPresent();
177  }
178  SetFadeLevel(256);
179  memcpy(logical_palette, orig_palette, sizeof(orig_palette));
180  sgbFadedIn = TRUE;
181 }
182 
183 void PaletteFadeOut(int fr)
184 {
185  int i;
186 
187  if (sgbFadedIn) {
188  DWORD tc = SDL_GetTicks();
189  for (i = 256; i > 0; i = 256 - (SDL_GetTicks() - tc) / 2.083) { // 32 frames @ 60hz
190  SetFadeLevel(i);
191  SDL_Rect SrcRect = { SCREEN_X, SCREEN_Y, SCREEN_WIDTH, SCREEN_HEIGHT };
192  BltFast(&SrcRect, NULL);
193  RenderPresent();
194  }
195  SetFadeLevel(0);
196  sgbFadedIn = FALSE;
197  }
198 }
199 
201 {
202  int i;
203  SDL_Color col;
204 
205  col = system_palette[1];
206  for (i = 1; i < 31; i++) {
207  system_palette[i] = system_palette[i + 1];
208  }
209  system_palette[i] = col;
210 
211  palette_update();
212 }
213 
214 #ifndef SPAWN
216 {
217  int i;
218 
219  for (i = 32 - n; i >= 0; i--) {
221  }
223  palette_update();
224 }
225 #endif
226 
228 {
229  return color_cycling_enabled;
230 }
231 
232 BOOL palette_set_color_cycling(BOOL enabled)
233 {
234  color_cycling_enabled = enabled;
235  return enabled;
236 }
237 
IncreaseGamma
void IncreaseGamma()
Definition: palette.cpp:116
system_palette
SDL_Color system_palette[256]
Definition: palette.cpp:8
force_redraw
int force_redraw
Definition: diablo.cpp:30
ApplyGamma
void ApplyGamma(SDL_Color *dst, const SDL_Color *src, int n)
Definition: palette.cpp:26
BlackPalette
void BlackPalette()
Definition: palette.cpp:161
SCREEN_Y
#define SCREEN_Y
Definition: defs.h:126
DecreaseGamma
void DecreaseGamma()
Definition: palette.cpp:127
palette_get_color_cycling
BOOL palette_get_color_cycling()
Definition: palette.cpp:227
RenderPresent
void RenderPresent()
PaletteFadeIn
void PaletteFadeIn(int fr)
Definition: palette.cpp:166
SetFadeLevel
void SetFadeLevel(DWORD fadeval)
Definition: palette.cpp:149
all.h
BltFast
void BltFast(SDL_Rect *src_rect, SDL_Rect *dst_rect)
WOpenFile
BOOL WOpenFile(const char *FileName, HANDLE *phsFile, BOOL mayNotExist)
Definition: wave.cpp:21
SCREEN_WIDTH
#define SCREEN_WIDTH
Definition: defs.h:105
LoadRndLvlPal
void LoadRndLvlPal(int l)
Definition: palette.cpp:98
palette_update
void palette_update()
Definition: palette.cpp:17
sgbFadedIn
BOOLEAN sgbFadedIn
Definition: palette.cpp:15
assert
#define assert(exp)
Definition: defs.h:168
palette_update_quest_palette
void palette_update_quest_palette(int n)
Definition: palette.cpp:215
palette_set_color_cycling
BOOL palette_set_color_cycling(BOOL enabled)
Definition: palette.cpp:232
DEVILUTION_END_NAMESPACE
#define DEVILUTION_END_NAMESPACE
Definition: types.h:10
LoadGamma
static void LoadGamma()
Definition: palette.cpp:49
palette_update_caves
void palette_update_caves()
Definition: palette.cpp:200
SaveGamma
void SaveGamma()
Definition: palette.cpp:43
UpdateGamma
int UpdateGamma(int gamma)
Definition: palette.cpp:138
palette_init
void palette_init()
Definition: palette.cpp:69
ErrSdl
#define ErrSdl()
Definition: defs.h:185
color_cycling_enabled
BOOL color_cycling_enabled
Definition: palette.cpp:14
WCloseFile
DEVILUTION_BEGIN_NAMESPACE BOOL WCloseFile(HANDLE file)
Definition: wave.cpp:6
LoadPalette
void LoadPalette(char *pszFileName)
Definition: palette.cpp:76
DTYPE_TOWN
@ DTYPE_TOWN
Definition: enums.h:1868
PaletteFadeOut
void PaletteFadeOut(int fr)
Definition: palette.cpp:183
ResetPal
void ResetPal()
Definition: palette.cpp:112
random_
int random_(BYTE idx, int v)
Main RNG function.
Definition: engine.cpp:752
orig_palette
SDL_Color orig_palette[256]
Definition: palette.cpp:9
gamma_correction
int gamma_correction
Definition: palette.cpp:13
InitPalette
void InitPalette()
DEVILUTION_BEGIN_NAMESPACE
Definition: sha.cpp:10
logical_palette
DEVILUTION_BEGIN_NAMESPACE SDL_Color logical_palette[256]
Definition: palette.cpp:7
SCREEN_X
#define SCREEN_X
Definition: defs.h:125
WReadFile
void WReadFile(HANDLE hsFile, LPVOID buf, DWORD to_read, const char *FileName)
Definition: wave.cpp:29
SCREEN_HEIGHT
#define SCREEN_HEIGHT
Definition: defs.h:106