1 /*
2    File:        SetupManager.cpp
3   Description: Setup management
4   Program:     BlockOut
5   Author:      Jean-Luc PONS
6 
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11 
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16 */
17 
18 #include "SetupManager.h"
19 #include <stdio.h>
20 
21 // Converts the 975 possible game configurations to 585 configurations.
22 // (By considering identical 2 configurations c1 and c2, where
23 // c1.width = c2.height and c1.height = c2.width)
24 // Index of the array corresponds to b*325 + (d-6)*25 + (w-3)*5 + (h-3)
25 // where b = blockSet (0 to 2) , d = Depth (6 to 18) , w = Width (3 to 7)
26 // and h = Height (3 to 7)
27 const int setupId[] = {
28       0,  1,  2,  4,  6,  1,  3,  5,  7,  9,  2,  5,  8, 10, 11,  4,  7, 10,
29      12, 13,  6,  9, 11, 13, 14, 15, 16, 17, 19, 21, 16, 18, 20, 22, 24, 17,
30      20, 23, 25, 26, 19, 22, 25, 27, 28, 21, 24, 26, 28, 29, 30, 31, 32, 34,
31      36, 31, 33, 35, 37, 39, 32, 35, 38, 40, 41, 34, 37, 40, 42, 43, 36, 39,
32      41, 43, 44, 45, 46, 47, 49, 51, 46, 48, 50, 52, 54, 47, 50, 53, 55, 56,
33      49, 52, 55, 57, 58, 51, 54, 56, 58, 59, 60, 61, 62, 64, 66, 61, 63, 65,
34      67, 69, 62, 65, 68, 70, 71, 64, 67, 70, 72, 73, 66, 69, 71, 73, 74, 75,
35      76, 77, 79, 81, 76, 78, 80, 82, 84, 77, 80, 83, 85, 86, 79, 82, 85, 87,
36      88, 81, 84, 86, 88, 89, 90, 91, 92, 94, 96, 91, 93, 95, 97, 99, 92, 95,
37      98,100,101, 94, 97,100,102,103, 96, 99,101,103,104,105,106,107,109,111,
38     106,108,110,112,114,107,110,113,115,116,109,112,115,117,118,111,114,116,
39     118,119,120,121,122,124,126,121,123,125,127,129,122,125,128,130,131,124,
40     127,130,132,133,126,129,131,133,134,135,136,137,139,141,136,138,140,142,
41     144,137,140,143,145,146,139,142,145,147,148,141,144,146,148,149,150,151,
42     152,154,156,151,153,155,157,159,152,155,158,160,161,154,157,160,162,163,
43     156,159,161,163,164,165,166,167,169,171,166,168,170,172,174,167,170,173,
44     175,176,169,172,175,177,178,171,174,176,178,179,180,181,182,184,186,181,
45     183,185,187,189,182,185,188,190,191,184,187,190,192,193,186,189,191,193,
46     194,195,196,197,199,201,196,198,200,202,204,197,200,203,205,206,199,202,
47     205,207,208,201,204,206,208,209,210,211,212,214,216,211,213,215,217,219,
48     212,215,218,220,221,214,217,220,222,223,216,219,221,223,224,225,226,227,
49     229,231,226,228,230,232,234,227,230,233,235,236,229,232,235,237,238,231,
50     234,236,238,239,240,241,242,244,246,241,243,245,247,249,242,245,248,250,
51     251,244,247,250,252,253,246,249,251,253,254,255,256,257,259,261,256,258,
52     260,262,264,257,260,263,265,266,259,262,265,267,268,261,264,266,268,269,
53     270,271,272,274,276,271,273,275,277,279,272,275,278,280,281,274,277,280,
54     282,283,276,279,281,283,284,285,286,287,289,291,286,288,290,292,294,287,
55     290,293,295,296,289,292,295,297,298,291,294,296,298,299,300,301,302,304,
56     306,301,303,305,307,309,302,305,308,310,311,304,307,310,312,313,306,309,
57     311,313,314,315,316,317,319,321,316,318,320,322,324,317,320,323,325,326,
58     319,322,325,327,328,321,324,326,328,329,330,331,332,334,336,331,333,335,
59     337,339,332,335,338,340,341,334,337,340,342,343,336,339,341,343,344,345,
60     346,347,349,351,346,348,350,352,354,347,350,353,355,356,349,352,355,357,
61     358,351,354,356,358,359,360,361,362,364,366,361,363,365,367,369,362,365,
62     368,370,371,364,367,370,372,373,366,369,371,373,374,375,376,377,379,381,
63     376,378,380,382,384,377,380,383,385,386,379,382,385,387,388,381,384,386,
64     388,389,390,391,392,394,396,391,393,395,397,399,392,395,398,400,401,394,
65     397,400,402,403,396,399,401,403,404,405,406,407,409,411,406,408,410,412,
66     414,407,410,413,415,416,409,412,415,417,418,411,414,416,418,419,420,421,
67     422,424,426,421,423,425,427,429,422,425,428,430,431,424,427,430,432,433,
68     426,429,431,433,434,435,436,437,439,441,436,438,440,442,444,437,440,443,
69     445,446,439,442,445,447,448,441,444,446,448,449,450,451,452,454,456,451,
70     453,455,457,459,452,455,458,460,461,454,457,460,462,463,456,459,461,463,
71     464,465,466,467,469,471,466,468,470,472,474,467,470,473,475,476,469,472,
72     475,477,478,471,474,476,478,479,480,481,482,484,486,481,483,485,487,489,
73     482,485,488,490,491,484,487,490,492,493,486,489,491,493,494,495,496,497,
74     499,501,496,498,500,502,504,497,500,503,505,506,499,502,505,507,508,501,
75     504,506,508,509,510,511,512,514,516,511,513,515,517,519,512,515,518,520,
76     521,514,517,520,522,523,516,519,521,523,524,525,526,527,529,531,526,528,
77     530,532,534,527,530,533,535,536,529,532,535,537,538,531,534,536,538,539,
78     540,541,542,544,546,541,543,545,547,549,542,545,548,550,551,544,547,550,
79     552,553,546,549,551,553,554,555,556,557,559,561,556,558,560,562,564,557,
80     560,563,565,566,559,562,565,567,568,561,564,566,568,569,570,571,572,574,
81     576,571,573,575,577,579,572,575,578,580,581,574,577,580,582,583,576,579,
82     581,583,584 };
83 
84 const char *BLOCKSET_NAME[]    = { "FLAT" , "BASIC" , "EXTENDED" };
85 const char *RESOLUTION_NAME[]  = { "640x480" , "800x600" , "1024x768" , "1280x1024" , "1600x1200" };
86 const POINT2D RESOLUTION_VALUE[] = { {640,480} , {800,600} , {1024,768} , {1280,1024} , {1600,1200} };
87 const char *STYLE_NAME[] = { "Nostalgia" , "Marble", "Arcade" };
88 const char *SOUND_NAME[] = { "Blockout II" , "Blockout" };
89 const char *FRLIMITER_NAME[] = { "Off" , "50 fps", "60 fps", "75 fps", "100 fps" , "VSync" };
90 const float FRLIMITER_VALUE[] = { 0.0f, 50.0f, 60.0f, 75.0f, 100.0f , 0.0f };
91 
92 // ------------------------------------------------
93 
SetupManager()94 SetupManager::SetupManager() {
95 
96   // Default
97   pitWidth  = 5;
98   pitHeight = 5;
99   pitDepth  = 12;
100   blockSet  = BLOCKSET_FLAT;
101   animationSpeed = 5;
102   startLevel = 0;
103   playSound = TRUE;
104   scoreList = NULL;
105   fullScreen = FALSE;
106   windowSize = RES_800x600;
107   transparentFace = 0;
108   style = STYLE_CLASSIC;
109   frLimit = 0;
110   lineWidth = LINEW_MIN;
111   soundType = SOUND_BLOCKOUT2;
112   ZeroMemory(httpHome,sizeof(httpHome));
113   ZeroMemory(httpProxy,sizeof(httpProxy));
114   strcpy(httpHome,"www.blockout.net/blockout2");
115   strcpy(httpProxy,"");
116   httpProxyPort=0;
117   useHttpProxy=FALSE;
118   httpTimeout=30;
119   ResetToQwerty();
120   LoadHighScore();
121   LoadSetup();
122 
123 }
124 
125 // ------------------------------------------------
126 
SetPitWidth(int width)127 void SetupManager::SetPitWidth(int width) {
128   pitWidth = Saturate(width,MIN_PITWIDTH,MAX_PITWIDTH);
129 }
130 
131 // ------------------------------------------------
132 
GetPitWidth()133 int SetupManager::GetPitWidth() {
134   return pitWidth;
135 }
136 
137 // ------------------------------------------------
138 
SetPitHeight(int height)139 void SetupManager::SetPitHeight(int height) {
140   pitHeight = Saturate(height,MIN_PITHEIGHT,MAX_PITHEIGHT);
141 }
142 
143 // ------------------------------------------------
144 
GetPitHeight()145 int SetupManager::GetPitHeight() {
146   return pitHeight;
147 }
148 
149 // ------------------------------------------------
150 
SetPitDepth(int depth)151 void SetupManager::SetPitDepth(int depth) {
152   pitDepth = Saturate(depth,MIN_PITDEPTH,MAX_PITDEPTH);
153 }
154 
155 // ------------------------------------------------
156 
GetPitDepth()157 int SetupManager::GetPitDepth() {
158   return pitDepth;
159 }
160 
161 // ------------------------------------------------
162 
SetBlockSet(int set)163 void SetupManager::SetBlockSet(int set) {
164   blockSet = Saturate(set,BLOCKSET_FLAT,BLOCKSET_EXTENDED);
165 }
166 
167 // ------------------------------------------------
168 
GetBlockSet()169 int SetupManager::GetBlockSet() {
170   return blockSet;
171 }
172 
173 // ------------------------------------------------
174 
SetStartingLevel(int level)175 void SetupManager::SetStartingLevel(int level) {
176   startLevel = Saturate(level,0,9);
177 }
178 
179 // ------------------------------------------------
180 
GetStartingLevel()181 int  SetupManager::GetStartingLevel() {
182   return startLevel;
183 }
184 
185 // ------------------------------------------------
186 
SetSound(BOOL play)187 void SetupManager::SetSound(BOOL play) {
188   playSound = play;
189 }
190 
191 // ------------------------------------------------
192 
GetSound()193 BOOL SetupManager::GetSound() {
194   return playSound;
195 }
196 
197 // ------------------------------------------------
198 
SetAnimationSpeed(int speed)199 void SetupManager::SetAnimationSpeed(int speed) {
200   animationSpeed = Saturate(speed,ASPEED_SLOW,ASPEED_FAST);
201 }
202 
203 // ------------------------------------------------
204 
GetAnimationSpeed()205 int SetupManager::GetAnimationSpeed() {
206   return animationSpeed;
207 }
208 
209 // ------------------------------------------------
GetKRx1()210 BYTE SetupManager::GetKRx1() { return keyRx1; };
GetKRy1()211 BYTE SetupManager::GetKRy1() { return keyRy1; };
GetKRz1()212 BYTE SetupManager::GetKRz1() { return keyRz1; };
GetKRx2()213 BYTE SetupManager::GetKRx2() { return keyRx2; };
GetKRy2()214 BYTE SetupManager::GetKRy2() { return keyRy2; };
GetKRz2()215 BYTE SetupManager::GetKRz2() { return keyRz2; };
216 
SetKRx1(BYTE key)217 void SetupManager::SetKRx1(BYTE key) { keyRx1=key; };
SetKRy1(BYTE key)218 void SetupManager::SetKRy1(BYTE key) { keyRy1=key; };
SetKRz1(BYTE key)219 void SetupManager::SetKRz1(BYTE key) { keyRz1=key; };
SetKRx2(BYTE key)220 void SetupManager::SetKRx2(BYTE key) { keyRx2=key; };
SetKRy2(BYTE key)221 void SetupManager::SetKRy2(BYTE key) { keyRy2=key; };
SetKRz2(BYTE key)222 void SetupManager::SetKRz2(BYTE key) { keyRz2=key; };
223 
ResetToQwerty()224 void SetupManager::ResetToQwerty() {
225   keyRx1 = 'Q';
226   keyRy1 = 'W';
227   keyRz1 = 'E';
228   keyRx2 = 'A';
229   keyRy2 = 'S';
230   keyRz2 = 'D';
231 }
232 
ResetToAzerty()233 void SetupManager::ResetToAzerty() {
234   keyRx1 = 'A';
235   keyRy1 = 'Z';
236   keyRz1 = 'E';
237   keyRx2 = 'Q';
238   keyRy2 = 'S';
239   keyRz2 = 'D';
240 }
241 
242 // ------------------------------------------------
243 
SetStyle(int style)244 void SetupManager::SetStyle(int style) {
245   this->style = Saturate(style,STYLE_CLASSIC,STYLE_ARCADE);
246 }
247 
GetStyle()248 int  SetupManager::GetStyle() {
249   return style;
250 }
251 
GetStyleName()252 const char *SetupManager::GetStyleName() {
253   return STYLE_NAME[style];
254 }
255 
256 // ------------------------------------------------
257 
SetSoundType(int stype)258 void SetupManager::SetSoundType(int stype) {
259   soundType = Saturate(stype,SOUND_BLOCKOUT2,SOUND_BLOCKOUT);
260 }
261 
GetSoundType()262 int  SetupManager::GetSoundType() {
263   return soundType;
264 }
265 
GetSoundTypeName()266 const char *SetupManager::GetSoundTypeName() {
267   return SOUND_NAME[soundType];
268 }
269 
270 // ------------------------------------------------
271 
272 
SetFrLimiter(int fLimit)273 void SetupManager::SetFrLimiter(int fLimit) {
274   frLimit = Saturate(fLimit,FR_NOLIMIT,FR_LIMITVSYNC);
275 }
276 
GetFrLimiter()277 int SetupManager::GetFrLimiter() {
278   return frLimit;
279 }
280 
GetFrLimitTime()281 float SetupManager::GetFrLimitTime() {
282   if(frLimit==0)
283     return 0.0f;
284   else
285     return 1.0f / FRLIMITER_VALUE[frLimit];
286 }
287 
GetFrLimitName()288 const char *SetupManager::GetFrLimitName() {
289   return FRLIMITER_NAME[frLimit];
290 }
291 
292 // ------------------------------------------------
293 
GetAnimationTime()294 float SetupManager::GetAnimationTime() {
295 
296   float min = 0.05f;
297   float max = 0.15f;
298   float speed = min + (max-min) * (float)(ASPEED_FAST - animationSpeed) / ((float)ASPEED_FAST);
299   return speed;
300 
301 }
302 
303 // ------------------------------------------------
304 
SetHttpHome(char * home)305 void SetupManager::SetHttpHome(char *home) {
306   strncpy(httpHome,home,255);
307   httpHome[255]=0;
308 }
309 
310 // ------------------------------------------------
311 
GetHttpHome()312 char *SetupManager::GetHttpHome() {
313   return httpHome;
314 }
315 
316 // ------------------------------------------------
317 
SetProxyName(char * name)318 void SetupManager::SetProxyName(char *name) {
319   strncpy(httpProxy,name,255);
320   httpProxy[255]=0;
321 }
322 
323 // ------------------------------------------------
324 
GetProxyName()325 char *SetupManager::GetProxyName() {
326   return httpProxy;
327 }
328 
329 // ------------------------------------------------
330 
SetProxyPort(int port)331 void SetupManager::SetProxyPort(int port) {
332   httpProxyPort = Saturate(port,0,65535);
333 }
334 
335 // ------------------------------------------------
336 
GetProxyPort()337 int SetupManager::GetProxyPort() {
338   return httpProxyPort;
339 }
340 
341 // ------------------------------------------------
342 
SetUseProxy(BOOL enable)343 void SetupManager::SetUseProxy(BOOL enable) {
344   useHttpProxy = enable;
345 }
346 
347 // ------------------------------------------------
348 
GetUseProxy()349 BOOL SetupManager::GetUseProxy() {
350   return useHttpProxy;
351 }
352 
353 // ------------------------------------------------
354 
SetTimeout(int timeout)355 void SetupManager::SetTimeout(int timeout) {
356   httpTimeout = timeout;
357 }
358 
359 // ------------------------------------------------
360 
GetTimeout()361 int SetupManager::GetTimeout() {
362   return httpTimeout;
363 }
364 
365 // ------------------------------------------------
366 
GetName()367 char *SetupManager::GetName() {
368 
369   static char ret[32];
370   strcpy(ret,"");
371 
372   // Default setup
373   if( Check(5,5,12,BLOCKSET_FLAT) ) {
374     return STR("[Flat Fun]");
375   } else if ( Check(3,3,10,BLOCKSET_BASIC) )  {
376     return STR("[3D Mania]");
377   } else if ( Check(5,5,10,BLOCKSET_EXTENDED) ) {
378     return STR("[Out of Control]");
379   }
380 
381   // Generic name
382   sprintf(ret,"[%dx%dx%d,%s]",pitWidth,pitHeight,pitDepth,GetBlockSetName());
383   return ret;
384 
385 }
386 
387 // ------------------------------------------------
388 
SetFullScreen(BOOL isFull)389 void SetupManager::SetFullScreen(BOOL isFull) {
390   fullScreen = isFull;
391 }
392 
393 // ------------------------------------------------
394 
GetFullScreen()395 BOOL SetupManager::GetFullScreen() {
396   return fullScreen;
397 }
398 
399 // ------------------------------------------------
400 
SetTransparentFace(int transparent)401 void SetupManager::SetTransparentFace(int transparent) {
402   transparentFace = Saturate(transparent,FTRANS_MIN,FTRANS_MAX);
403 }
404 
405 // ------------------------------------------------
406 
GetTransparentFace()407 int SetupManager::GetTransparentFace() {
408   return transparentFace;
409 }
410 
411 // ------------------------------------------------
412 
SetWindowSize(int size)413 void SetupManager::SetWindowSize(int size) {
414   windowSize = Saturate(size,RES_640x480,RES_1600x1200);
415 }
416 
417 // ------------------------------------------------
418 
GetWindowSize()419 int SetupManager::GetWindowSize() {
420   return windowSize;
421 }
422 
423 // ------------------------------------------------
424 
GetWindowWidth()425 int SetupManager::GetWindowWidth() {
426   return RESOLUTION_VALUE[windowSize].x;
427 }
428 
429 // ------------------------------------------------
430 
GetWindowHeight()431 int SetupManager::GetWindowHeight() {
432   return RESOLUTION_VALUE[windowSize].y;
433 }
434 
435 // ------------------------------------------------
436 
GetResName(int res)437 const char *SetupManager::GetResName(int res) {
438   return RESOLUTION_NAME[res];
439 }
440 
441 // ------------------------------------------------
442 
GetBlockSetName()443 const char *SetupManager::GetBlockSetName() {
444   return BLOCKSET_NAME[blockSet];
445 }
446 
447 // ------------------------------------------------
448 
SetLineWidth(int width)449 void SetupManager::SetLineWidth(int width) {
450   lineWidth = Saturate(width,LINEW_MIN,LINEW_MAX);
451 }
452 
GetLineWidth()453 int SetupManager::GetLineWidth() {
454   return lineWidth;
455 }
456 
GetLineRadius()457 float SetupManager::GetLineRadius() {
458 
459   if( lineWidth==0 ) {
460     return 0.0f;
461   } else {
462     return (float)lineWidth * 0.002f + 0.005f;
463   }
464 
465 }
466 
467 // ------------------------------------------------
468 
GetId()469 int SetupManager::GetId() {
470 
471   int idx = (blockSet-BLOCKSET_FLAT)*325 + (pitDepth-MIN_PITDEPTH)*25
472           + (pitWidth-MIN_PITWIDTH)*5 + (pitHeight-MIN_PITHEIGHT);
473 
474   return setupId[idx];
475 
476 }
477 
478 // ------------------------------------------------
479 
InsertHighScore(SCOREREC * score,SCOREREC ** added)480 int SetupManager::InsertHighScore(SCOREREC *score,SCOREREC **added) {
481 
482   *added = NULL;
483 
484   if( score->score == 0 ) {
485     // Does not insert null score
486     return 10;
487   }
488 
489   int pos = 0;
490   int id = GetId();
491   BOOL found = FALSE;
492 
493   SCOREREC *ptr = scoreList;
494   SCOREREC *lastPtr = NULL;
495 
496   // Look for insertion pos
497   while( (ptr!=NULL) && (pos<10) && (!found) ) {
498     found = score->score > ptr->score;
499     if( !found ) {
500       if( ptr->setupId == id )
501         pos++;
502       lastPtr = ptr;
503       ptr = ptr->next;
504     }
505   }
506 
507   if( pos<10 ) {
508     // Got a high score
509     SCOREREC *nPtr = (SCOREREC *)malloc( sizeof(SCOREREC) );
510     memcpy(nPtr,score,sizeof(SCOREREC));
511     if( lastPtr==NULL ) {
512       // New head
513       nPtr->next = scoreList;
514       scoreList = nPtr;
515     } else {
516       // Insert after lastPtr
517       nPtr->next = ptr;
518       lastPtr->next = nPtr;
519     }
520     *added = nPtr;
521     // Keep only 10
522     CleanHighScore(id);
523   }
524 
525   return pos;
526 
527 }
528 
529 // ------------------------------------------------
530 
CleanHighScore(int id)531 void SetupManager::CleanHighScore(int id) {
532 
533   int pos = 0;
534   BOOL removed;
535   SCOREREC *ptr = scoreList;
536   SCOREREC *lastPtr = NULL;
537 
538   // Keep the 10 best score of the given setup
539   while( ptr!=NULL ) {
540     removed = FALSE;
541     if( ptr->setupId == id ) {
542       if( pos>=10 ) {
543         // Remove (REM: lastPtr cannot be null)
544         lastPtr->next = ptr->next;
545         free(ptr);
546         // Next item
547         removed = TRUE;
548         ptr = lastPtr->next;
549       } else {
550         pos++;
551       }
552     }
553     if( !removed ) {
554       lastPtr = ptr;
555       ptr = ptr->next;
556     }
557   }
558 
559 }
560 
561 // ------------------------------------------------
562 
GetHighScore(SCOREREC * hScore)563 void SetupManager::GetHighScore(SCOREREC *hScore) {
564 
565   SCOREREC *ptr = scoreList;
566   int id = GetId();
567   int pos = 0;
568 
569   // Return all high score of the current game setup (10 MAX)
570   while( ptr!=NULL ) {
571     if( ptr->setupId == id ) {
572       if(pos<10) memcpy(hScore + pos,ptr,sizeof(SCOREREC));
573       pos++;
574     }
575     ptr = ptr->next;
576   }
577 
578   // Reset last items
579   for(;pos<10;pos++) {
580     memset(hScore + pos,0,sizeof(SCOREREC));
581     strcpy( hScore[pos].name , ".........." );
582   }
583 
584 }
585 
586 // ------------------------------------------------
587 
GetHighScore()588 int SetupManager::GetHighScore() {
589 
590   SCOREREC *ptr = scoreList;
591   int id = GetId();
592   BOOL found = FALSE;
593 
594   // Return the high score of the current game setup
595   while(ptr!=NULL && !found) {
596     found = (ptr->setupId == id);
597     if( !found ) ptr = ptr->next;
598   }
599 
600   if( found )
601     return ptr->score;
602   else
603     return 0;
604 
605 }
606 
607 // ------------------------------------------------
608 
ReadScoreItem(FILE * f,SCOREREC * dest)609 void SetupManager::ReadScoreItem(FILE *f,SCOREREC *dest) {
610 
611   DWORD nbRead;
612 
613   int structSzie = 64;
614 
615   // Done for 64/32 bits compatibily
616   // 4 last bytes are not used
617   nbRead=(DWORD)fread(dest, 1, structSzie, f);
618   dest->next = NULL;
619 
620 }
621 
622 // ------------------------------------------------
623 
LoadHighScore()624 void SetupManager::LoadHighScore() {
625 
626   SCOREREC *ptr;
627 
628   FILE *file = fopen(LHD(STR("hscore.dat")),"rb");
629 
630   if( file != NULL ) {
631 
632     int32 nbScore;
633     DWORD nbRead;
634 	nbRead = (DWORD)fread(&nbScore, 1, sizeof(int32), file);
635 
636     if( (nbRead == sizeof(int32)) && (nbScore <= 10*585) && (nbScore>=1) ) {
637 
638       scoreList = (SCOREREC *)malloc( sizeof(SCOREREC) );
639       ReadScoreItem(file,scoreList);
640       ptr = scoreList;
641 
642       for( int i = 1; i < nbScore ; i++ ) {
643 
644         SCOREREC *nPtr = (SCOREREC *)malloc( sizeof(SCOREREC) );
645         ReadScoreItem(file,nPtr);
646         ptr->next = nPtr;
647         ptr = ptr->next;
648 
649       }
650 
651       ptr->next = NULL;
652 
653     }
654 
655     fclose(file);
656 
657   }
658 
659 }
660 
661 // ------------------------------------------------
662 
WriteScoreItem(FILE * f,SCOREREC * dest)663 void SetupManager::WriteScoreItem(FILE *f,SCOREREC *dest) {
664 
665   int structSzie = 64;
666 
667   // Done for 64/32 bits compatibily
668   // 4 last bytes are not used
669   fwrite(dest, 1, structSzie , f);
670 
671 }
672 
673 // ------------------------------------------------
674 
SaveHighScore()675 void SetupManager::SaveHighScore() {
676 
677   SCOREREC *ptr = scoreList;
678 
679   FILE *file = fopen(LHD(STR("hscore.dat")),"wb");
680 
681   if( file != NULL ) {
682 
683     // Get number of score
684     int32 nbScore = 0;
685     while( ptr != NULL ) {
686       nbScore++;
687       ptr = ptr->next;
688     }
689 
690     // Write
691     DWORD nbWritten;
692 	nbWritten = (DWORD)fwrite(&nbScore, 1, sizeof(int32), file);
693     if( nbWritten == sizeof(int32) ) {
694 
695       ptr = scoreList;
696       while( ptr != NULL ) {
697         WriteScoreItem(file,ptr);
698         ptr = ptr->next;
699       }
700 
701     }
702     fclose(file);
703 
704   }
705 
706 }
707 
708 // ------------------------------------------------
709 
WriteSetup()710 void SetupManager::WriteSetup() {
711 
712 
713   FILE *file = fopen(LHD(STR("setup.dat")),"wb");
714 
715   if( file != NULL ) {
716 
717     // Write
718     DWORD nbWritten;
719     int32 version = SETUP_VERSION;
720 	nbWritten = (DWORD)fwrite(&version, 1, sizeof(int32), file);
721     if( nbWritten == sizeof(int32) ) {
722 
723       fwrite(&pitWidth,sizeof(int32),1,file);
724       fwrite(&pitHeight,sizeof(int32),1,file);
725       fwrite(&pitDepth,sizeof(int32),1,file);
726       fwrite(&blockSet,sizeof(int32),1,file);
727       fwrite(&animationSpeed,sizeof(int32),1,file);
728       fwrite(&startLevel,sizeof(int32),1,file);
729       fwrite(&playSound,sizeof(BOOL),1,file);
730       fwrite(&fullScreen,sizeof(BOOL),1,file);
731       fwrite(&windowSize,sizeof(int32),1,file);
732       fwrite(&keyRx1,sizeof(BYTE),1,file);
733       fwrite(&keyRy1,sizeof(BYTE),1,file);
734       fwrite(&keyRz1,sizeof(BYTE),1,file);
735       fwrite(&keyRx2,sizeof(BYTE),1,file);
736       fwrite(&keyRy2,sizeof(BYTE),1,file);
737       fwrite(&keyRz2,sizeof(BYTE),1,file);
738       fwrite(&transparentFace,sizeof(int32),1,file);
739       fwrite(httpHome,256,1,file);
740       fwrite(httpProxy,256,1,file);
741       fwrite(&httpProxyPort,sizeof(int32),1,file);
742       fwrite(&useHttpProxy,sizeof(BOOL),1,file);
743       fwrite(&httpTimeout,sizeof(int32),1,file);
744       fwrite(&style,sizeof(int32),1,file);
745       fwrite(&soundType,sizeof(int32),1,file);
746       fwrite(&frLimit,sizeof(int32),1,file);
747       fwrite(&lineWidth,sizeof(int32),1,file);
748 
749     }
750     fclose(file);
751 
752   }
753 
754 }
755 
756 // ------------------------------------------------
757 
LoadSetup()758 void SetupManager::LoadSetup() {
759 
760   FILE *file = fopen(LHD(STR("setup.dat")),"rb");
761 
762   if( file != NULL ) {
763 
764     int version = 0;
765     DWORD nbRead;
766 	nbRead = (DWORD)fread(&version, 1, sizeof(int32), file);
767 
768     if( nbRead == sizeof(int32) ) {
769 
770       if( version<=SETUP_VERSION ) {
771 
772         nbRead=(DWORD)fread(&pitWidth,sizeof(int32),1,file);
773 		nbRead=(DWORD)fread(&pitHeight, sizeof(int32), 1, file);
774 		nbRead=(DWORD)fread(&pitDepth, sizeof(int32), 1, file);
775 		nbRead=(DWORD)fread(&blockSet, sizeof(int32), 1, file);
776 		nbRead=(DWORD)fread(&animationSpeed, sizeof(int32), 1, file);
777         // Convert if version 1
778         if( version==1 ) {
779           switch(animationSpeed) {
780             case 1: // Medium (SETUP version 1)
781               animationSpeed = 5;
782               break;
783             case 2: // FAST (SETUP version 1)
784               animationSpeed = 10;
785               break;
786           }
787         }
788 		nbRead=(DWORD)fread(&startLevel, sizeof(int32), 1, file);
789 		nbRead=(DWORD)fread(&playSound, sizeof(BOOL), 1, file);
790 		nbRead=(DWORD)fread(&fullScreen, sizeof(BOOL), 1, file);
791 		nbRead=(DWORD)fread(&windowSize, sizeof(int32), 1, file);
792 		nbRead=(DWORD)fread(&keyRx1, sizeof(BYTE), 1, file);
793 		nbRead=(DWORD)fread(&keyRy1, sizeof(BYTE), 1, file);
794 		nbRead=(DWORD)fread(&keyRz1, sizeof(BYTE), 1, file);
795 		nbRead=(DWORD)fread(&keyRx2, sizeof(BYTE), 1, file);
796 		nbRead=(DWORD)fread(&keyRy2, sizeof(BYTE), 1, file);
797 		nbRead=(DWORD)fread(&keyRz2, sizeof(BYTE), 1, file);
798 		nbRead=(DWORD)fread(&transparentFace, sizeof(BOOL), 1, file);
799         // Convert if version < 3
800         if( version<3 ) {
801           if(transparentFace) {
802              // Enabled -> Medium transparency
803              transparentFace = 5;
804           }
805         }
806         if( version>=3 ) {
807           // Load Http proxy param
808 		  nbRead=(DWORD)fread(httpHome, 256, 1, file);
809 		  nbRead=(DWORD)fread(httpProxy, 256, 1, file);
810 		  nbRead=(DWORD)fread(&httpProxyPort, sizeof(int32), 1, file);
811 		  nbRead=(DWORD)fread(&useHttpProxy, sizeof(BOOL), 1, file);
812 		  nbRead=(DWORD)fread(&httpTimeout, sizeof(int32), 1, file);
813         }
814         if( version>=4 ) {
815           // Load style and sound type
816 		  nbRead=(DWORD)fread(&style, sizeof(int32), 1, file);
817 		  nbRead=(DWORD)fread(&soundType, sizeof(int32), 1, file);
818         }
819         if( version>=5 ) {
820           // Frame limiter
821 		  nbRead=(DWORD)fread(&frLimit, sizeof(int32), 1, file);
822         }
823 
824         if( version>=6 ) {
825           // Linewidth
826 		  nbRead=(DWORD)fread(&lineWidth, sizeof(int32), 1, file);
827         }
828 
829       }
830 
831     }
832 
833     fclose(file);
834 
835   }
836 
837 }
838 
839 // ------------------------------------------------
840 
Check(int w,int h,int d,int s)841 BOOL SetupManager::Check(int w,int h,int d,int s) {
842 
843   return (  (w == pitWidth) && (h == pitHeight) ||
844             (h == pitWidth) && (w == pitHeight) ) &&
845             (d == pitDepth) && (s == blockSet);
846 
847 }
848 
849 // ------------------------------------------------
850 
Saturate(int v,int min,int max)851 int SetupManager::Saturate(int v,int min,int max) {
852 
853   if( v < min ) {
854     return min;
855   } else if ( v > max ) {
856     return max;
857   }
858 
859   return v;
860 }
861