]> git.jsancho.org Git - lugaru.git/blob - Source/OpenGL_Windows.cpp
Lots of input work...demo is now fully playable.
[lugaru.git] / Source / OpenGL_Windows.cpp
1
2 #ifdef WIN32
3 #include <vld.h>
4 #endif
5
6 #include "Game.h"
7 #include "IL/il.h"
8 #include "IL/ilu.h"
9 #include "IL/ilut.h"
10
11 // ADDED GWC
12 #ifdef _MSC_VER
13 #pragma comment(lib, "opengl32.lib")
14 #pragma comment(lib, "glu32.lib")
15 #pragma comment(lib, "glaux.lib")
16 #endif
17
18 extern bool buttons[3];
19 extern float multiplier;
20 extern float screenwidth,screenheight;
21 extern float sps;
22 extern float realmultiplier;
23 extern int slomo;
24 extern bool ismotionblur;
25 extern float usermousesensitivity;
26 extern int detail;
27 extern bool floatjump;
28 extern bool cellophane;
29 // MODIFIED GWC
30 //extern int terraindetail;
31 //extern int texdetail;
32 extern float terraindetail;
33 extern float texdetail;
34 extern int bloodtoggle;
35 extern bool osx;
36 extern bool autoslomo;
37 extern bool foliage;
38 extern bool musictoggle;
39 extern bool trilinear;
40 extern float gamespeed;
41 extern int difficulty;
42 extern bool damageeffects;
43 extern int numplayers;
44 extern bool decals;
45 extern bool invertmouse;
46 extern bool texttoggle;
47 extern bool ambientsound;
48 extern bool mousejump;
49 extern bool freeze;
50 extern Person player[maxplayers];
51 extern bool vblsync;
52 extern bool stillloading;
53 extern bool showpoints;
54 extern bool alwaysblur;
55 extern bool immediate;
56 extern bool velocityblur;
57 extern bool debugmode;
58 extern int mainmenu;
59 /*extern*/ bool gameFocused;
60 extern int kBitsPerPixel;
61 extern float slomospeed;
62 extern float slomofreq;
63 extern float oldgamespeed;
64 extern float volume;
65
66 #include <math.h>
67 #include <stdio.h>
68 #include <string.h>
69 #include <fstream>
70 #include <iostream>
71 #include "gamegl.h"
72 #include "MacCompatibility.h"
73
74 #ifdef WIN32
75 #include <shellapi.h>
76 #endif
77
78 #include "fmod.h"
79
80 #include "res/resource.h"
81
82 using namespace std;
83
84
85 unsigned int resolutionDepths[8][2] = {0};
86
87 bool selectDetail(int & width, int & height, int & bpp, int & detail);
88 int closestResolution(int width, int height);
89 int resolutionID(int width, int height);
90
91 void ReportError (char * strError);
92
93 void SetupDSpFullScreen();
94 void ShutdownDSp();
95
96 void DrawGL(Game & game);
97
98 void CreateGLWindow (void);
99 Boolean SetUp (Game & game);
100 void DoKey (SInt8 theKey, SInt8 theCode);
101 void DoUpdate (Game & game);
102
103 void DoEvent (void);
104 void CleanUp (void);
105
106
107 // statics/globals (internal only) ------------------------------------------
108 #ifndef WIN32
109 typedef struct tagPOINT { 
110   int x;
111   int y;
112 } POINT, *PPOINT; 
113 #endif
114
115 #if USE_SDL
116 void sdlGetCursorPos(POINT *pt)
117 {
118     int x, y;
119     SDL_GetMouseState(&x, &y);
120     pt->x = x;
121     pt->y = y;
122 }
123 #define GetCursorPos(x) sdlGetCursorPos(x)
124 #define SetCursorPos(x, y) SDL_WarpMouse(x, y)
125 #define ScreenToClient(x, pt)
126 #define ClientToScreen(x, pt)
127 #define MessageBox(hwnd,text,title,flags) STUBBED("msgbox")
128 #endif
129
130 Point delta;
131
132 #ifdef WIN32
133 static const char g_wndClassName[]={ "LUGARUWINDOWCLASS" };
134 static HINSTANCE g_appInstance;
135 static HWND g_windowHandle;
136 static HGLRC hRC;
137 #endif
138
139 static bool g_button, fullscreen = true;
140
141
142 // Menu defs
143 enum 
144 {
145         kFileQuit = 1
146 };
147
148 enum 
149 {
150         kForegroundSleep = 10,
151         kBackgroundSleep = 10000
152 };
153
154
155 int kContextWidth;
156 int kContextHeight;
157
158 const RGBColor rgbBlack = { 0x0000, 0x0000, 0x0000 };
159
160 GLuint gFontList;
161 char gcstrMode [256] = "";
162
163 UInt32 gSleepTime = kForegroundSleep;
164 Boolean gDone = false, gfFrontProcess = true;
165
166 Game * pgame = 0;
167
168 static bool gMouseGrabbed = true;
169
170 // --------------------------------------------------------------------------
171
172 void ReportError (char * strError)
173 {
174 #ifdef WIN32  // !!! FIXME.  --ryan.
175         throw std::exception( strError);
176 #endif
177
178         /*      char errMsgCStr [256];
179         Str255 strErr;
180
181         sprintf (errMsgCStr, "%s", strError); 
182
183         // out as debug string
184         CToPStr (strErr, errMsgCStr);
185         DebugStr (strErr);
186         */
187 }
188
189 void SetupDSpFullScreen ()
190 {
191 #ifdef WIN32
192         LOGFUNC;
193
194         if (fullscreen)
195         {
196                 DEVMODE dmScreenSettings;
197                 memset( &dmScreenSettings, 0, sizeof( dmScreenSettings));
198                 dmScreenSettings.dmSize = sizeof( dmScreenSettings);
199                 dmScreenSettings.dmPelsWidth    = kContextWidth;
200                 dmScreenSettings.dmPelsHeight   = kContextHeight;
201                 dmScreenSettings.dmBitsPerPel   = kBitsPerPixel;
202                 dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
203
204                 // set video mode
205                 if (ChangeDisplaySettings( &dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
206                 {
207                         ReportError( "Could not set display mode");
208                         return;
209                 }
210         }
211
212         ShowCursor(FALSE);
213 #endif
214 }
215
216
217 void ShutdownDSp ()
218 {
219 #ifdef WIN32
220         LOGFUNC;
221
222         if (fullscreen)
223         {
224                 ChangeDisplaySettings( NULL, 0);
225         }
226
227         ShowCursor(TRUE);
228 #endif
229 }
230
231
232 //-----------------------------------------------------------------------------------------------------------------------
233
234 // OpenGL Drawing
235
236 void DrawGL (Game & game)
237 {
238 #ifdef WIN32
239         if (hDC == 0)
240                 return;
241 #endif
242
243         game.DrawGLScene();
244 }
245
246
247 static KeyMap g_theKeys;
248
249 void SetKey( int key)
250 {
251     g_theKeys[ key >> 3] |= (1 << (key & 7));
252 }
253
254 void ClearKey( int key)
255 {
256     g_theKeys[ key >> 3] &= (0xff ^ (1 << (key & 7)));
257 }
258
259 void GetKeys(  unsigned char theKeys[16])
260 {
261     memcpy( theKeys, &g_theKeys, 16);
262 }
263
264 Boolean Button()
265 {
266     return g_button;
267 }
268
269 #if !USE_SDL
270 static void initSDLKeyTable(void) {}
271 #else
272 #define MAX_SDLKEYS SDLK_LAST
273 static unsigned short KeyTable[MAX_SDLKEYS];
274
275 static void initSDLKeyTable(void)
276 {
277     memset(KeyTable, 0xFF, sizeof (KeyTable));
278     KeyTable[SDLK_BACKSPACE] = MAC_DELETE_KEY;
279     KeyTable[SDLK_TAB] = MAC_TAB_KEY;
280     KeyTable[SDLK_RETURN] = MAC_RETURN_KEY;
281     KeyTable[SDLK_ESCAPE] = MAC_ESCAPE_KEY;
282     KeyTable[SDLK_SPACE] = MAC_SPACE_KEY;
283     KeyTable[SDLK_PAGEUP] = MAC_PAGE_UP_KEY;
284     KeyTable[SDLK_PAGEDOWN] = MAC_PAGE_DOWN_KEY;
285     KeyTable[SDLK_END] = MAC_END_KEY;
286     KeyTable[SDLK_HOME] = MAC_HOME_KEY;
287     KeyTable[SDLK_LEFT] = MAC_ARROW_LEFT_KEY;
288     KeyTable[SDLK_UP] = MAC_ARROW_UP_KEY;
289     KeyTable[SDLK_RIGHT] = MAC_ARROW_RIGHT_KEY;
290     KeyTable[SDLK_DOWN] = MAC_ARROW_DOWN_KEY;
291     KeyTable[SDLK_INSERT] = MAC_INSERT_KEY;
292     KeyTable[SDLK_DELETE] = MAC_DEL_KEY;
293     KeyTable[SDLK_0] = MAC_0_KEY;
294     KeyTable[SDLK_1] = MAC_1_KEY;
295     KeyTable[SDLK_2] = MAC_2_KEY;
296     KeyTable[SDLK_3] = MAC_3_KEY;
297     KeyTable[SDLK_4] = MAC_4_KEY;
298     KeyTable[SDLK_5] = MAC_5_KEY;
299     KeyTable[SDLK_6] = MAC_6_KEY;
300     KeyTable[SDLK_7] = MAC_7_KEY;
301     KeyTable[SDLK_8] = MAC_8_KEY;
302     KeyTable[SDLK_9] = MAC_9_KEY;
303     KeyTable[SDLK_a] = MAC_A_KEY;
304     KeyTable[SDLK_b] = MAC_B_KEY;
305     KeyTable[SDLK_c] = MAC_C_KEY;
306     KeyTable[SDLK_d] = MAC_D_KEY;
307     KeyTable[SDLK_e] = MAC_E_KEY;
308     KeyTable[SDLK_f] = MAC_F_KEY;
309     KeyTable[SDLK_g] = MAC_G_KEY;
310     KeyTable[SDLK_h] = MAC_H_KEY;
311     KeyTable[SDLK_i] = MAC_I_KEY;
312     KeyTable[SDLK_j] = MAC_J_KEY;
313     KeyTable[SDLK_k] = MAC_K_KEY;
314     KeyTable[SDLK_l] = MAC_L_KEY;
315     KeyTable[SDLK_m] = MAC_M_KEY;
316     KeyTable[SDLK_n] = MAC_N_KEY;
317     KeyTable[SDLK_o] = MAC_O_KEY;
318     KeyTable[SDLK_p] = MAC_P_KEY;
319     KeyTable[SDLK_q] = MAC_Q_KEY;
320     KeyTable[SDLK_r] = MAC_R_KEY;
321     KeyTable[SDLK_s] = MAC_S_KEY;
322     KeyTable[SDLK_t] = MAC_T_KEY;
323     KeyTable[SDLK_u] = MAC_U_KEY;
324     KeyTable[SDLK_v] = MAC_V_KEY;
325     KeyTable[SDLK_w] = MAC_W_KEY;
326     KeyTable[SDLK_x] = MAC_X_KEY;
327     KeyTable[SDLK_y] = MAC_Y_KEY;
328     KeyTable[SDLK_z] = MAC_Z_KEY;
329     KeyTable[SDLK_KP0] = MAC_NUMPAD_0_KEY;
330     KeyTable[SDLK_KP1] = MAC_NUMPAD_1_KEY;
331     KeyTable[SDLK_KP2] = MAC_NUMPAD_2_KEY;
332     KeyTable[SDLK_KP3] = MAC_NUMPAD_3_KEY;
333     KeyTable[SDLK_KP4] = MAC_NUMPAD_4_KEY;
334     KeyTable[SDLK_KP5] = MAC_NUMPAD_5_KEY;
335     KeyTable[SDLK_KP6] = MAC_NUMPAD_6_KEY;
336     KeyTable[SDLK_KP7] = MAC_NUMPAD_7_KEY;
337     KeyTable[SDLK_KP8] = MAC_NUMPAD_8_KEY;
338     KeyTable[SDLK_KP9] = MAC_NUMPAD_9_KEY;
339     KeyTable[SDLK_KP_MULTIPLY] = MAC_NUMPAD_ASTERISK_KEY;
340     KeyTable[SDLK_KP_PLUS] = MAC_NUMPAD_PLUS_KEY;
341     KeyTable[SDLK_KP_ENTER] = MAC_NUMPAD_ENTER_KEY;
342     KeyTable[SDLK_KP_MINUS] = MAC_NUMPAD_MINUS_KEY;
343     KeyTable[SDLK_KP_PERIOD] = MAC_NUMPAD_PERIOD_KEY;
344     KeyTable[SDLK_KP_DIVIDE] = MAC_NUMPAD_SLASH_KEY;
345     KeyTable[SDLK_F1] = MAC_F1_KEY;
346     KeyTable[SDLK_F2] = MAC_F2_KEY;
347     KeyTable[SDLK_F3] = MAC_F3_KEY;
348     KeyTable[SDLK_F4] = MAC_F4_KEY;
349     KeyTable[SDLK_F5] = MAC_F5_KEY;
350     KeyTable[SDLK_F6] = MAC_F6_KEY;
351     KeyTable[SDLK_F7] = MAC_F7_KEY;
352     KeyTable[SDLK_F8] = MAC_F8_KEY;
353     KeyTable[SDLK_F9] = MAC_F9_KEY;
354     KeyTable[SDLK_F10] = MAC_F10_KEY;
355     KeyTable[SDLK_F11] = MAC_F11_KEY;
356     KeyTable[SDLK_F12] = MAC_F12_KEY;
357     KeyTable[SDLK_SEMICOLON] = MAC_SEMICOLON_KEY;
358     KeyTable[SDLK_PLUS] = MAC_PLUS_KEY;
359     KeyTable[SDLK_COMMA] = MAC_COMMA_KEY;
360     KeyTable[SDLK_MINUS] = MAC_MINUS_KEY;
361     KeyTable[SDLK_PERIOD] = MAC_PERIOD_KEY;
362     KeyTable[SDLK_SLASH] = MAC_SLASH_KEY;
363     KeyTable[SDLK_BACKQUOTE] = MAC_TILDE_KEY;
364     KeyTable[SDLK_LEFTBRACKET] = MAC_LEFTBRACKET_KEY;
365     KeyTable[SDLK_BACKSLASH] = MAC_BACKSLASH_KEY;
366     KeyTable[SDLK_RIGHTBRACKET] = MAC_RIGHTBRACKET_KEY;
367     KeyTable[SDLK_QUOTE] = MAC_APOSTROPHE_KEY;
368 }
369
370 static inline int clamp_sdl_mouse_button(Uint8 button)
371 {
372     if ((button >= 1) && (button <= 3))
373         return button - 1;
374     return -1;
375 }
376
377 static void sdlEventProc(const SDL_Event &e)
378 {
379     int val;
380     switch(e.type)
381         {
382                 case SDL_MOUSEBUTTONDOWN:
383                         {
384                 val = clamp_sdl_mouse_button(e.button.button);
385                 if (val >= 0)
386                 {
387                     if (val == 0)
388                     {
389                                     g_button = true;
390                         SetKey(MAC_MOUSEBUTTON1);
391                     }
392
393                     else if (val == 1)
394                         SetKey(MAC_MOUSEBUTTON2);
395
396                                 buttons[val] = true;
397                 }
398                         }
399                         return;
400
401                 case SDL_MOUSEBUTTONUP:
402                         {
403                 val = clamp_sdl_mouse_button(e.button.button);
404                 if (val >= 0)
405                 {
406                     if (val == 0)
407                     {
408                                     g_button = false;
409                         ClearKey(MAC_MOUSEBUTTON1);
410                     }
411
412                     else if (val == 1)
413                         ClearKey(MAC_MOUSEBUTTON2);
414
415                                 buttons[val] = false;
416                 }
417                         }
418             return;
419
420         case SDL_KEYDOWN:
421             if (e.key.keysym.sym == SDLK_g)
422             {
423                 if (e.key.keysym.mod & KMOD_CTRL)
424                     gMouseGrabbed = !gMouseGrabbed;
425             }
426
427             else if (e.key.keysym.sym == SDLK_RETURN)
428             {
429                 if (e.key.keysym.mod & KMOD_ALT)
430                     SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
431             }
432
433             if (e.key.keysym.sym < SDLK_LAST)
434             {
435                 if (KeyTable[e.key.keysym.sym] != 0xffff)
436                     SetKey(KeyTable[e.key.keysym.sym]);
437             }
438
439             if (e.key.keysym.mod & KMOD_CTRL)
440                 SetKey(MAC_CONTROL_KEY);
441             if (e.key.keysym.mod & KMOD_ALT)
442                 SetKey(MAC_OPTION_KEY);
443             if (e.key.keysym.mod & KMOD_SHIFT)
444                 SetKey(MAC_SHIFT_KEY);
445             if (e.key.keysym.mod & KMOD_CAPS)
446                 SetKey(MAC_CAPS_LOCK_KEY);
447
448             return;
449
450         case SDL_KEYUP:
451             if (e.key.keysym.sym < SDLK_LAST)
452             {
453                 if (KeyTable[e.key.keysym.sym] != 0xffff)
454                     ClearKey(KeyTable[e.key.keysym.sym]);
455             }
456
457             if (e.key.keysym.mod & KMOD_CTRL)
458                 ClearKey(MAC_CONTROL_KEY);
459             if (e.key.keysym.mod & KMOD_ALT)
460                 ClearKey(MAC_OPTION_KEY);
461             if (e.key.keysym.mod & KMOD_SHIFT)
462                 ClearKey(MAC_SHIFT_KEY);
463             if (e.key.keysym.mod & KMOD_CAPS)
464                 ClearKey(MAC_CAPS_LOCK_KEY);
465             return;
466     }
467 }
468 #endif
469
470 // --------------------------------------------------------------------------
471
472 static Point gMidPoint;
473
474 Boolean SetUp (Game & game)
475 {
476         char string[10];
477
478         LOGFUNC;
479
480         randSeed = UpTime().lo;
481
482         osx = 0;
483 //      ifstream ipstream(":Data:config.txt", std::ios::in /*| std::ios::nocreate*/);
484         ifstream ipstream("./Data/config.txt", std::ios::in /*| std::ios::nocreate*/);
485         detail=1;
486         ismotionblur=0;
487         usermousesensitivity=1;
488         kContextWidth=640;
489         kContextHeight=480;
490         kBitsPerPixel = 32;
491         floatjump=0;
492         cellophane=0;
493         texdetail=4;
494         autoslomo=1;
495         decals=1;
496         invertmouse=0;
497         bloodtoggle=0;
498         terraindetail=2;
499         foliage=1;
500         musictoggle=1;
501         trilinear=1;
502         gamespeed=1;
503         difficulty=1;
504         damageeffects=0;
505         texttoggle=1;
506         alwaysblur=0;
507         showpoints=0;
508         immediate=0;
509         velocityblur=0;
510
511         slomospeed=0.25;
512         slomofreq=8012;
513
514         volume = 0.8f;
515
516         game.crouchkey=MAC_SHIFT_KEY;
517         game.jumpkey=MAC_SPACE_KEY;
518         game.leftkey=MAC_A_KEY;
519         game.forwardkey=MAC_W_KEY;
520         game.backkey=MAC_S_KEY;
521         game.rightkey=MAC_D_KEY;
522         game.drawkey=MAC_E_KEY;
523         game.throwkey=MAC_Q_KEY;
524         game.attackkey=MAC_MOUSEBUTTON1;
525         game.chatkey=MAC_T_KEY;
526         numplayers=1;
527         ambientsound=1;
528         vblsync=0;
529         debugmode=0;
530
531         selectDetail(kContextWidth, kContextHeight, kBitsPerPixel, detail);
532
533         if(!ipstream) {
534                 //ofstream opstream(":Data:config.txt"); 
535                 ofstream opstream("./Data/config.txt"); 
536                 opstream << "Screenwidth:\n";
537                 opstream << kContextWidth;
538                 opstream << "\nScreenheight:\n";
539                 opstream << kContextHeight;
540                 opstream << "\nMouse sensitivity:\n";
541                 opstream << usermousesensitivity;
542                 opstream << "\nBlur(0,1):\n";
543                 opstream << ismotionblur;
544                 opstream << "\nOverall Detail(0,1,2) higher=better:\n";
545                 opstream << detail;
546                 opstream << "\nFloating jump:\n";
547                 opstream << floatjump;
548                 opstream << "\nMouse jump:\n";
549                 opstream << mousejump;
550                 opstream << "\nAmbient sound:\n";
551                 opstream << ambientsound;
552                 opstream << "\nBlood (0,1,2):\n";
553                 opstream << bloodtoggle;
554                 opstream << "\nAuto slomo:\n";
555                 opstream << autoslomo;
556                 opstream << "\nFoliage:\n";
557                 opstream << foliage;
558                 opstream << "\nMusic:\n";
559                 opstream << musictoggle;
560                 opstream << "\nTrilinear:\n";
561                 opstream << trilinear;
562                 opstream << "\nDecals(shadows,blood puddles,etc):\n";
563                 opstream << decals;
564                 opstream << "\nInvert mouse:\n";
565                 opstream << invertmouse;
566                 opstream << "\nGamespeed:\n";
567                 opstream << gamespeed;
568                 opstream << "\nDifficulty(0,1,2) higher=harder:\n";
569                 opstream << difficulty;
570                 opstream << "\nDamage effects(blackout, doublevision):\n";
571                 opstream << damageeffects;
572                 opstream << "\nText:\n";
573                 opstream << texttoggle;
574                 opstream << "\nDebug:\n";
575                 opstream << debugmode;
576                 opstream << "\nVBL Sync:\n";
577                 opstream << vblsync;
578                 opstream << "\nShow Points:\n";
579                 opstream << showpoints;
580                 opstream << "\nAlways Blur:\n";
581                 opstream << alwaysblur;
582                 opstream << "\nImmediate mode (turn on on G5):\n";
583                 opstream << immediate;
584                 opstream << "\nVelocity blur:\n";
585                 opstream << velocityblur;
586                 opstream << "\nVolume:\n";
587                 opstream << volume;
588                 opstream << "\nForward key:\n";
589                 opstream << KeyToChar(game.forwardkey);
590                 opstream << "\nBack key:\n";
591                 opstream << KeyToChar(game.backkey);
592                 opstream << "\nLeft key:\n";
593                 opstream << KeyToChar(game.leftkey);
594                 opstream << "\nRight key:\n";
595                 opstream << KeyToChar(game.rightkey);
596                 opstream << "\nJump key:\n";
597                 opstream << KeyToChar(game.jumpkey);
598                 opstream << "\nCrouch key:\n";
599                 opstream << KeyToChar(game.crouchkey);
600                 opstream << "\nDraw key:\n";
601                 opstream << KeyToChar(game.drawkey);
602                 opstream << "\nThrow key:\n";
603                 opstream << KeyToChar(game.throwkey);
604                 opstream << "\nAttack key:\n";
605                 opstream << KeyToChar(game.attackkey);
606                 opstream << "\nChat key:\n";
607                 opstream << KeyToChar(game.chatkey);
608                 opstream.close();
609         }
610         if(ipstream){
611                 int i;
612                 ipstream.ignore(256,'\n');
613                 ipstream >> kContextWidth;
614                 ipstream.ignore(256,'\n');
615                 ipstream.ignore(256,'\n');
616                 ipstream >> kContextHeight;
617                 ipstream.ignore(256,'\n');
618                 ipstream.ignore(256,'\n');
619                 ipstream >> usermousesensitivity;
620                 ipstream.ignore(256,'\n');
621                 ipstream.ignore(256,'\n');
622                 ipstream >> i;
623                 ismotionblur = (i != 0);
624                 ipstream.ignore(256,'\n');
625                 ipstream.ignore(256,'\n');
626                 ipstream >> detail;
627                 if(detail!=0)kBitsPerPixel=32;
628                 else kBitsPerPixel=16;
629                 ipstream.ignore(256,'\n');
630                 ipstream.ignore(256,'\n');
631                 ipstream >> i;
632                 floatjump = (i != 0);
633                 ipstream.ignore(256,'\n');
634                 ipstream.ignore(256,'\n');
635                 ipstream >> i;
636                 mousejump = (i != 0);
637                 ipstream.ignore(256,'\n');
638                 ipstream.ignore(256,'\n');
639                 ipstream >> i;
640                 ambientsound = (i != 0);
641                 ipstream.ignore(256,'\n');
642                 ipstream.ignore(256,'\n');
643                 ipstream >> bloodtoggle;
644                 ipstream.ignore(256,'\n');
645                 ipstream.ignore(256,'\n');
646                 ipstream >> i;
647                 autoslomo = (i != 0);
648                 ipstream.ignore(256,'\n');
649                 ipstream.ignore(256,'\n');
650                 ipstream >> i;
651                 foliage = (i != 0);
652                 ipstream.ignore(256,'\n');
653                 ipstream.ignore(256,'\n');
654                 ipstream >> i;
655                 musictoggle = (i != 0);
656                 ipstream.ignore(256,'\n');
657                 ipstream.ignore(256,'\n');
658                 ipstream >> i;
659                 trilinear = (i != 0);
660                 ipstream.ignore(256,'\n');
661                 ipstream.ignore(256,'\n');
662                 ipstream >> i;
663                 decals = (i != 0);
664                 ipstream.ignore(256,'\n');
665                 ipstream.ignore(256,'\n');
666                 ipstream >> i;
667                 invertmouse = (i != 0);
668                 ipstream.ignore(256,'\n');
669                 ipstream.ignore(256,'\n');
670                 ipstream >> gamespeed;
671                 oldgamespeed=gamespeed;
672                 if(oldgamespeed==0){
673                         gamespeed=1;
674                         oldgamespeed=1;
675                 }
676                 ipstream.ignore(256,'\n');
677                 ipstream.ignore(256,'\n');
678                 ipstream >> difficulty;
679                 ipstream.ignore(256,'\n');
680                 ipstream.ignore(256,'\n');
681                 ipstream >> i;
682                 damageeffects = (i != 0);
683                 ipstream.ignore(256,'\n');
684                 ipstream.ignore(256,'\n');
685                 ipstream >> i;
686                 texttoggle = (i != 0);
687                 ipstream.ignore(256,'\n');
688                 ipstream.ignore(256,'\n');
689                 ipstream >> i;
690                 debugmode = (i != 0);
691                 ipstream.ignore(256,'\n');
692                 ipstream.ignore(256,'\n');
693                 ipstream >> i;
694                 vblsync = (i != 0);
695                 ipstream.ignore(256,'\n');
696                 ipstream.ignore(256,'\n');
697                 ipstream >> i;
698                 showpoints = (i != 0);
699                 ipstream.ignore(256,'\n');
700                 ipstream.ignore(256,'\n');
701                 ipstream >> i;
702                 alwaysblur = (i != 0);
703                 ipstream.ignore(256,'\n');
704                 ipstream.ignore(256,'\n');
705                 ipstream >> i;
706                 immediate = (i != 0);
707                 ipstream.ignore(256,'\n');
708                 ipstream.ignore(256,'\n');
709                 ipstream >> i;
710                 velocityblur = (i != 0);
711                 ipstream.ignore(256,'\n');
712                 ipstream.ignore(256,'\n'); 
713                 ipstream >> volume;
714                 ipstream.ignore(256,'\n');
715                 ipstream.ignore(256,'\n'); 
716                 ipstream >> string;
717                 game.forwardkey=CharToKey(string);
718                 ipstream.ignore(256,'\n');
719                 ipstream.ignore(256,'\n');
720                 ipstream >> string;
721                 game.backkey=CharToKey(string);
722                 ipstream.ignore(256,'\n');
723                 ipstream.ignore(256,'\n');
724                 ipstream >> string;
725                 game.leftkey=CharToKey(string);
726                 ipstream.ignore(256,'\n');
727                 ipstream.ignore(256,'\n');
728                 ipstream >> string;
729                 game.rightkey=CharToKey(string);
730                 ipstream.ignore(256,'\n');
731                 ipstream.ignore(256,'\n');
732                 ipstream >> string;
733                 game.jumpkey=CharToKey(string);
734                 ipstream.ignore(256,'\n');
735                 ipstream.ignore(256,'\n');
736                 ipstream >> string;
737                 game.crouchkey=CharToKey(string);
738                 ipstream.ignore(256,'\n');
739                 ipstream.ignore(256,'\n');
740                 ipstream >> string;
741                 game.drawkey=CharToKey(string);
742                 ipstream.ignore(256,'\n');
743                 ipstream.ignore(256,'\n');
744                 ipstream >> string;
745                 game.throwkey=CharToKey(string);
746                 ipstream.ignore(256,'\n');
747                 ipstream.ignore(256,'\n');
748                 ipstream >> string;
749                 game.attackkey=CharToKey(string);
750                 ipstream.ignore(256,'\n');
751                 ipstream.ignore(256,'\n');
752                 ipstream >> string;
753                 game.chatkey=CharToKey(string);
754                 ipstream.close();
755
756                 if(detail>2)detail=2;
757                 if(detail<0)detail=0;
758                 if(screenwidth>3000)screenwidth=640;
759                 if(screenwidth<0)screenwidth=640;
760                 if(screenheight>3000)screenheight=480;
761                 if(screenheight<0)screenheight=480;
762         }
763         if(kBitsPerPixel!=32&&kBitsPerPixel!=16){
764                 kBitsPerPixel=16;
765         }
766
767
768         selectDetail(kContextWidth, kContextHeight, kBitsPerPixel, detail);
769
770         SetupDSpFullScreen();
771
772 #if USE_SDL
773     if (!SDL_WasInit(SDL_INIT_VIDEO))
774     {
775         if (SDL_Init(SDL_INIT_VIDEO) == -1)
776         {
777             fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
778             return false;
779         }
780     }
781
782     Uint32 sdlflags = SDL_OPENGL;
783     SDL_WM_SetCaption("Lugaru", "lugaru");
784     SDL_ShowCursor(0);
785     if (SDL_SetVideoMode(kContextWidth, kContextHeight, 0, sdlflags) == NULL)
786     {
787         fprintf(stderr, "SDL_SetVideoMode() failed: %s\n", SDL_GetError());
788         return false;
789     }
790
791 #elif (defined WIN32)
792         //------------------------------------------------------------------
793         // create window
794         int x = 0, y = 0;
795         RECT r = {0, 0, kContextWidth-1, kContextHeight-1};
796         DWORD dwStyle = WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE;
797         DWORD dwExStyle = WS_EX_APPWINDOW;
798
799         if (fullscreen)
800         {
801                 dwStyle |= WS_POPUP;
802         }
803         else
804         {
805
806                 dwStyle |= WS_OVERLAPPEDWINDOW;
807                 dwExStyle |= WS_EX_WINDOWEDGE;
808         }
809
810         AdjustWindowRectEx(&r, dwStyle, FALSE, dwExStyle);
811
812         if (!fullscreen)
813         {
814                 x = (GetSystemMetrics(SM_CXSCREEN) >> 1) - ((r.right - r.left + 1) >> 1);
815                 y = (GetSystemMetrics(SM_CYSCREEN) >> 1) - ((r.bottom - r.top + 1) >> 1);
816         }
817
818         g_windowHandle=CreateWindowEx(
819                 dwExStyle,
820                 g_wndClassName, "Lugaru", dwStyle,
821                 x, y,
822 //              kContextWidth, kContextHeight,
823                 r.right - r.left + 1, r.bottom - r.top + 1,
824                 NULL,NULL,g_appInstance,NULL );
825         if (!g_windowHandle)
826         {
827                 ReportError("Could not create window");
828                 return false;
829         }
830
831         //------------------------------------------------------------------
832         // setup OpenGL
833
834         static PIXELFORMATDESCRIPTOR pfd =
835         {
836                 sizeof(PIXELFORMATDESCRIPTOR),                          // Size Of This Pixel Format Descriptor
837                         1,                                                                                      // Version Number
838                         PFD_DRAW_TO_WINDOW |                                            // Format Must Support Window
839                         PFD_SUPPORT_OPENGL |                                            // Format Must Support OpenGL
840                         PFD_DOUBLEBUFFER,                                                       // Must Support Double Buffering
841                         PFD_TYPE_RGBA,                                                          // Request An RGBA Format
842                         kBitsPerPixel,                                                          // Select Our Color Depth
843                         0, 0, 0, 0, 0, 0,                                                       // Color Bits Ignored
844                         0,                                                                                      // No Alpha Buffer
845                         0,                                                                                      // Shift Bit Ignored
846                         0,                                                                                      // No Accumulation Buffer
847                         0, 0, 0, 0,                                                                     // Accumulation Bits Ignored
848                         16,                                                                                     // 16Bit Z-Buffer (Depth Buffer)  
849                         0,                                                                                      // No Stencil Buffer
850                         0,                                                                                      // No Auxiliary Buffer
851                         PFD_MAIN_PLANE,                                                         // Main Drawing Layer
852                         0,                                                                                      // Reserved
853                         0, 0, 0                                                                         // Layer Masks Ignored
854         };
855
856         if (!(hDC = GetDC( g_windowHandle)))
857                 ReportError( "Could not get device context");
858
859         GLuint PixelFormat;
860         if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
861         {
862                 ReportError( "Could not find appropriate pixel format");
863                 return false;
864         }
865
866         if (!DescribePixelFormat(hDC, PixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd))
867         {
868                 ReportError( "Could not retrieve pixel format");
869                 return false;
870         }
871
872         if (!SetPixelFormat( hDC, PixelFormat, &pfd))
873         {
874                 ReportError( "Could not set pixel format");
875                 return false;
876         }
877
878         if (!(hRC = wglCreateContext(hDC)))
879         {
880                 ReportError( "Could not create rendering context");
881                 return false;
882         }
883
884         if (!wglMakeCurrent(hDC, hRC))
885         {
886                 ReportError( "Could not activate rendering context");
887                 return false;
888         }
889
890         if (fullscreen)
891         {
892                 // Place the window above all topmost windows
893                 SetWindowPos( g_windowHandle, HWND_TOPMOST, 0,0,0,0,
894                         SWP_NOMOVE | SWP_NOSIZE );
895         }
896
897         SetForegroundWindow(g_windowHandle);
898         SetFocus(g_windowHandle);
899 #endif
900
901         glClear( GL_COLOR_BUFFER_BIT );
902         swap_gl_buffers();
903
904         // clear all states
905         glDisable( GL_ALPHA_TEST);
906         glDisable( GL_BLEND);
907         glDisable( GL_DEPTH_TEST);
908         //      glDisable( GL_DITHER);
909         glDisable( GL_FOG);
910         glDisable( GL_LIGHTING);
911         glDisable( GL_LOGIC_OP);
912         glDisable( GL_STENCIL_TEST);
913         glDisable( GL_TEXTURE_1D);
914         glDisable( GL_TEXTURE_2D);
915         glPixelTransferi( GL_MAP_COLOR, GL_FALSE);
916         glPixelTransferi( GL_RED_SCALE, 1);
917         glPixelTransferi( GL_RED_BIAS, 0);
918         glPixelTransferi( GL_GREEN_SCALE, 1);
919         glPixelTransferi( GL_GREEN_BIAS, 0);
920         glPixelTransferi( GL_BLUE_SCALE, 1);
921         glPixelTransferi( GL_BLUE_BIAS, 0);
922         glPixelTransferi( GL_ALPHA_SCALE, 1);
923         glPixelTransferi( GL_ALPHA_BIAS, 0);
924
925         // set initial rendering states
926         glShadeModel( GL_SMOOTH);
927         glClearDepth( 1.0f);
928         glDepthFunc( GL_LEQUAL);
929         glDepthMask( GL_TRUE);
930         //      glDepthRange( FRONT_CLIP, BACK_CLIP);
931         glEnable( GL_DEPTH_TEST);
932         glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
933         glCullFace( GL_FRONT);
934         glEnable( GL_CULL_FACE);
935         glEnable( GL_LIGHTING);
936 //      glEnable( GL_LIGHT_MODEL_AMBIENT);
937         glEnable( GL_DITHER);
938         glEnable( GL_COLOR_MATERIAL);
939         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
940         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
941         glAlphaFunc( GL_GREATER, 0.5f);
942
943         if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION ||
944                 iluGetInteger(ILU_VERSION_NUM) < ILU_VERSION ||
945                 ilutGetInteger(ILUT_VERSION_NUM) < ILUT_VERSION)
946         {
947                 ReportError("DevIL version is different...exiting!\n");
948                 return false;
949         }
950
951         ilInit();
952         iluInit();
953         ilutInit();
954
955         ilutRenderer(ILUT_OPENGL);
956
957         ilEnable(IL_ORIGIN_SET);
958         ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
959
960         GLint width = kContextWidth;
961         GLint height = kContextHeight;
962         gMidPoint.h = width / 2;
963         gMidPoint.v = height / 2;
964         screenwidth=width;
965         screenheight=height;
966
967         game.newdetail=detail;
968         game.newscreenwidth=screenwidth;
969         game.newscreenheight=screenheight;
970
971         game.InitGame();
972
973         return true;
974 }
975
976
977 static void DoMouse(Game & game)
978 {
979         static Point lastMouse = {-1,-1};
980         Point globalMouse;
981
982         POINT pos;
983         GetCursorPos(&pos);
984         ScreenToClient(g_windowHandle, &pos);
985         globalMouse.h = pos.x;
986         globalMouse.v = pos.y;
987
988         if (lastMouse.h == globalMouse.h && lastMouse.v == globalMouse.v)
989         {
990                 game.deltah=0;
991                 game.deltav=0;
992         }
993         else
994         {
995                 static Point virtualMouse = {0,0};
996                 delta = globalMouse;
997
998                 delta.h -= lastMouse.h;
999                 delta.v -= lastMouse.v;
1000                 lastMouse.h = pos.x;
1001                 lastMouse.v = pos.y;
1002
1003                 if(mainmenu||(abs(delta.h)<10*realmultiplier*1000&&abs(delta.v)<10*realmultiplier*1000)){
1004                         game.deltah=delta.h*usermousesensitivity;
1005                         game.deltav=delta.v*usermousesensitivity;
1006                         game.mousecoordh=globalMouse.h;
1007                         game.mousecoordv=globalMouse.v;
1008                 }
1009
1010                 if((!mainmenu)&&(gMouseGrabbed))
1011                 {
1012                         if(lastMouse.h>gMidPoint.h+100||lastMouse.h<gMidPoint.h-100||lastMouse.v>gMidPoint.v+100||lastMouse.v<gMidPoint.v-100){
1013                                 pos.x = gMidPoint.h;
1014                                 pos.y = gMidPoint.v;
1015                                 ClientToScreen(g_windowHandle, &pos);
1016                                 //SetCursorPos( gMidPoint.h,gMidPoint.v);
1017                                 SetCursorPos(pos.x, pos.y);
1018                                 lastMouse = gMidPoint;
1019                         }
1020                 }
1021         }
1022 }
1023
1024
1025
1026 // --------------------------------------------------------------------------
1027
1028 void DoKey (SInt8 theKey, SInt8 theCode)
1029 {
1030         // do nothing
1031 }
1032
1033 // --------------------------------------------------------------------------
1034
1035
1036
1037 void DoFrameRate (int update)
1038 {       
1039         static long frames = 0;
1040
1041         static AbsoluteTime time = {0,0};
1042         static AbsoluteTime frametime = {0,0};
1043         AbsoluteTime currTime = UpTime ();
1044         double deltaTime = (float) AbsoluteDeltaToDuration (currTime, frametime);
1045
1046         if (0 > deltaTime)      // if negative microseconds
1047                 deltaTime /= -1000000.0;
1048         else                            // else milliseconds
1049                 deltaTime /= 1000.0;
1050
1051         multiplier=deltaTime;
1052         if(multiplier<.001)multiplier=.001;
1053         if(multiplier>10)multiplier=10;
1054         if(update)frametime = currTime; // reset for next time interval
1055
1056         deltaTime = (float) AbsoluteDeltaToDuration (currTime, time);
1057
1058         if (0 > deltaTime)      // if negative microseconds
1059                 deltaTime /= -1000000.0;
1060         else                            // else milliseconds
1061                 deltaTime /= 1000.0;
1062         frames++;
1063         if (0.001 <= deltaTime) // has update interval passed
1064         {
1065                 if(update){
1066                         time = currTime;        // reset for next time interval
1067                         frames = 0;
1068                 }
1069         }
1070 }
1071
1072
1073 void DoUpdate (Game & game)
1074 {
1075         static float sps=200;
1076         static int count;
1077         static float oldmult;
1078
1079         DoFrameRate(1);
1080         if(multiplier>.6)multiplier=.6;
1081
1082         game.fps=1/multiplier;
1083
1084         count = multiplier*sps;
1085         if(count<2)count=2;
1086         //if(count>10)count=10;
1087
1088         realmultiplier=multiplier;
1089         multiplier*=gamespeed;
1090         if(difficulty==1)multiplier*=.9;
1091         if(difficulty==0)multiplier*=.8;
1092
1093         if(game.loading==4)multiplier*=.00001;
1094         //multiplier*.9;
1095         if(slomo&&!mainmenu)multiplier*=slomospeed;
1096         //if(freeze)multiplier*=0.00001;
1097         oldmult=multiplier;
1098         multiplier/=(float)count;
1099
1100         DoMouse(game);
1101
1102         game.TickOnce();
1103
1104         for(int i=0;i<count;i++)
1105         {
1106                 game.Tick();
1107         }
1108         multiplier=oldmult;
1109
1110         game.TickOnceAfter();
1111 /* - Debug code to test how many channels were active on average per frame
1112         static long frames = 0;
1113
1114         static AbsoluteTime start = {0,0};
1115         AbsoluteTime currTime = UpTime ();
1116         static int num_channels = 0;
1117         
1118         num_channels += FSOUND_GetChannelsPlaying();
1119         double deltaTime = (float) AbsoluteDeltaToDuration (currTime, start);
1120
1121         if (0 > deltaTime)      // if negative microseconds
1122                 deltaTime /= -1000000.0;
1123         else                            // else milliseconds
1124                 deltaTime /= 1000.0;
1125
1126         ++frames;
1127
1128         if (deltaTime >= 1)
1129         {
1130                 start = currTime;
1131                 float avg_channels = (float)num_channels / (float)frames;
1132
1133                 ofstream opstream("log.txt",ios::app); 
1134                 opstream << "Average frame count: ";
1135                 opstream << frames;
1136                 opstream << " frames - ";
1137                 opstream << avg_channels;
1138                 opstream << " per frame.\n";
1139                 opstream.close();
1140
1141                 frames = 0;
1142                 num_channels = 0;
1143         }
1144 */
1145         DrawGL (game);
1146 }
1147
1148 // --------------------------------------------------------------------------
1149
1150
1151 void CleanUp (void)
1152 {
1153         LOGFUNC;
1154
1155 //      game.Dispose();
1156
1157         ilShutDown();
1158
1159 #if USE_SDL
1160     SDL_Quit();
1161
1162 #elif (defined WIN32)
1163         if (hRC)
1164         {
1165                 wglMakeCurrent( NULL, NULL);
1166                 wglDeleteContext( hRC);
1167                 hRC = NULL;
1168         }
1169
1170         if (hDC)
1171         {
1172                 ReleaseDC( g_windowHandle, hDC);
1173                 hDC = NULL;
1174         }
1175
1176         if (g_windowHandle)
1177         {
1178                 ShowWindow( g_windowHandle, SW_HIDE );
1179                 DestroyWindow( g_windowHandle);
1180                 g_windowHandle = NULL;
1181         }
1182
1183         ShutdownDSp ();
1184         ClipCursor(NULL);
1185 #endif
1186 }
1187
1188 // --------------------------------------------------------------------------
1189
1190 static bool g_focused = true;
1191
1192
1193 static bool IsFocused()
1194 {
1195 #ifdef WIN32
1196         if (!g_focused)
1197                 return false;
1198
1199         if (GetActiveWindow() != g_windowHandle)
1200                 return false;
1201
1202         if (IsIconic( g_windowHandle))
1203                 return false;
1204 #endif
1205
1206         return true;
1207 }
1208
1209 int main (void)
1210 {
1211         LOGFUNC;
1212
1213 #ifndef WIN32  // this is in WinMain, too.
1214         logger.start(true);
1215         memset( &g_theKeys, 0, sizeof( KeyMap));
1216 #endif
1217
1218     initSDLKeyTable();
1219
1220         try
1221         {
1222                 bool regnow = false;
1223                 {
1224                         Game game;
1225                         pgame = &game;
1226
1227                         //ofstream os("error.txt");
1228                         //os.close();
1229                         //ofstream os("log.txt");
1230                         //os.close();
1231
1232                         SetUp (game);
1233
1234                         while (!gDone&&!game.quit&&(!game.tryquit||!game.registered))
1235                         {
1236                                 if (IsFocused())
1237                                 {
1238                                         gameFocused = true;
1239
1240                                         // check windows messages
1241                     #if USE_SDL
1242                                         SDL_Event e;
1243                                         // message pump
1244                                         while( SDL_PollEvent( &e ) )
1245                                         {
1246                                                 if( e.type == SDL_QUIT )
1247                                                 {
1248                                                         gDone=true;
1249                                                         break;
1250                                                 }
1251                         sdlEventProc(e);
1252                                         }
1253                     #elif (defined WIN32)
1254                                         MSG msg;
1255                                         // message pump
1256                                         while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE | PM_NOYIELD ) )
1257                                         {
1258                                                 if( msg.message == WM_QUIT )
1259                                                 {
1260                                                         gDone=true;
1261                                                         break;
1262                                                 }
1263                                                 else
1264                                                 {
1265                                                         TranslateMessage( &msg );
1266                                                         DispatchMessage( &msg );
1267                                                 }
1268                                         }
1269                     #endif
1270
1271                                         // game
1272                                         DoUpdate(game);
1273                                 }
1274                                 else
1275                                 {
1276                                         if (gameFocused)
1277                                         {
1278                                                 // allow game chance to pause
1279                                                 gameFocused = false;
1280                                                 DoUpdate(game);
1281                                         }
1282
1283                                         // game is not in focus, give CPU time to other apps by waiting for messages instead of 'peeking'
1284                     #ifdef WIN32
1285                                         MSG msg;
1286                                         BOOL bRet;
1287                                         //if (GetMessage( &msg, g_windowHandle, 0, 0 ))
1288                                         /*if (GetMessage( &msg, NULL, 0, 0 ))
1289                                         {
1290                                                 TranslateMessage(&msg);
1291                                                 DispatchMessage(&msg);
1292                                         }*/
1293                                         if ( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
1294                                         { 
1295                                                 if (bRet <= 0)
1296                                                 {
1297                                                         // handle the error and possibly exit
1298                                                         gDone=true;
1299                                                 }
1300                                                 else
1301                                                 {
1302                                                         TranslateMessage(&msg); 
1303                                                         DispatchMessage(&msg); 
1304                                                 }
1305                                         }
1306                     #else
1307                     STUBBED("give up CPU but sniff the event queue");
1308                     #endif
1309                                 }
1310                         }
1311
1312                         regnow = game.registernow;
1313                 }
1314                 pgame = 0;
1315
1316                 CleanUp ();
1317 //              if(game.registernow){
1318                 if(regnow)
1319                 {
1320             #ifndef WIN32
1321             STUBBED("launch a web browser");
1322             #else
1323                         char url[100];
1324                         sprintf(url,"http://www.wolfire.com/registerpc.html");
1325                         //                      LaunchURL(url);
1326                         ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);
1327             #endif
1328                 }
1329                 return 0;
1330         }
1331         catch (const std::exception& error)
1332         {
1333                 CleanUp();
1334
1335                 std::string e = "Caught exception: ";
1336                 e += error.what();
1337
1338                 LOG(e, Logger::LOG_ERR);
1339
1340                 MessageBox(g_windowHandle, error.what(), "ERROR", MB_OK | MB_ICONEXCLAMATION);
1341         }
1342
1343         CleanUp();
1344
1345         return -1;
1346 }
1347
1348
1349
1350         // --------------------------------------------------------------------------
1351
1352 #ifdef WIN32
1353 #define MAX_WINKEYS 256
1354         static unsigned short KeyTable[MAX_WINKEYS]=
1355         {
1356                 0xffff,  // (0)
1357                         MAC_MOUSEBUTTON1,  // VK_LBUTTON        (1)
1358                         MAC_MOUSEBUTTON2,  // VK_RBUTTON        (2)
1359                         0xffff,  // VK_CANCEL   (3)
1360                         0xffff,  // VK_MBUTTON  (4)
1361                         0xffff,  // (5)
1362                         0xffff,  // (6)
1363                         0xffff,  // (7)
1364                         MAC_DELETE_KEY,  // VK_BACK     (8)
1365                         MAC_TAB_KEY,  // VK_TAB (9)
1366                         0xffff,  // (10)
1367                         0xffff,  // (11)
1368                         0xffff,  // VK_CLEAR    (12)
1369                         MAC_RETURN_KEY,  // VK_RETURN   (13)
1370                         0xffff,  // (14)
1371                         0xffff,  // (15)
1372                         MAC_SHIFT_KEY,  // VK_SHIFT     (16)
1373                         MAC_CONTROL_KEY,  // VK_CONTROL (17)
1374                         MAC_OPTION_KEY,  // VK_MENU     (18)
1375                         0xffff,  // VK_PAUSE    (19)
1376                         MAC_CAPS_LOCK_KEY,  // #define VK_CAPITAL       (20)
1377                         0xffff,  // (21)
1378                         0xffff,  // (22)
1379                         0xffff,  // (23)
1380                         0xffff,  // (24)
1381                         0xffff,  // (25)
1382                         0xffff,  // (26)
1383                         MAC_ESCAPE_KEY,  // VK_ESCAPE   (27)
1384                         0xffff,  // (28)
1385                         0xffff,  // (29)
1386                         0xffff,  // (30)
1387                         0xffff,  // (31)
1388                         MAC_SPACE_KEY,  // VK_SPACE     (32)
1389                         MAC_PAGE_UP_KEY,  // VK_PRIOR   (33)
1390                         MAC_PAGE_DOWN_KEY,  // VK_NEXT  (34)
1391                         MAC_END_KEY,  // VK_END (35)
1392                         MAC_HOME_KEY,  // VK_HOME       (36)
1393                         MAC_ARROW_LEFT_KEY,  // VK_LEFT (37)
1394                         MAC_ARROW_UP_KEY,  // VK_UP     (38)
1395                         MAC_ARROW_RIGHT_KEY,  // VK_RIGHT       (39)
1396                         MAC_ARROW_DOWN_KEY,  // VK_DOWN (40)
1397                         0xffff,  // VK_SELECT   (41)
1398                         0xffff,  // VK_PRINT    (42)
1399                         0xffff,  // VK_EXECUTE  (43)
1400                         0xffff,  // VK_SNAPSHOT (44)
1401                         MAC_INSERT_KEY,  // VK_INSERT   (45)
1402                         MAC_DEL_KEY,  // VK_DELETE      (46)
1403                         0xffff,  // VK_HELP     (47)
1404                         MAC_0_KEY,  // VK_0     (48)
1405                         MAC_1_KEY,  // VK_1     (49)
1406                         MAC_2_KEY,  // VK_2     (50)
1407                         MAC_3_KEY,  // VK_3     (51)
1408                         MAC_4_KEY,  // VK_4     (52)
1409                         MAC_5_KEY,  // VK_5     (53)
1410                         MAC_6_KEY,  // VK_6     (54)
1411                         MAC_7_KEY,  // VK_7     (55)
1412                         MAC_8_KEY,  // VK_8     (56)
1413                         MAC_9_KEY,  // VK_9     (57)
1414                         0xffff,  // (58)
1415                         0xffff,  // (59)
1416                         0xffff,  // (60)
1417                         0xffff,  // (61)
1418                         0xffff,  // (62)
1419                         0xffff,  // (63)
1420                         0xffff,  // (64)
1421                         MAC_A_KEY,  // VK_A     (65)
1422                         MAC_B_KEY,  // VK_B     (66)
1423                         MAC_C_KEY,  // VK_C     (67)
1424                         MAC_D_KEY,  // VK_D     (68)
1425                         MAC_E_KEY,  // VK_E     (69)
1426                         MAC_F_KEY,  // VK_F     (70)
1427                         MAC_G_KEY,  // VK_G     (71)
1428                         MAC_H_KEY,  // VK_H     (72)
1429                         MAC_I_KEY,  // VK_I     (73)
1430                         MAC_J_KEY,  // VK_J     (74)
1431                         MAC_K_KEY,  // VK_K     (75)
1432                         MAC_L_KEY,  // VK_L     (76)
1433                         MAC_M_KEY,  // VK_M     (77)
1434                         MAC_N_KEY,  // VK_N     (78)
1435                         MAC_O_KEY,  // VK_O     (79)
1436                         MAC_P_KEY,  // VK_P     (80)
1437                         MAC_Q_KEY,  // VK_Q     (81)
1438                         MAC_R_KEY,  // VK_R     (82)
1439                         MAC_S_KEY,  // VK_S     (83)
1440                         MAC_T_KEY,  // VK_T     (84)
1441                         MAC_U_KEY,  // VK_U     (85)
1442                         MAC_V_KEY,  // VK_V     (86)
1443                         MAC_W_KEY,  // VK_W     (87)
1444                         MAC_X_KEY,  // VK_X     (88)
1445                         MAC_Y_KEY,  // VK_Y     (89)
1446                         MAC_Z_KEY,  // VK_Z     (90)
1447                         0xffff,  // (91)
1448                         0xffff,  // (92)
1449                         0xffff,  // (93)
1450                         0xffff,  // (94)
1451                         0xffff,  // (95)
1452                         MAC_NUMPAD_0_KEY,  // VK_NUMPAD0        (96)
1453                         MAC_NUMPAD_1_KEY,  // VK_NUMPAD1        (97)
1454                         MAC_NUMPAD_2_KEY,  // VK_NUMPAD2        (98)
1455                         MAC_NUMPAD_3_KEY,  // VK_NUMPAD3        (99)
1456                         MAC_NUMPAD_4_KEY,  // VK_NUMPAD4        (100)
1457                         MAC_NUMPAD_5_KEY,  // VK_NUMPAD5        (101)
1458                         MAC_NUMPAD_6_KEY,  // VK_NUMPAD6        (102)
1459                         MAC_NUMPAD_7_KEY,  // VK_NUMPAD7        (103)
1460                         MAC_NUMPAD_8_KEY,  // VK_NUMPAD8        (104)
1461                         MAC_NUMPAD_9_KEY,  // VK_NUMPAD9        (105)
1462                         MAC_NUMPAD_ASTERISK_KEY,  // VK_MULTIPLY        (106)
1463                         MAC_NUMPAD_PLUS_KEY,  // VK_ADD (107)
1464                         MAC_NUMPAD_ENTER_KEY,  // VK_SEPARATOR  (108)
1465                         MAC_NUMPAD_MINUS_KEY,  // VK_SUBTRACT   (109)
1466                         MAC_NUMPAD_PERIOD_KEY,  // VK_DECIMAL   (110)
1467                         MAC_NUMPAD_SLASH_KEY,  // VK_DIVIDE     (111)
1468                         MAC_F1_KEY,  // VK_F1   (112)
1469                         MAC_F2_KEY,  // VK_F2   (113)
1470                         MAC_F3_KEY,  // VK_F3   (114)
1471                         MAC_F4_KEY,  // VK_F4   (115)
1472                         MAC_F5_KEY,  // VK_F5   (116)
1473                         MAC_F6_KEY,  // VK_F6   (117)
1474                         MAC_F7_KEY,  // VK_F7   (118)
1475                         MAC_F8_KEY,  // VK_F8   (119)
1476                         MAC_F9_KEY,  // VK_F9   (120)
1477                         MAC_F10_KEY,  // VK_F10 (121)
1478                         MAC_F11_KEY,  // VK_F11 (122)
1479                         MAC_F12_KEY,  // VK_F12 (123)
1480                         0xffff,  // (124)
1481                         0xffff,  // (125)
1482                         0xffff,  // (126)
1483                         0xffff,  // (127)
1484                         0xffff,  // (128)
1485                         0xffff,  // (129)
1486                         0xffff,  // (130)
1487                         0xffff,  // (131)
1488                         0xffff,  // (132)
1489                         0xffff,  // (133)
1490                         0xffff,  // (134)
1491                         0xffff,  // (135)
1492                         0xffff,  // (136)
1493                         0xffff,  // (137)
1494                         0xffff,  // (138)
1495                         0xffff,  // (139)
1496                         0xffff,  // (130)
1497                         0xffff,  // (141)
1498                         0xffff,  // (142)
1499                         0xffff,  // (143)
1500                         0xffff,  // VK_NUMLOCK  (144)
1501                         0xffff,  // VK_SCROLL   (145)
1502                         0xffff,  // (146)
1503                         0xffff,  // (147)
1504                         0xffff,  // (148)
1505                         0xffff,  // (149)
1506                         0xffff,  // (150)
1507                         0xffff,  // (151)
1508                         0xffff,  // (152)
1509                         0xffff,  // (153)
1510                         0xffff,  // (154)
1511                         0xffff,  // (155)
1512                         0xffff,  // (156)
1513                         0xffff,  // (157)
1514                         0xffff,  // (158)
1515                         0xffff,  // (159)
1516                         MAC_SHIFT_KEY,  // VK_LSHIFT    (160)
1517                         MAC_SHIFT_KEY,  // VK_RSHIFT    (161)
1518                         MAC_CONTROL_KEY,  // VK_LCONTROL        (162)
1519                         MAC_CONTROL_KEY,  // VK_RCONTROL        (163)
1520                         MAC_OPTION_KEY,  // VK_LMENU    (164)
1521                         MAC_OPTION_KEY,  // VK_RMENU    (165)
1522                         0xffff,  // (166)
1523                         0xffff,  // (167)
1524                         0xffff,  // (168)
1525                         0xffff,  // (169)
1526                         0xffff,  // (170)
1527                         0xffff,  // (171)
1528                         0xffff,  // (172)
1529                         0xffff,  // (173)
1530                         0xffff,  // (174)
1531                         0xffff,  // (175)
1532                         0xffff,  // (176)
1533                         0xffff,  // (177)
1534                         0xffff,  // (178)
1535                         0xffff,  // (179)
1536                         0xffff,  // (180)
1537                         0xffff,  // (181)
1538                         0xffff,  // (182)
1539                         0xffff,  // (183)
1540                         0xffff,  // (184)
1541                         0xffff,  // (185)
1542                         MAC_SEMICOLON_KEY,  // (186)
1543                         MAC_PLUS_KEY,  // (187)
1544                         MAC_COMMA_KEY,  // (188)
1545                         MAC_MINUS_KEY,  // (189)
1546                         MAC_PERIOD_KEY,  // (190)
1547                         MAC_SLASH_KEY,  // (191)
1548                         MAC_TILDE_KEY,  // (192)
1549                         0xffff,  // (193)
1550                         0xffff,  // (194)
1551                         0xffff,  // (195)
1552                         0xffff,  // (196)
1553                         0xffff,  // (197)
1554                         0xffff,  // (198)
1555                         0xffff,  // (199)
1556                         0xffff,  // (200)
1557                         0xffff,  // (201)
1558                         0xffff,  // (202)
1559                         0xffff,  // (203)
1560                         0xffff,  // (204)
1561                         0xffff,  // (205)
1562                         0xffff,  // (206)
1563                         0xffff,  // (207)
1564                         0xffff,  // (208)
1565                         0xffff,  // (209)
1566                         0xffff,  // (210)
1567                         0xffff,  // (211)
1568                         0xffff,  // (212)
1569                         0xffff,  // (213)
1570                         0xffff,  // (214)
1571                         0xffff,  // (215)
1572                         0xffff,  // (216)
1573                         0xffff,  // (217)
1574                         0xffff,  // (218)
1575                         MAC_LEFTBRACKET_KEY,  // (219)
1576                         MAC_BACKSLASH_KEY,  // (220)
1577                         MAC_RIGHTBRACKET_KEY,  // (221)
1578                         MAC_APOSTROPHE_KEY,  // (222)
1579                         0xffff,  // (223)
1580                         0xffff,  // (224)
1581                         0xffff,  // (225)
1582                         0xffff,  // (226)
1583                         0xffff,  // (227)
1584                         0xffff,  // (228)
1585                         0xffff,  // (229)
1586                         0xffff,  // (230)
1587                         0xffff,  // (231)
1588                         0xffff,  // (232)
1589                         0xffff,  // (233)
1590                         0xffff,  // (234)
1591                         0xffff,  // (235)
1592                         0xffff,  // (236)
1593                         0xffff,  // (237)
1594                         0xffff,  // (238)
1595                         0xffff,  // (239)
1596                         0xffff,  // (240)
1597                         0xffff,  // (241)
1598                         0xffff,  // (242)
1599                         0xffff,  // (243)
1600                         0xffff,  // (244)
1601                         0xffff,  // (245)
1602                         0xffff,  // (246)
1603                         0xffff,  // (247)
1604                         0xffff,  // (248)
1605                         0xffff,  // (249)
1606                         0xffff,  // (250)
1607                         0xffff,  // (251)
1608                         0xffff,  // (252)
1609                         0xffff,  // (253)
1610                         0xffff,  // (254)
1611                         0xffff,  // (255)
1612         };
1613
1614         void ClipMouseToWindow(HWND window)
1615         {
1616                 RECT wRect;
1617
1618                 GetClientRect(window, &wRect);
1619
1620                 ClientToScreen(window, (LPPOINT)&wRect.left);
1621                 ClientToScreen(window, (LPPOINT)&wRect.right);
1622
1623                 ClipCursor(&wRect);
1624
1625                 return;
1626         }
1627
1628         LRESULT FAR PASCAL AppWndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
1629         {
1630                 /* this is where we receive all messages concerning this window
1631                 * we can either process a message or pass it on to the default
1632                 * message handler of windows */
1633
1634                 static PAINTSTRUCT ps;
1635
1636                 switch(msg)
1637                 {
1638                 case WM_ACTIVATE:       // Watch For Window Activate Message
1639                         {
1640                                 // Check Minimization State
1641                                 BOOL iconified = HIWORD(wParam) ? TRUE : FALSE;
1642
1643                                 if (LOWORD(wParam) == WA_INACTIVE)
1644                                 {
1645                                         ClipCursor(NULL);
1646
1647                                         if (fullscreen)
1648                                         {
1649                                                 if( !iconified )
1650                                                 {
1651                                                         // Minimize window
1652                                                         CloseWindow( hWnd );
1653
1654                                                         // The window is now iconified
1655                                                         iconified = GL_TRUE;
1656                                                 }
1657                                         }
1658
1659                                         ShutdownDSp();
1660
1661                                         g_focused=false;                                // Program Is Active
1662                                 }
1663                                 else
1664                                 {
1665                                         SetupDSpFullScreen();
1666
1667                                         if( iconified )
1668                                         {
1669                                                 // Minimize window
1670                                                 OpenIcon( hWnd );
1671
1672                                                 // The window is now iconified
1673                                                 iconified = GL_FALSE;
1674
1675                                                 // Activate window
1676                                                 ShowWindow( hWnd, SW_SHOW );
1677                                                 SetForegroundWindow( hWnd );
1678                                                 SetFocus( hWnd );
1679                                         }
1680
1681                                         ClipMouseToWindow(hWnd);
1682                                         g_focused=true;                 // Program Is No Longer Active
1683                                 }
1684
1685                                 return 0;                                               // Return To The Message Loop
1686                         }
1687
1688                 case WM_KEYDOWN:
1689                 case WM_SYSKEYDOWN:
1690                         {
1691                                 // check for Alt-F4 (exit hotkey)
1692                                 if (wParam == VK_F4)
1693                                 {
1694                                         if (GetKeyState( VK_MENU) & 0x8080)
1695                                         {
1696                                                 gDone = true;
1697                                                 break;
1698                                         }
1699                                 }
1700                                 if (wParam < MAX_WINKEYS)
1701                                 {
1702                                         if (KeyTable[wParam] != 0xffff)
1703                                                 SetKey( KeyTable[wParam]);
1704                                 }
1705                                 return (0);
1706                         }
1707
1708                 case WM_KEYUP:
1709                 case WM_SYSKEYUP:
1710                         {
1711                                 if (wParam < MAX_WINKEYS)
1712                                         if (KeyTable[wParam] != 0xffff)
1713                                                 ClearKey( KeyTable[wParam]);
1714                                 return (0);
1715                         }
1716
1717                 case WM_CHAR:
1718                 case WM_DEADCHAR:
1719                 case WM_SYSCHAR:
1720                 case WM_SYSDEADCHAR:
1721                         return (0);
1722
1723                 case WM_NCLBUTTONDOWN:
1724                 case WM_LBUTTONDOWN:
1725                         {
1726                                 g_button = true;
1727                                 buttons[ 0] = true;
1728                         }
1729                         return (0);
1730
1731                 case WM_NCRBUTTONDOWN:
1732                 case WM_RBUTTONDOWN:
1733                         {
1734                                 buttons[ 1] = true;
1735                         }
1736                         return (0);
1737
1738                 case WM_NCMBUTTONDOWN:
1739                 case WM_MBUTTONDOWN:
1740                         {
1741                                 buttons[ 2] = true;
1742                         }
1743                         return (0);
1744
1745                 case WM_NCLBUTTONUP:
1746                 case WM_LBUTTONUP:
1747                         {
1748                                 g_button = false;
1749                                 buttons[ 0] = false;
1750                         }
1751                         return (0);
1752
1753                 case WM_NCRBUTTONUP:
1754                 case WM_RBUTTONUP:
1755                         {
1756                                 buttons[ 1] = false;
1757                         }
1758                         return (0);
1759
1760                 case WM_NCMBUTTONUP:
1761                 case WM_MBUTTONUP:
1762                         {
1763                                 buttons[ 2] = false;
1764                         }
1765                         return (0);
1766
1767                 case WM_NCLBUTTONDBLCLK:
1768                 case WM_NCRBUTTONDBLCLK:
1769                 case WM_NCMBUTTONDBLCLK:
1770                 case WM_LBUTTONDBLCLK:
1771                         return (0);
1772                 case WM_RBUTTONDBLCLK:
1773                 case WM_MBUTTONDBLCLK:
1774                         return (0);
1775
1776                 case WM_NCMOUSEMOVE:
1777                 case WM_MOUSEMOVE:
1778                         /*                      ((WindowInfo *)g_lastWindow->GetInfo())->m_mouseX = (signed short)(lParam & 0xffff);
1779                         ((WindowInfo *)g_lastWindow->GetInfo())->m_mouseY = (signed short)(lParam >> 16);
1780                         if (g_lastWindow->m_mouseCallbacksEnabled) g_lastWindow->MouseMoveCallback();
1781                         *///                    goto winmessage;
1782                         return (0);
1783
1784                 case WM_SYSCOMMAND:                                             // Intercept System Commands
1785                         {
1786                                 switch (wParam)                                         // Check System Calls
1787                                 {
1788                                 case SC_SCREENSAVE:                             // Screensaver Trying To Start?
1789                                 case SC_MONITORPOWER:                   // Monitor Trying To Enter Powersave?
1790                                         return 0;                                       // Prevent From Happening
1791
1792                                         // User trying to access application menu using ALT?
1793                                 case SC_KEYMENU:
1794                                         return 0;
1795                                 }
1796                         }
1797                         break;
1798
1799                 case WM_MOVE:
1800 //                      {
1801 //                              ReleaseCapture();
1802 //                              ClipMouseToWindow(hWnd);
1803 //                      }
1804                         break;
1805
1806                 case WM_SIZE:
1807                         break;
1808
1809                 case WM_CLOSE:
1810                         {
1811                                 //gDone =  true;
1812                                 //game.tryquit=1;
1813                         }
1814                         //return (0);
1815
1816                 case WM_DESTROY:
1817                         {
1818                                 //ClipCursor(NULL);
1819                                 PostQuitMessage(0);  /* Terminate Application */
1820                         }
1821                         return (0);
1822
1823                 case WM_ERASEBKGND:
1824                         break;
1825
1826                 case WM_PAINT:
1827 //                      BeginPaint( g_windowHandle,&ps);
1828 //                      EndPaint( g_windowHandle,&ps);
1829                         break;
1830
1831                 default:
1832                         break;
1833                 }
1834
1835                 /* We processed the message and there
1836                 * is no processing by Windows necessary */
1837
1838                 /* We didn't process the message so let Windows do it */
1839                 return DefWindowProc(hWnd,msg,wParam,lParam);
1840         }
1841
1842
1843         static BOOL RegisterWindowClasses(HINSTANCE hFirstInstance)
1844         {
1845                 WNDCLASSEX wc;
1846                 memset( &wc, 0, sizeof( wc));
1847
1848                 /* Register the window class. */
1849                 wc.cbSize = sizeof(wc);
1850 #undef style
1851                 wc.style = (CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_OWNDC);  /* Combination of Class Styles */
1852                 wc.lpfnWndProc = AppWndProc;       /* Adress of Window Procedure */
1853                 wc.cbClsExtra = 0;                 /* Extra Bytes allocated for this Class */
1854                 wc.cbWndExtra = 0;                 /* Extra Bytes allocated for each Window */
1855                 wc.hInstance = hFirstInstance;     /* Handle of program instance */
1856                 wc.hIcon = LoadIcon( hFirstInstance, MAKEINTRESOURCE(IDI_LUGARU) );
1857                 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
1858                 wc.hbrBackground = NULL;
1859                 wc.lpszMenuName  = NULL;
1860                 wc.lpszClassName = g_wndClassName; /* Name of the Window Class */
1861                 wc.hIconSm = LoadIcon( hFirstInstance, MAKEINTRESOURCE(IDI_LUGARU) );
1862
1863                 if (!RegisterClassEx(&wc)) return FALSE;  /* Register Class failed */
1864
1865                 return TRUE;
1866         }
1867 #endif
1868
1869         int resolutionID(int width, int height)
1870         {
1871                 int whichres;
1872                 whichres=-1;
1873                 if(width==640 && height==480)whichres=0;
1874                 if(width==800 && height==600)whichres=1;
1875                 if(width==1024 && height==768)whichres=2;
1876                 if(width==1280 && height==1024)whichres=3;
1877                 if(width==1600 && height==1200)whichres=4;
1878                 if(width==840 && height==524)whichres=5;
1879                 if(width==1024 && height==640)whichres=6;
1880                 if(width==1344 && height==840)whichres=7;
1881
1882                 return whichres;
1883         }
1884
1885         int closestResolution(int width, int height)
1886         {
1887                 int whichres;
1888                 whichres=-1;
1889                 if(width>=640 && height>=480)whichres=0;
1890                 if(width>=800 && height>=600)whichres=1;
1891                 if(width>=1024 && height>=768)whichres=2;
1892                 if(width>=1280 && height>=1024)whichres=3;
1893                 if(width>=1600 && height>=1200)whichres=4;
1894                 if(width==840 && height==524)whichres=5;
1895                 if(width==1024 && height==640)whichres=6;
1896                 if(width==1344 && height==840)whichres=7;
1897
1898                 return whichres;
1899         }
1900
1901         bool selectDetail(int & width, int & height, int & bpp, int & detail)
1902         {
1903                 bool res = true;
1904                 int whichres = closestResolution(width, height);
1905
1906                 while (true)
1907                 {
1908                         if(whichres<=0 || whichres>7){
1909                                 whichres = 0;
1910                                 width=640;
1911                                 height=480;
1912                         }
1913                         if(whichres==1){
1914                                 width=800;
1915                                 height=600;
1916                         }
1917                         if(whichres==2){
1918                                 width=1024;
1919                                 height=768;
1920                         }
1921                         if(whichres==3){
1922                                 width=1280;
1923                                 height=1024;
1924                         }
1925                         if(whichres==4){
1926                                 width=1600;
1927                                 height=1200;
1928                         }
1929                         if(whichres==5){
1930                                 width=840;
1931                                 height=524;
1932                         }
1933                         if(whichres==6){
1934                                 width=1024;
1935                                 height=640;
1936                         }
1937                         if(whichres==7){
1938                                 width=1344;
1939                                 height=840;
1940                         }
1941
1942                         if ((detail != 0) && (resolutionDepths[whichres][1] != 0))
1943                         {
1944                                 break;
1945                         }
1946                         else if ((detail == 0) && (resolutionDepths[whichres][0] != 0))
1947                         {
1948                                 break;
1949                         }
1950                         else if ((detail != 0) && (resolutionDepths[whichres][0] != 0))
1951                         {
1952                                 res = false;
1953                                 detail = 0;
1954                                 break;
1955                         }
1956                         else if (0 == whichres)
1957                         {
1958                                 break;
1959                         }
1960
1961                         --whichres;
1962                 }
1963
1964                 bpp = resolutionDepths[whichres][(detail != 0)];
1965
1966                 return res;
1967         }
1968
1969     #ifdef WIN32
1970         int __stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
1971         {
1972                 int argc = 0;
1973                 LPWSTR * cl = CommandLineToArgvW(GetCommandLineW(), &argc);
1974                 if (argc > 1)
1975                 {
1976                         if (0 == _wcsicmp(cl[1], L"-windowed"))
1977                         {
1978                                 fullscreen = false;
1979                         }
1980                 }
1981
1982                 logger.start(true);
1983
1984                 memset( &g_theKeys, 0, sizeof( KeyMap));
1985
1986                 unsigned int i = 0;
1987                 DEVMODE mode;
1988                 memset(&mode, 0, sizeof(mode));
1989                 mode.dmSize = sizeof(mode);
1990                 while (EnumDisplaySettings(NULL, i++, &mode))
1991                 {
1992                         if (mode.dmBitsPerPel < 16)
1993                         {
1994                                 continue;
1995                         }
1996
1997                         int res = resolutionID(mode.dmPelsWidth, mode.dmPelsHeight);
1998
1999                         if (res > -1 && res < 8)
2000                         {
2001                                 if (DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettings(&mode, CDS_TEST))
2002                                 {
2003                                         continue;
2004                                 }
2005
2006                                 switch(mode.dmBitsPerPel)
2007                                 {
2008                                 case 32:
2009                                 case 24:
2010                                         resolutionDepths[res][1] = mode.dmBitsPerPel;
2011                                         break;
2012                                 case 16:
2013                                         resolutionDepths[res][0] = mode.dmBitsPerPel;
2014                                         break;
2015                                 }
2016                         }
2017                 }
2018
2019                 /* if there is no Instance of our program in memory then register the window class */
2020                 if (hPrevInstance == NULL && !RegisterWindowClasses(hInstance))
2021                         return FALSE;  /* registration failed! */
2022
2023                 g_appInstance=hInstance;
2024
2025                 main();
2026
2027                 UnregisterClass( g_wndClassName, hInstance);
2028
2029                 return TRUE;
2030
2031         }
2032     #endif
2033
2034         extern int channels[100];
2035         extern FSOUND_SAMPLE * samp[100];
2036         extern FSOUND_STREAM * strm[10];
2037
2038         extern "C" void PlaySoundEx(int chan, FSOUND_SAMPLE *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
2039         {
2040                 const FSOUND_SAMPLE * currSample = FSOUND_GetCurrentSample(channels[chan]);
2041                 if (currSample && currSample == samp[chan])
2042                 {
2043                         if (FSOUND_GetPaused(channels[chan]))
2044                         {
2045                                 FSOUND_StopSound(channels[chan]);
2046                                 channels[chan] = FSOUND_FREE;
2047                         }
2048                         else if (FSOUND_IsPlaying(channels[chan]))
2049                         {
2050                                 int loop_mode = FSOUND_GetLoopMode(channels[chan]);
2051                                 if (loop_mode & FSOUND_LOOP_OFF)
2052                                 {
2053                                         channels[chan] = FSOUND_FREE;
2054                                 }
2055                         }
2056                 }
2057                 else
2058                 {
2059                         channels[chan] = FSOUND_FREE;
2060                 }
2061
2062                 channels[chan] = FSOUND_PlaySoundEx(channels[chan], sptr, dsp, startpaused);
2063                 if (channels[chan] < 0)
2064                 {
2065                         channels[chan] = FSOUND_PlaySoundEx(FSOUND_FREE, sptr, dsp, startpaused);
2066                 }
2067         }
2068
2069         extern "C" void PlayStreamEx(int chan, FSOUND_STREAM *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
2070         {
2071                 const FSOUND_SAMPLE * currSample = FSOUND_GetCurrentSample(channels[chan]);
2072                 if (currSample && currSample == FSOUND_Stream_GetSample(sptr))
2073                 {
2074                                 FSOUND_StopSound(channels[chan]);
2075                                 FSOUND_Stream_Stop(sptr);
2076                 }
2077                 else
2078                 {
2079                         FSOUND_Stream_Stop(sptr);
2080                         channels[chan] = FSOUND_FREE;
2081                 }
2082
2083                 channels[chan] = FSOUND_Stream_PlayEx(channels[chan], sptr, dsp, startpaused);
2084                 if (channels[chan] < 0)
2085                 {
2086                         channels[chan] = FSOUND_Stream_PlayEx(FSOUND_FREE, sptr, dsp, startpaused);
2087                 }
2088         }
2089
2090         bool LoadImage(const char * fname, TGAImageRec & tex)
2091         {
2092                 bool res = true;
2093
2094                 if ( tex.data == NULL )
2095                 {
2096                         return false;
2097                 }
2098
2099                 ILstring f = strdup(ConvertFileName(fname));
2100                 if (!f)
2101                 {
2102                         return false;
2103                 }
2104
2105                 ILuint iid=0;
2106                 ilGenImages(1, &iid);
2107                 ilBindImage(iid);
2108                 if (ilLoadImage(f))
2109                 {
2110                         //iluFlipImage();
2111                         tex.sizeX = ilGetInteger(IL_IMAGE_WIDTH);
2112                         tex.sizeY = ilGetInteger(IL_IMAGE_HEIGHT);
2113                         tex.bpp = ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);
2114                         ILuint Bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL),
2115                                 imageSize = tex.sizeX * tex.sizeY * Bpp;
2116                         ILubyte *Data = ilGetData();
2117                         memcpy(tex.data, Data, imageSize);
2118
2119                         // Truvision Targa files are stored as BGR colors
2120                         // We want RGB so Blue and Red bytes are switched
2121                         if (IL_TGA == ilGetInteger(IL_IMAGE_FORMAT))
2122                         {
2123                                 // Loop Through The Image Data
2124                                 for (GLuint i = 0; i < int(imageSize); i += Bpp)
2125                                 {
2126                                         // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
2127                                         GLbyte temp;                                            // Temporary Variable
2128                                         temp = tex.data[i];                                     // Temporarily Store The Value At Image Data 'i'
2129                                         tex.data[i] = tex.data[i + 2];          // Set The 1st Byte To The Value Of The 3rd Byte
2130                                         tex.data[i + 2] = temp;                         // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
2131                                 }
2132                         }
2133                 }
2134                 else
2135                 {
2136                         res = false;
2137                 }
2138                 ilDeleteImages(1, &iid);
2139 /*
2140                 if (tid)
2141                 {
2142                         GLuint texid = ilutGLLoadImage(f);
2143                         *tid = texid;
2144                 }
2145                 else if (mip)
2146                 {
2147                         ilutGLBuildMipmaps()
2148                 }
2149                 else
2150                 {
2151                         ilutGLTexImage(0);
2152                 }
2153 */
2154                 free(f);
2155
2156                 return res;
2157         }
2158
2159         void ScreenShot(const char * fname)
2160         {
2161                 ILstring f = strdup(fname);
2162                 if (!f)
2163                 {
2164                         return;
2165                 }
2166
2167                 ILuint iid;
2168                 ilGenImages(1, &iid);
2169                 ilBindImage(iid);
2170                 if (ilutGLScreen())
2171                 {
2172                         ilSaveImage(f);
2173                 }
2174                 ilDeleteImages(1, &iid);
2175
2176                 free(f);
2177         }
2178
2179