5 #pragma comment(lib, "opengl32.lib")
6 #pragma comment(lib, "glu32.lib")
7 #pragma comment(lib, "glaux.lib")
9 extern bool buttons[3];
10 extern float multiplier;
11 extern float screenwidth,screenheight;
13 extern float realmultiplier;
15 extern bool ismotionblur;
16 extern float usermousesensitivity;
18 extern bool floatjump;
19 extern bool cellophane;
21 //extern int terraindetail;
22 //extern int texdetail;
23 extern float terraindetail;
24 extern float texdetail;
25 extern int bloodtoggle;
27 extern bool autoslomo;
29 extern bool musictoggle;
30 extern bool trilinear;
31 extern float gamespeed;
32 extern int difficulty;
33 extern bool damageeffects;
34 extern int numplayers;
36 extern bool invertmouse;
37 extern bool texttoggle;
38 extern bool ambientsound;
39 extern bool mousejump;
41 extern Person player[maxplayers];
43 extern bool stillloading;
44 extern bool showpoints;
45 extern bool alwaysblur;
46 extern bool immediate;
47 extern bool velocityblur;
48 extern bool debugmode;
50 /*extern*/ bool gameFocused;
51 extern int kBitsPerPixel;
52 extern float slomospeed;
53 extern float slomofreq;
54 extern float oldgamespeed;
67 #include "res/resource.h"
72 unsigned int resolutionDepths[8][2] = {0};
74 bool selectDetail(int & width, int & height, int & bpp, int & detail);
75 int closestResolution(int width, int height);
76 int resolutionID(int width, int height);
78 void ReportError (char * strError);
80 void SetupDSpFullScreen();
83 void DrawGL(Game & game);
85 void CreateGLWindow (void);
86 Boolean SetUp (Game & game);
87 void DoKey (SInt8 theKey, SInt8 theCode);
88 void DoUpdate (Game & game);
94 // statics/globals (internal only) ------------------------------------------
98 static const char g_wndClassName[]={ "LUGARUWINDOWCLASS" };
100 static HINSTANCE g_appInstance;
101 static HWND g_windowHandle;
103 static bool g_button, fullscreen = true;
114 kForegroundSleep = 10,
115 kBackgroundSleep = 10000
122 const RGBColor rgbBlack = { 0x0000, 0x0000, 0x0000 };
126 char gcstrMode [256] = "";
128 UInt32 gSleepTime = kForegroundSleep;
129 Boolean gDone = false, gfFrontProcess = true;
133 // --------------------------------------------------------------------------
135 void ReportError (char * strError)
137 throw std::exception( strError);
139 /* char errMsgCStr [256];
142 sprintf (errMsgCStr, "%s", strError);
144 // out as debug string
145 CToPStr (strErr, errMsgCStr);
150 void SetupDSpFullScreen ()
156 DEVMODE dmScreenSettings;
157 memset( &dmScreenSettings, 0, sizeof( dmScreenSettings));
158 dmScreenSettings.dmSize = sizeof( dmScreenSettings);
159 dmScreenSettings.dmPelsWidth = kContextWidth;
160 dmScreenSettings.dmPelsHeight = kContextHeight;
161 dmScreenSettings.dmBitsPerPel = kBitsPerPixel;
162 dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
165 if (ChangeDisplaySettings( &dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
167 ReportError( "Could not set display mode");
182 ChangeDisplaySettings( NULL, 0);
189 //-----------------------------------------------------------------------------------------------------------------------
193 void DrawGL (Game & game)
203 // --------------------------------------------------------------------------
205 static Point gMidPoint;
207 Boolean SetUp (Game & game)
213 randSeed = UpTime().lo;
216 // ifstream ipstream(":Data:config.txt", std::ios::in /*| std::ios::nocreate*/);
217 ifstream ipstream("./Data/config.txt", std::ios::in /*| std::ios::nocreate*/);
220 usermousesensitivity=1;
249 game.crouchkey=MAC_SHIFT_KEY;
250 game.jumpkey=MAC_SPACE_KEY;
251 game.leftkey=MAC_A_KEY;
252 game.forwardkey=MAC_W_KEY;
253 game.backkey=MAC_S_KEY;
254 game.rightkey=MAC_D_KEY;
255 game.drawkey=MAC_E_KEY;
256 game.throwkey=MAC_Q_KEY;
257 game.attackkey=MAC_MOUSEBUTTON1;
258 game.chatkey=MAC_T_KEY;
264 selectDetail(kContextWidth, kContextHeight, kBitsPerPixel, detail);
267 //ofstream opstream(":Data:config.txt");
268 ofstream opstream("./Data/config.txt");
269 opstream << "Screenwidth:\n";
270 opstream << kContextWidth;
271 opstream << "\nScreenheight:\n";
272 opstream << kContextHeight;
273 opstream << "\nMouse sensitivity:\n";
274 opstream << usermousesensitivity;
275 opstream << "\nBlur(0,1):\n";
276 opstream << ismotionblur;
277 opstream << "\nOverall Detail(0,1,2) higher=better:\n";
279 opstream << "\nFloating jump:\n";
280 opstream << floatjump;
281 opstream << "\nMouse jump:\n";
282 opstream << mousejump;
283 opstream << "\nAmbient sound:\n";
284 opstream << ambientsound;
285 opstream << "\nBlood (0,1,2):\n";
286 opstream << bloodtoggle;
287 opstream << "\nAuto slomo:\n";
288 opstream << autoslomo;
289 opstream << "\nFoliage:\n";
291 opstream << "\nMusic:\n";
292 opstream << musictoggle;
293 opstream << "\nTrilinear:\n";
294 opstream << trilinear;
295 opstream << "\nDecals(shadows,blood puddles,etc):\n";
297 opstream << "\nInvert mouse:\n";
298 opstream << invertmouse;
299 opstream << "\nGamespeed:\n";
300 opstream << gamespeed;
301 opstream << "\nDifficulty(0,1,2) higher=harder:\n";
302 opstream << difficulty;
303 opstream << "\nDamage effects(blackout, doublevision):\n";
304 opstream << damageeffects;
305 opstream << "\nText:\n";
306 opstream << texttoggle;
307 opstream << "\nDebug:\n";
308 opstream << debugmode;
309 opstream << "\nVBL Sync:\n";
311 opstream << "\nShow Points:\n";
312 opstream << showpoints;
313 opstream << "\nAlways Blur:\n";
314 opstream << alwaysblur;
315 opstream << "\nImmediate mode (turn on on G5):\n";
316 opstream << immediate;
317 opstream << "\nVelocity blur:\n";
318 opstream << velocityblur;
319 opstream << "\nVolume:\n";
321 opstream << "\nForward key:\n";
322 opstream << KeyToChar(game.forwardkey);
323 opstream << "\nBack key:\n";
324 opstream << KeyToChar(game.backkey);
325 opstream << "\nLeft key:\n";
326 opstream << KeyToChar(game.leftkey);
327 opstream << "\nRight key:\n";
328 opstream << KeyToChar(game.rightkey);
329 opstream << "\nJump key:\n";
330 opstream << KeyToChar(game.jumpkey);
331 opstream << "\nCrouch key:\n";
332 opstream << KeyToChar(game.crouchkey);
333 opstream << "\nDraw key:\n";
334 opstream << KeyToChar(game.drawkey);
335 opstream << "\nThrow key:\n";
336 opstream << KeyToChar(game.throwkey);
337 opstream << "\nAttack key:\n";
338 opstream << KeyToChar(game.attackkey);
339 opstream << "\nChat key:\n";
340 opstream << KeyToChar(game.chatkey);
345 ipstream.ignore(256,'\n');
346 ipstream >> kContextWidth;
347 ipstream.ignore(256,'\n');
348 ipstream.ignore(256,'\n');
349 ipstream >> kContextHeight;
350 ipstream.ignore(256,'\n');
351 ipstream.ignore(256,'\n');
352 ipstream >> usermousesensitivity;
353 ipstream.ignore(256,'\n');
354 ipstream.ignore(256,'\n');
356 ismotionblur = (i != 0);
357 ipstream.ignore(256,'\n');
358 ipstream.ignore(256,'\n');
360 if(detail!=0)kBitsPerPixel=32;
361 else kBitsPerPixel=16;
362 ipstream.ignore(256,'\n');
363 ipstream.ignore(256,'\n');
365 floatjump = (i != 0);
366 ipstream.ignore(256,'\n');
367 ipstream.ignore(256,'\n');
369 mousejump = (i != 0);
370 ipstream.ignore(256,'\n');
371 ipstream.ignore(256,'\n');
373 ambientsound = (i != 0);
374 ipstream.ignore(256,'\n');
375 ipstream.ignore(256,'\n');
376 ipstream >> bloodtoggle;
377 ipstream.ignore(256,'\n');
378 ipstream.ignore(256,'\n');
380 autoslomo = (i != 0);
381 ipstream.ignore(256,'\n');
382 ipstream.ignore(256,'\n');
385 ipstream.ignore(256,'\n');
386 ipstream.ignore(256,'\n');
388 musictoggle = (i != 0);
389 ipstream.ignore(256,'\n');
390 ipstream.ignore(256,'\n');
392 trilinear = (i != 0);
393 ipstream.ignore(256,'\n');
394 ipstream.ignore(256,'\n');
397 ipstream.ignore(256,'\n');
398 ipstream.ignore(256,'\n');
400 invertmouse = (i != 0);
401 ipstream.ignore(256,'\n');
402 ipstream.ignore(256,'\n');
403 ipstream >> gamespeed;
404 oldgamespeed=gamespeed;
409 ipstream.ignore(256,'\n');
410 ipstream.ignore(256,'\n');
411 ipstream >> difficulty;
412 ipstream.ignore(256,'\n');
413 ipstream.ignore(256,'\n');
415 damageeffects = (i != 0);
416 ipstream.ignore(256,'\n');
417 ipstream.ignore(256,'\n');
419 texttoggle = (i != 0);
420 ipstream.ignore(256,'\n');
421 ipstream.ignore(256,'\n');
423 debugmode = (i != 0);
424 ipstream.ignore(256,'\n');
425 ipstream.ignore(256,'\n');
428 ipstream.ignore(256,'\n');
429 ipstream.ignore(256,'\n');
431 showpoints = (i != 0);
432 ipstream.ignore(256,'\n');
433 ipstream.ignore(256,'\n');
435 alwaysblur = (i != 0);
436 ipstream.ignore(256,'\n');
437 ipstream.ignore(256,'\n');
439 immediate = (i != 0);
440 ipstream.ignore(256,'\n');
441 ipstream.ignore(256,'\n');
443 velocityblur = (i != 0);
444 ipstream.ignore(256,'\n');
445 ipstream.ignore(256,'\n');
447 ipstream.ignore(256,'\n');
448 ipstream.ignore(256,'\n');
450 game.forwardkey=CharToKey(string);
451 ipstream.ignore(256,'\n');
452 ipstream.ignore(256,'\n');
454 game.backkey=CharToKey(string);
455 ipstream.ignore(256,'\n');
456 ipstream.ignore(256,'\n');
458 game.leftkey=CharToKey(string);
459 ipstream.ignore(256,'\n');
460 ipstream.ignore(256,'\n');
462 game.rightkey=CharToKey(string);
463 ipstream.ignore(256,'\n');
464 ipstream.ignore(256,'\n');
466 game.jumpkey=CharToKey(string);
467 ipstream.ignore(256,'\n');
468 ipstream.ignore(256,'\n');
470 game.crouchkey=CharToKey(string);
471 ipstream.ignore(256,'\n');
472 ipstream.ignore(256,'\n');
474 game.drawkey=CharToKey(string);
475 ipstream.ignore(256,'\n');
476 ipstream.ignore(256,'\n');
478 game.throwkey=CharToKey(string);
479 ipstream.ignore(256,'\n');
480 ipstream.ignore(256,'\n');
482 game.attackkey=CharToKey(string);
483 ipstream.ignore(256,'\n');
484 ipstream.ignore(256,'\n');
486 game.chatkey=CharToKey(string);
489 if(detail>2)detail=2;
490 if(detail<0)detail=0;
491 if(screenwidth>3000)screenwidth=640;
492 if(screenwidth<0)screenwidth=640;
493 if(screenheight>3000)screenheight=480;
494 if(screenheight<0)screenheight=480;
496 if(kBitsPerPixel!=32&&kBitsPerPixel!=16){
501 selectDetail(kContextWidth, kContextHeight, kBitsPerPixel, detail);
503 SetupDSpFullScreen();
505 //------------------------------------------------------------------
508 RECT r = {0, 0, kContextWidth-1, kContextHeight-1};
509 DWORD dwStyle = WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE;
510 DWORD dwExStyle = WS_EX_APPWINDOW;
519 dwStyle |= WS_OVERLAPPEDWINDOW;
520 dwExStyle |= WS_EX_WINDOWEDGE;
523 AdjustWindowRectEx(&r, dwStyle, FALSE, dwExStyle);
527 x = (GetSystemMetrics(SM_CXSCREEN) >> 1) - ((r.right - r.left + 1) >> 1);
528 y = (GetSystemMetrics(SM_CYSCREEN) >> 1) - ((r.bottom - r.top + 1) >> 1);
531 g_windowHandle=CreateWindowEx(
533 g_wndClassName, "Lugaru", dwStyle,
535 // kContextWidth, kContextHeight,
536 r.right - r.left + 1, r.bottom - r.top + 1,
537 NULL,NULL,g_appInstance,NULL );
540 ReportError("Could not create window");
545 //------------------------------------------------------------------
548 static PIXELFORMATDESCRIPTOR pfd =
550 sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
552 PFD_DRAW_TO_WINDOW | // Format Must Support Window
553 PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
554 PFD_DOUBLEBUFFER, // Must Support Double Buffering
555 PFD_TYPE_RGBA, // Request An RGBA Format
556 kBitsPerPixel, // Select Our Color Depth
557 0, 0, 0, 0, 0, 0, // Color Bits Ignored
558 0, // No Alpha Buffer
559 0, // Shift Bit Ignored
560 0, // No Accumulation Buffer
561 0, 0, 0, 0, // Accumulation Bits Ignored
562 16, // 16Bit Z-Buffer (Depth Buffer)
563 0, // No Stencil Buffer
564 0, // No Auxiliary Buffer
565 PFD_MAIN_PLANE, // Main Drawing Layer
567 0, 0, 0 // Layer Masks Ignored
570 if (!(hDC = GetDC( g_windowHandle)))
571 ReportError( "Could not get device context");
574 if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
576 ReportError( "Could not find appropriate pixel format");
580 if (!DescribePixelFormat(hDC, PixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd))
582 ReportError( "Could not retrieve pixel format");
586 if (!SetPixelFormat( hDC, PixelFormat, &pfd))
588 ReportError( "Could not set pixel format");
592 if (!(hRC = wglCreateContext(hDC)))
594 ReportError( "Could not create rendering context");
598 if (!wglMakeCurrent(hDC, hRC))
600 ReportError( "Could not activate rendering context");
606 // Place the window above all topmost windows
607 SetWindowPos( g_windowHandle, HWND_TOPMOST, 0,0,0,0,
608 SWP_NOMOVE | SWP_NOSIZE );
611 SetForegroundWindow(g_windowHandle);
612 SetFocus(g_windowHandle);
614 glClear( GL_COLOR_BUFFER_BIT );
618 glDisable( GL_ALPHA_TEST);
619 glDisable( GL_BLEND);
620 glDisable( GL_DEPTH_TEST);
621 // glDisable( GL_DITHER);
623 glDisable( GL_LIGHTING);
624 glDisable( GL_LOGIC_OP);
625 glDisable( GL_STENCIL_TEST);
626 glDisable( GL_TEXTURE_1D);
627 glDisable( GL_TEXTURE_2D);
628 glPixelTransferi( GL_MAP_COLOR, GL_FALSE);
629 glPixelTransferi( GL_RED_SCALE, 1);
630 glPixelTransferi( GL_RED_BIAS, 0);
631 glPixelTransferi( GL_GREEN_SCALE, 1);
632 glPixelTransferi( GL_GREEN_BIAS, 0);
633 glPixelTransferi( GL_BLUE_SCALE, 1);
634 glPixelTransferi( GL_BLUE_BIAS, 0);
635 glPixelTransferi( GL_ALPHA_SCALE, 1);
636 glPixelTransferi( GL_ALPHA_BIAS, 0);
638 // set initial rendering states
639 glShadeModel( GL_SMOOTH);
641 glDepthFunc( GL_LEQUAL);
642 glDepthMask( GL_TRUE);
643 // glDepthRange( FRONT_CLIP, BACK_CLIP);
644 glEnable( GL_DEPTH_TEST);
645 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
646 glCullFace( GL_FRONT);
647 glEnable( GL_CULL_FACE);
648 glEnable( GL_LIGHTING);
649 // glEnable( GL_LIGHT_MODEL_AMBIENT);
650 glEnable( GL_DITHER);
651 glEnable( GL_COLOR_MATERIAL);
652 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
653 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
654 glAlphaFunc( GL_GREATER, 0.5f);
656 if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION ||
657 iluGetInteger(ILU_VERSION_NUM) < ILU_VERSION ||
658 ilutGetInteger(ILUT_VERSION_NUM) < ILUT_VERSION)
660 ReportError("DevIL version is different...exiting!\n");
668 ilutRenderer(ILUT_OPENGL);
670 ilEnable(IL_ORIGIN_SET);
671 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
673 GLint width = kContextWidth;
674 GLint height = kContextHeight;
675 gMidPoint.h = width / 2;
676 gMidPoint.v = height / 2;
680 game.newdetail=detail;
681 game.newscreenwidth=screenwidth;
682 game.newscreenheight=screenheight;
690 static void DoMouse(Game & game)
692 static Point lastMouse = {-1,-1};
697 ScreenToClient(g_windowHandle, &pos);
698 globalMouse.h = pos.x;
699 globalMouse.v = pos.y;
701 if (lastMouse.h == globalMouse.h && lastMouse.v == globalMouse.v)
708 static Point virtualMouse = {0,0};
711 delta.h -= lastMouse.h;
712 delta.v -= lastMouse.v;
716 if(mainmenu||(abs(delta.h)<10*realmultiplier*1000&&abs(delta.v)<10*realmultiplier*1000)){
717 game.deltah=delta.h*usermousesensitivity;
718 game.deltav=delta.v*usermousesensitivity;
719 game.mousecoordh=globalMouse.h;
720 game.mousecoordv=globalMouse.v;
725 if(lastMouse.h>gMidPoint.h+100||lastMouse.h<gMidPoint.h-100||lastMouse.v>gMidPoint.v+100||lastMouse.v<gMidPoint.v-100){
728 ClientToScreen(g_windowHandle, &pos);
729 //SetCursorPos( gMidPoint.h,gMidPoint.v);
730 SetCursorPos(pos.x, pos.y);
731 lastMouse = gMidPoint;
739 // --------------------------------------------------------------------------
741 void DoKey (SInt8 theKey, SInt8 theCode)
746 // --------------------------------------------------------------------------
750 void DoFrameRate (int update)
752 static long frames = 0;
754 static AbsoluteTime time = {0,0};
755 static AbsoluteTime frametime = {0,0};
756 AbsoluteTime currTime = UpTime ();
757 double deltaTime = (float) AbsoluteDeltaToDuration (currTime, frametime);
759 if (0 > deltaTime) // if negative microseconds
760 deltaTime /= -1000000.0;
761 else // else milliseconds
764 multiplier=deltaTime;
765 if(multiplier<.001)multiplier=.001;
766 if(multiplier>10)multiplier=10;
767 if(update)frametime = currTime; // reset for next time interval
769 deltaTime = (float) AbsoluteDeltaToDuration (currTime, time);
771 if (0 > deltaTime) // if negative microseconds
772 deltaTime /= -1000000.0;
773 else // else milliseconds
776 if (0.001 <= deltaTime) // has update interval passed
779 time = currTime; // reset for next time interval
786 void DoUpdate (Game & game)
788 static float sps=200;
790 static float oldmult;
793 if(multiplier>.6)multiplier=.6;
795 game.fps=1/multiplier;
797 count = multiplier*sps;
799 //if(count>10)count=10;
801 realmultiplier=multiplier;
802 multiplier*=gamespeed;
803 if(difficulty==1)multiplier*=.9;
804 if(difficulty==0)multiplier*=.8;
806 if(game.loading==4)multiplier*=.00001;
808 if(slomo&&!mainmenu)multiplier*=slomospeed;
809 //if(freeze)multiplier*=0.00001;
811 multiplier/=(float)count;
817 for(int i=0;i<count;i++)
823 game.TickOnceAfter();
824 /* - Debug code to test how many channels were active on average per frame
825 static long frames = 0;
827 static AbsoluteTime start = {0,0};
828 AbsoluteTime currTime = UpTime ();
829 static int num_channels = 0;
831 num_channels += FSOUND_GetChannelsPlaying();
832 double deltaTime = (float) AbsoluteDeltaToDuration (currTime, start);
834 if (0 > deltaTime) // if negative microseconds
835 deltaTime /= -1000000.0;
836 else // else milliseconds
844 float avg_channels = (float)num_channels / (float)frames;
846 ofstream opstream("log.txt",ios::app);
847 opstream << "Average frame count: ";
849 opstream << " frames - ";
850 opstream << avg_channels;
851 opstream << " per frame.\n";
861 // --------------------------------------------------------------------------
874 wglMakeCurrent( NULL, NULL);
875 wglDeleteContext( hRC);
881 ReleaseDC( g_windowHandle, hDC);
887 ShowWindow( g_windowHandle, SW_HIDE );
888 DestroyWindow( g_windowHandle);
889 g_windowHandle = NULL;
896 // --------------------------------------------------------------------------
898 static bool g_focused = true;
901 static bool IsFocused()
906 if (GetActiveWindow() != g_windowHandle)
909 if (IsIconic( g_windowHandle))
927 //ofstream os("error.txt");
929 //ofstream os("log.txt");
934 while (!gDone&&!game.quit&&(!game.tryquit||!game.registered))
940 // check windows messages
943 while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE | PM_NOYIELD ) )
945 if( msg.message == WM_QUIT )
952 TranslateMessage( &msg );
953 DispatchMessage( &msg );
964 // allow game chance to pause
969 // game is not in focus, give CPU time to other apps by waiting for messages instead of 'peeking'
972 //if (GetMessage( &msg, g_windowHandle, 0, 0 ))
973 /*if (GetMessage( &msg, NULL, 0, 0 ))
975 TranslateMessage(&msg);
976 DispatchMessage(&msg);
978 if ( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
982 // handle the error and possibly exit
987 TranslateMessage(&msg);
988 DispatchMessage(&msg);
994 regnow = game.registernow;
999 // if(game.registernow){
1003 sprintf(url,"http://www.wolfire.com/registerpc.html");
1005 ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);
1009 catch (const std::exception& error)
1013 std::string e = "Caught exception: ";
1016 LOG(e, Logger::LOG_ERR);
1018 MessageBox(g_windowHandle, error.what(), "ERROR", MB_OK | MB_ICONEXCLAMATION);
1028 // --------------------------------------------------------------------------
1031 #define MAX_WINKEYS 256
1032 static unsigned short KeyTable[MAX_WINKEYS]=
1035 MAC_MOUSEBUTTON1, // VK_LBUTTON (1)
1036 MAC_MOUSEBUTTON2, // VK_RBUTTON (2)
1037 0xffff, // VK_CANCEL (3)
1038 0xffff, // VK_MBUTTON (4)
1042 MAC_DELETE_KEY, // VK_BACK (8)
1043 MAC_TAB_KEY, // VK_TAB (9)
1046 0xffff, // VK_CLEAR (12)
1047 MAC_RETURN_KEY, // VK_RETURN (13)
1050 MAC_SHIFT_KEY, // VK_SHIFT (16)
1051 MAC_CONTROL_KEY, // VK_CONTROL (17)
1052 MAC_OPTION_KEY, // VK_MENU (18)
1053 0xffff, // VK_PAUSE (19)
1054 MAC_CAPS_LOCK_KEY, // #define VK_CAPITAL (20)
1061 MAC_ESCAPE_KEY, // VK_ESCAPE (27)
1066 MAC_SPACE_KEY, // VK_SPACE (32)
1067 MAC_PAGE_UP_KEY, // VK_PRIOR (33)
1068 MAC_PAGE_DOWN_KEY, // VK_NEXT (34)
1069 MAC_END_KEY, // VK_END (35)
1070 MAC_HOME_KEY, // VK_HOME (36)
1071 MAC_ARROW_LEFT_KEY, // VK_LEFT (37)
1072 MAC_ARROW_UP_KEY, // VK_UP (38)
1073 MAC_ARROW_RIGHT_KEY, // VK_RIGHT (39)
1074 MAC_ARROW_DOWN_KEY, // VK_DOWN (40)
1075 0xffff, // VK_SELECT (41)
1076 0xffff, // VK_PRINT (42)
1077 0xffff, // VK_EXECUTE (43)
1078 0xffff, // VK_SNAPSHOT (44)
1079 MAC_INSERT_KEY, // VK_INSERT (45)
1080 MAC_DEL_KEY, // VK_DELETE (46)
1081 0xffff, // VK_HELP (47)
1082 MAC_0_KEY, // VK_0 (48)
1083 MAC_1_KEY, // VK_1 (49)
1084 MAC_2_KEY, // VK_2 (50)
1085 MAC_3_KEY, // VK_3 (51)
1086 MAC_4_KEY, // VK_4 (52)
1087 MAC_5_KEY, // VK_5 (53)
1088 MAC_6_KEY, // VK_6 (54)
1089 MAC_7_KEY, // VK_7 (55)
1090 MAC_8_KEY, // VK_8 (56)
1091 MAC_9_KEY, // VK_9 (57)
1099 MAC_A_KEY, // VK_A (65)
1100 MAC_B_KEY, // VK_B (66)
1101 MAC_C_KEY, // VK_C (67)
1102 MAC_D_KEY, // VK_D (68)
1103 MAC_E_KEY, // VK_E (69)
1104 MAC_F_KEY, // VK_F (70)
1105 MAC_G_KEY, // VK_G (71)
1106 MAC_H_KEY, // VK_H (72)
1107 MAC_I_KEY, // VK_I (73)
1108 MAC_J_KEY, // VK_J (74)
1109 MAC_K_KEY, // VK_K (75)
1110 MAC_L_KEY, // VK_L (76)
1111 MAC_M_KEY, // VK_M (77)
1112 MAC_N_KEY, // VK_N (78)
1113 MAC_O_KEY, // VK_O (79)
1114 MAC_P_KEY, // VK_P (80)
1115 MAC_Q_KEY, // VK_Q (81)
1116 MAC_R_KEY, // VK_R (82)
1117 MAC_S_KEY, // VK_S (83)
1118 MAC_T_KEY, // VK_T (84)
1119 MAC_U_KEY, // VK_U (85)
1120 MAC_V_KEY, // VK_V (86)
1121 MAC_W_KEY, // VK_W (87)
1122 MAC_X_KEY, // VK_X (88)
1123 MAC_Y_KEY, // VK_Y (89)
1124 MAC_Z_KEY, // VK_Z (90)
1130 MAC_NUMPAD_0_KEY, // VK_NUMPAD0 (96)
1131 MAC_NUMPAD_1_KEY, // VK_NUMPAD1 (97)
1132 MAC_NUMPAD_2_KEY, // VK_NUMPAD2 (98)
1133 MAC_NUMPAD_3_KEY, // VK_NUMPAD3 (99)
1134 MAC_NUMPAD_4_KEY, // VK_NUMPAD4 (100)
1135 MAC_NUMPAD_5_KEY, // VK_NUMPAD5 (101)
1136 MAC_NUMPAD_6_KEY, // VK_NUMPAD6 (102)
1137 MAC_NUMPAD_7_KEY, // VK_NUMPAD7 (103)
1138 MAC_NUMPAD_8_KEY, // VK_NUMPAD8 (104)
1139 MAC_NUMPAD_9_KEY, // VK_NUMPAD9 (105)
1140 MAC_NUMPAD_ASTERISK_KEY, // VK_MULTIPLY (106)
1141 MAC_NUMPAD_PLUS_KEY, // VK_ADD (107)
1142 MAC_NUMPAD_ENTER_KEY, // VK_SEPARATOR (108)
1143 MAC_NUMPAD_MINUS_KEY, // VK_SUBTRACT (109)
1144 MAC_NUMPAD_PERIOD_KEY, // VK_DECIMAL (110)
1145 MAC_NUMPAD_SLASH_KEY, // VK_DIVIDE (111)
1146 MAC_F1_KEY, // VK_F1 (112)
1147 MAC_F2_KEY, // VK_F2 (113)
1148 MAC_F3_KEY, // VK_F3 (114)
1149 MAC_F4_KEY, // VK_F4 (115)
1150 MAC_F5_KEY, // VK_F5 (116)
1151 MAC_F6_KEY, // VK_F6 (117)
1152 MAC_F7_KEY, // VK_F7 (118)
1153 MAC_F8_KEY, // VK_F8 (119)
1154 MAC_F9_KEY, // VK_F9 (120)
1155 MAC_F10_KEY, // VK_F10 (121)
1156 MAC_F11_KEY, // VK_F11 (122)
1157 MAC_F12_KEY, // VK_F12 (123)
1178 0xffff, // VK_NUMLOCK (144)
1179 0xffff, // VK_SCROLL (145)
1194 MAC_SHIFT_KEY, // VK_LSHIFT (160)
1195 MAC_SHIFT_KEY, // VK_RSHIFT (161)
1196 MAC_CONTROL_KEY, // VK_LCONTROL (162)
1197 MAC_CONTROL_KEY, // VK_RCONTROL (163)
1198 MAC_OPTION_KEY, // VK_LMENU (164)
1199 MAC_OPTION_KEY, // VK_RMENU (165)
1220 MAC_SEMICOLON_KEY, // (186)
1221 MAC_PLUS_KEY, // (187)
1222 MAC_COMMA_KEY, // (188)
1223 MAC_MINUS_KEY, // (189)
1224 MAC_PERIOD_KEY, // (190)
1225 MAC_SLASH_KEY, // (191)
1226 MAC_TILDE_KEY, // (192)
1253 MAC_LEFTBRACKET_KEY, // (219)
1254 MAC_BACKSLASH_KEY, // (220)
1255 MAC_RIGHTBRACKET_KEY, // (221)
1256 MAC_APOSTROPHE_KEY, // (222)
1293 static KeyMap g_theKeys;
1295 void SetKey( int key)
1297 g_theKeys[ key >> 3] |= (1 << (key & 7));
1300 void ClearKey( int key)
1302 g_theKeys[ key >> 3] &= (0xff ^ (1 << (key & 7)));
1305 void GetKeys( unsigned char theKeys[16])
1307 memcpy( theKeys, &g_theKeys, 16);
1316 void ClipMouseToWindow(HWND window)
1320 GetClientRect(window, &wRect);
1322 ClientToScreen(window, (LPPOINT)&wRect.left);
1323 ClientToScreen(window, (LPPOINT)&wRect.right);
1330 LRESULT FAR PASCAL AppWndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
1332 /* this is where we receive all messages concerning this window
1333 * we can either process a message or pass it on to the default
1334 * message handler of windows */
1336 static PAINTSTRUCT ps;
1340 case WM_ACTIVATE: // Watch For Window Activate Message
1342 // Check Minimization State
1343 BOOL iconified = HIWORD(wParam) ? TRUE : FALSE;
1345 if (LOWORD(wParam) == WA_INACTIVE)
1354 CloseWindow( hWnd );
1356 // The window is now iconified
1357 iconified = GL_TRUE;
1363 g_focused=false; // Program Is Active
1367 SetupDSpFullScreen();
1374 // The window is now iconified
1375 iconified = GL_FALSE;
1378 ShowWindow( hWnd, SW_SHOW );
1379 SetForegroundWindow( hWnd );
1383 ClipMouseToWindow(hWnd);
1384 g_focused=true; // Program Is No Longer Active
1387 return 0; // Return To The Message Loop
1393 // check for Alt-F4 (exit hotkey)
1394 if (wParam == VK_F4)
1396 if (GetKeyState( VK_MENU) & 0x8080)
1402 if (wParam < MAX_WINKEYS)
1404 if (KeyTable[wParam] != 0xffff)
1405 SetKey( KeyTable[wParam]);
1413 if (wParam < MAX_WINKEYS)
1414 if (KeyTable[wParam] != 0xffff)
1415 ClearKey( KeyTable[wParam]);
1422 case WM_SYSDEADCHAR:
1425 case WM_NCLBUTTONDOWN:
1426 case WM_LBUTTONDOWN:
1433 case WM_NCRBUTTONDOWN:
1434 case WM_RBUTTONDOWN:
1440 case WM_NCMBUTTONDOWN:
1441 case WM_MBUTTONDOWN:
1447 case WM_NCLBUTTONUP:
1451 buttons[ 0] = false;
1455 case WM_NCRBUTTONUP:
1458 buttons[ 1] = false;
1462 case WM_NCMBUTTONUP:
1465 buttons[ 2] = false;
1469 case WM_NCLBUTTONDBLCLK:
1470 case WM_NCRBUTTONDBLCLK:
1471 case WM_NCMBUTTONDBLCLK:
1472 case WM_LBUTTONDBLCLK:
1474 case WM_RBUTTONDBLCLK:
1475 case WM_MBUTTONDBLCLK:
1478 case WM_NCMOUSEMOVE:
1480 /* ((WindowInfo *)g_lastWindow->GetInfo())->m_mouseX = (signed short)(lParam & 0xffff);
1481 ((WindowInfo *)g_lastWindow->GetInfo())->m_mouseY = (signed short)(lParam >> 16);
1482 if (g_lastWindow->m_mouseCallbacksEnabled) g_lastWindow->MouseMoveCallback();
1483 */// goto winmessage;
1486 case WM_SYSCOMMAND: // Intercept System Commands
1488 switch (wParam) // Check System Calls
1490 case SC_SCREENSAVE: // Screensaver Trying To Start?
1491 case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
1492 return 0; // Prevent From Happening
1494 // User trying to access application menu using ALT?
1503 // ReleaseCapture();
1504 // ClipMouseToWindow(hWnd);
1521 PostQuitMessage(0); /* Terminate Application */
1529 // BeginPaint( g_windowHandle,&ps);
1530 // EndPaint( g_windowHandle,&ps);
1537 /* We processed the message and there
1538 * is no processing by Windows necessary */
1540 /* We didn't process the message so let Windows do it */
1541 return DefWindowProc(hWnd,msg,wParam,lParam);
1545 static BOOL RegisterWindowClasses(HINSTANCE hFirstInstance)
1548 memset( &wc, 0, sizeof( wc));
1550 /* Register the window class. */
1551 wc.cbSize = sizeof(wc);
1553 wc.style = (CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_OWNDC); /* Combination of Class Styles */
1554 wc.lpfnWndProc = AppWndProc; /* Adress of Window Procedure */
1555 wc.cbClsExtra = 0; /* Extra Bytes allocated for this Class */
1556 wc.cbWndExtra = 0; /* Extra Bytes allocated for each Window */
1557 wc.hInstance = hFirstInstance; /* Handle of program instance */
1558 wc.hIcon = LoadIcon( hFirstInstance, MAKEINTRESOURCE(IDI_LUGARU) );
1559 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
1560 wc.hbrBackground = NULL;
1561 wc.lpszMenuName = NULL;
1562 wc.lpszClassName = g_wndClassName; /* Name of the Window Class */
1563 wc.hIconSm = LoadIcon( hFirstInstance, MAKEINTRESOURCE(IDI_LUGARU) );
1565 if (!RegisterClassEx(&wc)) return FALSE; /* Register Class failed */
1570 int resolutionID(int width, int height)
1574 if(width==640 && height==480)whichres=0;
1575 if(width==800 && height==600)whichres=1;
1576 if(width==1024 && height==768)whichres=2;
1577 if(width==1280 && height==1024)whichres=3;
1578 if(width==1600 && height==1200)whichres=4;
1579 if(width==840 && height==524)whichres=5;
1580 if(width==1024 && height==640)whichres=6;
1581 if(width==1344 && height==840)whichres=7;
1586 int closestResolution(int width, int height)
1590 if(width>=640 && height>=480)whichres=0;
1591 if(width>=800 && height>=600)whichres=1;
1592 if(width>=1024 && height>=768)whichres=2;
1593 if(width>=1280 && height>=1024)whichres=3;
1594 if(width>=1600 && height>=1200)whichres=4;
1595 if(width==840 && height==524)whichres=5;
1596 if(width==1024 && height==640)whichres=6;
1597 if(width==1344 && height==840)whichres=7;
1602 bool selectDetail(int & width, int & height, int & bpp, int & detail)
1605 int whichres = closestResolution(width, height);
1609 if(whichres<=0 || whichres>7){
1643 if ((detail != 0) && (resolutionDepths[whichres][1] != 0))
1647 else if ((detail == 0) && (resolutionDepths[whichres][0] != 0))
1651 else if ((detail != 0) && (resolutionDepths[whichres][0] != 0))
1657 else if (0 == whichres)
1665 bpp = resolutionDepths[whichres][(detail != 0)];
1670 int __stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
1673 LPWSTR * cl = CommandLineToArgvW(GetCommandLineW(), &argc);
1676 if (0 == _wcsicmp(cl[1], L"-windowed"))
1684 memset( &g_theKeys, 0, sizeof( KeyMap));
1688 memset(&mode, 0, sizeof(mode));
1689 mode.dmSize = sizeof(mode);
1690 while (EnumDisplaySettings(NULL, i++, &mode))
1692 if (mode.dmBitsPerPel < 16)
1697 int res = resolutionID(mode.dmPelsWidth, mode.dmPelsHeight);
1699 if (res > -1 && res < 8)
1701 if (DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettings(&mode, CDS_TEST))
1706 switch(mode.dmBitsPerPel)
1710 resolutionDepths[res][1] = mode.dmBitsPerPel;
1713 resolutionDepths[res][0] = mode.dmBitsPerPel;
1719 /* if there is no Instance of our program in memory then register the window class */
1720 if (hPrevInstance == NULL && !RegisterWindowClasses(hInstance))
1721 return FALSE; /* registration failed! */
1723 g_appInstance=hInstance;
1727 UnregisterClass( g_wndClassName, hInstance);
1733 extern int channels[100];
1734 extern FSOUND_SAMPLE * samp[100];
1735 extern FSOUND_STREAM * strm[10];
1737 extern "C" void PlaySoundEx(int chan, FSOUND_SAMPLE *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
1739 const FSOUND_SAMPLE * currSample = FSOUND_GetCurrentSample(channels[chan]);
1740 if (currSample && currSample == samp[chan])
1742 if (FSOUND_GetPaused(channels[chan]))
1744 FSOUND_StopSound(channels[chan]);
1745 channels[chan] = FSOUND_FREE;
1747 else if (FSOUND_IsPlaying(channels[chan]))
1749 int loop_mode = FSOUND_GetLoopMode(channels[chan]);
1750 if (loop_mode & FSOUND_LOOP_OFF)
1752 channels[chan] = FSOUND_FREE;
1758 channels[chan] = FSOUND_FREE;
1761 channels[chan] = FSOUND_PlaySoundEx(channels[chan], sptr, dsp, startpaused);
1762 if (channels[chan] < 0)
1764 channels[chan] = FSOUND_PlaySoundEx(FSOUND_FREE, sptr, dsp, startpaused);
1768 extern "C" void PlayStreamEx(int chan, FSOUND_STREAM *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
1770 const FSOUND_SAMPLE * currSample = FSOUND_GetCurrentSample(channels[chan]);
1771 if (currSample && currSample == FSOUND_Stream_GetSample(sptr))
1773 FSOUND_StopSound(channels[chan]);
1774 FSOUND_Stream_Stop(sptr);
1778 FSOUND_Stream_Stop(sptr);
1779 channels[chan] = FSOUND_FREE;
1782 channels[chan] = FSOUND_Stream_PlayEx(channels[chan], sptr, dsp, startpaused);
1783 if (channels[chan] < 0)
1785 channels[chan] = FSOUND_Stream_PlayEx(FSOUND_FREE, sptr, dsp, startpaused);
1789 bool LoadImage(const char * fname, TGAImageRec & tex)
1793 if ( tex.data == NULL )
1798 ILstring f = strdup(ConvertFileName(fname));
1805 ilGenImages(1, &iid);
1810 tex.sizeX = ilGetInteger(IL_IMAGE_WIDTH);
1811 tex.sizeY = ilGetInteger(IL_IMAGE_HEIGHT);
1812 tex.bpp = ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);
1813 ILuint Bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL),
1814 imageSize = tex.sizeX * tex.sizeY * Bpp;
1815 ILubyte *Data = ilGetData();
1816 memcpy(tex.data, Data, imageSize);
1818 // Truvision Targa files are stored as BGR colors
1819 // We want RGB so Blue and Red bytes are switched
1820 if (IL_TGA == ilGetInteger(IL_IMAGE_FORMAT))
1822 // Loop Through The Image Data
1823 for (GLuint i = 0; i < int(imageSize); i += Bpp)
1825 // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
1826 GLbyte temp; // Temporary Variable
1827 temp = tex.data[i]; // Temporarily Store The Value At Image Data 'i'
1828 tex.data[i] = tex.data[i + 2]; // Set The 1st Byte To The Value Of The 3rd Byte
1829 tex.data[i + 2] = temp; // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
1837 ilDeleteImages(1, &iid);
1841 GLuint texid = ilutGLLoadImage(f);
1846 ilutGLBuildMipmaps()
1858 void ScreenShot(const char * fname)
1860 ILstring f = strdup(fname);
1867 ilGenImages(1, &iid);
1873 ilDeleteImages(1, &iid);