World of Might and Magic  0.2.0
Open reimplementation of Might and Magic 6 7 8 game engine
PaletteManager.cpp
См. документацию.
2 
3 #include "Engine/Engine.h"
4 #include "Engine/LOD.h"
5 #include "Engine/OurMath.h"
6 
8 
9 //----- (00452AE2) --------------------------------------------------------
11  int64_t v1 = 4294967296;
12  if (a1 > 0) {
13  do {
14  HEXRAYS_LODWORD(v1) = HEXRAYS_HIDWORD(v1) + v1;
15  HEXRAYS_HIDWORD(v1) *= 2;
16  --a1;
17  } while (a1);
18  }
19  return v1;
20 }
21 
22 //----- (0048A643) --------------------------------------------------------
23 bool HSV2RGB(float *redo, float *greeno, float *blueo, float hin, float sin, float vin) {
24  // r,g,b outputs 0-1
25 
26  if (hin > 360 || sin > 1 || vin > 1) __debugbreak();
27 
28  if (sin == 0.0) {
29  if (vin > 1) vin = 1;
30  if (vin < 0) vin = 0;
31  *blueo = vin;
32  *greeno = vin;
33  *redo = vin;
34  return 1;
35  }
36 
37  if (hin == 360.0) hin = 0.0;
38 
39  double hh = hin / 60; // to sixth segments
40  unsigned int segment = (unsigned int)hh;
41  double fractionrem = hh - segment;
42  double p = (1.0 - sin) * vin;
43  double q = (1.0 - fractionrem * sin) * vin;
44  double t = (1.0 - (1.0 - fractionrem) * sin) * vin;
45 
46  switch (segment) {
47  case 0:
48  *redo = vin;
49  *greeno = t;
50  *blueo = p;
51  break;
52  case 1:
53  *redo = q;
54  *greeno = vin;
55  *blueo = p;
56  break;
57  case 2:
58  *redo = p;
59  *greeno = vin;
60  *blueo = t;
61  break;
62 
63  case 3:
64  *redo = p;
65  *greeno = q;
66  *blueo = vin;
67  break;
68  case 4:
69  *redo = t;
70  *greeno = p;
71  *blueo = vin;
72  break;
73  case 5:
74  default:
75  *redo = vin;
76  *greeno = p;
77  *blueo = q;
78  break;
79  }
80 
81  return 1;
82 }
83 
84 //----- (0048A7AA) --------------------------------------------------------
85 void RGB2HSV(float *outh, float *outs, float redin, float greenin, float bluein, float *outv) {
86  // RGB inputs 0-1
87  if (redin > 1 || greenin > 1 || bluein > 1) __debugbreak();
88 
89  double max;
90  double min;
91  double outhcalc;
92  float delta;
93 
94  if (redin <= (double)greenin)
95  max = greenin;
96  else
97  max = redin;
98 
99  if (max < bluein) max = bluein;
100  // max is value of dominant hue
101 
102  if (redin <= (double)greenin)
103  min = redin;
104  else
105  min = greenin;
106 
107  if (min > bluein) min = bluein;
108  // min is value of least hue
109 
110  *outv = max;
111  delta = max - min;
112 
113  if (max == 0.0) { // r=g=b=0
114  *outs = 0.0;
115  *outh = 0.0;
116  return;
117  } else {
118  *outs = delta / max;
119  }
120 
121  if (redin == max) {
122  outhcalc = (greenin - bluein) / delta; // yellow and mag
123  } else {
124  if (greenin == max)
125  outhcalc = (bluein - redin) / delta + 2.0; // cyan and yellow
126  else
127  outhcalc = (redin - greenin) / delta + 4.0; // mag and cyan
128  }
129 
130  *outh = outhcalc * 60.0; // to degree
131  if (*outh < 0) {
132  *outh += 360.0;
133  }
134 }
135 
136 //----- (0048A8A3) --------------------------------------------------------
138  int *v1; // edx@1
139  signed int v2; // eax@1
140 
141  v1 = &this->pPaletteIDs[1];
142  v2 = 1;
143  do {
144  if (*v1) this->_num_locked = v2 + 1;
145  ++v2;
146  ++v1;
147  } while (v2 < 50);
148  return this->_num_locked;
149 }
150 
151 //----- (0048A8CC) --------------------------------------------------------
153  char *v1; // edx@1
154  signed int v2; // eax@1
155 
156  v1 = (char *)&this->pPaletteIDs[1];
157  v2 = 1;
158  do {
159  if (*(int *)v1) this->_pal_lock_test = v2 + 1;
160  ++v2;
161  v1 += 4;
162  } while (v2 < 50);
163  return this->_pal_lock_test;
164 }
165 
166 //----- (0048A8F5) --------------------------------------------------------
167 void PaletteManager::SetColorChannelInfo(int uNumRBits, int uNumGBits,
168  int uNumBBits) {
169  PaletteManager *v4; // esi@1
170  int v5; // edi@1
171  int v6; // eax@1
172  int v7; // ebx@1
173 
174  v4 = this;
175  this->uNumTargetRBits = uNumRBits;
176  this->uNumTargetGBits = uNumGBits;
177  v5 = this->uNumTargetGBits;
178  this->uNumTargetBBits = uNumBBits;
179  v6 = MakeColorMaskFromBitDepth(uNumRBits);
180  v7 = v4->uNumTargetBBits;
181  v4->uTargetRMask = v6 << (v5 + v4->uNumTargetBBits);
182  v4->uTargetGMask = MakeColorMaskFromBitDepth(v5) << v7;
184 }
185 
186 //----- (00489BE0) --------------------------------------------------------
188  PaletteManager *v2; // esi@1
189  // char *v3; // edi@1
190  // signed int v4; // ebx@4
191  // float v5; // ST08_4@5
192  // float v6; // ST04_4@5
193  // float v7; // ST00_4@5
194  // int v8; // eax@7
195  // float *v9; // edx@8
196  // float *v10; // ST0C_4@8
197  // float *v11; // ecx@8
198  // int v12; // ebx@8
199  // int v13; // eax@8
200  // float v14; // ebx@8
201  // float v15; // ST08_4@8
202  // float v16; // ST04_4@8
203  // float v17; // ST00_4@8
204  // unsigned __int8 v18; // sf@8
205  // unsigned __int8 v19; // of@8
206  // int v20; // eax@10
207  double v21; // st5@11
208  // float v22; // ST0C_4@13
209  unsigned int v23; // eax@13
210  // __int16 v24; // bx@16
211  // int v25; // eax@16
212  double v26; // st7@20
213  // float v27; // ST0C_4@22
214  unsigned int v28; // eax@22
215  // __int16 v29; // bx@25
216  // __int16 *v30; // eax@25
217  // int v31; // eax@27
218  double v32; // st5@28
219  // float v33; // ST0C_4@30
220  // float v34; // ST08_4@30
221  unsigned int v35; // ebx@30
222  signed __int64 v36; // qax@33
223  signed int v37; // edx@33
224  char v38; // cl@33
225  unsigned int v39; // ebx@33
226  signed int v40; // edi@33
227  unsigned int v41; // ecx@33
228  unsigned int v42; // ecx@35
229  // int v43; // eax@39
230  signed int v44; // edx@39
231  // unsigned __int8 v45; // al@40
232  // double v46; // st6@43
233  // signed int v47; // eax@43
234  // int v48; // eax@45
235  double v49; // st6@47
236  // float v50; // ST08_4@49
237  // unsigned int v51; // ebx@49
238  int v52; // edi@55
239  int v53; // ebx@55
240  signed __int64 v54; // qax@55
241  double v55; // st7@56
242  unsigned int v56; // ecx@57
243  unsigned int v57; // ecx@59
244  // int v58; // edx@61
245  unsigned int v59; // ecx@61
246  unsigned int v60; // eax@63
247  char v61; // cl@63
248  // int result; // eax@63
249  float v63[256]; // [sp+1Ch] [bp-C38h]@5
250  float v64[256]; // [sp+41Ch] [bp-838h]@5
251  float a6[256]; // [sp+81Ch] [bp-438h]@5
252  // int v66; // [sp+C1Ch] [bp-38h]@43
253  float v67; // [sp+C20h] [bp-34h]@43
254  float v68; // [sp+C24h] [bp-30h]@43
255  // PaletteManager *v69; // [sp+C28h] [bp-2Ch]@9
256  // float v70; // [sp+C2Ch] [bp-28h]@43
257  // double v71; // [sp+C30h] [bp-24h]@10
258  // int v72; // [sp+C38h] [bp-1Ch]@9
259  // int v73; // [sp+C3Ch] [bp-18h]@9
260  // int i; // [sp+C40h] [bp-14h]@7
261  // float v75; // [sp+C44h] [bp-10h]@5
262  float a2a; // [sp+C48h] [bp-Ch]@13
263  float a1; // [sp+C4Ch] [bp-8h]@13
264  float a3; // [sp+C50h] [bp-4h]@13
265  signed int v79; // [sp+C5Ch] [bp+8h]@33
266  // signed int v80; // [sp+C5Ch] [bp+8h]@43
267  int v81; // [sp+C5Ch] [bp+8h]@57
268 
269  v2 = this;
270  // v3 = (char *)pBaseColors[a2];
271  if (pPalette_tintColor[0] || pPalette_tintColor[1] ||
272  pPalette_tintColor[2]) {
273  // v8 = 0;
274  // i = 0;
275 
276  for (uint i = 0; i < 256; ++i)
277  RGB2HSV(&v64[i], &v63[i],
278  (pBaseColors[a2][i][0] + pPalette_tintColor[0]) /
279  (255.0f + 255.0f), // Uninitialized memory access
280  (pBaseColors[a2][i][1] + pPalette_tintColor[1]) /
281  (255.0f + 255.0f),
282  (pBaseColors[a2][i][2] + pPalette_tintColor[2]) /
283  (255.0f + 255.0f),
284  &a6[i]);
285  // do
286  //{
287  // v9 = (float *)((char *)v63 + v8);
288  // v10 = (float *)((char *)a6 + v8);
289  // v11 = (float *)((char *)v64 + v8);
290  // v12 = pPalette_tintColor[1];
291  // LODWORD(v75) = pPalette_tintColor[2] + (unsigned __int8)v3[2];
292  // v13 = pPalette_tintColor[1] + (unsigned __int8)v3[1];
293  // LODWORD(v14) = (unsigned __int8)*v3;
294  // v15 = (double)((unsigned __int8)v3[2] + pPalette_tintColor[2]) /
295  // (2.0f * 255.0f); LODWORD(v75) = v13; LODWORD(v75) = (unsigned
296  // __int8)*v3 + pPalette_tintColor[0]; v16 = (double)((unsigned
297  // __int8)v3[1] + pPalette_tintColor[1]) / 510.0f; v17 =
298  // (double)((unsigned __int8)*v3 + pPalette_tintColor[0]) / 510.0f;
299  // RGB2HSV(&v64[i], &v63[i], v17, v16, v15, &a6[i]);
300  // v3 += 3;
301  // v8 = i + 4;
302  // v19 = __OFSUB__(i + 4, 1024);
303  // v18 = i - 1020 < 0;
304  // i += 4;
305  //}
306  // while ( i < );
307  } else {
308  for (uint i = 0; i < 256; ++i)
309  RGB2HSV(&v64[i], &v63[i], pBaseColors[a2][i][0] / 255.0f,
310  pBaseColors[a2][i][1] / 255.0f,
311  pBaseColors[a2][i][2] / 255.0f, &a6[i]);
312  /*v4 = 0;
313  do
314  {
315  LODWORD(v75) = (unsigned __int8)v3[2];
316  v5 = (double)SLODWORD(v75) * 0.00392156862745098;
317  LODWORD(v75) = (unsigned __int8)v3[1];
318  v6 = (double)SLODWORD(v75) * 0.00392156862745098;
319  LODWORD(v75) = (unsigned __int8)*v3;
320  v7 = (double)SLODWORD(v75) * 0.00392156862745098;
321  RGB2HSV(&v64[v4], &v63[v4], v7, v6, v5, &a6[v4]);
322  ++v4;
323  v3 += 3;
324  }
325  while ( v4 < 256 );*/
326  }
327 
328  // v69 = (PaletteManager *)((char *)v2 + 16384 * a2);
329  // v72 = 0;
330  // v73 = (int)pPalette1[a2];
331  for (uint i = 0; i < 32; ++i) {
332  // v20 = 0;
333  // i = 0;
334  // v71 = 1.0 - (double)v72 / 31.0f;
335  // do
336  for (uint j = 0; j < 256; ++j) {
337  v21 = a6[j] * (1.0f - i / 32.0f);
338  if (v21 < 0.0) v21 = 0.0;
339 
340  // v22 = v21;
341  HSV2RGB(&a1, &a2a, &a3, v64[j], v63[j], v21);
342  v23 = v2->uNumTargetGBits;
343  if (v23 == 6) { // r5g6b5
344  a1 = a1 * 31.0;
345  a2a = a2a * 62.0;
346  a3 = a3 * 31.0;
347  } else if (v23 == 5) { // r5g5b5
348  a1 = a1 * 31.0;
349  a2a = a2a * 31.0;
350  a3 = a3 * 31.0;
351  } else if (v23 == 0) {
352  // logger->Warning(L"Calling palette manager with
353  // num_target_bits == 0");
354  } else {
355  Error("(%u)", v23);
356  }
357  // v24 = (unsigned __int32)a3 | ((unsigned __int32)a2a <<
358  // v2->uNumTargetBBits) | ((unsigned __int32)a1 <<
359  // (v2->uNumTargetBBits + v2->uNumTargetGBits)); v25 = v73; v73 += 2;
360  // *(short *)v25 = v24;
361  pPalette1[a2][i][j] =
362  (unsigned __int32)a3 |
363  ((unsigned __int32)a2a << v2->uNumTargetBBits) |
364  ((unsigned __int32)a1
365  << (v2->uNumTargetBBits + v2->uNumTargetGBits));
366  // v20 = i + 4;
367  // v19 = __OFSUB__(i + 4, 1024);
368  // v18 = i - 1020 < 0;
369  // i += 4;
370  }
371  // while ( v18 ^ v19 );
372  // ++v72;
373  }
374  // while ( v72 < 32 );
375 
376  // v72 = 0;
377  // v73 = (int)v69->field_199600_palettes;
378  // do
379  for (uint i = 0; i < 32; ++i) {
380  // i = 0;
381  // v71 = 1.0 - (double)v72 / 31.0f;
382  // do
383  for (uint j = 0; j < 256; ++j) {
384  v26 = a6[j] * (1.0 - i / 31.0f);
385  if (v26 < 0.0) v26 = 0.0;
386 
387  // v27 = v26;
388  HSV2RGB(&a1, &a2a, &a3, 1.0, 1.0, v26);
389  v28 = v2->uNumTargetGBits;
390  if (v28 == 6) {
391  a1 = a1 * 31.0;
392  a2a = a2a * 62.0;
393  a3 = a3 * 31.0;
394  } else if (v28 == 5) {
395  a1 = a1 * 31.0;
396  a2a = a2a * 31.0;
397  a3 = a3 * 31.0;
398  } else if (v23 == 0) {
399  // logger->Warning(L"Calling palette manager with
400  // num_target_bits == 0");
401  } else {
402  Error("(%u)", v23);
403  }
404 
405  // v29 = (unsigned __int64)(signed __int64)a3 | ((unsigned
406  // __int16)(signed __int64)a2a << v2->uNumTargetBBits) | (unsigned
407  // __int16)((unsigned __int16)(signed __int64)a1 <<
408  // (v2->uNumTargetBBits + v28)); v30 = (__int16 *)v73;
409  // ++i;
410  // v73 += 2;
411  // v19 = __OFSUB__(i, 256);
412  // v18 = i - 256 < 0;
413  // *v30 = v29;
414  field_199600_palettes[a2][i][j] =
415  (unsigned __int64)(signed __int64)a3 |
416  ((unsigned __int16)(signed __int64)a2a << v2->uNumTargetBBits) |
417  (unsigned __int16)((unsigned __int16)(signed __int64)a1
418  << (v2->uNumTargetBBits + v28));
419  }
420  // while ( v18 ^ v19 );
421  // ++v72;
422  }
423  // while ( v72 < 32 );
424 
425  // v73 = (int)((char *)v2 + 512 * (a2 + 4875)); // field_261600[a2]
426  // v31 = 0;
427  // i = 0;
428  for (uint i = 0; i < 256; ++i) {
429  // v32 = (*(float *)((char *)a6 + v31) - 0.8) * 0.8387096774193549 +
430  // 0.8;
431  v32 = (a6[i] - 0.8f) * 0.8387096774193549 + 0.8;
432  if (v32 < 0.0) v32 = 0.0;
433 
434  // v33 = v32;
435  // v34 = v63[i] * 0.7034339229968783;
436  HSV2RGB(&a1, &a2a, &a3, v64[i], v63[i] * 0.7034339229968783, v32);
437  v35 = v2->uNumTargetGBits;
438  if (v35 == 6) {
439  a1 = a1 * 31.0;
440  a2a = a2a * 62.0;
441  a3 = a3 * 31.0;
442  } else if (v35 == 5) {
443  a1 = a1 * 31.0;
444  a2a = a2a * 31.0;
445  a3 = a3 * 31.0;
446  } else if (v23 == 0) {
447  // logger->Warning(L"Calling palette manager with num_target_bits
448  // == 0");
449  } else {
450  Error("(%u)", v23);
451  }
452 
453  v36 = (signed __int64)((a1 + a2a + a3) * 0.3333333333333333 * 8.0);
454  v37 = (signed int)v36 >> (8 - v2->uNumTargetRBits);
455  v38 = 8 - v35;
456  v39 = v2->uNumTargetBBits;
457  v40 = (signed int)v36 >> v38;
458  v41 = v2->uNumTargetRBits;
459  v79 = (signed int)v36 >> (8 - v39);
460  if (v37 > (1 << v41) - 1) v37 = (1 << v41) - 1;
461  v42 = v2->uNumTargetGBits;
462  if (v40 > (1 << v42) - 1) v40 = (1 << v42) - 1;
463  if (v79 > (1 << v39) - 1) v79 = (1 << v39) - 1;
464  // v43 = v73;
465  v44 = v37 << (v39 + v2->uNumTargetGBits);
466  // v73 += 2;
467  // *(short *)v43 = v79 | ((short)v40 << v39) | (unsigned __int16)v44;
468  field_261600[a2][i] =
469  v79 | ((unsigned short)v40 << v39) | (unsigned __int16)v44;
470  // v31 = i + 4;
471  // v19 = __OFSUB__(i + 4, 1024);
472  // v18 = i - 1020 < 0;
473  // i += 4;
474  }
475  // while ( v18 ^ v19 );
476 
477  // v45 = pPalette_mistColor[0];
478  float mist_a, mist_b, mist_c;
479  if (pPalette_mistColor[0] || pPalette_mistColor[1] ||
480  pPalette_mistColor[2]) {
481  // v46 = (double)v45;
482  // v80 = pPalette_mistColor[1];
483  // v47 = pPalette_mistColor[2];
484  // v70 = v46 * 0.00392156862745098;
485  // *((float *)&v71 + 1) = (double)v80 * 0.00392156862745098;
486  // v75 = (double)v47 * 0.00392156862745098;
487  mist_a = pPalette_mistColor[0] / 255.0f;
488  mist_b = pPalette_mistColor[1] / 255.0f;
489  mist_c = pPalette_mistColor[2] / 255.0f;
490 
491  float unused;
492  RGB2HSV(&v68, &v67, mist_a, mist_b, mist_c, &unused);
493  }
494 
495  // v72 = 0;
496  // v73 = (int)v69->field_D1600;
497  // do
498  for (uint i = 0; i < 32; ++i) {
499  // v48 = 0;
500  // for ( i = 0; ; v48 = i )
501  for (uint j = 0; j < 256; ++j) {
502  v49 = v63[j];
503  if (v49 < 0.0) v49 = 0.0;
504 
505  // v50 = v49;
506  HSV2RGB(&a1, &a2a, &a3, v64[j], v49, a6[j]);
507  // v51 = v2->uNumTargetGBits;
508  if (v2->uNumTargetGBits == 6) {
509  a1 = a1 * 31.0;
510  a2a = a2a * 62.0;
511  a3 = a3 * 31.0;
512  } else if (v2->uNumTargetGBits == 5) {
513  a1 = a1 * 31.0;
514  a2a = a2a * 31.0;
515  a3 = a3 * 31.0;
516  } else if (v23 == 0) {
517  // logger->Warning(L"Calling palette manager with
518  // num_target_bits == 0");
519  } else {
520  Error("(%u)", v23);
521  }
522 
523  if (pPalette_mistColor[0] || pPalette_mistColor[1] ||
524  pPalette_mistColor[2]) {
525  v55 = (double)i / 31.0f;
526  v52 = (signed __int64)((double)(1 << v2->uNumTargetRBits) *
527  mist_a * v55 +
528  a1 * (1.0 - v55));
529  v53 = (signed __int64)((double)(1 << v2->uNumTargetGBits) *
530  mist_b * v55 +
531  a2a * (1.0 - v55));
532  v54 = (signed __int64)((double)(1 << v2->uNumTargetBBits) *
533  mist_c * v55 +
534  a3 * (1.0 - v55));
535  } else {
536  v52 = (signed __int64)a1;
537  v53 = (signed __int64)a2a;
538  v54 = (signed __int64)a3;
539  }
540 
541  v56 = v2->uNumTargetRBits;
542  v81 = v54;
543  if (v52 > (1 << v56) - 1) v52 = (1 << v56) - 1;
544  v57 = v2->uNumTargetGBits;
545  if (v53 > (1 << v57) - 1) v53 = (1 << v57) - 1;
546  HEXRAYS_HIDWORD(v54) = v2->uNumTargetBBits;
547  v59 = v2->uNumTargetBBits;
548  if ((signed int)v54 > (1 << v59) - 1) v81 = (1 << v59) - 1;
549  v60 = v2->uNumTargetGBits;
550  // i += 4;
551  v61 = uNumTargetBBits + v60;
552  // result = v73;
553  // v73 += 2;
554  // v19 = __OFSUB__(i, 1024);
555  // v18 = i - 1024 < 0;
556  // *(short *)result = v81 | ((short)v53 << uNumTargetBBits) | (v52 <<
557  // v61);
558  field_D1600[a2][i][j] =
559  v81 | ((short)v53 << uNumTargetBBits) | (v52 << v61);
560  // if ( !(v18 ^ v19) )
561  // break;
562  }
563  // ++v72;
564  }
565  // while ( v72 < 32 );
566 }
567 
568 //----- (0048A300) --------------------------------------------------------
570  : uNumTargetRBits(0),
571  uNumTargetGBits(0),
572  uNumTargetBBits(0),
573  _num_locked(0) {
574  for (uint i = 0; i < 256; ++i) {
575  pBaseColors[0][i][0] = i;
576  pBaseColors[0][i][1] = i;
577  pBaseColors[0][i][2] = i;
578  }
579 
580  memset(pPaletteIDs, 0, sizeof(pPaletteIDs));
581  memset(pPalette_tintColor, 0, sizeof(pPalette_tintColor));
582  memset(pPalette_mistColor, 0, sizeof(pPalette_mistColor));
583  CalcPalettes_LUT(0);
584 }
585 
586 //----- (0048A336) --------------------------------------------------------
587 // make grayscale palette at 0, clear all palettes ids that aren't locked
589  PaletteManager *v1; // esi@1
590  signed int v2; // ecx@1
591  int v3; // eax@1
592  signed int result; // eax@3
593  void *v5; // edi@4
594  int v6; // ecx@4
595 
596  v1 = this;
597  v2 = 0;
598  v3 = (int)&v1->pBaseColors[0][0][1];
599  do {
600  *(char *)(v3 + 1) = v2;
601  *(char *)v3 = v2;
602  *(char *)(v3 - 1) = v2++;
603  v3 += 3;
604  } while (v2 < 256);
605  CalcPalettes_LUT(0);
606  result = v1->_num_locked;
607  if (result < 50) {
608  v5 = &v1->pPaletteIDs[result];
609  v6 = 50 - result;
610  result = 0;
611  memset(v5, 0, sizeof(int) * v6);
612  }
613  return result;
614 }
615 
616 //----- (0048A379) --------------------------------------------------------
617 // make grayscale palette at 0, clear all palettes ids that aren't in
618 // "lock_test"
620  PaletteManager *v1; // esi@1
621  signed int v2; // ecx@1
622  int v3; // eax@1
623  signed int result; // eax@3
624  void *v5; // edi@4
625  int v6; // ecx@4
626 
627  v1 = this;
628  v2 = 0;
629  v3 = (int)&v1->pBaseColors[0][0][1];
630  do {
631  *(char *)(v3 + 1) = v2;
632  *(char *)v3 = v2;
633  *(char *)(v3 - 1) = v2++;
634  v3 += 3;
635  } while (v2 < 256);
636  CalcPalettes_LUT(0);
637  result = v1->_pal_lock_test;
638  if (result < 50) {
639  v5 = &v1->pPaletteIDs[result];
640  v6 = 50 - result;
641  result = 0;
642  memset(v5, 0, sizeof(int) * v6);
643  }
644  return result;
645 }
646 
647 //----- (0048A3BC) --------------------------------------------------------
648 int PaletteManager::LoadPalette(unsigned int uPaletteID) {
649  unsigned int *v2; // ecx@1
650  signed int result; // eax@1
651  signed int index; // esi@6
652  double v5; // st7@7
653  double v6; // st7@12
654  double v7; // st6@17
655  signed __int64 v8; // qax@17
656  double v9; // st6@17
657  char colourstore[768]; // [sp+18h] [bp-388h]@6
658  // char v11; // [sp+19h] [bp-387h]@17
659  // char v12[766]; // [sp+1Ah] [bp-386h]@17
660  char Source[32]; // [sp+360h] [bp-40h]@4
661  // PaletteManager *v15; // [sp+380h] [bp-20h]@1
662  float v16; // [sp+384h] [bp-1Ch]@7
663  int v17; // [sp+388h] [bp-18h]@6
664  float v18; // [sp+38Ch] [bp-14h]@7
665  float green; // [sp+390h] [bp-10h]@7
666  float red; // [sp+394h] [bp-Ch]@7
667  float a6; // [sp+398h] [bp-8h]@7
668  float blue; // [sp+39Ch] [bp-4h]@7
669 
670  // v15 = this;
671  v2 = (unsigned int *)&this->pPaletteIDs[1];
672  result = 1;
673  while (*v2 != uPaletteID) { // search through loaded palettes
674  ++result;
675  ++v2;
676  if (result >= 50) { // not found in list so load
677  sprintf(Source, "pal%03i", uPaletteID);
678 
679  Texture_MM7 tex; // [sp+318h] [bp-88h]@4
680  // Texture_MM7::Texture_MM7(&tex);
681 
682  if (pBitmaps_LOD->LoadTextureFromLOD(&tex, Source,
683  TEXTURE_24BIT_PALETTE) == 1) {
684  index = 0;
685  v17 = 1 - (int)&colourstore;
686  do {
687  // LODWORD(a1) = tex.pPalette24[v4];
688  red = (double)tex.pPalette24[index] / 255.0f;
689  /*HEXRAYS_LODWORD(green) = (unsigned __int8)*(
690  &v10 + v4 + v17 + (unsigned int)tex.pPalette24);*/
691  green = (double)tex.pPalette24[index + 1] / 255.0f;
692  // a3 = tex.pPalette24[v4 + 2];
693  blue = (double)tex.pPalette24[index + 2] / 255.0f;
694  RGB2HSV(&v16, &v18, red, green, blue, &a6);
695 
696  v5 = a6 * 1.1;
697  if (v5 >= 0.0 && v5 >= 1.0) {
698  v5 = 1.0;
699  } else {
700  if (v5 < 0.0) v5 = 0.0;
701  }
702  a6 = v5;
703  v6 = v18 * 0.64999998;
704  if (v6 >= 0.0 && v6 >= 1.0) {
705  v6 = 1.0;
706  } else {
707  if (v6 < 0.0) v6 = 0.0;
708  }
709  v18 = v6;
710 
711  // covert back and store
712  HSV2RGB(&red, &green, &blue, v16, v18, a6);
713  colourstore[index] = (signed __int64)(red * 255.0);
714  colourstore[index + 1] = (signed __int64)(green * 255.0);
715  colourstore[index + 2] = (signed __int64)(blue * 255.0);
716  index += 3;
717  } while (index < 768);
718 
719  tex.Release();
720  result = this->MakeBasePaletteLut(uPaletteID, colourstore);
721  } else {
722  result = 0;
723  }
724  return result;
725  }
726  }
727  return result;
728 }
729 // 48A3BC: using guessed type char var_386[766];
730 
731 //----- (0048A5A4) --------------------------------------------------------
732 int PaletteManager::MakeBasePaletteLut(int idx, char *entries) {
733  // PaletteManager *v3; // edi@1
734  // signed int result; // eax@1
735  // int *v5; // ecx@1
736  int v6; // eax@4
737  int v7; // esi@4
738  // int v8; // eax@9
739  // signed int v9; // ecx@9
740  // int v10; // edx@9
741 
742  // v3 = this;
743  // result = 0;
744  // v5 = this->pPaletteIDs;
745 
746  for (uint i = 0; i < 50; ++i)
747  if (pPaletteIDs[i] == idx) return i;
748 
749  v6 = (int)&pPaletteIDs[1];
750  v7 = 1;
751  while (*(int *)v6) {
752  ++v7;
753  v6 += 4;
754  if (v7 >= 50) return 0;
755  }
756  /*v8 = (int)pBaseColors[v7];//(int)((char *)v3 + 768 * v7);
757  v9 = 768;
758  v10 = (int)(entries - v8);
759  do
760  {
761  *(char *)v8 = *(char *)(v10 + v8);
762  ++v8;
763  --v9;
764  }
765  while ( v9 );*/
766  unsigned __int8 *dst = (unsigned __int8 *)pBaseColors[v7];
767  for (uint i = 0; i < 768; ++i) dst[i] = entries[i];
768 
769  pPaletteIDs[v7] = idx;
770  CalcPalettes_LUT(v7);
771  return v7;
772 }
773 
774 // inlined
775 //----- (mm6c::0045C610) ---------------------------------------------------
776 void PaletteManager::SetMistColor(unsigned char r, unsigned char g,
777  unsigned char b) {
778  pPalette_mistColor[0] = r;
779  pPalette_mistColor[1] = g;
780  pPalette_mistColor[2] = b;
781 }
782 
783 //----- (0048A614) --------------------------------------------------------
785  CalcPalettes_LUT(0);
786 
787  for (uint i = 1; i < 50; ++i)
788  if (pPaletteIDs[i]) CalcPalettes_LUT(i);
789 }
790 
791 //----- (0047BE67) --------------------------------------------------------
792 unsigned __int16 *PaletteManager::Get(int a1) {
793  return (unsigned __int16 *)pPaletteManager->field_199600_palettes[a1];
794 }
795 
796 //----- (0047BE72) --------------------------------------------------------
797 unsigned __int16 *PaletteManager::Get_Mist_or_Red_LUT(int a1, int a2, char a3) {
798  int v3; // eax@4
799 
800  if (a3 & 2 || _4D864C_force_sw_render_rules && engine->config->AlterPalettes())
801  v3 = 32 * a1 + a2 + 3275;
802  else
803  v3 = 32 * a1 + a2 + 1675;
804  return (unsigned __int16 *)((char *)&pPaletteManager + 512 * v3);
805 }
806 // 4D864C: using guessed type char _4D864C_force_sw_render_rules;
807 
808 //----- (0041F50D) --------------------------------------------------------
809 unsigned __int16 *PaletteManager::Get_Dark_or_Red_LUT(int a1, int a2, char a3) {
810  int v3; // eax@4
811 
812  if (a3 & 2 || _4D864C_force_sw_render_rules && engine->config->AlterPalettes())
813  v3 = 32 * a1 + a2 + 3275;
814  else
815  v3 = 32 * a1 + a2 + 75;
816  return (unsigned __int16 *)((char *)&pPaletteManager + 512 * v3);
817 }
818 // 4D864C: using guessed type char _4D864C_force_sw_render_rules;
819 
820 //----- (0047C30E) --------------------------------------------------------
821 unsigned __int16 *PaletteManager::_47C30E_get_palette(int a1, char a2) {
822  char *result; // eax@4
823 
824  if (a2 & 2 || _4D864C_force_sw_render_rules && engine->config->AlterPalettes())
826  else
827  result = (char *)pPaletteManager->field_D1600[a1];
828  return (unsigned __int16 *)result;
829 }
830 
831 //----- (0047C33F) --------------------------------------------------------
832 unsigned __int16 *PaletteManager::_47C33F_get_palette(int a1, char a2) {
833  unsigned __int16 *result; // eax@4
834 
835  if (a2 & 2 || _4D864C_force_sw_render_rules && engine->config->AlterPalettes())
836  result = (unsigned __int16 *)pPaletteManager->field_199600_palettes[a1];
837  else
838  result = (unsigned __int16 *)pPaletteManager->pPalette1[a1];
839  return result;
840 }
841 
842 //----- (0048A959) --------------------------------------------------------
843 int ReplaceHSV(unsigned int uColor, float h_replace, float s_replace, float v_replace) {
844  float r = ((uColor & 0x00FF0000) >> 16) / 255.0f,
845  g = ((uColor & 0x0000FF00) >> 8) / 255.0f,
846  b = (uColor & 0x000000FF) / 255.0f;
847 
848  float h, s, v;
849  RGB2HSV(&h, &s, r, g, b, &v);
850 
851  if (h_replace != -1.0) h = h_replace;
852  if (s_replace != -1.0) s = s_replace;
853  if (v_replace != -1.0) v = v_replace;
854  HSV2RGB(&r, &g, &b, h, s, v);
855 
856  return (((uint)round_to_int(r * 255.0f) & 0xFF) << 16) |
857  (((uint)round_to_int(g * 255.0f) & 0xFF) << 8) |
858  (((uint)round_to_int(b * 255.0f) & 0xFF));
859 }
s
GLdouble s
Definition: SDL_opengl.h:2063
PaletteManager::_47C33F_get_palette
static uint16_t * _47C33F_get_palette(int a1, char a2)
Definition: PaletteManager.cpp:832
v
const GLdouble * v
Definition: SDL_opengl.h:2064
LOD.h
PaletteManager
Definition: PaletteManager.h:7
PaletteManager::SetColorChannelInfo
void SetColorChannelInfo(int uNumRBits, int uNumGBits, int uNumBBits)
Definition: PaletteManager.cpp:167
PaletteManager::CalcPalettes_LUT
void CalcPalettes_LUT(int a2)
Definition: PaletteManager.cpp:187
PaletteManager::pBaseColors
uint8_t pBaseColors[50][256][3]
Definition: PaletteManager.h:27
PaletteManager::uNumTargetGBits
unsigned int uNumTargetGBits
Definition: PaletteManager.h:40
PaletteManager::field_199600_palettes
uint16_t field_199600_palettes[50][32][256]
Definition: PaletteManager.h:30
PaletteManager::Get_Dark_or_Red_LUT
static uint16_t * Get_Dark_or_Red_LUT(int paletteIdx, int a2, char a3)
Definition: PaletteManager.cpp:809
engine
std::shared_ptr< Engine > engine
Definition: Engine.cpp:130
round_to_int
int round_to_int(float x)
Definition: OurMath.h:18
q
GLdouble GLdouble GLdouble GLdouble q
Definition: SDL_opengl.h:2087
PaletteManager.h
PaletteManager::LoadPalette
int LoadPalette(unsigned int uPaletteID)
Definition: PaletteManager.cpp:648
int64_t
__int64 int64_t
Definition: alext.h:31
PaletteManager::LockAll
int LockAll()
Definition: PaletteManager.cpp:137
index
GLuint index
Definition: SDL_opengl_glext.h:663
PaletteManager::MakeBasePaletteLut
int MakeBasePaletteLut(int a2, char *entries)
Definition: PaletteManager.cpp:732
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1949
Engine.h
result
GLuint64EXT * result
Definition: SDL_opengl_glext.h:9435
TEXTURE_24BIT_PALETTE
@ TEXTURE_24BIT_PALETTE
Definition: LOD.h:17
PaletteManager::_num_locked
int _num_locked
Definition: PaletteManager.h:33
LODFile_IconsBitmaps::LoadTextureFromLOD
int LoadTextureFromLOD(struct Texture_MM7 *pOutTex, const char *pContainer, enum TEXTURE_TYPE eTextureType)
Definition: LOD.cpp:1063
RGB2HSV
void RGB2HSV(float *outh, float *outs, float redin, float greenin, float bluein, float *outv)
Definition: PaletteManager.cpp:85
HSV2RGB
bool HSV2RGB(float *redo, float *greeno, float *blueo, float hin, float sin, float vin)
Definition: PaletteManager.cpp:23
PaletteManager::field_D1600
uint16_t field_D1600[50][32][256]
Definition: PaletteManager.h:29
PaletteManager::pPalette_mistColor
uint8_t pPalette_mistColor[3]
Definition: PaletteManager.h:35
PaletteManager::Get
static uint16_t * Get(int a1)
Definition: PaletteManager.cpp:792
p
GLfloat GLfloat p
Definition: SDL_opengl_glext.h:11093
PaletteManager::LockTestAll
int LockTestAll()
Definition: PaletteManager.cpp:152
PaletteManager::uTargetRMask
unsigned int uTargetRMask
Definition: PaletteManager.h:42
v1
GLfloat GLfloat v1
Definition: SDL_opengl_glext.h:694
PaletteManager::Get_Mist_or_Red_LUT
static uint16_t * Get_Mist_or_Red_LUT(int paletteIdx, int a2, char a3)
Definition: PaletteManager.cpp:797
f
GLfloat f
Definition: SDL_opengl_glext.h:1873
PaletteManager::_pal_lock_test
int _pal_lock_test
Definition: PaletteManager.h:34
blue
GLbyte GLbyte blue
Definition: SDL_opengl_glext.h:382
green
GLbyte green
Definition: SDL_opengl_glext.h:382
dst
GLenum GLenum dst
Definition: SDL_opengl_glext.h:1740
pPaletteManager
PaletteManager * pPaletteManager
Definition: PaletteManager.cpp:7
Texture_MM7
Definition: Image.h:97
t
GLdouble GLdouble t
Definition: SDL_opengl.h:2071
PaletteManager::_47C30E_get_palette
static uint16_t * _47C30E_get_palette(int a1, char a2)
Definition: PaletteManager.cpp:821
PaletteManager::ResetNonLocked
int ResetNonLocked()
Definition: PaletteManager.cpp:588
PaletteManager::pPaletteIDs
int pPaletteIDs[50]
Definition: PaletteManager.h:32
PaletteManager::RecalculateAll
void RecalculateAll()
Definition: PaletteManager.cpp:784
_4D864C_force_sw_render_rules
char _4D864C_force_sw_render_rules
Definition: mm7_data.cpp:208
PaletteManager::ResetNonTestLocked
int ResetNonTestLocked()
Definition: PaletteManager.cpp:619
PaletteManager::uTargetGMask
unsigned int uTargetGMask
Definition: PaletteManager.h:43
b
GLboolean GLboolean GLboolean b
Definition: SDL_opengl_glext.h:1112
r
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
ReplaceHSV
int ReplaceHSV(unsigned int uColor, float h_replace, float s_replace, float v_replace)
Definition: PaletteManager.cpp:843
PaletteManager::SetMistColor
void SetMistColor(unsigned char r, unsigned char g, unsigned char b)
Definition: PaletteManager.cpp:776
v2
GLfloat GLfloat GLfloat v2
Definition: SDL_opengl_glext.h:695
uint
unsigned int uint
Definition: MM7.h:4
v3
GLfloat GLfloat GLfloat GLfloat v3
Definition: SDL_opengl_glext.h:696
__debugbreak
void __cdecl __debugbreak(void)
PaletteManager::uNumTargetRBits
unsigned int uNumTargetRBits
Definition: PaletteManager.h:39
pBitmaps_LOD
LODFile_IconsBitmaps * pBitmaps_LOD
Definition: LOD.cpp:16
PaletteManager::pPalette1
uint16_t pPalette1[50][32][256]
Definition: PaletteManager.h:28
OurMath.h
PaletteManager::uNumTargetBBits
unsigned int uNumTargetBBits
Definition: PaletteManager.h:41
PaletteManager::PaletteManager
PaletteManager()
Definition: PaletteManager.cpp:569
PaletteManager::field_261600
uint16_t field_261600[50][256]
Definition: PaletteManager.h:31
Texture_MM7::pPalette24
uint8_t * pPalette24
Definition: Image.h:104
PaletteManager::pPalette_tintColor
unsigned char pPalette_tintColor[3]
Definition: PaletteManager.h:36
g
GLboolean GLboolean g
Definition: SDL_opengl_glext.h:1112
MakeColorMaskFromBitDepth
int MakeColorMaskFromBitDepth(int a1)
Definition: PaletteManager.cpp:10
PaletteManager::uTargetBMask
unsigned int uTargetBMask
Definition: PaletteManager.h:44
Texture_MM7::Release
void Release()
Definition: Image.cpp:165