19 // just use libpng and libjpg directly; it's lighter-weight and easier
20 // to manage the dependencies on Linux...
22 static bool load_image(const char * fname, TGAImageRec & tex);
23 static bool load_png(const char * fname, TGAImageRec & tex);
24 static bool load_jpg(const char * fname, TGAImageRec & tex);
29 #pragma comment(lib, "opengl32.lib")
30 #pragma comment(lib, "glu32.lib")
31 #pragma comment(lib, "glaux.lib")
34 extern bool buttons[3];
35 extern float multiplier;
36 extern float screenwidth,screenheight;
38 extern float realmultiplier;
40 extern bool ismotionblur;
41 extern float usermousesensitivity;
43 extern bool floatjump;
44 extern bool cellophane;
46 //extern int terraindetail;
47 //extern int texdetail;
48 extern float terraindetail;
49 extern float texdetail;
50 extern int bloodtoggle;
52 extern bool autoslomo;
54 extern bool musictoggle;
55 extern bool trilinear;
56 extern float gamespeed;
57 extern int difficulty;
58 extern bool damageeffects;
59 extern int numplayers;
61 extern bool invertmouse;
62 extern bool texttoggle;
63 extern bool ambientsound;
64 extern bool mousejump;
66 extern Person player[maxplayers];
68 extern bool stillloading;
69 extern bool showpoints;
70 extern bool alwaysblur;
71 extern bool immediate;
72 extern bool velocityblur;
73 extern bool debugmode;
75 /*extern*/ bool gameFocused;
76 extern int kBitsPerPixel;
77 extern float slomospeed;
78 extern float slomofreq;
79 extern float oldgamespeed;
88 #include "MacCompatibility.h"
96 #include "res/resource.h"
101 unsigned int resolutionDepths[8][2] = {0};
103 bool selectDetail(int & width, int & height, int & bpp, int & detail);
104 int closestResolution(int width, int height);
105 int resolutionID(int width, int height);
107 void ReportError (char * strError);
109 void SetupDSpFullScreen();
112 void DrawGL(Game & game);
114 void CreateGLWindow (void);
115 Boolean SetUp (Game & game);
116 void DoKey (SInt8 theKey, SInt8 theCode);
117 void DoUpdate (Game & game);
123 // statics/globals (internal only) ------------------------------------------
125 typedef struct tagPOINT {
132 void sdlGetCursorPos(POINT *pt)
135 SDL_GetMouseState(&x, &y);
139 #define GetCursorPos(x) sdlGetCursorPos(x)
140 #define SetCursorPos(x, y) SDL_WarpMouse(x, y)
141 #define ScreenToClient(x, pt)
142 #define ClientToScreen(x, pt)
143 #define MessageBox(hwnd,text,title,flags) STUBBED("msgbox")
149 static const char g_wndClassName[]={ "LUGARUWINDOWCLASS" };
150 static HINSTANCE g_appInstance;
151 static HWND g_windowHandle;
155 static bool g_button, fullscreen = true;
166 kForegroundSleep = 10,
167 kBackgroundSleep = 10000
174 const RGBColor rgbBlack = { 0x0000, 0x0000, 0x0000 };
177 char gcstrMode [256] = "";
179 UInt32 gSleepTime = kForegroundSleep;
180 Boolean gDone = false, gfFrontProcess = true;
184 static bool gMouseGrabbed = true;
186 // --------------------------------------------------------------------------
188 void ReportError (char * strError)
190 #ifdef WIN32 // !!! FIXME. --ryan.
191 throw std::exception( strError);
194 /* char errMsgCStr [256];
197 sprintf (errMsgCStr, "%s", strError);
199 // out as debug string
200 CToPStr (strErr, errMsgCStr);
205 void SetupDSpFullScreen ()
212 DEVMODE dmScreenSettings;
213 memset( &dmScreenSettings, 0, sizeof( dmScreenSettings));
214 dmScreenSettings.dmSize = sizeof( dmScreenSettings);
215 dmScreenSettings.dmPelsWidth = kContextWidth;
216 dmScreenSettings.dmPelsHeight = kContextHeight;
217 dmScreenSettings.dmBitsPerPel = kBitsPerPixel;
218 dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
221 if (ChangeDisplaySettings( &dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
223 ReportError( "Could not set display mode");
240 ChangeDisplaySettings( NULL, 0);
248 //-----------------------------------------------------------------------------------------------------------------------
252 void DrawGL (Game & game)
263 static KeyMap g_theKeys;
265 void SetKey( int key)
267 g_theKeys[ key >> 3] |= (1 << (key & 7));
270 void ClearKey( int key)
272 g_theKeys[ key >> 3] &= (0xff ^ (1 << (key & 7)));
275 void GetKeys( unsigned char theKeys[16])
277 memcpy( theKeys, &g_theKeys, 16);
286 static void initSDLKeyTable(void) {}
288 #define MAX_SDLKEYS SDLK_LAST
289 static unsigned short KeyTable[MAX_SDLKEYS];
291 static void initSDLKeyTable(void)
293 memset(KeyTable, 0xFF, sizeof (KeyTable));
294 KeyTable[SDLK_BACKSPACE] = MAC_DELETE_KEY;
295 KeyTable[SDLK_TAB] = MAC_TAB_KEY;
296 KeyTable[SDLK_RETURN] = MAC_RETURN_KEY;
297 KeyTable[SDLK_ESCAPE] = MAC_ESCAPE_KEY;
298 KeyTable[SDLK_SPACE] = MAC_SPACE_KEY;
299 KeyTable[SDLK_PAGEUP] = MAC_PAGE_UP_KEY;
300 KeyTable[SDLK_PAGEDOWN] = MAC_PAGE_DOWN_KEY;
301 KeyTable[SDLK_END] = MAC_END_KEY;
302 KeyTable[SDLK_HOME] = MAC_HOME_KEY;
303 KeyTable[SDLK_LEFT] = MAC_ARROW_LEFT_KEY;
304 KeyTable[SDLK_UP] = MAC_ARROW_UP_KEY;
305 KeyTable[SDLK_RIGHT] = MAC_ARROW_RIGHT_KEY;
306 KeyTable[SDLK_DOWN] = MAC_ARROW_DOWN_KEY;
307 KeyTable[SDLK_INSERT] = MAC_INSERT_KEY;
308 KeyTable[SDLK_DELETE] = MAC_DEL_KEY;
309 KeyTable[SDLK_0] = MAC_0_KEY;
310 KeyTable[SDLK_1] = MAC_1_KEY;
311 KeyTable[SDLK_2] = MAC_2_KEY;
312 KeyTable[SDLK_3] = MAC_3_KEY;
313 KeyTable[SDLK_4] = MAC_4_KEY;
314 KeyTable[SDLK_5] = MAC_5_KEY;
315 KeyTable[SDLK_6] = MAC_6_KEY;
316 KeyTable[SDLK_7] = MAC_7_KEY;
317 KeyTable[SDLK_8] = MAC_8_KEY;
318 KeyTable[SDLK_9] = MAC_9_KEY;
319 KeyTable[SDLK_a] = MAC_A_KEY;
320 KeyTable[SDLK_b] = MAC_B_KEY;
321 KeyTable[SDLK_c] = MAC_C_KEY;
322 KeyTable[SDLK_d] = MAC_D_KEY;
323 KeyTable[SDLK_e] = MAC_E_KEY;
324 KeyTable[SDLK_f] = MAC_F_KEY;
325 KeyTable[SDLK_g] = MAC_G_KEY;
326 KeyTable[SDLK_h] = MAC_H_KEY;
327 KeyTable[SDLK_i] = MAC_I_KEY;
328 KeyTable[SDLK_j] = MAC_J_KEY;
329 KeyTable[SDLK_k] = MAC_K_KEY;
330 KeyTable[SDLK_l] = MAC_L_KEY;
331 KeyTable[SDLK_m] = MAC_M_KEY;
332 KeyTable[SDLK_n] = MAC_N_KEY;
333 KeyTable[SDLK_o] = MAC_O_KEY;
334 KeyTable[SDLK_p] = MAC_P_KEY;
335 KeyTable[SDLK_q] = MAC_Q_KEY;
336 KeyTable[SDLK_r] = MAC_R_KEY;
337 KeyTable[SDLK_s] = MAC_S_KEY;
338 KeyTable[SDLK_t] = MAC_T_KEY;
339 KeyTable[SDLK_u] = MAC_U_KEY;
340 KeyTable[SDLK_v] = MAC_V_KEY;
341 KeyTable[SDLK_w] = MAC_W_KEY;
342 KeyTable[SDLK_x] = MAC_X_KEY;
343 KeyTable[SDLK_y] = MAC_Y_KEY;
344 KeyTable[SDLK_z] = MAC_Z_KEY;
345 KeyTable[SDLK_KP0] = MAC_NUMPAD_0_KEY;
346 KeyTable[SDLK_KP1] = MAC_NUMPAD_1_KEY;
347 KeyTable[SDLK_KP2] = MAC_NUMPAD_2_KEY;
348 KeyTable[SDLK_KP3] = MAC_NUMPAD_3_KEY;
349 KeyTable[SDLK_KP4] = MAC_NUMPAD_4_KEY;
350 KeyTable[SDLK_KP5] = MAC_NUMPAD_5_KEY;
351 KeyTable[SDLK_KP6] = MAC_NUMPAD_6_KEY;
352 KeyTable[SDLK_KP7] = MAC_NUMPAD_7_KEY;
353 KeyTable[SDLK_KP8] = MAC_NUMPAD_8_KEY;
354 KeyTable[SDLK_KP9] = MAC_NUMPAD_9_KEY;
355 KeyTable[SDLK_KP_MULTIPLY] = MAC_NUMPAD_ASTERISK_KEY;
356 KeyTable[SDLK_KP_PLUS] = MAC_NUMPAD_PLUS_KEY;
357 KeyTable[SDLK_KP_ENTER] = MAC_NUMPAD_ENTER_KEY;
358 KeyTable[SDLK_KP_MINUS] = MAC_NUMPAD_MINUS_KEY;
359 KeyTable[SDLK_KP_PERIOD] = MAC_NUMPAD_PERIOD_KEY;
360 KeyTable[SDLK_KP_DIVIDE] = MAC_NUMPAD_SLASH_KEY;
361 KeyTable[SDLK_F1] = MAC_F1_KEY;
362 KeyTable[SDLK_F2] = MAC_F2_KEY;
363 KeyTable[SDLK_F3] = MAC_F3_KEY;
364 KeyTable[SDLK_F4] = MAC_F4_KEY;
365 KeyTable[SDLK_F5] = MAC_F5_KEY;
366 KeyTable[SDLK_F6] = MAC_F6_KEY;
367 KeyTable[SDLK_F7] = MAC_F7_KEY;
368 KeyTable[SDLK_F8] = MAC_F8_KEY;
369 KeyTable[SDLK_F9] = MAC_F9_KEY;
370 KeyTable[SDLK_F10] = MAC_F10_KEY;
371 KeyTable[SDLK_F11] = MAC_F11_KEY;
372 KeyTable[SDLK_F12] = MAC_F12_KEY;
373 KeyTable[SDLK_SEMICOLON] = MAC_SEMICOLON_KEY;
374 KeyTable[SDLK_PLUS] = MAC_PLUS_KEY;
375 KeyTable[SDLK_COMMA] = MAC_COMMA_KEY;
376 KeyTable[SDLK_MINUS] = MAC_MINUS_KEY;
377 KeyTable[SDLK_PERIOD] = MAC_PERIOD_KEY;
378 KeyTable[SDLK_SLASH] = MAC_SLASH_KEY;
379 KeyTable[SDLK_BACKQUOTE] = MAC_TILDE_KEY;
380 KeyTable[SDLK_LEFTBRACKET] = MAC_LEFTBRACKET_KEY;
381 KeyTable[SDLK_BACKSLASH] = MAC_BACKSLASH_KEY;
382 KeyTable[SDLK_RIGHTBRACKET] = MAC_RIGHTBRACKET_KEY;
383 KeyTable[SDLK_QUOTE] = MAC_APOSTROPHE_KEY;
386 static inline int clamp_sdl_mouse_button(Uint8 button)
388 if ((button >= 1) && (button <= 3))
393 static void sdlEventProc(const SDL_Event &e)
398 case SDL_MOUSEBUTTONDOWN:
400 val = clamp_sdl_mouse_button(e.button.button);
406 SetKey(MAC_MOUSEBUTTON1);
410 SetKey(MAC_MOUSEBUTTON2);
417 case SDL_MOUSEBUTTONUP:
419 val = clamp_sdl_mouse_button(e.button.button);
425 ClearKey(MAC_MOUSEBUTTON1);
429 ClearKey(MAC_MOUSEBUTTON2);
431 buttons[val] = false;
437 if (e.key.keysym.sym == SDLK_g)
439 if (e.key.keysym.mod & KMOD_CTRL)
440 gMouseGrabbed = !gMouseGrabbed;
443 else if (e.key.keysym.sym == SDLK_RETURN)
445 if (e.key.keysym.mod & KMOD_ALT)
446 SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
449 if (e.key.keysym.sym < SDLK_LAST)
451 if (KeyTable[e.key.keysym.sym] != 0xffff)
452 SetKey(KeyTable[e.key.keysym.sym]);
455 if (e.key.keysym.mod & KMOD_CTRL)
456 SetKey(MAC_CONTROL_KEY);
457 if (e.key.keysym.mod & KMOD_ALT)
458 SetKey(MAC_OPTION_KEY);
459 if (e.key.keysym.mod & KMOD_SHIFT)
460 SetKey(MAC_SHIFT_KEY);
461 if (e.key.keysym.mod & KMOD_CAPS)
462 SetKey(MAC_CAPS_LOCK_KEY);
467 if (e.key.keysym.sym < SDLK_LAST)
469 if (KeyTable[e.key.keysym.sym] != 0xffff)
470 ClearKey(KeyTable[e.key.keysym.sym]);
473 if (e.key.keysym.mod & KMOD_CTRL)
474 ClearKey(MAC_CONTROL_KEY);
475 if (e.key.keysym.mod & KMOD_ALT)
476 ClearKey(MAC_OPTION_KEY);
477 if (e.key.keysym.mod & KMOD_SHIFT)
478 ClearKey(MAC_SHIFT_KEY);
479 if (e.key.keysym.mod & KMOD_CAPS)
480 ClearKey(MAC_CAPS_LOCK_KEY);
486 // --------------------------------------------------------------------------
488 static Point gMidPoint;
490 Boolean SetUp (Game & game)
496 randSeed = UpTime().lo;
499 // ifstream ipstream(":Data:config.txt", std::ios::in /*| std::ios::nocreate*/);
500 ifstream ipstream("./Data/config.txt", std::ios::in /*| std::ios::nocreate*/);
503 usermousesensitivity=1;
532 game.crouchkey=MAC_SHIFT_KEY;
533 game.jumpkey=MAC_SPACE_KEY;
534 game.leftkey=MAC_A_KEY;
535 game.forwardkey=MAC_W_KEY;
536 game.backkey=MAC_S_KEY;
537 game.rightkey=MAC_D_KEY;
538 game.drawkey=MAC_E_KEY;
539 game.throwkey=MAC_Q_KEY;
540 game.attackkey=MAC_MOUSEBUTTON1;
541 game.chatkey=MAC_T_KEY;
547 selectDetail(kContextWidth, kContextHeight, kBitsPerPixel, detail);
550 //ofstream opstream(":Data:config.txt");
551 ofstream opstream("./Data/config.txt");
552 opstream << "Screenwidth:\n";
553 opstream << kContextWidth;
554 opstream << "\nScreenheight:\n";
555 opstream << kContextHeight;
556 opstream << "\nMouse sensitivity:\n";
557 opstream << usermousesensitivity;
558 opstream << "\nBlur(0,1):\n";
559 opstream << ismotionblur;
560 opstream << "\nOverall Detail(0,1,2) higher=better:\n";
562 opstream << "\nFloating jump:\n";
563 opstream << floatjump;
564 opstream << "\nMouse jump:\n";
565 opstream << mousejump;
566 opstream << "\nAmbient sound:\n";
567 opstream << ambientsound;
568 opstream << "\nBlood (0,1,2):\n";
569 opstream << bloodtoggle;
570 opstream << "\nAuto slomo:\n";
571 opstream << autoslomo;
572 opstream << "\nFoliage:\n";
574 opstream << "\nMusic:\n";
575 opstream << musictoggle;
576 opstream << "\nTrilinear:\n";
577 opstream << trilinear;
578 opstream << "\nDecals(shadows,blood puddles,etc):\n";
580 opstream << "\nInvert mouse:\n";
581 opstream << invertmouse;
582 opstream << "\nGamespeed:\n";
583 opstream << gamespeed;
584 opstream << "\nDifficulty(0,1,2) higher=harder:\n";
585 opstream << difficulty;
586 opstream << "\nDamage effects(blackout, doublevision):\n";
587 opstream << damageeffects;
588 opstream << "\nText:\n";
589 opstream << texttoggle;
590 opstream << "\nDebug:\n";
591 opstream << debugmode;
592 opstream << "\nVBL Sync:\n";
594 opstream << "\nShow Points:\n";
595 opstream << showpoints;
596 opstream << "\nAlways Blur:\n";
597 opstream << alwaysblur;
598 opstream << "\nImmediate mode (turn on on G5):\n";
599 opstream << immediate;
600 opstream << "\nVelocity blur:\n";
601 opstream << velocityblur;
602 opstream << "\nVolume:\n";
604 opstream << "\nForward key:\n";
605 opstream << KeyToChar(game.forwardkey);
606 opstream << "\nBack key:\n";
607 opstream << KeyToChar(game.backkey);
608 opstream << "\nLeft key:\n";
609 opstream << KeyToChar(game.leftkey);
610 opstream << "\nRight key:\n";
611 opstream << KeyToChar(game.rightkey);
612 opstream << "\nJump key:\n";
613 opstream << KeyToChar(game.jumpkey);
614 opstream << "\nCrouch key:\n";
615 opstream << KeyToChar(game.crouchkey);
616 opstream << "\nDraw key:\n";
617 opstream << KeyToChar(game.drawkey);
618 opstream << "\nThrow key:\n";
619 opstream << KeyToChar(game.throwkey);
620 opstream << "\nAttack key:\n";
621 opstream << KeyToChar(game.attackkey);
622 opstream << "\nChat key:\n";
623 opstream << KeyToChar(game.chatkey);
628 ipstream.ignore(256,'\n');
629 ipstream >> kContextWidth;
630 ipstream.ignore(256,'\n');
631 ipstream.ignore(256,'\n');
632 ipstream >> kContextHeight;
633 ipstream.ignore(256,'\n');
634 ipstream.ignore(256,'\n');
635 ipstream >> usermousesensitivity;
636 ipstream.ignore(256,'\n');
637 ipstream.ignore(256,'\n');
639 ismotionblur = (i != 0);
640 ipstream.ignore(256,'\n');
641 ipstream.ignore(256,'\n');
643 if(detail!=0)kBitsPerPixel=32;
644 else kBitsPerPixel=16;
645 ipstream.ignore(256,'\n');
646 ipstream.ignore(256,'\n');
648 floatjump = (i != 0);
649 ipstream.ignore(256,'\n');
650 ipstream.ignore(256,'\n');
652 mousejump = (i != 0);
653 ipstream.ignore(256,'\n');
654 ipstream.ignore(256,'\n');
656 ambientsound = (i != 0);
657 ipstream.ignore(256,'\n');
658 ipstream.ignore(256,'\n');
659 ipstream >> bloodtoggle;
660 ipstream.ignore(256,'\n');
661 ipstream.ignore(256,'\n');
663 autoslomo = (i != 0);
664 ipstream.ignore(256,'\n');
665 ipstream.ignore(256,'\n');
668 ipstream.ignore(256,'\n');
669 ipstream.ignore(256,'\n');
671 musictoggle = (i != 0);
672 ipstream.ignore(256,'\n');
673 ipstream.ignore(256,'\n');
675 trilinear = (i != 0);
676 ipstream.ignore(256,'\n');
677 ipstream.ignore(256,'\n');
680 ipstream.ignore(256,'\n');
681 ipstream.ignore(256,'\n');
683 invertmouse = (i != 0);
684 ipstream.ignore(256,'\n');
685 ipstream.ignore(256,'\n');
686 ipstream >> gamespeed;
687 oldgamespeed=gamespeed;
692 ipstream.ignore(256,'\n');
693 ipstream.ignore(256,'\n');
694 ipstream >> difficulty;
695 ipstream.ignore(256,'\n');
696 ipstream.ignore(256,'\n');
698 damageeffects = (i != 0);
699 ipstream.ignore(256,'\n');
700 ipstream.ignore(256,'\n');
702 texttoggle = (i != 0);
703 ipstream.ignore(256,'\n');
704 ipstream.ignore(256,'\n');
706 debugmode = (i != 0);
707 ipstream.ignore(256,'\n');
708 ipstream.ignore(256,'\n');
711 ipstream.ignore(256,'\n');
712 ipstream.ignore(256,'\n');
714 showpoints = (i != 0);
715 ipstream.ignore(256,'\n');
716 ipstream.ignore(256,'\n');
718 alwaysblur = (i != 0);
719 ipstream.ignore(256,'\n');
720 ipstream.ignore(256,'\n');
722 immediate = (i != 0);
723 ipstream.ignore(256,'\n');
724 ipstream.ignore(256,'\n');
726 velocityblur = (i != 0);
727 ipstream.ignore(256,'\n');
728 ipstream.ignore(256,'\n');
730 ipstream.ignore(256,'\n');
731 ipstream.ignore(256,'\n');
733 game.forwardkey=CharToKey(string);
734 ipstream.ignore(256,'\n');
735 ipstream.ignore(256,'\n');
737 game.backkey=CharToKey(string);
738 ipstream.ignore(256,'\n');
739 ipstream.ignore(256,'\n');
741 game.leftkey=CharToKey(string);
742 ipstream.ignore(256,'\n');
743 ipstream.ignore(256,'\n');
745 game.rightkey=CharToKey(string);
746 ipstream.ignore(256,'\n');
747 ipstream.ignore(256,'\n');
749 game.jumpkey=CharToKey(string);
750 ipstream.ignore(256,'\n');
751 ipstream.ignore(256,'\n');
753 game.crouchkey=CharToKey(string);
754 ipstream.ignore(256,'\n');
755 ipstream.ignore(256,'\n');
757 game.drawkey=CharToKey(string);
758 ipstream.ignore(256,'\n');
759 ipstream.ignore(256,'\n');
761 game.throwkey=CharToKey(string);
762 ipstream.ignore(256,'\n');
763 ipstream.ignore(256,'\n');
765 game.attackkey=CharToKey(string);
766 ipstream.ignore(256,'\n');
767 ipstream.ignore(256,'\n');
769 game.chatkey=CharToKey(string);
772 if(detail>2)detail=2;
773 if(detail<0)detail=0;
774 if(screenwidth>3000)screenwidth=640;
775 if(screenwidth<0)screenwidth=640;
776 if(screenheight>3000)screenheight=480;
777 if(screenheight<0)screenheight=480;
779 if(kBitsPerPixel!=32&&kBitsPerPixel!=16){
784 selectDetail(kContextWidth, kContextHeight, kBitsPerPixel, detail);
786 SetupDSpFullScreen();
789 if (!SDL_WasInit(SDL_INIT_VIDEO))
791 if (SDL_Init(SDL_INIT_VIDEO) == -1)
793 fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
798 Uint32 sdlflags = SDL_OPENGL;
799 SDL_WM_SetCaption("Lugaru", "lugaru");
801 if (SDL_SetVideoMode(kContextWidth, kContextHeight, 0, sdlflags) == NULL)
803 fprintf(stderr, "SDL_SetVideoMode() failed: %s\n", SDL_GetError());
807 #elif (defined WIN32)
808 //------------------------------------------------------------------
811 RECT r = {0, 0, kContextWidth-1, kContextHeight-1};
812 DWORD dwStyle = WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE;
813 DWORD dwExStyle = WS_EX_APPWINDOW;
822 dwStyle |= WS_OVERLAPPEDWINDOW;
823 dwExStyle |= WS_EX_WINDOWEDGE;
826 AdjustWindowRectEx(&r, dwStyle, FALSE, dwExStyle);
830 x = (GetSystemMetrics(SM_CXSCREEN) >> 1) - ((r.right - r.left + 1) >> 1);
831 y = (GetSystemMetrics(SM_CYSCREEN) >> 1) - ((r.bottom - r.top + 1) >> 1);
834 g_windowHandle=CreateWindowEx(
836 g_wndClassName, "Lugaru", dwStyle,
838 // kContextWidth, kContextHeight,
839 r.right - r.left + 1, r.bottom - r.top + 1,
840 NULL,NULL,g_appInstance,NULL );
843 ReportError("Could not create window");
847 //------------------------------------------------------------------
850 static PIXELFORMATDESCRIPTOR pfd =
852 sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
854 PFD_DRAW_TO_WINDOW | // Format Must Support Window
855 PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
856 PFD_DOUBLEBUFFER, // Must Support Double Buffering
857 PFD_TYPE_RGBA, // Request An RGBA Format
858 kBitsPerPixel, // Select Our Color Depth
859 0, 0, 0, 0, 0, 0, // Color Bits Ignored
860 0, // No Alpha Buffer
861 0, // Shift Bit Ignored
862 0, // No Accumulation Buffer
863 0, 0, 0, 0, // Accumulation Bits Ignored
864 16, // 16Bit Z-Buffer (Depth Buffer)
865 0, // No Stencil Buffer
866 0, // No Auxiliary Buffer
867 PFD_MAIN_PLANE, // Main Drawing Layer
869 0, 0, 0 // Layer Masks Ignored
872 if (!(hDC = GetDC( g_windowHandle)))
873 ReportError( "Could not get device context");
876 if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
878 ReportError( "Could not find appropriate pixel format");
882 if (!DescribePixelFormat(hDC, PixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd))
884 ReportError( "Could not retrieve pixel format");
888 if (!SetPixelFormat( hDC, PixelFormat, &pfd))
890 ReportError( "Could not set pixel format");
894 if (!(hRC = wglCreateContext(hDC)))
896 ReportError( "Could not create rendering context");
900 if (!wglMakeCurrent(hDC, hRC))
902 ReportError( "Could not activate rendering context");
908 // Place the window above all topmost windows
909 SetWindowPos( g_windowHandle, HWND_TOPMOST, 0,0,0,0,
910 SWP_NOMOVE | SWP_NOSIZE );
913 SetForegroundWindow(g_windowHandle);
914 SetFocus(g_windowHandle);
917 glClear( GL_COLOR_BUFFER_BIT );
921 glDisable( GL_ALPHA_TEST);
922 glDisable( GL_BLEND);
923 glDisable( GL_DEPTH_TEST);
924 // glDisable( GL_DITHER);
926 glDisable( GL_LIGHTING);
927 glDisable( GL_LOGIC_OP);
928 glDisable( GL_STENCIL_TEST);
929 glDisable( GL_TEXTURE_1D);
930 glDisable( GL_TEXTURE_2D);
931 glPixelTransferi( GL_MAP_COLOR, GL_FALSE);
932 glPixelTransferi( GL_RED_SCALE, 1);
933 glPixelTransferi( GL_RED_BIAS, 0);
934 glPixelTransferi( GL_GREEN_SCALE, 1);
935 glPixelTransferi( GL_GREEN_BIAS, 0);
936 glPixelTransferi( GL_BLUE_SCALE, 1);
937 glPixelTransferi( GL_BLUE_BIAS, 0);
938 glPixelTransferi( GL_ALPHA_SCALE, 1);
939 glPixelTransferi( GL_ALPHA_BIAS, 0);
941 // set initial rendering states
942 glShadeModel( GL_SMOOTH);
944 glDepthFunc( GL_LEQUAL);
945 glDepthMask( GL_TRUE);
946 // glDepthRange( FRONT_CLIP, BACK_CLIP);
947 glEnable( GL_DEPTH_TEST);
948 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
949 glCullFace( GL_FRONT);
950 glEnable( GL_CULL_FACE);
951 glEnable( GL_LIGHTING);
952 // glEnable( GL_LIGHT_MODEL_AMBIENT);
953 glEnable( GL_DITHER);
954 glEnable( GL_COLOR_MATERIAL);
955 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
956 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
957 glAlphaFunc( GL_GREATER, 0.5f);
960 if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION ||
961 iluGetInteger(ILU_VERSION_NUM) < ILU_VERSION ||
962 ilutGetInteger(ILUT_VERSION_NUM) < ILUT_VERSION)
964 ReportError("DevIL version is different...exiting!\n");
972 ilutRenderer(ILUT_OPENGL);
974 ilEnable(IL_ORIGIN_SET);
975 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
978 GLint width = kContextWidth;
979 GLint height = kContextHeight;
980 gMidPoint.h = width / 2;
981 gMidPoint.v = height / 2;
985 game.newdetail=detail;
986 game.newscreenwidth=screenwidth;
987 game.newscreenheight=screenheight;
995 static void DoMouse(Game & game)
997 static Point lastMouse = {-1,-1};
1002 ScreenToClient(g_windowHandle, &pos);
1003 globalMouse.h = pos.x;
1004 globalMouse.v = pos.y;
1006 if (lastMouse.h == globalMouse.h && lastMouse.v == globalMouse.v)
1013 static Point virtualMouse = {0,0};
1014 delta = globalMouse;
1016 delta.h -= lastMouse.h;
1017 delta.v -= lastMouse.v;
1018 lastMouse.h = pos.x;
1019 lastMouse.v = pos.y;
1021 if(mainmenu||(abs(delta.h)<10*realmultiplier*1000&&abs(delta.v)<10*realmultiplier*1000)){
1022 game.deltah=delta.h*usermousesensitivity;
1023 game.deltav=delta.v*usermousesensitivity;
1024 game.mousecoordh=globalMouse.h;
1025 game.mousecoordv=globalMouse.v;
1028 if((!mainmenu)&&(gMouseGrabbed))
1030 if(lastMouse.h>gMidPoint.h+100||lastMouse.h<gMidPoint.h-100||lastMouse.v>gMidPoint.v+100||lastMouse.v<gMidPoint.v-100){
1031 pos.x = gMidPoint.h;
1032 pos.y = gMidPoint.v;
1033 ClientToScreen(g_windowHandle, &pos);
1034 //SetCursorPos( gMidPoint.h,gMidPoint.v);
1035 SetCursorPos(pos.x, pos.y);
1036 lastMouse = gMidPoint;
1044 // --------------------------------------------------------------------------
1046 void DoKey (SInt8 theKey, SInt8 theCode)
1051 // --------------------------------------------------------------------------
1055 void DoFrameRate (int update)
1057 static long frames = 0;
1059 static AbsoluteTime time = {0,0};
1060 static AbsoluteTime frametime = {0,0};
1061 AbsoluteTime currTime = UpTime ();
1062 double deltaTime = (float) AbsoluteDeltaToDuration (currTime, frametime);
1064 if (0 > deltaTime) // if negative microseconds
1065 deltaTime /= -1000000.0;
1066 else // else milliseconds
1067 deltaTime /= 1000.0;
1069 multiplier=deltaTime;
1070 if(multiplier<.001)multiplier=.001;
1071 if(multiplier>10)multiplier=10;
1072 if(update)frametime = currTime; // reset for next time interval
1074 deltaTime = (float) AbsoluteDeltaToDuration (currTime, time);
1076 if (0 > deltaTime) // if negative microseconds
1077 deltaTime /= -1000000.0;
1078 else // else milliseconds
1079 deltaTime /= 1000.0;
1081 if (0.001 <= deltaTime) // has update interval passed
1084 time = currTime; // reset for next time interval
1091 void DoUpdate (Game & game)
1093 static float sps=200;
1095 static float oldmult;
1098 if(multiplier>.6)multiplier=.6;
1100 game.fps=1/multiplier;
1102 count = multiplier*sps;
1104 //if(count>10)count=10;
1106 realmultiplier=multiplier;
1107 multiplier*=gamespeed;
1108 if(difficulty==1)multiplier*=.9;
1109 if(difficulty==0)multiplier*=.8;
1111 if(game.loading==4)multiplier*=.00001;
1113 if(slomo&&!mainmenu)multiplier*=slomospeed;
1114 //if(freeze)multiplier*=0.00001;
1116 multiplier/=(float)count;
1122 for(int i=0;i<count;i++)
1128 game.TickOnceAfter();
1129 /* - Debug code to test how many channels were active on average per frame
1130 static long frames = 0;
1132 static AbsoluteTime start = {0,0};
1133 AbsoluteTime currTime = UpTime ();
1134 static int num_channels = 0;
1136 num_channels += FSOUND_GetChannelsPlaying();
1137 double deltaTime = (float) AbsoluteDeltaToDuration (currTime, start);
1139 if (0 > deltaTime) // if negative microseconds
1140 deltaTime /= -1000000.0;
1141 else // else milliseconds
1142 deltaTime /= 1000.0;
1149 float avg_channels = (float)num_channels / (float)frames;
1151 ofstream opstream("log.txt",ios::app);
1152 opstream << "Average frame count: ";
1154 opstream << " frames - ";
1155 opstream << avg_channels;
1156 opstream << " per frame.\n";
1166 // --------------------------------------------------------------------------
1182 #elif (defined WIN32)
1185 wglMakeCurrent( NULL, NULL);
1186 wglDeleteContext( hRC);
1192 ReleaseDC( g_windowHandle, hDC);
1198 ShowWindow( g_windowHandle, SW_HIDE );
1199 DestroyWindow( g_windowHandle);
1200 g_windowHandle = NULL;
1208 // --------------------------------------------------------------------------
1210 static bool g_focused = true;
1213 static bool IsFocused()
1219 if (GetActiveWindow() != g_windowHandle)
1222 if (IsIconic( g_windowHandle))
1233 #ifndef WIN32 // this is in WinMain, too.
1235 memset( &g_theKeys, 0, sizeof( KeyMap));
1242 bool regnow = false;
1247 //ofstream os("error.txt");
1249 //ofstream os("log.txt");
1254 while (!gDone&&!game.quit&&(!game.tryquit||!game.registered))
1260 // check windows messages
1264 while( SDL_PollEvent( &e ) )
1266 if( e.type == SDL_QUIT )
1273 #elif (defined WIN32)
1276 while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE | PM_NOYIELD ) )
1278 if( msg.message == WM_QUIT )
1285 TranslateMessage( &msg );
1286 DispatchMessage( &msg );
1298 // allow game chance to pause
1299 gameFocused = false;
1303 // game is not in focus, give CPU time to other apps by waiting for messages instead of 'peeking'
1307 //if (GetMessage( &msg, g_windowHandle, 0, 0 ))
1308 /*if (GetMessage( &msg, NULL, 0, 0 ))
1310 TranslateMessage(&msg);
1311 DispatchMessage(&msg);
1313 if ( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
1317 // handle the error and possibly exit
1322 TranslateMessage(&msg);
1323 DispatchMessage(&msg);
1327 STUBBED("give up CPU but sniff the event queue");
1332 regnow = game.registernow;
1337 // if(game.registernow){
1341 STUBBED("launch a web browser");
1344 sprintf(url,"http://www.wolfire.com/registerpc.html");
1346 ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);
1351 catch (const std::exception& error)
1355 std::string e = "Caught exception: ";
1358 LOG(e, Logger::LOG_ERR);
1360 MessageBox(g_windowHandle, error.what(), "ERROR", MB_OK | MB_ICONEXCLAMATION);
1370 // --------------------------------------------------------------------------
1373 #define MAX_WINKEYS 256
1374 static unsigned short KeyTable[MAX_WINKEYS]=
1377 MAC_MOUSEBUTTON1, // VK_LBUTTON (1)
1378 MAC_MOUSEBUTTON2, // VK_RBUTTON (2)
1379 0xffff, // VK_CANCEL (3)
1380 0xffff, // VK_MBUTTON (4)
1384 MAC_DELETE_KEY, // VK_BACK (8)
1385 MAC_TAB_KEY, // VK_TAB (9)
1388 0xffff, // VK_CLEAR (12)
1389 MAC_RETURN_KEY, // VK_RETURN (13)
1392 MAC_SHIFT_KEY, // VK_SHIFT (16)
1393 MAC_CONTROL_KEY, // VK_CONTROL (17)
1394 MAC_OPTION_KEY, // VK_MENU (18)
1395 0xffff, // VK_PAUSE (19)
1396 MAC_CAPS_LOCK_KEY, // #define VK_CAPITAL (20)
1403 MAC_ESCAPE_KEY, // VK_ESCAPE (27)
1408 MAC_SPACE_KEY, // VK_SPACE (32)
1409 MAC_PAGE_UP_KEY, // VK_PRIOR (33)
1410 MAC_PAGE_DOWN_KEY, // VK_NEXT (34)
1411 MAC_END_KEY, // VK_END (35)
1412 MAC_HOME_KEY, // VK_HOME (36)
1413 MAC_ARROW_LEFT_KEY, // VK_LEFT (37)
1414 MAC_ARROW_UP_KEY, // VK_UP (38)
1415 MAC_ARROW_RIGHT_KEY, // VK_RIGHT (39)
1416 MAC_ARROW_DOWN_KEY, // VK_DOWN (40)
1417 0xffff, // VK_SELECT (41)
1418 0xffff, // VK_PRINT (42)
1419 0xffff, // VK_EXECUTE (43)
1420 0xffff, // VK_SNAPSHOT (44)
1421 MAC_INSERT_KEY, // VK_INSERT (45)
1422 MAC_DEL_KEY, // VK_DELETE (46)
1423 0xffff, // VK_HELP (47)
1424 MAC_0_KEY, // VK_0 (48)
1425 MAC_1_KEY, // VK_1 (49)
1426 MAC_2_KEY, // VK_2 (50)
1427 MAC_3_KEY, // VK_3 (51)
1428 MAC_4_KEY, // VK_4 (52)
1429 MAC_5_KEY, // VK_5 (53)
1430 MAC_6_KEY, // VK_6 (54)
1431 MAC_7_KEY, // VK_7 (55)
1432 MAC_8_KEY, // VK_8 (56)
1433 MAC_9_KEY, // VK_9 (57)
1441 MAC_A_KEY, // VK_A (65)
1442 MAC_B_KEY, // VK_B (66)
1443 MAC_C_KEY, // VK_C (67)
1444 MAC_D_KEY, // VK_D (68)
1445 MAC_E_KEY, // VK_E (69)
1446 MAC_F_KEY, // VK_F (70)
1447 MAC_G_KEY, // VK_G (71)
1448 MAC_H_KEY, // VK_H (72)
1449 MAC_I_KEY, // VK_I (73)
1450 MAC_J_KEY, // VK_J (74)
1451 MAC_K_KEY, // VK_K (75)
1452 MAC_L_KEY, // VK_L (76)
1453 MAC_M_KEY, // VK_M (77)
1454 MAC_N_KEY, // VK_N (78)
1455 MAC_O_KEY, // VK_O (79)
1456 MAC_P_KEY, // VK_P (80)
1457 MAC_Q_KEY, // VK_Q (81)
1458 MAC_R_KEY, // VK_R (82)
1459 MAC_S_KEY, // VK_S (83)
1460 MAC_T_KEY, // VK_T (84)
1461 MAC_U_KEY, // VK_U (85)
1462 MAC_V_KEY, // VK_V (86)
1463 MAC_W_KEY, // VK_W (87)
1464 MAC_X_KEY, // VK_X (88)
1465 MAC_Y_KEY, // VK_Y (89)
1466 MAC_Z_KEY, // VK_Z (90)
1472 MAC_NUMPAD_0_KEY, // VK_NUMPAD0 (96)
1473 MAC_NUMPAD_1_KEY, // VK_NUMPAD1 (97)
1474 MAC_NUMPAD_2_KEY, // VK_NUMPAD2 (98)
1475 MAC_NUMPAD_3_KEY, // VK_NUMPAD3 (99)
1476 MAC_NUMPAD_4_KEY, // VK_NUMPAD4 (100)
1477 MAC_NUMPAD_5_KEY, // VK_NUMPAD5 (101)
1478 MAC_NUMPAD_6_KEY, // VK_NUMPAD6 (102)
1479 MAC_NUMPAD_7_KEY, // VK_NUMPAD7 (103)
1480 MAC_NUMPAD_8_KEY, // VK_NUMPAD8 (104)
1481 MAC_NUMPAD_9_KEY, // VK_NUMPAD9 (105)
1482 MAC_NUMPAD_ASTERISK_KEY, // VK_MULTIPLY (106)
1483 MAC_NUMPAD_PLUS_KEY, // VK_ADD (107)
1484 MAC_NUMPAD_ENTER_KEY, // VK_SEPARATOR (108)
1485 MAC_NUMPAD_MINUS_KEY, // VK_SUBTRACT (109)
1486 MAC_NUMPAD_PERIOD_KEY, // VK_DECIMAL (110)
1487 MAC_NUMPAD_SLASH_KEY, // VK_DIVIDE (111)
1488 MAC_F1_KEY, // VK_F1 (112)
1489 MAC_F2_KEY, // VK_F2 (113)
1490 MAC_F3_KEY, // VK_F3 (114)
1491 MAC_F4_KEY, // VK_F4 (115)
1492 MAC_F5_KEY, // VK_F5 (116)
1493 MAC_F6_KEY, // VK_F6 (117)
1494 MAC_F7_KEY, // VK_F7 (118)
1495 MAC_F8_KEY, // VK_F8 (119)
1496 MAC_F9_KEY, // VK_F9 (120)
1497 MAC_F10_KEY, // VK_F10 (121)
1498 MAC_F11_KEY, // VK_F11 (122)
1499 MAC_F12_KEY, // VK_F12 (123)
1520 0xffff, // VK_NUMLOCK (144)
1521 0xffff, // VK_SCROLL (145)
1536 MAC_SHIFT_KEY, // VK_LSHIFT (160)
1537 MAC_SHIFT_KEY, // VK_RSHIFT (161)
1538 MAC_CONTROL_KEY, // VK_LCONTROL (162)
1539 MAC_CONTROL_KEY, // VK_RCONTROL (163)
1540 MAC_OPTION_KEY, // VK_LMENU (164)
1541 MAC_OPTION_KEY, // VK_RMENU (165)
1562 MAC_SEMICOLON_KEY, // (186)
1563 MAC_PLUS_KEY, // (187)
1564 MAC_COMMA_KEY, // (188)
1565 MAC_MINUS_KEY, // (189)
1566 MAC_PERIOD_KEY, // (190)
1567 MAC_SLASH_KEY, // (191)
1568 MAC_TILDE_KEY, // (192)
1595 MAC_LEFTBRACKET_KEY, // (219)
1596 MAC_BACKSLASH_KEY, // (220)
1597 MAC_RIGHTBRACKET_KEY, // (221)
1598 MAC_APOSTROPHE_KEY, // (222)
1634 void ClipMouseToWindow(HWND window)
1638 GetClientRect(window, &wRect);
1640 ClientToScreen(window, (LPPOINT)&wRect.left);
1641 ClientToScreen(window, (LPPOINT)&wRect.right);
1648 LRESULT FAR PASCAL AppWndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
1650 /* this is where we receive all messages concerning this window
1651 * we can either process a message or pass it on to the default
1652 * message handler of windows */
1654 static PAINTSTRUCT ps;
1658 case WM_ACTIVATE: // Watch For Window Activate Message
1660 // Check Minimization State
1661 BOOL iconified = HIWORD(wParam) ? TRUE : FALSE;
1663 if (LOWORD(wParam) == WA_INACTIVE)
1672 CloseWindow( hWnd );
1674 // The window is now iconified
1675 iconified = GL_TRUE;
1681 g_focused=false; // Program Is Active
1685 SetupDSpFullScreen();
1692 // The window is now iconified
1693 iconified = GL_FALSE;
1696 ShowWindow( hWnd, SW_SHOW );
1697 SetForegroundWindow( hWnd );
1701 ClipMouseToWindow(hWnd);
1702 g_focused=true; // Program Is No Longer Active
1705 return 0; // Return To The Message Loop
1711 // check for Alt-F4 (exit hotkey)
1712 if (wParam == VK_F4)
1714 if (GetKeyState( VK_MENU) & 0x8080)
1720 if (wParam < MAX_WINKEYS)
1722 if (KeyTable[wParam] != 0xffff)
1723 SetKey( KeyTable[wParam]);
1731 if (wParam < MAX_WINKEYS)
1732 if (KeyTable[wParam] != 0xffff)
1733 ClearKey( KeyTable[wParam]);
1740 case WM_SYSDEADCHAR:
1743 case WM_NCLBUTTONDOWN:
1744 case WM_LBUTTONDOWN:
1751 case WM_NCRBUTTONDOWN:
1752 case WM_RBUTTONDOWN:
1758 case WM_NCMBUTTONDOWN:
1759 case WM_MBUTTONDOWN:
1765 case WM_NCLBUTTONUP:
1769 buttons[ 0] = false;
1773 case WM_NCRBUTTONUP:
1776 buttons[ 1] = false;
1780 case WM_NCMBUTTONUP:
1783 buttons[ 2] = false;
1787 case WM_NCLBUTTONDBLCLK:
1788 case WM_NCRBUTTONDBLCLK:
1789 case WM_NCMBUTTONDBLCLK:
1790 case WM_LBUTTONDBLCLK:
1792 case WM_RBUTTONDBLCLK:
1793 case WM_MBUTTONDBLCLK:
1796 case WM_NCMOUSEMOVE:
1798 /* ((WindowInfo *)g_lastWindow->GetInfo())->m_mouseX = (signed short)(lParam & 0xffff);
1799 ((WindowInfo *)g_lastWindow->GetInfo())->m_mouseY = (signed short)(lParam >> 16);
1800 if (g_lastWindow->m_mouseCallbacksEnabled) g_lastWindow->MouseMoveCallback();
1801 */// goto winmessage;
1804 case WM_SYSCOMMAND: // Intercept System Commands
1806 switch (wParam) // Check System Calls
1808 case SC_SCREENSAVE: // Screensaver Trying To Start?
1809 case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
1810 return 0; // Prevent From Happening
1812 // User trying to access application menu using ALT?
1821 // ReleaseCapture();
1822 // ClipMouseToWindow(hWnd);
1839 PostQuitMessage(0); /* Terminate Application */
1847 // BeginPaint( g_windowHandle,&ps);
1848 // EndPaint( g_windowHandle,&ps);
1855 /* We processed the message and there
1856 * is no processing by Windows necessary */
1858 /* We didn't process the message so let Windows do it */
1859 return DefWindowProc(hWnd,msg,wParam,lParam);
1863 static BOOL RegisterWindowClasses(HINSTANCE hFirstInstance)
1866 memset( &wc, 0, sizeof( wc));
1868 /* Register the window class. */
1869 wc.cbSize = sizeof(wc);
1871 wc.style = (CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_OWNDC); /* Combination of Class Styles */
1872 wc.lpfnWndProc = AppWndProc; /* Adress of Window Procedure */
1873 wc.cbClsExtra = 0; /* Extra Bytes allocated for this Class */
1874 wc.cbWndExtra = 0; /* Extra Bytes allocated for each Window */
1875 wc.hInstance = hFirstInstance; /* Handle of program instance */
1876 wc.hIcon = LoadIcon( hFirstInstance, MAKEINTRESOURCE(IDI_LUGARU) );
1877 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
1878 wc.hbrBackground = NULL;
1879 wc.lpszMenuName = NULL;
1880 wc.lpszClassName = g_wndClassName; /* Name of the Window Class */
1881 wc.hIconSm = LoadIcon( hFirstInstance, MAKEINTRESOURCE(IDI_LUGARU) );
1883 if (!RegisterClassEx(&wc)) return FALSE; /* Register Class failed */
1889 int resolutionID(int width, int height)
1893 if(width==640 && height==480)whichres=0;
1894 if(width==800 && height==600)whichres=1;
1895 if(width==1024 && height==768)whichres=2;
1896 if(width==1280 && height==1024)whichres=3;
1897 if(width==1600 && height==1200)whichres=4;
1898 if(width==840 && height==524)whichres=5;
1899 if(width==1024 && height==640)whichres=6;
1900 if(width==1344 && height==840)whichres=7;
1905 int closestResolution(int width, int height)
1909 if(width>=640 && height>=480)whichres=0;
1910 if(width>=800 && height>=600)whichres=1;
1911 if(width>=1024 && height>=768)whichres=2;
1912 if(width>=1280 && height>=1024)whichres=3;
1913 if(width>=1600 && height>=1200)whichres=4;
1914 if(width==840 && height==524)whichres=5;
1915 if(width==1024 && height==640)whichres=6;
1916 if(width==1344 && height==840)whichres=7;
1921 bool selectDetail(int & width, int & height, int & bpp, int & detail)
1924 int whichres = closestResolution(width, height);
1928 if(whichres<=0 || whichres>7){
1962 if ((detail != 0) && (resolutionDepths[whichres][1] != 0))
1966 else if ((detail == 0) && (resolutionDepths[whichres][0] != 0))
1970 else if ((detail != 0) && (resolutionDepths[whichres][0] != 0))
1976 else if (0 == whichres)
1984 bpp = resolutionDepths[whichres][(detail != 0)];
1990 int __stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
1993 LPWSTR * cl = CommandLineToArgvW(GetCommandLineW(), &argc);
1996 if (0 == _wcsicmp(cl[1], L"-windowed"))
2004 memset( &g_theKeys, 0, sizeof( KeyMap));
2008 memset(&mode, 0, sizeof(mode));
2009 mode.dmSize = sizeof(mode);
2010 while (EnumDisplaySettings(NULL, i++, &mode))
2012 if (mode.dmBitsPerPel < 16)
2017 int res = resolutionID(mode.dmPelsWidth, mode.dmPelsHeight);
2019 if (res > -1 && res < 8)
2021 if (DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettings(&mode, CDS_TEST))
2026 switch(mode.dmBitsPerPel)
2030 resolutionDepths[res][1] = mode.dmBitsPerPel;
2033 resolutionDepths[res][0] = mode.dmBitsPerPel;
2039 /* if there is no Instance of our program in memory then register the window class */
2040 if (hPrevInstance == NULL && !RegisterWindowClasses(hInstance))
2041 return FALSE; /* registration failed! */
2043 g_appInstance=hInstance;
2047 UnregisterClass( g_wndClassName, hInstance);
2054 extern int channels[100];
2055 extern FSOUND_SAMPLE * samp[100];
2056 extern FSOUND_STREAM * strm[10];
2058 extern "C" void PlaySoundEx(int chan, FSOUND_SAMPLE *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
2060 const FSOUND_SAMPLE * currSample = FSOUND_GetCurrentSample(channels[chan]);
2061 if (currSample && currSample == samp[chan])
2063 if (FSOUND_GetPaused(channels[chan]))
2065 FSOUND_StopSound(channels[chan]);
2066 channels[chan] = FSOUND_FREE;
2068 else if (FSOUND_IsPlaying(channels[chan]))
2070 int loop_mode = FSOUND_GetLoopMode(channels[chan]);
2071 if (loop_mode & FSOUND_LOOP_OFF)
2073 channels[chan] = FSOUND_FREE;
2079 channels[chan] = FSOUND_FREE;
2082 channels[chan] = FSOUND_PlaySoundEx(channels[chan], sptr, dsp, startpaused);
2083 if (channels[chan] < 0)
2085 channels[chan] = FSOUND_PlaySoundEx(FSOUND_FREE, sptr, dsp, startpaused);
2089 extern "C" void PlayStreamEx(int chan, FSOUND_STREAM *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
2091 const FSOUND_SAMPLE * currSample = FSOUND_GetCurrentSample(channels[chan]);
2092 if (currSample && currSample == FSOUND_Stream_GetSample(sptr))
2094 FSOUND_StopSound(channels[chan]);
2095 FSOUND_Stream_Stop(sptr);
2099 FSOUND_Stream_Stop(sptr);
2100 channels[chan] = FSOUND_FREE;
2103 channels[chan] = FSOUND_Stream_PlayEx(channels[chan], sptr, dsp, startpaused);
2104 if (channels[chan] < 0)
2106 channels[chan] = FSOUND_Stream_PlayEx(FSOUND_FREE, sptr, dsp, startpaused);
2110 bool LoadImage(const char * fname, TGAImageRec & tex)
2114 if ( tex.data == NULL )
2120 ILstring f = strdup(ConvertFileName(fname));
2127 ilGenImages(1, &iid);
2132 tex.sizeX = ilGetInteger(IL_IMAGE_WIDTH);
2133 tex.sizeY = ilGetInteger(IL_IMAGE_HEIGHT);
2134 tex.bpp = ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);
2135 ILuint Bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL),
2136 imageSize = tex.sizeX * tex.sizeY * Bpp;
2137 ILubyte *Data = ilGetData();
2138 memcpy(tex.data, Data, imageSize);
2140 // Truvision Targa files are stored as BGR colors
2141 // We want RGB so Blue and Red bytes are switched
2142 if (IL_TGA == ilGetInteger(IL_IMAGE_FORMAT))
2144 // Loop Through The Image Data
2145 for (GLuint i = 0; i < int(imageSize); i += Bpp)
2147 // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
2148 GLbyte temp; // Temporary Variable
2149 temp = tex.data[i]; // Temporarily Store The Value At Image Data 'i'
2150 tex.data[i] = tex.data[i + 2]; // Set The 1st Byte To The Value Of The 3rd Byte
2151 tex.data[i + 2] = temp; // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
2159 ilDeleteImages(1, &iid);
2163 GLuint texid = ilutGLLoadImage(f);
2168 ilutGLBuildMipmaps()
2177 res = load_image(fname, tex);
2178 if (!res) STUBBED("load_image() failed!");
2184 void ScreenShot(const char * fname)
2187 ILstring f = strdup(fname);
2194 ilGenImages(1, &iid);
2200 ilDeleteImages(1, &iid);
2204 STUBBED("Non-DevIL screenshot");
2210 static bool load_image(const char *file_name, TGAImageRec &tex)
2212 char *ptr = strrchr(file_name, '.');
2215 if (stricmp(ptr+1, "png") == 0)
2216 return load_png(file_name, tex);
2217 else if (stricmp(ptr+1, "jpg") == 0)
2218 return load_jpg(file_name, tex);
2221 STUBBED("Unsupported image type");
2225 static bool load_jpg(const char *file_name, TGAImageRec &tex)
2227 // !!! FIXME: write this.
2228 STUBBED("load_jpg");
2232 /* stolen from public domain example.c code in libpng distribution. */
2233 static bool load_png(const char *file_name, TGAImageRec &tex)
2235 bool hasalpha = false;
2236 png_structp png_ptr = NULL;
2237 png_infop info_ptr = NULL;
2238 png_uint_32 width, height;
2239 int bit_depth, color_type, interlace_type;
2240 png_byte **rows = NULL;
2241 bool retval = false;
2242 png_byte **row_pointers = NULL;
2243 FILE *fp = fopen(file_name, "rb");
2248 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2249 if (png_ptr == NULL)
2252 info_ptr = png_create_info_struct(png_ptr);
2253 if (info_ptr == NULL)
2256 if (setjmp(png_jmpbuf(png_ptr)))
2259 png_init_io(png_ptr, fp);
2260 png_read_png(png_ptr, info_ptr,
2261 PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING,
2263 png_get_IHDR(png_ptr, info_ptr, &width, &height,
2264 &bit_depth, &color_type, &interlace_type, NULL, NULL);
2266 if (bit_depth != 8) // transform SHOULD handle this...
2269 if (color_type & PNG_COLOR_MASK_PALETTE) // !!! FIXME?
2272 if ((color_type & PNG_COLOR_MASK_COLOR) == 0) // !!! FIXME?
2275 hasalpha = ((color_type & PNG_COLOR_MASK_ALPHA) != 0);
2276 row_pointers = png_get_rows(png_ptr, info_ptr);
2280 retval = malloc(width * height * 4);
2286 png_byte *dst = tex.data;
2287 for (int i = height-1; i >= 0; i--)
2289 png_byte *src = row_pointers[i];
2290 for (int j = 0; j < width; j++)
2304 png_byte *dst = tex.data;
2305 int pitch = width * 4;
2306 for (int i = height-1; i >= 0; i--, dst += pitch)
2307 memcpy(dst, row_pointers[i], pitch);
2316 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);