13 #pragma comment(lib, "opengl32.lib")
14 #pragma comment(lib, "glu32.lib")
15 #pragma comment(lib, "glaux.lib")
18 extern bool buttons[3];
19 extern float multiplier;
20 extern float screenwidth,screenheight;
22 extern float realmultiplier;
24 extern bool ismotionblur;
25 extern float usermousesensitivity;
27 extern bool floatjump;
28 extern bool cellophane;
30 //extern int terraindetail;
31 //extern int texdetail;
32 extern float terraindetail;
33 extern float texdetail;
34 extern int bloodtoggle;
36 extern bool autoslomo;
38 extern bool musictoggle;
39 extern bool trilinear;
40 extern float gamespeed;
41 extern int difficulty;
42 extern bool damageeffects;
43 extern int numplayers;
45 extern bool invertmouse;
46 extern bool texttoggle;
47 extern bool ambientsound;
48 extern bool mousejump;
50 extern Person player[maxplayers];
52 extern bool stillloading;
53 extern bool showpoints;
54 extern bool alwaysblur;
55 extern bool immediate;
56 extern bool velocityblur;
57 extern bool debugmode;
59 /*extern*/ bool gameFocused;
60 extern int kBitsPerPixel;
61 extern float slomospeed;
62 extern float slomofreq;
63 extern float oldgamespeed;
72 #include "MacCompatibility.h"
80 #include "res/resource.h"
85 unsigned int resolutionDepths[8][2] = {0};
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);
91 void ReportError (char * strError);
93 void SetupDSpFullScreen();
96 void DrawGL(Game & game);
98 void CreateGLWindow (void);
99 Boolean SetUp (Game & game);
100 void DoKey (SInt8 theKey, SInt8 theCode);
101 void DoUpdate (Game & game);
107 // statics/globals (internal only) ------------------------------------------
109 typedef struct tagPOINT {
116 void sdlGetCursorPos(POINT *pt)
119 SDL_GetMouseState(&x, &y);
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")
133 static const char g_wndClassName[]={ "LUGARUWINDOWCLASS" };
134 static HINSTANCE g_appInstance;
135 static HWND g_windowHandle;
139 static bool g_button, fullscreen = true;
150 kForegroundSleep = 10,
151 kBackgroundSleep = 10000
158 const RGBColor rgbBlack = { 0x0000, 0x0000, 0x0000 };
161 char gcstrMode [256] = "";
163 UInt32 gSleepTime = kForegroundSleep;
164 Boolean gDone = false, gfFrontProcess = true;
168 // --------------------------------------------------------------------------
170 void ReportError (char * strError)
172 #ifdef WIN32 // !!! FIXME. --ryan.
173 throw std::exception( strError);
176 /* char errMsgCStr [256];
179 sprintf (errMsgCStr, "%s", strError);
181 // out as debug string
182 CToPStr (strErr, errMsgCStr);
187 void SetupDSpFullScreen ()
194 DEVMODE dmScreenSettings;
195 memset( &dmScreenSettings, 0, sizeof( dmScreenSettings));
196 dmScreenSettings.dmSize = sizeof( dmScreenSettings);
197 dmScreenSettings.dmPelsWidth = kContextWidth;
198 dmScreenSettings.dmPelsHeight = kContextHeight;
199 dmScreenSettings.dmBitsPerPel = kBitsPerPixel;
200 dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
203 if (ChangeDisplaySettings( &dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
205 ReportError( "Could not set display mode");
222 ChangeDisplaySettings( NULL, 0);
230 //-----------------------------------------------------------------------------------------------------------------------
234 void DrawGL (Game & game)
246 static inline int clamp_sdl_mouse_button(Uint8 button)
248 if ((button >= 1) && (button <= 3))
253 static void sdlEventProc(const SDL_Event &e)
258 case SDL_MOUSEBUTTONDOWN:
260 val = clamp_sdl_mouse_button(e.button.button);
270 case SDL_MOUSEBUTTONUP:
272 val = clamp_sdl_mouse_button(e.button.button);
277 buttons[val] = false;
285 // --------------------------------------------------------------------------
287 static Point gMidPoint;
289 Boolean SetUp (Game & game)
295 randSeed = UpTime().lo;
298 // ifstream ipstream(":Data:config.txt", std::ios::in /*| std::ios::nocreate*/);
299 ifstream ipstream("./Data/config.txt", std::ios::in /*| std::ios::nocreate*/);
302 usermousesensitivity=1;
331 game.crouchkey=MAC_SHIFT_KEY;
332 game.jumpkey=MAC_SPACE_KEY;
333 game.leftkey=MAC_A_KEY;
334 game.forwardkey=MAC_W_KEY;
335 game.backkey=MAC_S_KEY;
336 game.rightkey=MAC_D_KEY;
337 game.drawkey=MAC_E_KEY;
338 game.throwkey=MAC_Q_KEY;
339 game.attackkey=MAC_MOUSEBUTTON1;
340 game.chatkey=MAC_T_KEY;
346 selectDetail(kContextWidth, kContextHeight, kBitsPerPixel, detail);
349 //ofstream opstream(":Data:config.txt");
350 ofstream opstream("./Data/config.txt");
351 opstream << "Screenwidth:\n";
352 opstream << kContextWidth;
353 opstream << "\nScreenheight:\n";
354 opstream << kContextHeight;
355 opstream << "\nMouse sensitivity:\n";
356 opstream << usermousesensitivity;
357 opstream << "\nBlur(0,1):\n";
358 opstream << ismotionblur;
359 opstream << "\nOverall Detail(0,1,2) higher=better:\n";
361 opstream << "\nFloating jump:\n";
362 opstream << floatjump;
363 opstream << "\nMouse jump:\n";
364 opstream << mousejump;
365 opstream << "\nAmbient sound:\n";
366 opstream << ambientsound;
367 opstream << "\nBlood (0,1,2):\n";
368 opstream << bloodtoggle;
369 opstream << "\nAuto slomo:\n";
370 opstream << autoslomo;
371 opstream << "\nFoliage:\n";
373 opstream << "\nMusic:\n";
374 opstream << musictoggle;
375 opstream << "\nTrilinear:\n";
376 opstream << trilinear;
377 opstream << "\nDecals(shadows,blood puddles,etc):\n";
379 opstream << "\nInvert mouse:\n";
380 opstream << invertmouse;
381 opstream << "\nGamespeed:\n";
382 opstream << gamespeed;
383 opstream << "\nDifficulty(0,1,2) higher=harder:\n";
384 opstream << difficulty;
385 opstream << "\nDamage effects(blackout, doublevision):\n";
386 opstream << damageeffects;
387 opstream << "\nText:\n";
388 opstream << texttoggle;
389 opstream << "\nDebug:\n";
390 opstream << debugmode;
391 opstream << "\nVBL Sync:\n";
393 opstream << "\nShow Points:\n";
394 opstream << showpoints;
395 opstream << "\nAlways Blur:\n";
396 opstream << alwaysblur;
397 opstream << "\nImmediate mode (turn on on G5):\n";
398 opstream << immediate;
399 opstream << "\nVelocity blur:\n";
400 opstream << velocityblur;
401 opstream << "\nVolume:\n";
403 opstream << "\nForward key:\n";
404 opstream << KeyToChar(game.forwardkey);
405 opstream << "\nBack key:\n";
406 opstream << KeyToChar(game.backkey);
407 opstream << "\nLeft key:\n";
408 opstream << KeyToChar(game.leftkey);
409 opstream << "\nRight key:\n";
410 opstream << KeyToChar(game.rightkey);
411 opstream << "\nJump key:\n";
412 opstream << KeyToChar(game.jumpkey);
413 opstream << "\nCrouch key:\n";
414 opstream << KeyToChar(game.crouchkey);
415 opstream << "\nDraw key:\n";
416 opstream << KeyToChar(game.drawkey);
417 opstream << "\nThrow key:\n";
418 opstream << KeyToChar(game.throwkey);
419 opstream << "\nAttack key:\n";
420 opstream << KeyToChar(game.attackkey);
421 opstream << "\nChat key:\n";
422 opstream << KeyToChar(game.chatkey);
427 ipstream.ignore(256,'\n');
428 ipstream >> kContextWidth;
429 ipstream.ignore(256,'\n');
430 ipstream.ignore(256,'\n');
431 ipstream >> kContextHeight;
432 ipstream.ignore(256,'\n');
433 ipstream.ignore(256,'\n');
434 ipstream >> usermousesensitivity;
435 ipstream.ignore(256,'\n');
436 ipstream.ignore(256,'\n');
438 ismotionblur = (i != 0);
439 ipstream.ignore(256,'\n');
440 ipstream.ignore(256,'\n');
442 if(detail!=0)kBitsPerPixel=32;
443 else kBitsPerPixel=16;
444 ipstream.ignore(256,'\n');
445 ipstream.ignore(256,'\n');
447 floatjump = (i != 0);
448 ipstream.ignore(256,'\n');
449 ipstream.ignore(256,'\n');
451 mousejump = (i != 0);
452 ipstream.ignore(256,'\n');
453 ipstream.ignore(256,'\n');
455 ambientsound = (i != 0);
456 ipstream.ignore(256,'\n');
457 ipstream.ignore(256,'\n');
458 ipstream >> bloodtoggle;
459 ipstream.ignore(256,'\n');
460 ipstream.ignore(256,'\n');
462 autoslomo = (i != 0);
463 ipstream.ignore(256,'\n');
464 ipstream.ignore(256,'\n');
467 ipstream.ignore(256,'\n');
468 ipstream.ignore(256,'\n');
470 musictoggle = (i != 0);
471 ipstream.ignore(256,'\n');
472 ipstream.ignore(256,'\n');
474 trilinear = (i != 0);
475 ipstream.ignore(256,'\n');
476 ipstream.ignore(256,'\n');
479 ipstream.ignore(256,'\n');
480 ipstream.ignore(256,'\n');
482 invertmouse = (i != 0);
483 ipstream.ignore(256,'\n');
484 ipstream.ignore(256,'\n');
485 ipstream >> gamespeed;
486 oldgamespeed=gamespeed;
491 ipstream.ignore(256,'\n');
492 ipstream.ignore(256,'\n');
493 ipstream >> difficulty;
494 ipstream.ignore(256,'\n');
495 ipstream.ignore(256,'\n');
497 damageeffects = (i != 0);
498 ipstream.ignore(256,'\n');
499 ipstream.ignore(256,'\n');
501 texttoggle = (i != 0);
502 ipstream.ignore(256,'\n');
503 ipstream.ignore(256,'\n');
505 debugmode = (i != 0);
506 ipstream.ignore(256,'\n');
507 ipstream.ignore(256,'\n');
510 ipstream.ignore(256,'\n');
511 ipstream.ignore(256,'\n');
513 showpoints = (i != 0);
514 ipstream.ignore(256,'\n');
515 ipstream.ignore(256,'\n');
517 alwaysblur = (i != 0);
518 ipstream.ignore(256,'\n');
519 ipstream.ignore(256,'\n');
521 immediate = (i != 0);
522 ipstream.ignore(256,'\n');
523 ipstream.ignore(256,'\n');
525 velocityblur = (i != 0);
526 ipstream.ignore(256,'\n');
527 ipstream.ignore(256,'\n');
529 ipstream.ignore(256,'\n');
530 ipstream.ignore(256,'\n');
532 game.forwardkey=CharToKey(string);
533 ipstream.ignore(256,'\n');
534 ipstream.ignore(256,'\n');
536 game.backkey=CharToKey(string);
537 ipstream.ignore(256,'\n');
538 ipstream.ignore(256,'\n');
540 game.leftkey=CharToKey(string);
541 ipstream.ignore(256,'\n');
542 ipstream.ignore(256,'\n');
544 game.rightkey=CharToKey(string);
545 ipstream.ignore(256,'\n');
546 ipstream.ignore(256,'\n');
548 game.jumpkey=CharToKey(string);
549 ipstream.ignore(256,'\n');
550 ipstream.ignore(256,'\n');
552 game.crouchkey=CharToKey(string);
553 ipstream.ignore(256,'\n');
554 ipstream.ignore(256,'\n');
556 game.drawkey=CharToKey(string);
557 ipstream.ignore(256,'\n');
558 ipstream.ignore(256,'\n');
560 game.throwkey=CharToKey(string);
561 ipstream.ignore(256,'\n');
562 ipstream.ignore(256,'\n');
564 game.attackkey=CharToKey(string);
565 ipstream.ignore(256,'\n');
566 ipstream.ignore(256,'\n');
568 game.chatkey=CharToKey(string);
571 if(detail>2)detail=2;
572 if(detail<0)detail=0;
573 if(screenwidth>3000)screenwidth=640;
574 if(screenwidth<0)screenwidth=640;
575 if(screenheight>3000)screenheight=480;
576 if(screenheight<0)screenheight=480;
578 if(kBitsPerPixel!=32&&kBitsPerPixel!=16){
583 selectDetail(kContextWidth, kContextHeight, kBitsPerPixel, detail);
585 SetupDSpFullScreen();
588 if (!SDL_WasInit(SDL_INIT_VIDEO))
590 if (SDL_Init(SDL_INIT_VIDEO) == -1)
592 fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
597 Uint32 sdlflags = SDL_OPENGL;
598 SDL_WM_SetCaption("Lugaru", "lugaru");
600 if (SDL_SetVideoMode(kContextWidth, kContextHeight, 0, sdlflags) == NULL)
602 fprintf(stderr, "SDL_SetVideoMode() failed: %s\n", SDL_GetError());
606 #elif (defined WIN32)
607 //------------------------------------------------------------------
610 RECT r = {0, 0, kContextWidth-1, kContextHeight-1};
611 DWORD dwStyle = WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE;
612 DWORD dwExStyle = WS_EX_APPWINDOW;
621 dwStyle |= WS_OVERLAPPEDWINDOW;
622 dwExStyle |= WS_EX_WINDOWEDGE;
625 AdjustWindowRectEx(&r, dwStyle, FALSE, dwExStyle);
629 x = (GetSystemMetrics(SM_CXSCREEN) >> 1) - ((r.right - r.left + 1) >> 1);
630 y = (GetSystemMetrics(SM_CYSCREEN) >> 1) - ((r.bottom - r.top + 1) >> 1);
633 g_windowHandle=CreateWindowEx(
635 g_wndClassName, "Lugaru", dwStyle,
637 // kContextWidth, kContextHeight,
638 r.right - r.left + 1, r.bottom - r.top + 1,
639 NULL,NULL,g_appInstance,NULL );
642 ReportError("Could not create window");
646 //------------------------------------------------------------------
649 static PIXELFORMATDESCRIPTOR pfd =
651 sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
653 PFD_DRAW_TO_WINDOW | // Format Must Support Window
654 PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
655 PFD_DOUBLEBUFFER, // Must Support Double Buffering
656 PFD_TYPE_RGBA, // Request An RGBA Format
657 kBitsPerPixel, // Select Our Color Depth
658 0, 0, 0, 0, 0, 0, // Color Bits Ignored
659 0, // No Alpha Buffer
660 0, // Shift Bit Ignored
661 0, // No Accumulation Buffer
662 0, 0, 0, 0, // Accumulation Bits Ignored
663 16, // 16Bit Z-Buffer (Depth Buffer)
664 0, // No Stencil Buffer
665 0, // No Auxiliary Buffer
666 PFD_MAIN_PLANE, // Main Drawing Layer
668 0, 0, 0 // Layer Masks Ignored
671 if (!(hDC = GetDC( g_windowHandle)))
672 ReportError( "Could not get device context");
675 if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
677 ReportError( "Could not find appropriate pixel format");
681 if (!DescribePixelFormat(hDC, PixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd))
683 ReportError( "Could not retrieve pixel format");
687 if (!SetPixelFormat( hDC, PixelFormat, &pfd))
689 ReportError( "Could not set pixel format");
693 if (!(hRC = wglCreateContext(hDC)))
695 ReportError( "Could not create rendering context");
699 if (!wglMakeCurrent(hDC, hRC))
701 ReportError( "Could not activate rendering context");
707 // Place the window above all topmost windows
708 SetWindowPos( g_windowHandle, HWND_TOPMOST, 0,0,0,0,
709 SWP_NOMOVE | SWP_NOSIZE );
712 SetForegroundWindow(g_windowHandle);
713 SetFocus(g_windowHandle);
716 glClear( GL_COLOR_BUFFER_BIT );
720 glDisable( GL_ALPHA_TEST);
721 glDisable( GL_BLEND);
722 glDisable( GL_DEPTH_TEST);
723 // glDisable( GL_DITHER);
725 glDisable( GL_LIGHTING);
726 glDisable( GL_LOGIC_OP);
727 glDisable( GL_STENCIL_TEST);
728 glDisable( GL_TEXTURE_1D);
729 glDisable( GL_TEXTURE_2D);
730 glPixelTransferi( GL_MAP_COLOR, GL_FALSE);
731 glPixelTransferi( GL_RED_SCALE, 1);
732 glPixelTransferi( GL_RED_BIAS, 0);
733 glPixelTransferi( GL_GREEN_SCALE, 1);
734 glPixelTransferi( GL_GREEN_BIAS, 0);
735 glPixelTransferi( GL_BLUE_SCALE, 1);
736 glPixelTransferi( GL_BLUE_BIAS, 0);
737 glPixelTransferi( GL_ALPHA_SCALE, 1);
738 glPixelTransferi( GL_ALPHA_BIAS, 0);
740 // set initial rendering states
741 glShadeModel( GL_SMOOTH);
743 glDepthFunc( GL_LEQUAL);
744 glDepthMask( GL_TRUE);
745 // glDepthRange( FRONT_CLIP, BACK_CLIP);
746 glEnable( GL_DEPTH_TEST);
747 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
748 glCullFace( GL_FRONT);
749 glEnable( GL_CULL_FACE);
750 glEnable( GL_LIGHTING);
751 // glEnable( GL_LIGHT_MODEL_AMBIENT);
752 glEnable( GL_DITHER);
753 glEnable( GL_COLOR_MATERIAL);
754 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
755 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
756 glAlphaFunc( GL_GREATER, 0.5f);
758 if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION ||
759 iluGetInteger(ILU_VERSION_NUM) < ILU_VERSION ||
760 ilutGetInteger(ILUT_VERSION_NUM) < ILUT_VERSION)
762 ReportError("DevIL version is different...exiting!\n");
770 ilutRenderer(ILUT_OPENGL);
772 ilEnable(IL_ORIGIN_SET);
773 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
775 GLint width = kContextWidth;
776 GLint height = kContextHeight;
777 gMidPoint.h = width / 2;
778 gMidPoint.v = height / 2;
782 game.newdetail=detail;
783 game.newscreenwidth=screenwidth;
784 game.newscreenheight=screenheight;
792 static void DoMouse(Game & game)
794 static Point lastMouse = {-1,-1};
799 ScreenToClient(g_windowHandle, &pos);
800 globalMouse.h = pos.x;
801 globalMouse.v = pos.y;
803 if (lastMouse.h == globalMouse.h && lastMouse.v == globalMouse.v)
810 static Point virtualMouse = {0,0};
813 delta.h -= lastMouse.h;
814 delta.v -= lastMouse.v;
818 if(mainmenu||(abs(delta.h)<10*realmultiplier*1000&&abs(delta.v)<10*realmultiplier*1000)){
819 game.deltah=delta.h*usermousesensitivity;
820 game.deltav=delta.v*usermousesensitivity;
821 game.mousecoordh=globalMouse.h;
822 game.mousecoordv=globalMouse.v;
827 if(lastMouse.h>gMidPoint.h+100||lastMouse.h<gMidPoint.h-100||lastMouse.v>gMidPoint.v+100||lastMouse.v<gMidPoint.v-100){
830 ClientToScreen(g_windowHandle, &pos);
831 //SetCursorPos( gMidPoint.h,gMidPoint.v);
832 SetCursorPos(pos.x, pos.y);
833 lastMouse = gMidPoint;
841 // --------------------------------------------------------------------------
843 void DoKey (SInt8 theKey, SInt8 theCode)
848 // --------------------------------------------------------------------------
852 void DoFrameRate (int update)
854 static long frames = 0;
856 static AbsoluteTime time = {0,0};
857 static AbsoluteTime frametime = {0,0};
858 AbsoluteTime currTime = UpTime ();
859 double deltaTime = (float) AbsoluteDeltaToDuration (currTime, frametime);
861 if (0 > deltaTime) // if negative microseconds
862 deltaTime /= -1000000.0;
863 else // else milliseconds
866 multiplier=deltaTime;
867 if(multiplier<.001)multiplier=.001;
868 if(multiplier>10)multiplier=10;
869 if(update)frametime = currTime; // reset for next time interval
871 deltaTime = (float) AbsoluteDeltaToDuration (currTime, time);
873 if (0 > deltaTime) // if negative microseconds
874 deltaTime /= -1000000.0;
875 else // else milliseconds
878 if (0.001 <= deltaTime) // has update interval passed
881 time = currTime; // reset for next time interval
888 void DoUpdate (Game & game)
890 static float sps=200;
892 static float oldmult;
895 if(multiplier>.6)multiplier=.6;
897 game.fps=1/multiplier;
899 count = multiplier*sps;
901 //if(count>10)count=10;
903 realmultiplier=multiplier;
904 multiplier*=gamespeed;
905 if(difficulty==1)multiplier*=.9;
906 if(difficulty==0)multiplier*=.8;
908 if(game.loading==4)multiplier*=.00001;
910 if(slomo&&!mainmenu)multiplier*=slomospeed;
911 //if(freeze)multiplier*=0.00001;
913 multiplier/=(float)count;
919 for(int i=0;i<count;i++)
925 game.TickOnceAfter();
926 /* - Debug code to test how many channels were active on average per frame
927 static long frames = 0;
929 static AbsoluteTime start = {0,0};
930 AbsoluteTime currTime = UpTime ();
931 static int num_channels = 0;
933 num_channels += FSOUND_GetChannelsPlaying();
934 double deltaTime = (float) AbsoluteDeltaToDuration (currTime, start);
936 if (0 > deltaTime) // if negative microseconds
937 deltaTime /= -1000000.0;
938 else // else milliseconds
946 float avg_channels = (float)num_channels / (float)frames;
948 ofstream opstream("log.txt",ios::app);
949 opstream << "Average frame count: ";
951 opstream << " frames - ";
952 opstream << avg_channels;
953 opstream << " per frame.\n";
963 // --------------------------------------------------------------------------
977 #elif (defined WIN32)
980 wglMakeCurrent( NULL, NULL);
981 wglDeleteContext( hRC);
987 ReleaseDC( g_windowHandle, hDC);
993 ShowWindow( g_windowHandle, SW_HIDE );
994 DestroyWindow( g_windowHandle);
995 g_windowHandle = NULL;
1003 // --------------------------------------------------------------------------
1005 static bool g_focused = true;
1008 static bool IsFocused()
1014 if (GetActiveWindow() != g_windowHandle)
1017 if (IsIconic( g_windowHandle))
1031 bool regnow = false;
1036 //ofstream os("error.txt");
1038 //ofstream os("log.txt");
1043 while (!gDone&&!game.quit&&(!game.tryquit||!game.registered))
1049 // check windows messages
1053 while( SDL_PollEvent( &e ) )
1055 if( e.type == SDL_QUIT )
1062 #elif (defined WIN32)
1065 while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE | PM_NOYIELD ) )
1067 if( msg.message == WM_QUIT )
1074 TranslateMessage( &msg );
1075 DispatchMessage( &msg );
1087 // allow game chance to pause
1088 gameFocused = false;
1092 // game is not in focus, give CPU time to other apps by waiting for messages instead of 'peeking'
1096 //if (GetMessage( &msg, g_windowHandle, 0, 0 ))
1097 /*if (GetMessage( &msg, NULL, 0, 0 ))
1099 TranslateMessage(&msg);
1100 DispatchMessage(&msg);
1102 if ( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
1106 // handle the error and possibly exit
1111 TranslateMessage(&msg);
1112 DispatchMessage(&msg);
1116 STUBBED("give up CPU but sniff the event queue");
1121 regnow = game.registernow;
1126 // if(game.registernow){
1130 STUBBED("launch a web browser");
1133 sprintf(url,"http://www.wolfire.com/registerpc.html");
1135 ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);
1140 catch (const std::exception& error)
1144 std::string e = "Caught exception: ";
1147 LOG(e, Logger::LOG_ERR);
1149 MessageBox(g_windowHandle, error.what(), "ERROR", MB_OK | MB_ICONEXCLAMATION);
1159 // --------------------------------------------------------------------------
1162 #define MAX_WINKEYS 256
1163 static unsigned short KeyTable[MAX_WINKEYS]=
1166 MAC_MOUSEBUTTON1, // VK_LBUTTON (1)
1167 MAC_MOUSEBUTTON2, // VK_RBUTTON (2)
1168 0xffff, // VK_CANCEL (3)
1169 0xffff, // VK_MBUTTON (4)
1173 MAC_DELETE_KEY, // VK_BACK (8)
1174 MAC_TAB_KEY, // VK_TAB (9)
1177 0xffff, // VK_CLEAR (12)
1178 MAC_RETURN_KEY, // VK_RETURN (13)
1181 MAC_SHIFT_KEY, // VK_SHIFT (16)
1182 MAC_CONTROL_KEY, // VK_CONTROL (17)
1183 MAC_OPTION_KEY, // VK_MENU (18)
1184 0xffff, // VK_PAUSE (19)
1185 MAC_CAPS_LOCK_KEY, // #define VK_CAPITAL (20)
1192 MAC_ESCAPE_KEY, // VK_ESCAPE (27)
1197 MAC_SPACE_KEY, // VK_SPACE (32)
1198 MAC_PAGE_UP_KEY, // VK_PRIOR (33)
1199 MAC_PAGE_DOWN_KEY, // VK_NEXT (34)
1200 MAC_END_KEY, // VK_END (35)
1201 MAC_HOME_KEY, // VK_HOME (36)
1202 MAC_ARROW_LEFT_KEY, // VK_LEFT (37)
1203 MAC_ARROW_UP_KEY, // VK_UP (38)
1204 MAC_ARROW_RIGHT_KEY, // VK_RIGHT (39)
1205 MAC_ARROW_DOWN_KEY, // VK_DOWN (40)
1206 0xffff, // VK_SELECT (41)
1207 0xffff, // VK_PRINT (42)
1208 0xffff, // VK_EXECUTE (43)
1209 0xffff, // VK_SNAPSHOT (44)
1210 MAC_INSERT_KEY, // VK_INSERT (45)
1211 MAC_DEL_KEY, // VK_DELETE (46)
1212 0xffff, // VK_HELP (47)
1213 MAC_0_KEY, // VK_0 (48)
1214 MAC_1_KEY, // VK_1 (49)
1215 MAC_2_KEY, // VK_2 (50)
1216 MAC_3_KEY, // VK_3 (51)
1217 MAC_4_KEY, // VK_4 (52)
1218 MAC_5_KEY, // VK_5 (53)
1219 MAC_6_KEY, // VK_6 (54)
1220 MAC_7_KEY, // VK_7 (55)
1221 MAC_8_KEY, // VK_8 (56)
1222 MAC_9_KEY, // VK_9 (57)
1230 MAC_A_KEY, // VK_A (65)
1231 MAC_B_KEY, // VK_B (66)
1232 MAC_C_KEY, // VK_C (67)
1233 MAC_D_KEY, // VK_D (68)
1234 MAC_E_KEY, // VK_E (69)
1235 MAC_F_KEY, // VK_F (70)
1236 MAC_G_KEY, // VK_G (71)
1237 MAC_H_KEY, // VK_H (72)
1238 MAC_I_KEY, // VK_I (73)
1239 MAC_J_KEY, // VK_J (74)
1240 MAC_K_KEY, // VK_K (75)
1241 MAC_L_KEY, // VK_L (76)
1242 MAC_M_KEY, // VK_M (77)
1243 MAC_N_KEY, // VK_N (78)
1244 MAC_O_KEY, // VK_O (79)
1245 MAC_P_KEY, // VK_P (80)
1246 MAC_Q_KEY, // VK_Q (81)
1247 MAC_R_KEY, // VK_R (82)
1248 MAC_S_KEY, // VK_S (83)
1249 MAC_T_KEY, // VK_T (84)
1250 MAC_U_KEY, // VK_U (85)
1251 MAC_V_KEY, // VK_V (86)
1252 MAC_W_KEY, // VK_W (87)
1253 MAC_X_KEY, // VK_X (88)
1254 MAC_Y_KEY, // VK_Y (89)
1255 MAC_Z_KEY, // VK_Z (90)
1261 MAC_NUMPAD_0_KEY, // VK_NUMPAD0 (96)
1262 MAC_NUMPAD_1_KEY, // VK_NUMPAD1 (97)
1263 MAC_NUMPAD_2_KEY, // VK_NUMPAD2 (98)
1264 MAC_NUMPAD_3_KEY, // VK_NUMPAD3 (99)
1265 MAC_NUMPAD_4_KEY, // VK_NUMPAD4 (100)
1266 MAC_NUMPAD_5_KEY, // VK_NUMPAD5 (101)
1267 MAC_NUMPAD_6_KEY, // VK_NUMPAD6 (102)
1268 MAC_NUMPAD_7_KEY, // VK_NUMPAD7 (103)
1269 MAC_NUMPAD_8_KEY, // VK_NUMPAD8 (104)
1270 MAC_NUMPAD_9_KEY, // VK_NUMPAD9 (105)
1271 MAC_NUMPAD_ASTERISK_KEY, // VK_MULTIPLY (106)
1272 MAC_NUMPAD_PLUS_KEY, // VK_ADD (107)
1273 MAC_NUMPAD_ENTER_KEY, // VK_SEPARATOR (108)
1274 MAC_NUMPAD_MINUS_KEY, // VK_SUBTRACT (109)
1275 MAC_NUMPAD_PERIOD_KEY, // VK_DECIMAL (110)
1276 MAC_NUMPAD_SLASH_KEY, // VK_DIVIDE (111)
1277 MAC_F1_KEY, // VK_F1 (112)
1278 MAC_F2_KEY, // VK_F2 (113)
1279 MAC_F3_KEY, // VK_F3 (114)
1280 MAC_F4_KEY, // VK_F4 (115)
1281 MAC_F5_KEY, // VK_F5 (116)
1282 MAC_F6_KEY, // VK_F6 (117)
1283 MAC_F7_KEY, // VK_F7 (118)
1284 MAC_F8_KEY, // VK_F8 (119)
1285 MAC_F9_KEY, // VK_F9 (120)
1286 MAC_F10_KEY, // VK_F10 (121)
1287 MAC_F11_KEY, // VK_F11 (122)
1288 MAC_F12_KEY, // VK_F12 (123)
1309 0xffff, // VK_NUMLOCK (144)
1310 0xffff, // VK_SCROLL (145)
1325 MAC_SHIFT_KEY, // VK_LSHIFT (160)
1326 MAC_SHIFT_KEY, // VK_RSHIFT (161)
1327 MAC_CONTROL_KEY, // VK_LCONTROL (162)
1328 MAC_CONTROL_KEY, // VK_RCONTROL (163)
1329 MAC_OPTION_KEY, // VK_LMENU (164)
1330 MAC_OPTION_KEY, // VK_RMENU (165)
1351 MAC_SEMICOLON_KEY, // (186)
1352 MAC_PLUS_KEY, // (187)
1353 MAC_COMMA_KEY, // (188)
1354 MAC_MINUS_KEY, // (189)
1355 MAC_PERIOD_KEY, // (190)
1356 MAC_SLASH_KEY, // (191)
1357 MAC_TILDE_KEY, // (192)
1384 MAC_LEFTBRACKET_KEY, // (219)
1385 MAC_BACKSLASH_KEY, // (220)
1386 MAC_RIGHTBRACKET_KEY, // (221)
1387 MAC_APOSTROPHE_KEY, // (222)
1424 static KeyMap g_theKeys;
1426 void SetKey( int key)
1428 g_theKeys[ key >> 3] |= (1 << (key & 7));
1431 void ClearKey( int key)
1433 g_theKeys[ key >> 3] &= (0xff ^ (1 << (key & 7)));
1436 void GetKeys( unsigned char theKeys[16])
1438 memcpy( theKeys, &g_theKeys, 16);
1448 void ClipMouseToWindow(HWND window)
1452 GetClientRect(window, &wRect);
1454 ClientToScreen(window, (LPPOINT)&wRect.left);
1455 ClientToScreen(window, (LPPOINT)&wRect.right);
1462 LRESULT FAR PASCAL AppWndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
1464 /* this is where we receive all messages concerning this window
1465 * we can either process a message or pass it on to the default
1466 * message handler of windows */
1468 static PAINTSTRUCT ps;
1472 case WM_ACTIVATE: // Watch For Window Activate Message
1474 // Check Minimization State
1475 BOOL iconified = HIWORD(wParam) ? TRUE : FALSE;
1477 if (LOWORD(wParam) == WA_INACTIVE)
1486 CloseWindow( hWnd );
1488 // The window is now iconified
1489 iconified = GL_TRUE;
1495 g_focused=false; // Program Is Active
1499 SetupDSpFullScreen();
1506 // The window is now iconified
1507 iconified = GL_FALSE;
1510 ShowWindow( hWnd, SW_SHOW );
1511 SetForegroundWindow( hWnd );
1515 ClipMouseToWindow(hWnd);
1516 g_focused=true; // Program Is No Longer Active
1519 return 0; // Return To The Message Loop
1525 // check for Alt-F4 (exit hotkey)
1526 if (wParam == VK_F4)
1528 if (GetKeyState( VK_MENU) & 0x8080)
1534 if (wParam < MAX_WINKEYS)
1536 if (KeyTable[wParam] != 0xffff)
1537 SetKey( KeyTable[wParam]);
1545 if (wParam < MAX_WINKEYS)
1546 if (KeyTable[wParam] != 0xffff)
1547 ClearKey( KeyTable[wParam]);
1554 case WM_SYSDEADCHAR:
1557 case WM_NCLBUTTONDOWN:
1558 case WM_LBUTTONDOWN:
1565 case WM_NCRBUTTONDOWN:
1566 case WM_RBUTTONDOWN:
1572 case WM_NCMBUTTONDOWN:
1573 case WM_MBUTTONDOWN:
1579 case WM_NCLBUTTONUP:
1583 buttons[ 0] = false;
1587 case WM_NCRBUTTONUP:
1590 buttons[ 1] = false;
1594 case WM_NCMBUTTONUP:
1597 buttons[ 2] = false;
1601 case WM_NCLBUTTONDBLCLK:
1602 case WM_NCRBUTTONDBLCLK:
1603 case WM_NCMBUTTONDBLCLK:
1604 case WM_LBUTTONDBLCLK:
1606 case WM_RBUTTONDBLCLK:
1607 case WM_MBUTTONDBLCLK:
1610 case WM_NCMOUSEMOVE:
1612 /* ((WindowInfo *)g_lastWindow->GetInfo())->m_mouseX = (signed short)(lParam & 0xffff);
1613 ((WindowInfo *)g_lastWindow->GetInfo())->m_mouseY = (signed short)(lParam >> 16);
1614 if (g_lastWindow->m_mouseCallbacksEnabled) g_lastWindow->MouseMoveCallback();
1615 */// goto winmessage;
1618 case WM_SYSCOMMAND: // Intercept System Commands
1620 switch (wParam) // Check System Calls
1622 case SC_SCREENSAVE: // Screensaver Trying To Start?
1623 case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
1624 return 0; // Prevent From Happening
1626 // User trying to access application menu using ALT?
1635 // ReleaseCapture();
1636 // ClipMouseToWindow(hWnd);
1653 PostQuitMessage(0); /* Terminate Application */
1661 // BeginPaint( g_windowHandle,&ps);
1662 // EndPaint( g_windowHandle,&ps);
1669 /* We processed the message and there
1670 * is no processing by Windows necessary */
1672 /* We didn't process the message so let Windows do it */
1673 return DefWindowProc(hWnd,msg,wParam,lParam);
1677 static BOOL RegisterWindowClasses(HINSTANCE hFirstInstance)
1680 memset( &wc, 0, sizeof( wc));
1682 /* Register the window class. */
1683 wc.cbSize = sizeof(wc);
1685 wc.style = (CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_OWNDC); /* Combination of Class Styles */
1686 wc.lpfnWndProc = AppWndProc; /* Adress of Window Procedure */
1687 wc.cbClsExtra = 0; /* Extra Bytes allocated for this Class */
1688 wc.cbWndExtra = 0; /* Extra Bytes allocated for each Window */
1689 wc.hInstance = hFirstInstance; /* Handle of program instance */
1690 wc.hIcon = LoadIcon( hFirstInstance, MAKEINTRESOURCE(IDI_LUGARU) );
1691 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
1692 wc.hbrBackground = NULL;
1693 wc.lpszMenuName = NULL;
1694 wc.lpszClassName = g_wndClassName; /* Name of the Window Class */
1695 wc.hIconSm = LoadIcon( hFirstInstance, MAKEINTRESOURCE(IDI_LUGARU) );
1697 if (!RegisterClassEx(&wc)) return FALSE; /* Register Class failed */
1703 int resolutionID(int width, int height)
1707 if(width==640 && height==480)whichres=0;
1708 if(width==800 && height==600)whichres=1;
1709 if(width==1024 && height==768)whichres=2;
1710 if(width==1280 && height==1024)whichres=3;
1711 if(width==1600 && height==1200)whichres=4;
1712 if(width==840 && height==524)whichres=5;
1713 if(width==1024 && height==640)whichres=6;
1714 if(width==1344 && height==840)whichres=7;
1719 int closestResolution(int width, int height)
1723 if(width>=640 && height>=480)whichres=0;
1724 if(width>=800 && height>=600)whichres=1;
1725 if(width>=1024 && height>=768)whichres=2;
1726 if(width>=1280 && height>=1024)whichres=3;
1727 if(width>=1600 && height>=1200)whichres=4;
1728 if(width==840 && height==524)whichres=5;
1729 if(width==1024 && height==640)whichres=6;
1730 if(width==1344 && height==840)whichres=7;
1735 bool selectDetail(int & width, int & height, int & bpp, int & detail)
1738 int whichres = closestResolution(width, height);
1742 if(whichres<=0 || whichres>7){
1776 if ((detail != 0) && (resolutionDepths[whichres][1] != 0))
1780 else if ((detail == 0) && (resolutionDepths[whichres][0] != 0))
1784 else if ((detail != 0) && (resolutionDepths[whichres][0] != 0))
1790 else if (0 == whichres)
1798 bpp = resolutionDepths[whichres][(detail != 0)];
1804 int __stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
1807 LPWSTR * cl = CommandLineToArgvW(GetCommandLineW(), &argc);
1810 if (0 == _wcsicmp(cl[1], L"-windowed"))
1818 memset( &g_theKeys, 0, sizeof( KeyMap));
1822 memset(&mode, 0, sizeof(mode));
1823 mode.dmSize = sizeof(mode);
1824 while (EnumDisplaySettings(NULL, i++, &mode))
1826 if (mode.dmBitsPerPel < 16)
1831 int res = resolutionID(mode.dmPelsWidth, mode.dmPelsHeight);
1833 if (res > -1 && res < 8)
1835 if (DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettings(&mode, CDS_TEST))
1840 switch(mode.dmBitsPerPel)
1844 resolutionDepths[res][1] = mode.dmBitsPerPel;
1847 resolutionDepths[res][0] = mode.dmBitsPerPel;
1853 /* if there is no Instance of our program in memory then register the window class */
1854 if (hPrevInstance == NULL && !RegisterWindowClasses(hInstance))
1855 return FALSE; /* registration failed! */
1857 g_appInstance=hInstance;
1861 UnregisterClass( g_wndClassName, hInstance);
1868 extern int channels[100];
1869 extern FSOUND_SAMPLE * samp[100];
1870 extern FSOUND_STREAM * strm[10];
1872 extern "C" void PlaySoundEx(int chan, FSOUND_SAMPLE *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
1874 const FSOUND_SAMPLE * currSample = FSOUND_GetCurrentSample(channels[chan]);
1875 if (currSample && currSample == samp[chan])
1877 if (FSOUND_GetPaused(channels[chan]))
1879 FSOUND_StopSound(channels[chan]);
1880 channels[chan] = FSOUND_FREE;
1882 else if (FSOUND_IsPlaying(channels[chan]))
1884 int loop_mode = FSOUND_GetLoopMode(channels[chan]);
1885 if (loop_mode & FSOUND_LOOP_OFF)
1887 channels[chan] = FSOUND_FREE;
1893 channels[chan] = FSOUND_FREE;
1896 channels[chan] = FSOUND_PlaySoundEx(channels[chan], sptr, dsp, startpaused);
1897 if (channels[chan] < 0)
1899 channels[chan] = FSOUND_PlaySoundEx(FSOUND_FREE, sptr, dsp, startpaused);
1903 extern "C" void PlayStreamEx(int chan, FSOUND_STREAM *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
1905 const FSOUND_SAMPLE * currSample = FSOUND_GetCurrentSample(channels[chan]);
1906 if (currSample && currSample == FSOUND_Stream_GetSample(sptr))
1908 FSOUND_StopSound(channels[chan]);
1909 FSOUND_Stream_Stop(sptr);
1913 FSOUND_Stream_Stop(sptr);
1914 channels[chan] = FSOUND_FREE;
1917 channels[chan] = FSOUND_Stream_PlayEx(channels[chan], sptr, dsp, startpaused);
1918 if (channels[chan] < 0)
1920 channels[chan] = FSOUND_Stream_PlayEx(FSOUND_FREE, sptr, dsp, startpaused);
1924 bool LoadImage(const char * fname, TGAImageRec & tex)
1928 if ( tex.data == NULL )
1933 ILstring f = strdup(ConvertFileName(fname));
1940 ilGenImages(1, &iid);
1945 tex.sizeX = ilGetInteger(IL_IMAGE_WIDTH);
1946 tex.sizeY = ilGetInteger(IL_IMAGE_HEIGHT);
1947 tex.bpp = ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);
1948 ILuint Bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL),
1949 imageSize = tex.sizeX * tex.sizeY * Bpp;
1950 ILubyte *Data = ilGetData();
1951 memcpy(tex.data, Data, imageSize);
1953 // Truvision Targa files are stored as BGR colors
1954 // We want RGB so Blue and Red bytes are switched
1955 if (IL_TGA == ilGetInteger(IL_IMAGE_FORMAT))
1957 // Loop Through The Image Data
1958 for (GLuint i = 0; i < int(imageSize); i += Bpp)
1960 // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
1961 GLbyte temp; // Temporary Variable
1962 temp = tex.data[i]; // Temporarily Store The Value At Image Data 'i'
1963 tex.data[i] = tex.data[i + 2]; // Set The 1st Byte To The Value Of The 3rd Byte
1964 tex.data[i + 2] = temp; // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
1972 ilDeleteImages(1, &iid);
1976 GLuint texid = ilutGLLoadImage(f);
1981 ilutGLBuildMipmaps()
1993 void ScreenShot(const char * fname)
1995 ILstring f = strdup(fname);
2002 ilGenImages(1, &iid);
2008 ilDeleteImages(1, &iid);