19 // just use libpng and libjpg directly; it's lighter-weight and easier
20 // to manage the dependencies on Linux...
25 static bool load_image(const char * fname, TGAImageRec & tex);
26 static bool load_png(const char * fname, TGAImageRec & tex);
27 static bool load_jpg(const char * fname, TGAImageRec & tex);
32 #pragma comment(lib, "opengl32.lib")
33 #pragma comment(lib, "glu32.lib")
34 #pragma comment(lib, "glaux.lib")
37 extern bool buttons[3];
38 extern float multiplier;
39 extern float screenwidth,screenheight;
41 extern float realmultiplier;
43 extern bool ismotionblur;
44 extern float usermousesensitivity;
46 extern bool floatjump;
47 extern bool cellophane;
49 //extern int terraindetail;
50 //extern int texdetail;
51 extern float terraindetail;
52 extern float texdetail;
53 extern int bloodtoggle;
55 extern bool autoslomo;
57 extern bool musictoggle;
58 extern bool trilinear;
59 extern float gamespeed;
60 extern int difficulty;
61 extern bool damageeffects;
62 extern int numplayers;
64 extern bool invertmouse;
65 extern bool texttoggle;
66 extern bool ambientsound;
67 extern bool mousejump;
69 extern Person player[maxplayers];
71 extern bool stillloading;
72 extern bool showpoints;
73 extern bool alwaysblur;
74 extern bool immediate;
75 extern bool velocityblur;
76 extern bool debugmode;
78 /*extern*/ bool gameFocused;
79 extern int kBitsPerPixel;
80 extern float slomospeed;
81 extern float slomofreq;
82 extern float oldgamespeed;
91 #include "MacCompatibility.h"
99 #include "res/resource.h"
104 unsigned int resolutionDepths[8][2] = {0};
106 bool selectDetail(int & width, int & height, int & bpp, int & detail);
107 int closestResolution(int width, int height);
108 int resolutionID(int width, int height);
110 void ReportError (char * strError);
112 void SetupDSpFullScreen();
115 void DrawGL(Game & game);
117 void CreateGLWindow (void);
118 Boolean SetUp (Game & game);
119 void DoKey (SInt8 theKey, SInt8 theCode);
120 void DoUpdate (Game & game);
126 // statics/globals (internal only) ------------------------------------------
128 typedef struct tagPOINT {
135 void sdlGetCursorPos(POINT *pt)
138 SDL_GetMouseState(&x, &y);
142 #define GetCursorPos(x) sdlGetCursorPos(x)
143 #define SetCursorPos(x, y) SDL_WarpMouse(x, y)
144 #define ScreenToClient(x, pt)
145 #define ClientToScreen(x, pt)
146 #define MessageBox(hwnd,text,title,flags) STUBBED("msgbox")
152 static const char g_wndClassName[]={ "LUGARUWINDOWCLASS" };
153 static HINSTANCE g_appInstance;
154 static HWND g_windowHandle;
158 static bool g_button, fullscreen = true;
169 kForegroundSleep = 10,
170 kBackgroundSleep = 10000
177 const RGBColor rgbBlack = { 0x0000, 0x0000, 0x0000 };
180 char gcstrMode [256] = "";
182 UInt32 gSleepTime = kForegroundSleep;
183 Boolean gDone = false, gfFrontProcess = true;
187 static bool gMouseGrabbed = true;
189 // --------------------------------------------------------------------------
191 void ReportError (char * strError)
193 #ifdef WIN32 // !!! FIXME. --ryan.
194 throw std::exception( strError);
197 /* char errMsgCStr [256];
200 sprintf (errMsgCStr, "%s", strError);
202 // out as debug string
203 CToPStr (strErr, errMsgCStr);
208 void SetupDSpFullScreen ()
215 DEVMODE dmScreenSettings;
216 memset( &dmScreenSettings, 0, sizeof( dmScreenSettings));
217 dmScreenSettings.dmSize = sizeof( dmScreenSettings);
218 dmScreenSettings.dmPelsWidth = kContextWidth;
219 dmScreenSettings.dmPelsHeight = kContextHeight;
220 dmScreenSettings.dmBitsPerPel = kBitsPerPixel;
221 dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
224 if (ChangeDisplaySettings( &dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
226 ReportError( "Could not set display mode");
243 ChangeDisplaySettings( NULL, 0);
251 //-----------------------------------------------------------------------------------------------------------------------
255 void DrawGL (Game & game)
266 static KeyMap g_theKeys;
268 void SetKey( int key)
270 g_theKeys[ key >> 3] |= (1 << (key & 7));
273 void ClearKey( int key)
275 g_theKeys[ key >> 3] &= (0xff ^ (1 << (key & 7)));
278 void GetKeys( unsigned char theKeys[16])
280 memcpy( theKeys, &g_theKeys, 16);
289 static void initSDLKeyTable(void) {}
291 #define MAX_SDLKEYS SDLK_LAST
292 static unsigned short KeyTable[MAX_SDLKEYS];
294 static void initSDLKeyTable(void)
296 memset(KeyTable, 0xFF, sizeof (KeyTable));
297 KeyTable[SDLK_BACKSPACE] = MAC_DELETE_KEY;
298 KeyTable[SDLK_TAB] = MAC_TAB_KEY;
299 KeyTable[SDLK_RETURN] = MAC_RETURN_KEY;
300 KeyTable[SDLK_ESCAPE] = MAC_ESCAPE_KEY;
301 KeyTable[SDLK_SPACE] = MAC_SPACE_KEY;
302 KeyTable[SDLK_PAGEUP] = MAC_PAGE_UP_KEY;
303 KeyTable[SDLK_PAGEDOWN] = MAC_PAGE_DOWN_KEY;
304 KeyTable[SDLK_END] = MAC_END_KEY;
305 KeyTable[SDLK_HOME] = MAC_HOME_KEY;
306 KeyTable[SDLK_LEFT] = MAC_ARROW_LEFT_KEY;
307 KeyTable[SDLK_UP] = MAC_ARROW_UP_KEY;
308 KeyTable[SDLK_RIGHT] = MAC_ARROW_RIGHT_KEY;
309 KeyTable[SDLK_DOWN] = MAC_ARROW_DOWN_KEY;
310 KeyTable[SDLK_INSERT] = MAC_INSERT_KEY;
311 KeyTable[SDLK_DELETE] = MAC_DEL_KEY;
312 KeyTable[SDLK_0] = MAC_0_KEY;
313 KeyTable[SDLK_1] = MAC_1_KEY;
314 KeyTable[SDLK_2] = MAC_2_KEY;
315 KeyTable[SDLK_3] = MAC_3_KEY;
316 KeyTable[SDLK_4] = MAC_4_KEY;
317 KeyTable[SDLK_5] = MAC_5_KEY;
318 KeyTable[SDLK_6] = MAC_6_KEY;
319 KeyTable[SDLK_7] = MAC_7_KEY;
320 KeyTable[SDLK_8] = MAC_8_KEY;
321 KeyTable[SDLK_9] = MAC_9_KEY;
322 KeyTable[SDLK_a] = MAC_A_KEY;
323 KeyTable[SDLK_b] = MAC_B_KEY;
324 KeyTable[SDLK_c] = MAC_C_KEY;
325 KeyTable[SDLK_d] = MAC_D_KEY;
326 KeyTable[SDLK_e] = MAC_E_KEY;
327 KeyTable[SDLK_f] = MAC_F_KEY;
328 KeyTable[SDLK_g] = MAC_G_KEY;
329 KeyTable[SDLK_h] = MAC_H_KEY;
330 KeyTable[SDLK_i] = MAC_I_KEY;
331 KeyTable[SDLK_j] = MAC_J_KEY;
332 KeyTable[SDLK_k] = MAC_K_KEY;
333 KeyTable[SDLK_l] = MAC_L_KEY;
334 KeyTable[SDLK_m] = MAC_M_KEY;
335 KeyTable[SDLK_n] = MAC_N_KEY;
336 KeyTable[SDLK_o] = MAC_O_KEY;
337 KeyTable[SDLK_p] = MAC_P_KEY;
338 KeyTable[SDLK_q] = MAC_Q_KEY;
339 KeyTable[SDLK_r] = MAC_R_KEY;
340 KeyTable[SDLK_s] = MAC_S_KEY;
341 KeyTable[SDLK_t] = MAC_T_KEY;
342 KeyTable[SDLK_u] = MAC_U_KEY;
343 KeyTable[SDLK_v] = MAC_V_KEY;
344 KeyTable[SDLK_w] = MAC_W_KEY;
345 KeyTable[SDLK_x] = MAC_X_KEY;
346 KeyTable[SDLK_y] = MAC_Y_KEY;
347 KeyTable[SDLK_z] = MAC_Z_KEY;
348 KeyTable[SDLK_KP0] = MAC_NUMPAD_0_KEY;
349 KeyTable[SDLK_KP1] = MAC_NUMPAD_1_KEY;
350 KeyTable[SDLK_KP2] = MAC_NUMPAD_2_KEY;
351 KeyTable[SDLK_KP3] = MAC_NUMPAD_3_KEY;
352 KeyTable[SDLK_KP4] = MAC_NUMPAD_4_KEY;
353 KeyTable[SDLK_KP5] = MAC_NUMPAD_5_KEY;
354 KeyTable[SDLK_KP6] = MAC_NUMPAD_6_KEY;
355 KeyTable[SDLK_KP7] = MAC_NUMPAD_7_KEY;
356 KeyTable[SDLK_KP8] = MAC_NUMPAD_8_KEY;
357 KeyTable[SDLK_KP9] = MAC_NUMPAD_9_KEY;
358 KeyTable[SDLK_KP_MULTIPLY] = MAC_NUMPAD_ASTERISK_KEY;
359 KeyTable[SDLK_KP_PLUS] = MAC_NUMPAD_PLUS_KEY;
360 KeyTable[SDLK_KP_ENTER] = MAC_NUMPAD_ENTER_KEY;
361 KeyTable[SDLK_KP_MINUS] = MAC_NUMPAD_MINUS_KEY;
362 KeyTable[SDLK_KP_PERIOD] = MAC_NUMPAD_PERIOD_KEY;
363 KeyTable[SDLK_KP_DIVIDE] = MAC_NUMPAD_SLASH_KEY;
364 KeyTable[SDLK_F1] = MAC_F1_KEY;
365 KeyTable[SDLK_F2] = MAC_F2_KEY;
366 KeyTable[SDLK_F3] = MAC_F3_KEY;
367 KeyTable[SDLK_F4] = MAC_F4_KEY;
368 KeyTable[SDLK_F5] = MAC_F5_KEY;
369 KeyTable[SDLK_F6] = MAC_F6_KEY;
370 KeyTable[SDLK_F7] = MAC_F7_KEY;
371 KeyTable[SDLK_F8] = MAC_F8_KEY;
372 KeyTable[SDLK_F9] = MAC_F9_KEY;
373 KeyTable[SDLK_F10] = MAC_F10_KEY;
374 KeyTable[SDLK_F11] = MAC_F11_KEY;
375 KeyTable[SDLK_F12] = MAC_F12_KEY;
376 KeyTable[SDLK_SEMICOLON] = MAC_SEMICOLON_KEY;
377 KeyTable[SDLK_PLUS] = MAC_PLUS_KEY;
378 KeyTable[SDLK_COMMA] = MAC_COMMA_KEY;
379 KeyTable[SDLK_MINUS] = MAC_MINUS_KEY;
380 KeyTable[SDLK_PERIOD] = MAC_PERIOD_KEY;
381 KeyTable[SDLK_SLASH] = MAC_SLASH_KEY;
382 KeyTable[SDLK_BACKQUOTE] = MAC_TILDE_KEY;
383 KeyTable[SDLK_LEFTBRACKET] = MAC_LEFTBRACKET_KEY;
384 KeyTable[SDLK_BACKSLASH] = MAC_BACKSLASH_KEY;
385 KeyTable[SDLK_RIGHTBRACKET] = MAC_RIGHTBRACKET_KEY;
386 KeyTable[SDLK_QUOTE] = MAC_APOSTROPHE_KEY;
389 static inline int clamp_sdl_mouse_button(Uint8 button)
391 if ((button >= 1) && (button <= 3))
396 static void sdlEventProc(const SDL_Event &e)
401 case SDL_MOUSEBUTTONDOWN:
403 val = clamp_sdl_mouse_button(e.button.button);
409 SetKey(MAC_MOUSEBUTTON1);
413 SetKey(MAC_MOUSEBUTTON2);
420 case SDL_MOUSEBUTTONUP:
422 val = clamp_sdl_mouse_button(e.button.button);
428 ClearKey(MAC_MOUSEBUTTON1);
432 ClearKey(MAC_MOUSEBUTTON2);
434 buttons[val] = false;
440 if (e.key.keysym.sym == SDLK_g)
442 if (e.key.keysym.mod & KMOD_CTRL)
443 gMouseGrabbed = !gMouseGrabbed;
446 else if (e.key.keysym.sym == SDLK_RETURN)
448 if (e.key.keysym.mod & KMOD_ALT)
449 SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
452 if (e.key.keysym.sym < SDLK_LAST)
454 if (KeyTable[e.key.keysym.sym] != 0xffff)
455 SetKey(KeyTable[e.key.keysym.sym]);
458 if (e.key.keysym.mod & KMOD_CTRL)
459 SetKey(MAC_CONTROL_KEY);
460 if (e.key.keysym.mod & KMOD_ALT)
461 SetKey(MAC_OPTION_KEY);
462 if (e.key.keysym.mod & KMOD_SHIFT)
463 SetKey(MAC_SHIFT_KEY);
464 if (e.key.keysym.mod & KMOD_CAPS)
465 SetKey(MAC_CAPS_LOCK_KEY);
470 if (e.key.keysym.sym < SDLK_LAST)
472 if (KeyTable[e.key.keysym.sym] != 0xffff)
473 ClearKey(KeyTable[e.key.keysym.sym]);
476 if (e.key.keysym.mod & KMOD_CTRL)
477 ClearKey(MAC_CONTROL_KEY);
478 if (e.key.keysym.mod & KMOD_ALT)
479 ClearKey(MAC_OPTION_KEY);
480 if (e.key.keysym.mod & KMOD_SHIFT)
481 ClearKey(MAC_SHIFT_KEY);
482 if (e.key.keysym.mod & KMOD_CAPS)
483 ClearKey(MAC_CAPS_LOCK_KEY);
489 // --------------------------------------------------------------------------
491 static Point gMidPoint;
493 Boolean SetUp (Game & game)
499 randSeed = UpTime().lo;
502 // ifstream ipstream(":Data:config.txt", std::ios::in /*| std::ios::nocreate*/);
503 ifstream ipstream("./Data/config.txt", std::ios::in /*| std::ios::nocreate*/);
506 usermousesensitivity=1;
535 game.crouchkey=MAC_SHIFT_KEY;
536 game.jumpkey=MAC_SPACE_KEY;
537 game.leftkey=MAC_A_KEY;
538 game.forwardkey=MAC_W_KEY;
539 game.backkey=MAC_S_KEY;
540 game.rightkey=MAC_D_KEY;
541 game.drawkey=MAC_E_KEY;
542 game.throwkey=MAC_Q_KEY;
543 game.attackkey=MAC_MOUSEBUTTON1;
544 game.chatkey=MAC_T_KEY;
550 selectDetail(kContextWidth, kContextHeight, kBitsPerPixel, detail);
553 //ofstream opstream(":Data:config.txt");
554 ofstream opstream("./Data/config.txt");
555 opstream << "Screenwidth:\n";
556 opstream << kContextWidth;
557 opstream << "\nScreenheight:\n";
558 opstream << kContextHeight;
559 opstream << "\nMouse sensitivity:\n";
560 opstream << usermousesensitivity;
561 opstream << "\nBlur(0,1):\n";
562 opstream << ismotionblur;
563 opstream << "\nOverall Detail(0,1,2) higher=better:\n";
565 opstream << "\nFloating jump:\n";
566 opstream << floatjump;
567 opstream << "\nMouse jump:\n";
568 opstream << mousejump;
569 opstream << "\nAmbient sound:\n";
570 opstream << ambientsound;
571 opstream << "\nBlood (0,1,2):\n";
572 opstream << bloodtoggle;
573 opstream << "\nAuto slomo:\n";
574 opstream << autoslomo;
575 opstream << "\nFoliage:\n";
577 opstream << "\nMusic:\n";
578 opstream << musictoggle;
579 opstream << "\nTrilinear:\n";
580 opstream << trilinear;
581 opstream << "\nDecals(shadows,blood puddles,etc):\n";
583 opstream << "\nInvert mouse:\n";
584 opstream << invertmouse;
585 opstream << "\nGamespeed:\n";
586 opstream << gamespeed;
587 opstream << "\nDifficulty(0,1,2) higher=harder:\n";
588 opstream << difficulty;
589 opstream << "\nDamage effects(blackout, doublevision):\n";
590 opstream << damageeffects;
591 opstream << "\nText:\n";
592 opstream << texttoggle;
593 opstream << "\nDebug:\n";
594 opstream << debugmode;
595 opstream << "\nVBL Sync:\n";
597 opstream << "\nShow Points:\n";
598 opstream << showpoints;
599 opstream << "\nAlways Blur:\n";
600 opstream << alwaysblur;
601 opstream << "\nImmediate mode (turn on on G5):\n";
602 opstream << immediate;
603 opstream << "\nVelocity blur:\n";
604 opstream << velocityblur;
605 opstream << "\nVolume:\n";
607 opstream << "\nForward key:\n";
608 opstream << KeyToChar(game.forwardkey);
609 opstream << "\nBack key:\n";
610 opstream << KeyToChar(game.backkey);
611 opstream << "\nLeft key:\n";
612 opstream << KeyToChar(game.leftkey);
613 opstream << "\nRight key:\n";
614 opstream << KeyToChar(game.rightkey);
615 opstream << "\nJump key:\n";
616 opstream << KeyToChar(game.jumpkey);
617 opstream << "\nCrouch key:\n";
618 opstream << KeyToChar(game.crouchkey);
619 opstream << "\nDraw key:\n";
620 opstream << KeyToChar(game.drawkey);
621 opstream << "\nThrow key:\n";
622 opstream << KeyToChar(game.throwkey);
623 opstream << "\nAttack key:\n";
624 opstream << KeyToChar(game.attackkey);
625 opstream << "\nChat key:\n";
626 opstream << KeyToChar(game.chatkey);
631 ipstream.ignore(256,'\n');
632 ipstream >> kContextWidth;
633 ipstream.ignore(256,'\n');
634 ipstream.ignore(256,'\n');
635 ipstream >> kContextHeight;
636 ipstream.ignore(256,'\n');
637 ipstream.ignore(256,'\n');
638 ipstream >> usermousesensitivity;
639 ipstream.ignore(256,'\n');
640 ipstream.ignore(256,'\n');
642 ismotionblur = (i != 0);
643 ipstream.ignore(256,'\n');
644 ipstream.ignore(256,'\n');
646 if(detail!=0)kBitsPerPixel=32;
647 else kBitsPerPixel=16;
648 ipstream.ignore(256,'\n');
649 ipstream.ignore(256,'\n');
651 floatjump = (i != 0);
652 ipstream.ignore(256,'\n');
653 ipstream.ignore(256,'\n');
655 mousejump = (i != 0);
656 ipstream.ignore(256,'\n');
657 ipstream.ignore(256,'\n');
659 ambientsound = (i != 0);
660 ipstream.ignore(256,'\n');
661 ipstream.ignore(256,'\n');
662 ipstream >> bloodtoggle;
663 ipstream.ignore(256,'\n');
664 ipstream.ignore(256,'\n');
666 autoslomo = (i != 0);
667 ipstream.ignore(256,'\n');
668 ipstream.ignore(256,'\n');
671 ipstream.ignore(256,'\n');
672 ipstream.ignore(256,'\n');
674 musictoggle = (i != 0);
675 ipstream.ignore(256,'\n');
676 ipstream.ignore(256,'\n');
678 trilinear = (i != 0);
679 ipstream.ignore(256,'\n');
680 ipstream.ignore(256,'\n');
683 ipstream.ignore(256,'\n');
684 ipstream.ignore(256,'\n');
686 invertmouse = (i != 0);
687 ipstream.ignore(256,'\n');
688 ipstream.ignore(256,'\n');
689 ipstream >> gamespeed;
690 oldgamespeed=gamespeed;
695 ipstream.ignore(256,'\n');
696 ipstream.ignore(256,'\n');
697 ipstream >> difficulty;
698 ipstream.ignore(256,'\n');
699 ipstream.ignore(256,'\n');
701 damageeffects = (i != 0);
702 ipstream.ignore(256,'\n');
703 ipstream.ignore(256,'\n');
705 texttoggle = (i != 0);
706 ipstream.ignore(256,'\n');
707 ipstream.ignore(256,'\n');
709 debugmode = (i != 0);
710 ipstream.ignore(256,'\n');
711 ipstream.ignore(256,'\n');
714 ipstream.ignore(256,'\n');
715 ipstream.ignore(256,'\n');
717 showpoints = (i != 0);
718 ipstream.ignore(256,'\n');
719 ipstream.ignore(256,'\n');
721 alwaysblur = (i != 0);
722 ipstream.ignore(256,'\n');
723 ipstream.ignore(256,'\n');
725 immediate = (i != 0);
726 ipstream.ignore(256,'\n');
727 ipstream.ignore(256,'\n');
729 velocityblur = (i != 0);
730 ipstream.ignore(256,'\n');
731 ipstream.ignore(256,'\n');
733 ipstream.ignore(256,'\n');
734 ipstream.ignore(256,'\n');
736 game.forwardkey=CharToKey(string);
737 ipstream.ignore(256,'\n');
738 ipstream.ignore(256,'\n');
740 game.backkey=CharToKey(string);
741 ipstream.ignore(256,'\n');
742 ipstream.ignore(256,'\n');
744 game.leftkey=CharToKey(string);
745 ipstream.ignore(256,'\n');
746 ipstream.ignore(256,'\n');
748 game.rightkey=CharToKey(string);
749 ipstream.ignore(256,'\n');
750 ipstream.ignore(256,'\n');
752 game.jumpkey=CharToKey(string);
753 ipstream.ignore(256,'\n');
754 ipstream.ignore(256,'\n');
756 game.crouchkey=CharToKey(string);
757 ipstream.ignore(256,'\n');
758 ipstream.ignore(256,'\n');
760 game.drawkey=CharToKey(string);
761 ipstream.ignore(256,'\n');
762 ipstream.ignore(256,'\n');
764 game.throwkey=CharToKey(string);
765 ipstream.ignore(256,'\n');
766 ipstream.ignore(256,'\n');
768 game.attackkey=CharToKey(string);
769 ipstream.ignore(256,'\n');
770 ipstream.ignore(256,'\n');
772 game.chatkey=CharToKey(string);
775 if(detail>2)detail=2;
776 if(detail<0)detail=0;
777 if(screenwidth>3000)screenwidth=640;
778 if(screenwidth<0)screenwidth=640;
779 if(screenheight>3000)screenheight=480;
780 if(screenheight<0)screenheight=480;
782 if(kBitsPerPixel!=32&&kBitsPerPixel!=16){
787 selectDetail(kContextWidth, kContextHeight, kBitsPerPixel, detail);
789 SetupDSpFullScreen();
792 if (!SDL_WasInit(SDL_INIT_VIDEO))
794 if (SDL_Init(SDL_INIT_VIDEO) == -1)
796 fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
801 Uint32 sdlflags = SDL_OPENGL;
802 SDL_WM_SetCaption("Lugaru", "lugaru");
804 if (SDL_SetVideoMode(kContextWidth, kContextHeight, 0, sdlflags) == NULL)
806 fprintf(stderr, "SDL_SetVideoMode() failed: %s\n", SDL_GetError());
810 #elif (defined WIN32)
811 //------------------------------------------------------------------
814 RECT r = {0, 0, kContextWidth-1, kContextHeight-1};
815 DWORD dwStyle = WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE;
816 DWORD dwExStyle = WS_EX_APPWINDOW;
825 dwStyle |= WS_OVERLAPPEDWINDOW;
826 dwExStyle |= WS_EX_WINDOWEDGE;
829 AdjustWindowRectEx(&r, dwStyle, FALSE, dwExStyle);
833 x = (GetSystemMetrics(SM_CXSCREEN) >> 1) - ((r.right - r.left + 1) >> 1);
834 y = (GetSystemMetrics(SM_CYSCREEN) >> 1) - ((r.bottom - r.top + 1) >> 1);
837 g_windowHandle=CreateWindowEx(
839 g_wndClassName, "Lugaru", dwStyle,
841 // kContextWidth, kContextHeight,
842 r.right - r.left + 1, r.bottom - r.top + 1,
843 NULL,NULL,g_appInstance,NULL );
846 ReportError("Could not create window");
850 //------------------------------------------------------------------
853 static PIXELFORMATDESCRIPTOR pfd =
855 sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
857 PFD_DRAW_TO_WINDOW | // Format Must Support Window
858 PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
859 PFD_DOUBLEBUFFER, // Must Support Double Buffering
860 PFD_TYPE_RGBA, // Request An RGBA Format
861 kBitsPerPixel, // Select Our Color Depth
862 0, 0, 0, 0, 0, 0, // Color Bits Ignored
863 0, // No Alpha Buffer
864 0, // Shift Bit Ignored
865 0, // No Accumulation Buffer
866 0, 0, 0, 0, // Accumulation Bits Ignored
867 16, // 16Bit Z-Buffer (Depth Buffer)
868 0, // No Stencil Buffer
869 0, // No Auxiliary Buffer
870 PFD_MAIN_PLANE, // Main Drawing Layer
872 0, 0, 0 // Layer Masks Ignored
875 if (!(hDC = GetDC( g_windowHandle)))
876 ReportError( "Could not get device context");
879 if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
881 ReportError( "Could not find appropriate pixel format");
885 if (!DescribePixelFormat(hDC, PixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd))
887 ReportError( "Could not retrieve pixel format");
891 if (!SetPixelFormat( hDC, PixelFormat, &pfd))
893 ReportError( "Could not set pixel format");
897 if (!(hRC = wglCreateContext(hDC)))
899 ReportError( "Could not create rendering context");
903 if (!wglMakeCurrent(hDC, hRC))
905 ReportError( "Could not activate rendering context");
911 // Place the window above all topmost windows
912 SetWindowPos( g_windowHandle, HWND_TOPMOST, 0,0,0,0,
913 SWP_NOMOVE | SWP_NOSIZE );
916 SetForegroundWindow(g_windowHandle);
917 SetFocus(g_windowHandle);
920 glClear( GL_COLOR_BUFFER_BIT );
924 glDisable( GL_ALPHA_TEST);
925 glDisable( GL_BLEND);
926 glDisable( GL_DEPTH_TEST);
927 // glDisable( GL_DITHER);
929 glDisable( GL_LIGHTING);
930 glDisable( GL_LOGIC_OP);
931 glDisable( GL_STENCIL_TEST);
932 glDisable( GL_TEXTURE_1D);
933 glDisable( GL_TEXTURE_2D);
934 glPixelTransferi( GL_MAP_COLOR, GL_FALSE);
935 glPixelTransferi( GL_RED_SCALE, 1);
936 glPixelTransferi( GL_RED_BIAS, 0);
937 glPixelTransferi( GL_GREEN_SCALE, 1);
938 glPixelTransferi( GL_GREEN_BIAS, 0);
939 glPixelTransferi( GL_BLUE_SCALE, 1);
940 glPixelTransferi( GL_BLUE_BIAS, 0);
941 glPixelTransferi( GL_ALPHA_SCALE, 1);
942 glPixelTransferi( GL_ALPHA_BIAS, 0);
944 // set initial rendering states
945 glShadeModel( GL_SMOOTH);
947 glDepthFunc( GL_LEQUAL);
948 glDepthMask( GL_TRUE);
949 // glDepthRange( FRONT_CLIP, BACK_CLIP);
950 glEnable( GL_DEPTH_TEST);
951 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
952 glCullFace( GL_FRONT);
953 glEnable( GL_CULL_FACE);
954 glEnable( GL_LIGHTING);
955 // glEnable( GL_LIGHT_MODEL_AMBIENT);
956 glEnable( GL_DITHER);
957 glEnable( GL_COLOR_MATERIAL);
958 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
959 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
960 glAlphaFunc( GL_GREATER, 0.5f);
963 if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION ||
964 iluGetInteger(ILU_VERSION_NUM) < ILU_VERSION ||
965 ilutGetInteger(ILUT_VERSION_NUM) < ILUT_VERSION)
967 ReportError("DevIL version is different...exiting!\n");
975 ilutRenderer(ILUT_OPENGL);
977 ilEnable(IL_ORIGIN_SET);
978 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
981 GLint width = kContextWidth;
982 GLint height = kContextHeight;
983 gMidPoint.h = width / 2;
984 gMidPoint.v = height / 2;
988 game.newdetail=detail;
989 game.newscreenwidth=screenwidth;
990 game.newscreenheight=screenheight;
998 static void DoMouse(Game & game)
1000 static Point lastMouse = {-1,-1};
1005 ScreenToClient(g_windowHandle, &pos);
1006 globalMouse.h = pos.x;
1007 globalMouse.v = pos.y;
1009 if (lastMouse.h == globalMouse.h && lastMouse.v == globalMouse.v)
1016 static Point virtualMouse = {0,0};
1017 delta = globalMouse;
1019 delta.h -= lastMouse.h;
1020 delta.v -= lastMouse.v;
1021 lastMouse.h = pos.x;
1022 lastMouse.v = pos.y;
1024 if(mainmenu||(abs(delta.h)<10*realmultiplier*1000&&abs(delta.v)<10*realmultiplier*1000)){
1025 game.deltah=delta.h*usermousesensitivity;
1026 game.deltav=delta.v*usermousesensitivity;
1027 game.mousecoordh=globalMouse.h;
1028 game.mousecoordv=globalMouse.v;
1031 if((!mainmenu)&&(gMouseGrabbed))
1033 if(lastMouse.h>gMidPoint.h+100||lastMouse.h<gMidPoint.h-100||lastMouse.v>gMidPoint.v+100||lastMouse.v<gMidPoint.v-100){
1034 pos.x = gMidPoint.h;
1035 pos.y = gMidPoint.v;
1036 ClientToScreen(g_windowHandle, &pos);
1037 //SetCursorPos( gMidPoint.h,gMidPoint.v);
1038 SetCursorPos(pos.x, pos.y);
1039 lastMouse = gMidPoint;
1047 // --------------------------------------------------------------------------
1049 void DoKey (SInt8 theKey, SInt8 theCode)
1054 // --------------------------------------------------------------------------
1058 void DoFrameRate (int update)
1060 static long frames = 0;
1062 static AbsoluteTime time = {0,0};
1063 static AbsoluteTime frametime = {0,0};
1064 AbsoluteTime currTime = UpTime ();
1065 double deltaTime = (float) AbsoluteDeltaToDuration (currTime, frametime);
1067 if (0 > deltaTime) // if negative microseconds
1068 deltaTime /= -1000000.0;
1069 else // else milliseconds
1070 deltaTime /= 1000.0;
1072 multiplier=deltaTime;
1073 if(multiplier<.001)multiplier=.001;
1074 if(multiplier>10)multiplier=10;
1075 if(update)frametime = currTime; // reset for next time interval
1077 deltaTime = (float) AbsoluteDeltaToDuration (currTime, time);
1079 if (0 > deltaTime) // if negative microseconds
1080 deltaTime /= -1000000.0;
1081 else // else milliseconds
1082 deltaTime /= 1000.0;
1084 if (0.001 <= deltaTime) // has update interval passed
1087 time = currTime; // reset for next time interval
1094 void DoUpdate (Game & game)
1096 static float sps=200;
1098 static float oldmult;
1101 if(multiplier>.6)multiplier=.6;
1103 game.fps=1/multiplier;
1105 count = multiplier*sps;
1107 //if(count>10)count=10;
1109 realmultiplier=multiplier;
1110 multiplier*=gamespeed;
1111 if(difficulty==1)multiplier*=.9;
1112 if(difficulty==0)multiplier*=.8;
1114 if(game.loading==4)multiplier*=.00001;
1116 if(slomo&&!mainmenu)multiplier*=slomospeed;
1117 //if(freeze)multiplier*=0.00001;
1119 multiplier/=(float)count;
1125 for(int i=0;i<count;i++)
1131 game.TickOnceAfter();
1132 /* - Debug code to test how many channels were active on average per frame
1133 static long frames = 0;
1135 static AbsoluteTime start = {0,0};
1136 AbsoluteTime currTime = UpTime ();
1137 static int num_channels = 0;
1139 num_channels += FSOUND_GetChannelsPlaying();
1140 double deltaTime = (float) AbsoluteDeltaToDuration (currTime, start);
1142 if (0 > deltaTime) // if negative microseconds
1143 deltaTime /= -1000000.0;
1144 else // else milliseconds
1145 deltaTime /= 1000.0;
1152 float avg_channels = (float)num_channels / (float)frames;
1154 ofstream opstream("log.txt",ios::app);
1155 opstream << "Average frame count: ";
1157 opstream << " frames - ";
1158 opstream << avg_channels;
1159 opstream << " per frame.\n";
1169 // --------------------------------------------------------------------------
1185 #elif (defined WIN32)
1188 wglMakeCurrent( NULL, NULL);
1189 wglDeleteContext( hRC);
1195 ReleaseDC( g_windowHandle, hDC);
1201 ShowWindow( g_windowHandle, SW_HIDE );
1202 DestroyWindow( g_windowHandle);
1203 g_windowHandle = NULL;
1211 // --------------------------------------------------------------------------
1213 static bool g_focused = true;
1216 static bool IsFocused()
1222 if (GetActiveWindow() != g_windowHandle)
1225 if (IsIconic( g_windowHandle))
1236 #ifndef WIN32 // this is in WinMain, too.
1238 memset( &g_theKeys, 0, sizeof( KeyMap));
1245 bool regnow = false;
1250 //ofstream os("error.txt");
1252 //ofstream os("log.txt");
1257 while (!gDone&&!game.quit&&(!game.tryquit||!game.registered))
1263 // check windows messages
1267 while( SDL_PollEvent( &e ) )
1269 if( e.type == SDL_QUIT )
1276 #elif (defined WIN32)
1279 while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE | PM_NOYIELD ) )
1281 if( msg.message == WM_QUIT )
1288 TranslateMessage( &msg );
1289 DispatchMessage( &msg );
1301 // allow game chance to pause
1302 gameFocused = false;
1306 // game is not in focus, give CPU time to other apps by waiting for messages instead of 'peeking'
1310 //if (GetMessage( &msg, g_windowHandle, 0, 0 ))
1311 /*if (GetMessage( &msg, NULL, 0, 0 ))
1313 TranslateMessage(&msg);
1314 DispatchMessage(&msg);
1316 if ( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
1320 // handle the error and possibly exit
1325 TranslateMessage(&msg);
1326 DispatchMessage(&msg);
1330 STUBBED("give up CPU but sniff the event queue");
1335 regnow = game.registernow;
1340 // if(game.registernow){
1344 STUBBED("launch a web browser");
1347 sprintf(url,"http://www.wolfire.com/registerpc.html");
1349 ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);
1354 catch (const std::exception& error)
1358 std::string e = "Caught exception: ";
1361 LOG(e, Logger::LOG_ERR);
1363 MessageBox(g_windowHandle, error.what(), "ERROR", MB_OK | MB_ICONEXCLAMATION);
1373 // --------------------------------------------------------------------------
1376 #define MAX_WINKEYS 256
1377 static unsigned short KeyTable[MAX_WINKEYS]=
1380 MAC_MOUSEBUTTON1, // VK_LBUTTON (1)
1381 MAC_MOUSEBUTTON2, // VK_RBUTTON (2)
1382 0xffff, // VK_CANCEL (3)
1383 0xffff, // VK_MBUTTON (4)
1387 MAC_DELETE_KEY, // VK_BACK (8)
1388 MAC_TAB_KEY, // VK_TAB (9)
1391 0xffff, // VK_CLEAR (12)
1392 MAC_RETURN_KEY, // VK_RETURN (13)
1395 MAC_SHIFT_KEY, // VK_SHIFT (16)
1396 MAC_CONTROL_KEY, // VK_CONTROL (17)
1397 MAC_OPTION_KEY, // VK_MENU (18)
1398 0xffff, // VK_PAUSE (19)
1399 MAC_CAPS_LOCK_KEY, // #define VK_CAPITAL (20)
1406 MAC_ESCAPE_KEY, // VK_ESCAPE (27)
1411 MAC_SPACE_KEY, // VK_SPACE (32)
1412 MAC_PAGE_UP_KEY, // VK_PRIOR (33)
1413 MAC_PAGE_DOWN_KEY, // VK_NEXT (34)
1414 MAC_END_KEY, // VK_END (35)
1415 MAC_HOME_KEY, // VK_HOME (36)
1416 MAC_ARROW_LEFT_KEY, // VK_LEFT (37)
1417 MAC_ARROW_UP_KEY, // VK_UP (38)
1418 MAC_ARROW_RIGHT_KEY, // VK_RIGHT (39)
1419 MAC_ARROW_DOWN_KEY, // VK_DOWN (40)
1420 0xffff, // VK_SELECT (41)
1421 0xffff, // VK_PRINT (42)
1422 0xffff, // VK_EXECUTE (43)
1423 0xffff, // VK_SNAPSHOT (44)
1424 MAC_INSERT_KEY, // VK_INSERT (45)
1425 MAC_DEL_KEY, // VK_DELETE (46)
1426 0xffff, // VK_HELP (47)
1427 MAC_0_KEY, // VK_0 (48)
1428 MAC_1_KEY, // VK_1 (49)
1429 MAC_2_KEY, // VK_2 (50)
1430 MAC_3_KEY, // VK_3 (51)
1431 MAC_4_KEY, // VK_4 (52)
1432 MAC_5_KEY, // VK_5 (53)
1433 MAC_6_KEY, // VK_6 (54)
1434 MAC_7_KEY, // VK_7 (55)
1435 MAC_8_KEY, // VK_8 (56)
1436 MAC_9_KEY, // VK_9 (57)
1444 MAC_A_KEY, // VK_A (65)
1445 MAC_B_KEY, // VK_B (66)
1446 MAC_C_KEY, // VK_C (67)
1447 MAC_D_KEY, // VK_D (68)
1448 MAC_E_KEY, // VK_E (69)
1449 MAC_F_KEY, // VK_F (70)
1450 MAC_G_KEY, // VK_G (71)
1451 MAC_H_KEY, // VK_H (72)
1452 MAC_I_KEY, // VK_I (73)
1453 MAC_J_KEY, // VK_J (74)
1454 MAC_K_KEY, // VK_K (75)
1455 MAC_L_KEY, // VK_L (76)
1456 MAC_M_KEY, // VK_M (77)
1457 MAC_N_KEY, // VK_N (78)
1458 MAC_O_KEY, // VK_O (79)
1459 MAC_P_KEY, // VK_P (80)
1460 MAC_Q_KEY, // VK_Q (81)
1461 MAC_R_KEY, // VK_R (82)
1462 MAC_S_KEY, // VK_S (83)
1463 MAC_T_KEY, // VK_T (84)
1464 MAC_U_KEY, // VK_U (85)
1465 MAC_V_KEY, // VK_V (86)
1466 MAC_W_KEY, // VK_W (87)
1467 MAC_X_KEY, // VK_X (88)
1468 MAC_Y_KEY, // VK_Y (89)
1469 MAC_Z_KEY, // VK_Z (90)
1475 MAC_NUMPAD_0_KEY, // VK_NUMPAD0 (96)
1476 MAC_NUMPAD_1_KEY, // VK_NUMPAD1 (97)
1477 MAC_NUMPAD_2_KEY, // VK_NUMPAD2 (98)
1478 MAC_NUMPAD_3_KEY, // VK_NUMPAD3 (99)
1479 MAC_NUMPAD_4_KEY, // VK_NUMPAD4 (100)
1480 MAC_NUMPAD_5_KEY, // VK_NUMPAD5 (101)
1481 MAC_NUMPAD_6_KEY, // VK_NUMPAD6 (102)
1482 MAC_NUMPAD_7_KEY, // VK_NUMPAD7 (103)
1483 MAC_NUMPAD_8_KEY, // VK_NUMPAD8 (104)
1484 MAC_NUMPAD_9_KEY, // VK_NUMPAD9 (105)
1485 MAC_NUMPAD_ASTERISK_KEY, // VK_MULTIPLY (106)
1486 MAC_NUMPAD_PLUS_KEY, // VK_ADD (107)
1487 MAC_NUMPAD_ENTER_KEY, // VK_SEPARATOR (108)
1488 MAC_NUMPAD_MINUS_KEY, // VK_SUBTRACT (109)
1489 MAC_NUMPAD_PERIOD_KEY, // VK_DECIMAL (110)
1490 MAC_NUMPAD_SLASH_KEY, // VK_DIVIDE (111)
1491 MAC_F1_KEY, // VK_F1 (112)
1492 MAC_F2_KEY, // VK_F2 (113)
1493 MAC_F3_KEY, // VK_F3 (114)
1494 MAC_F4_KEY, // VK_F4 (115)
1495 MAC_F5_KEY, // VK_F5 (116)
1496 MAC_F6_KEY, // VK_F6 (117)
1497 MAC_F7_KEY, // VK_F7 (118)
1498 MAC_F8_KEY, // VK_F8 (119)
1499 MAC_F9_KEY, // VK_F9 (120)
1500 MAC_F10_KEY, // VK_F10 (121)
1501 MAC_F11_KEY, // VK_F11 (122)
1502 MAC_F12_KEY, // VK_F12 (123)
1523 0xffff, // VK_NUMLOCK (144)
1524 0xffff, // VK_SCROLL (145)
1539 MAC_SHIFT_KEY, // VK_LSHIFT (160)
1540 MAC_SHIFT_KEY, // VK_RSHIFT (161)
1541 MAC_CONTROL_KEY, // VK_LCONTROL (162)
1542 MAC_CONTROL_KEY, // VK_RCONTROL (163)
1543 MAC_OPTION_KEY, // VK_LMENU (164)
1544 MAC_OPTION_KEY, // VK_RMENU (165)
1565 MAC_SEMICOLON_KEY, // (186)
1566 MAC_PLUS_KEY, // (187)
1567 MAC_COMMA_KEY, // (188)
1568 MAC_MINUS_KEY, // (189)
1569 MAC_PERIOD_KEY, // (190)
1570 MAC_SLASH_KEY, // (191)
1571 MAC_TILDE_KEY, // (192)
1598 MAC_LEFTBRACKET_KEY, // (219)
1599 MAC_BACKSLASH_KEY, // (220)
1600 MAC_RIGHTBRACKET_KEY, // (221)
1601 MAC_APOSTROPHE_KEY, // (222)
1637 void ClipMouseToWindow(HWND window)
1641 GetClientRect(window, &wRect);
1643 ClientToScreen(window, (LPPOINT)&wRect.left);
1644 ClientToScreen(window, (LPPOINT)&wRect.right);
1651 LRESULT FAR PASCAL AppWndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
1653 /* this is where we receive all messages concerning this window
1654 * we can either process a message or pass it on to the default
1655 * message handler of windows */
1657 static PAINTSTRUCT ps;
1661 case WM_ACTIVATE: // Watch For Window Activate Message
1663 // Check Minimization State
1664 BOOL iconified = HIWORD(wParam) ? TRUE : FALSE;
1666 if (LOWORD(wParam) == WA_INACTIVE)
1675 CloseWindow( hWnd );
1677 // The window is now iconified
1678 iconified = GL_TRUE;
1684 g_focused=false; // Program Is Active
1688 SetupDSpFullScreen();
1695 // The window is now iconified
1696 iconified = GL_FALSE;
1699 ShowWindow( hWnd, SW_SHOW );
1700 SetForegroundWindow( hWnd );
1704 ClipMouseToWindow(hWnd);
1705 g_focused=true; // Program Is No Longer Active
1708 return 0; // Return To The Message Loop
1714 // check for Alt-F4 (exit hotkey)
1715 if (wParam == VK_F4)
1717 if (GetKeyState( VK_MENU) & 0x8080)
1723 if (wParam < MAX_WINKEYS)
1725 if (KeyTable[wParam] != 0xffff)
1726 SetKey( KeyTable[wParam]);
1734 if (wParam < MAX_WINKEYS)
1735 if (KeyTable[wParam] != 0xffff)
1736 ClearKey( KeyTable[wParam]);
1743 case WM_SYSDEADCHAR:
1746 case WM_NCLBUTTONDOWN:
1747 case WM_LBUTTONDOWN:
1754 case WM_NCRBUTTONDOWN:
1755 case WM_RBUTTONDOWN:
1761 case WM_NCMBUTTONDOWN:
1762 case WM_MBUTTONDOWN:
1768 case WM_NCLBUTTONUP:
1772 buttons[ 0] = false;
1776 case WM_NCRBUTTONUP:
1779 buttons[ 1] = false;
1783 case WM_NCMBUTTONUP:
1786 buttons[ 2] = false;
1790 case WM_NCLBUTTONDBLCLK:
1791 case WM_NCRBUTTONDBLCLK:
1792 case WM_NCMBUTTONDBLCLK:
1793 case WM_LBUTTONDBLCLK:
1795 case WM_RBUTTONDBLCLK:
1796 case WM_MBUTTONDBLCLK:
1799 case WM_NCMOUSEMOVE:
1801 /* ((WindowInfo *)g_lastWindow->GetInfo())->m_mouseX = (signed short)(lParam & 0xffff);
1802 ((WindowInfo *)g_lastWindow->GetInfo())->m_mouseY = (signed short)(lParam >> 16);
1803 if (g_lastWindow->m_mouseCallbacksEnabled) g_lastWindow->MouseMoveCallback();
1804 */// goto winmessage;
1807 case WM_SYSCOMMAND: // Intercept System Commands
1809 switch (wParam) // Check System Calls
1811 case SC_SCREENSAVE: // Screensaver Trying To Start?
1812 case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
1813 return 0; // Prevent From Happening
1815 // User trying to access application menu using ALT?
1824 // ReleaseCapture();
1825 // ClipMouseToWindow(hWnd);
1842 PostQuitMessage(0); /* Terminate Application */
1850 // BeginPaint( g_windowHandle,&ps);
1851 // EndPaint( g_windowHandle,&ps);
1858 /* We processed the message and there
1859 * is no processing by Windows necessary */
1861 /* We didn't process the message so let Windows do it */
1862 return DefWindowProc(hWnd,msg,wParam,lParam);
1866 static BOOL RegisterWindowClasses(HINSTANCE hFirstInstance)
1869 memset( &wc, 0, sizeof( wc));
1871 /* Register the window class. */
1872 wc.cbSize = sizeof(wc);
1874 wc.style = (CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_OWNDC); /* Combination of Class Styles */
1875 wc.lpfnWndProc = AppWndProc; /* Adress of Window Procedure */
1876 wc.cbClsExtra = 0; /* Extra Bytes allocated for this Class */
1877 wc.cbWndExtra = 0; /* Extra Bytes allocated for each Window */
1878 wc.hInstance = hFirstInstance; /* Handle of program instance */
1879 wc.hIcon = LoadIcon( hFirstInstance, MAKEINTRESOURCE(IDI_LUGARU) );
1880 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
1881 wc.hbrBackground = NULL;
1882 wc.lpszMenuName = NULL;
1883 wc.lpszClassName = g_wndClassName; /* Name of the Window Class */
1884 wc.hIconSm = LoadIcon( hFirstInstance, MAKEINTRESOURCE(IDI_LUGARU) );
1886 if (!RegisterClassEx(&wc)) return FALSE; /* Register Class failed */
1892 int resolutionID(int width, int height)
1896 if(width==640 && height==480)whichres=0;
1897 if(width==800 && height==600)whichres=1;
1898 if(width==1024 && height==768)whichres=2;
1899 if(width==1280 && height==1024)whichres=3;
1900 if(width==1600 && height==1200)whichres=4;
1901 if(width==840 && height==524)whichres=5;
1902 if(width==1024 && height==640)whichres=6;
1903 if(width==1344 && height==840)whichres=7;
1908 int closestResolution(int width, int height)
1912 if(width>=640 && height>=480)whichres=0;
1913 if(width>=800 && height>=600)whichres=1;
1914 if(width>=1024 && height>=768)whichres=2;
1915 if(width>=1280 && height>=1024)whichres=3;
1916 if(width>=1600 && height>=1200)whichres=4;
1917 if(width==840 && height==524)whichres=5;
1918 if(width==1024 && height==640)whichres=6;
1919 if(width==1344 && height==840)whichres=7;
1924 bool selectDetail(int & width, int & height, int & bpp, int & detail)
1927 int whichres = closestResolution(width, height);
1931 if(whichres<=0 || whichres>7){
1965 if ((detail != 0) && (resolutionDepths[whichres][1] != 0))
1969 else if ((detail == 0) && (resolutionDepths[whichres][0] != 0))
1973 else if ((detail != 0) && (resolutionDepths[whichres][0] != 0))
1979 else if (0 == whichres)
1987 bpp = resolutionDepths[whichres][(detail != 0)];
1993 int __stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
1996 LPWSTR * cl = CommandLineToArgvW(GetCommandLineW(), &argc);
1999 if (0 == _wcsicmp(cl[1], L"-windowed"))
2007 memset( &g_theKeys, 0, sizeof( KeyMap));
2011 memset(&mode, 0, sizeof(mode));
2012 mode.dmSize = sizeof(mode);
2013 while (EnumDisplaySettings(NULL, i++, &mode))
2015 if (mode.dmBitsPerPel < 16)
2020 int res = resolutionID(mode.dmPelsWidth, mode.dmPelsHeight);
2022 if (res > -1 && res < 8)
2024 if (DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettings(&mode, CDS_TEST))
2029 switch(mode.dmBitsPerPel)
2033 resolutionDepths[res][1] = mode.dmBitsPerPel;
2036 resolutionDepths[res][0] = mode.dmBitsPerPel;
2042 /* if there is no Instance of our program in memory then register the window class */
2043 if (hPrevInstance == NULL && !RegisterWindowClasses(hInstance))
2044 return FALSE; /* registration failed! */
2046 g_appInstance=hInstance;
2050 UnregisterClass( g_wndClassName, hInstance);
2057 extern int channels[100];
2058 extern FSOUND_SAMPLE * samp[100];
2059 extern FSOUND_STREAM * strm[10];
2061 extern "C" void PlaySoundEx(int chan, FSOUND_SAMPLE *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
2063 const FSOUND_SAMPLE * currSample = FSOUND_GetCurrentSample(channels[chan]);
2064 if (currSample && currSample == samp[chan])
2066 if (FSOUND_GetPaused(channels[chan]))
2068 FSOUND_StopSound(channels[chan]);
2069 channels[chan] = FSOUND_FREE;
2071 else if (FSOUND_IsPlaying(channels[chan]))
2073 int loop_mode = FSOUND_GetLoopMode(channels[chan]);
2074 if (loop_mode & FSOUND_LOOP_OFF)
2076 channels[chan] = FSOUND_FREE;
2082 channels[chan] = FSOUND_FREE;
2085 channels[chan] = FSOUND_PlaySoundEx(channels[chan], sptr, dsp, startpaused);
2086 if (channels[chan] < 0)
2088 channels[chan] = FSOUND_PlaySoundEx(FSOUND_FREE, sptr, dsp, startpaused);
2092 extern "C" void PlayStreamEx(int chan, FSOUND_STREAM *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
2094 const FSOUND_SAMPLE * currSample = FSOUND_GetCurrentSample(channels[chan]);
2095 if (currSample && currSample == FSOUND_Stream_GetSample(sptr))
2097 FSOUND_StopSound(channels[chan]);
2098 FSOUND_Stream_Stop(sptr);
2102 FSOUND_Stream_Stop(sptr);
2103 channels[chan] = FSOUND_FREE;
2106 channels[chan] = FSOUND_Stream_PlayEx(channels[chan], sptr, dsp, startpaused);
2107 if (channels[chan] < 0)
2109 channels[chan] = FSOUND_Stream_PlayEx(FSOUND_FREE, sptr, dsp, startpaused);
2113 bool LoadImage(const char * fname, TGAImageRec & tex)
2117 if ( tex.data == NULL )
2123 ILstring f = strdup(ConvertFileName(fname));
2130 ilGenImages(1, &iid);
2135 tex.sizeX = ilGetInteger(IL_IMAGE_WIDTH);
2136 tex.sizeY = ilGetInteger(IL_IMAGE_HEIGHT);
2137 tex.bpp = ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);
2138 ILuint Bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL),
2139 imageSize = tex.sizeX * tex.sizeY * Bpp;
2140 ILubyte *Data = ilGetData();
2141 memcpy(tex.data, Data, imageSize);
2143 // Truvision Targa files are stored as BGR colors
2144 // We want RGB so Blue and Red bytes are switched
2145 if (IL_TGA == ilGetInteger(IL_IMAGE_FORMAT))
2147 // Loop Through The Image Data
2148 for (GLuint i = 0; i < int(imageSize); i += Bpp)
2150 // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
2151 GLbyte temp; // Temporary Variable
2152 temp = tex.data[i]; // Temporarily Store The Value At Image Data 'i'
2153 tex.data[i] = tex.data[i + 2]; // Set The 1st Byte To The Value Of The 3rd Byte
2154 tex.data[i + 2] = temp; // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
2162 ilDeleteImages(1, &iid);
2166 GLuint texid = ilutGLLoadImage(f);
2171 ilutGLBuildMipmaps()
2180 res = load_image(fname, tex);
2181 //if (!res) printf("failed to load %s\n", fname);
2187 void ScreenShot(const char * fname)
2190 ILstring f = strdup(fname);
2197 ilGenImages(1, &iid);
2203 ilDeleteImages(1, &iid);
2207 STUBBED("Non-DevIL screenshot");
2213 static bool load_image(const char *file_name, TGAImageRec &tex)
2215 char *ptr = strrchr(file_name, '.');
2218 if (stricmp(ptr+1, "png") == 0)
2219 return load_png(file_name, tex);
2220 else if (stricmp(ptr+1, "jpg") == 0)
2221 return load_jpg(file_name, tex);
2224 STUBBED("Unsupported image type");
2229 struct my_error_mgr {
2230 struct jpeg_error_mgr pub; /* "public" fields */
2231 jmp_buf setjmp_buffer; /* for return to caller */
2233 typedef struct my_error_mgr * my_error_ptr;
2236 static void my_error_exit(j_common_ptr cinfo)
2238 struct my_error_mgr *err = (struct my_error_mgr *)cinfo->err;
2239 longjmp(err->setjmp_buffer, 1);
2242 /* stolen from public domain example.c code in libjpg distribution. */
2243 static bool load_jpg(const char *file_name, TGAImageRec &tex)
2245 struct jpeg_decompress_struct cinfo;
2246 struct my_error_mgr jerr;
2247 JSAMPROW buffer[1]; /* Output row buffer */
2248 int row_stride; /* physical row width in output buffer */
2249 FILE *infile = fopen(file_name, "rb")
2254 cinfo.err = jpeg_std_error(&jerr.pub);
2255 jerr.pub.error_exit = my_error_exit;
2256 if (setjmp(jerr.setjmp_buffer)) {
2257 jpeg_destroy_decompress(&cinfo);
2262 jpeg_create_decompress(&cinfo);
2263 jpeg_stdio_src(&cinfo, infile);
2264 (void) jpeg_read_header(&cinfo, TRUE);
2266 cinfo.out_color_space = JCS_RGB;
2267 cinfo.quantize_colors = 0;
2268 (void) jpeg_calc_output_dimensions(&cinfo);
2269 (void) jpeg_start_decompress(&cinfo);
2271 row_stride = cinfo.output_width * cinfo.output_components;
2272 tex.sizeX = cinfo.output_width;
2273 tex.sizeY = cinfo.output_height;
2276 while (cinfo.output_scanline < cinfo.output_height) {
2277 buffer[0] = (JSAMPROW)(char *)tex.data +
2278 ((cinfo.output_height-1) - cinfo.output_scanline) * row_stride;
2279 (void) jpeg_read_scanlines(&cinfo, buffer, 1);
2282 (void) jpeg_finish_decompress(&cinfo);
2283 jpeg_destroy_decompress(&cinfo);
2289 /* stolen from public domain example.c code in libpng distribution. */
2290 static bool load_png(const char *file_name, TGAImageRec &tex)
2292 bool hasalpha = false;
2293 png_structp png_ptr = NULL;
2294 png_infop info_ptr = NULL;
2295 png_uint_32 width, height;
2296 int bit_depth, color_type, interlace_type;
2297 png_byte **rows = NULL;
2298 bool retval = false;
2299 png_byte **row_pointers = NULL;
2300 FILE *fp = fopen(file_name, "rb");
2305 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2306 if (png_ptr == NULL)
2309 info_ptr = png_create_info_struct(png_ptr);
2310 if (info_ptr == NULL)
2313 if (setjmp(png_jmpbuf(png_ptr)))
2316 png_init_io(png_ptr, fp);
2317 png_read_png(png_ptr, info_ptr,
2318 PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING,
2320 png_get_IHDR(png_ptr, info_ptr, &width, &height,
2321 &bit_depth, &color_type, &interlace_type, NULL, NULL);
2323 if (bit_depth != 8) // transform SHOULD handle this...
2326 if (color_type & PNG_COLOR_MASK_PALETTE) // !!! FIXME?
2329 if ((color_type & PNG_COLOR_MASK_COLOR) == 0) // !!! FIXME?
2332 hasalpha = ((color_type & PNG_COLOR_MASK_ALPHA) != 0);
2333 row_pointers = png_get_rows(png_ptr, info_ptr);
2337 retval = malloc(width * height * 4);
2343 png_byte *dst = tex.data;
2344 for (int i = height-1; i >= 0; i--)
2346 png_byte *src = row_pointers[i];
2347 for (int j = 0; j < width; j++)
2361 png_byte *dst = tex.data;
2362 int pitch = width * 4;
2363 for (int i = height-1; i >= 0; i--, dst += pitch)
2364 memcpy(dst, row_pointers[i], pitch);
2373 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
2379 void save_png(char *file_name /* , ... other image information ... */)
2382 png_structp png_ptr;
2386 fp = fopen(file_name, "wb");
2390 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2391 if (png_ptr == NULL)
2397 info_ptr = png_create_info_struct(png_ptr);
2398 if (info_ptr == NULL)
2401 png_destroy_write_struct(&png_ptr, png_infopp_NULL);
2405 if (setjmp(png_jmpbuf(png_ptr)))
2407 /* If we get here, we had a problem reading the file */
2409 png_destroy_write_struct(&png_ptr, &info_ptr);
2413 png_init_io(png_ptr, fp);
2414 png_write_png(png_ptr, info_ptr, png_transforms, png_voidp_NULL);
2415 png_destroy_write_struct(&png_ptr, &info_ptr);