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);
28 static bool save_image(const char * fname);
29 static bool save_png(const char * fname);
34 #pragma comment(lib, "opengl32.lib")
35 #pragma comment(lib, "glu32.lib")
36 #pragma comment(lib, "glaux.lib")
39 extern bool buttons[3];
40 extern float multiplier;
41 extern float screenwidth,screenheight;
43 extern float realmultiplier;
45 extern bool ismotionblur;
46 extern float usermousesensitivity;
48 extern bool floatjump;
49 extern bool cellophane;
51 //extern int terraindetail;
52 //extern int texdetail;
53 extern float terraindetail;
54 extern float texdetail;
55 extern int bloodtoggle;
57 extern bool autoslomo;
59 extern bool musictoggle;
60 extern bool trilinear;
61 extern float gamespeed;
62 extern int difficulty;
63 extern bool damageeffects;
64 extern int numplayers;
66 extern bool invertmouse;
67 extern bool texttoggle;
68 extern bool ambientsound;
69 extern bool mousejump;
71 extern Person player[maxplayers];
73 extern bool stillloading;
74 extern bool showpoints;
75 extern bool alwaysblur;
76 extern bool immediate;
77 extern bool velocityblur;
78 extern bool debugmode;
80 /*extern*/ bool gameFocused;
81 extern int kBitsPerPixel;
82 extern float slomospeed;
83 extern float slomofreq;
84 extern float oldgamespeed;
93 #include "MacCompatibility.h"
101 #include "res/resource.h"
106 unsigned int resolutionDepths[8][2] = {0};
108 bool selectDetail(int & width, int & height, int & bpp, int & detail);
109 int closestResolution(int width, int height);
110 int resolutionID(int width, int height);
112 void ReportError (char * strError);
114 void SetupDSpFullScreen();
117 void DrawGL(Game & game);
119 void CreateGLWindow (void);
120 Boolean SetUp (Game & game);
121 void DoKey (SInt8 theKey, SInt8 theCode);
122 void DoUpdate (Game & game);
128 // statics/globals (internal only) ------------------------------------------
130 typedef struct tagPOINT {
137 #define GL_FUNC(ret,fn,params,call,rt) \
139 static ret GLAPIENTRY (*p##fn) params = NULL; \
140 ret GLAPIENTRY fn params { rt p##fn call; } \
145 static bool lookup_glsym(const char *funcname, void **func, const char *libname)
147 *func = SDL_GL_GetProcAddress(funcname);
150 fprintf(stderr, "Failed to find OpenGL symbol \"%s\" in \"%s\"\n",
157 static bool lookup_all_glsyms(const char *libname)
160 #define GL_FUNC(ret,fn,params,call,rt) \
161 if (!lookup_glsym(#fn, (void **) &p##fn, libname)) retval = false;
167 static void GLAPIENTRY glDeleteTextures_doNothing(GLsizei n, const GLuint *textures)
174 void sdlGetCursorPos(POINT *pt)
177 SDL_GetMouseState(&x, &y);
181 #define GetCursorPos(x) sdlGetCursorPos(x)
182 #define SetCursorPos(x, y) SDL_WarpMouse(x, y)
183 #define ScreenToClient(x, pt)
184 #define ClientToScreen(x, pt)
185 #define MessageBox(hwnd,text,title,flags) STUBBED("msgbox")
191 static const char g_wndClassName[]={ "LUGARUWINDOWCLASS" };
192 static HINSTANCE g_appInstance;
193 static HWND g_windowHandle;
197 static bool g_button, fullscreen = true;
208 kForegroundSleep = 10,
209 kBackgroundSleep = 10000
216 const RGBColor rgbBlack = { 0x0000, 0x0000, 0x0000 };
219 char gcstrMode [256] = "";
221 UInt32 gSleepTime = kForegroundSleep;
222 Boolean gDone = false, gfFrontProcess = true;
226 static bool gMouseGrabbed = true;
228 // --------------------------------------------------------------------------
230 void ReportError (char * strError)
232 #ifdef WIN32 // !!! FIXME. --ryan.
233 throw std::exception( strError);
236 /* char errMsgCStr [256];
239 sprintf (errMsgCStr, "%s", strError);
241 // out as debug string
242 CToPStr (strErr, errMsgCStr);
247 void SetupDSpFullScreen ()
254 DEVMODE dmScreenSettings;
255 memset( &dmScreenSettings, 0, sizeof( dmScreenSettings));
256 dmScreenSettings.dmSize = sizeof( dmScreenSettings);
257 dmScreenSettings.dmPelsWidth = kContextWidth;
258 dmScreenSettings.dmPelsHeight = kContextHeight;
259 dmScreenSettings.dmBitsPerPel = kBitsPerPixel;
260 dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
263 if (ChangeDisplaySettings( &dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
265 ReportError( "Could not set display mode");
282 ChangeDisplaySettings( NULL, 0);
290 //-----------------------------------------------------------------------------------------------------------------------
294 void DrawGL (Game & game)
305 static KeyMap g_theKeys;
307 void SetKey( int key)
309 g_theKeys[ key >> 3] |= (1 << (key & 7));
312 void ClearKey( int key)
314 g_theKeys[ key >> 3] &= (0xff ^ (1 << (key & 7)));
317 void GetKeys( unsigned char theKeys[16])
319 memcpy( theKeys, &g_theKeys, 16);
328 static void initSDLKeyTable(void) {}
330 #define MAX_SDLKEYS SDLK_LAST
331 static unsigned short KeyTable[MAX_SDLKEYS];
333 static void initSDLKeyTable(void)
335 memset(KeyTable, 0xFF, sizeof (KeyTable));
336 KeyTable[SDLK_BACKSPACE] = MAC_DELETE_KEY;
337 KeyTable[SDLK_TAB] = MAC_TAB_KEY;
338 KeyTable[SDLK_RETURN] = MAC_RETURN_KEY;
339 KeyTable[SDLK_ESCAPE] = MAC_ESCAPE_KEY;
340 KeyTable[SDLK_SPACE] = MAC_SPACE_KEY;
341 KeyTable[SDLK_PAGEUP] = MAC_PAGE_UP_KEY;
342 KeyTable[SDLK_PAGEDOWN] = MAC_PAGE_DOWN_KEY;
343 KeyTable[SDLK_END] = MAC_END_KEY;
344 KeyTable[SDLK_HOME] = MAC_HOME_KEY;
345 KeyTable[SDLK_LEFT] = MAC_ARROW_LEFT_KEY;
346 KeyTable[SDLK_UP] = MAC_ARROW_UP_KEY;
347 KeyTable[SDLK_RIGHT] = MAC_ARROW_RIGHT_KEY;
348 KeyTable[SDLK_DOWN] = MAC_ARROW_DOWN_KEY;
349 KeyTable[SDLK_INSERT] = MAC_INSERT_KEY;
350 KeyTable[SDLK_DELETE] = MAC_DEL_KEY;
351 KeyTable[SDLK_0] = MAC_0_KEY;
352 KeyTable[SDLK_1] = MAC_1_KEY;
353 KeyTable[SDLK_2] = MAC_2_KEY;
354 KeyTable[SDLK_3] = MAC_3_KEY;
355 KeyTable[SDLK_4] = MAC_4_KEY;
356 KeyTable[SDLK_5] = MAC_5_KEY;
357 KeyTable[SDLK_6] = MAC_6_KEY;
358 KeyTable[SDLK_7] = MAC_7_KEY;
359 KeyTable[SDLK_8] = MAC_8_KEY;
360 KeyTable[SDLK_9] = MAC_9_KEY;
361 KeyTable[SDLK_a] = MAC_A_KEY;
362 KeyTable[SDLK_b] = MAC_B_KEY;
363 KeyTable[SDLK_c] = MAC_C_KEY;
364 KeyTable[SDLK_d] = MAC_D_KEY;
365 KeyTable[SDLK_e] = MAC_E_KEY;
366 KeyTable[SDLK_f] = MAC_F_KEY;
367 KeyTable[SDLK_g] = MAC_G_KEY;
368 KeyTable[SDLK_h] = MAC_H_KEY;
369 KeyTable[SDLK_i] = MAC_I_KEY;
370 KeyTable[SDLK_j] = MAC_J_KEY;
371 KeyTable[SDLK_k] = MAC_K_KEY;
372 KeyTable[SDLK_l] = MAC_L_KEY;
373 KeyTable[SDLK_m] = MAC_M_KEY;
374 KeyTable[SDLK_n] = MAC_N_KEY;
375 KeyTable[SDLK_o] = MAC_O_KEY;
376 KeyTable[SDLK_p] = MAC_P_KEY;
377 KeyTable[SDLK_q] = MAC_Q_KEY;
378 KeyTable[SDLK_r] = MAC_R_KEY;
379 KeyTable[SDLK_s] = MAC_S_KEY;
380 KeyTable[SDLK_t] = MAC_T_KEY;
381 KeyTable[SDLK_u] = MAC_U_KEY;
382 KeyTable[SDLK_v] = MAC_V_KEY;
383 KeyTable[SDLK_w] = MAC_W_KEY;
384 KeyTable[SDLK_x] = MAC_X_KEY;
385 KeyTable[SDLK_y] = MAC_Y_KEY;
386 KeyTable[SDLK_z] = MAC_Z_KEY;
387 KeyTable[SDLK_KP0] = MAC_NUMPAD_0_KEY;
388 KeyTable[SDLK_KP1] = MAC_NUMPAD_1_KEY;
389 KeyTable[SDLK_KP2] = MAC_NUMPAD_2_KEY;
390 KeyTable[SDLK_KP3] = MAC_NUMPAD_3_KEY;
391 KeyTable[SDLK_KP4] = MAC_NUMPAD_4_KEY;
392 KeyTable[SDLK_KP5] = MAC_NUMPAD_5_KEY;
393 KeyTable[SDLK_KP6] = MAC_NUMPAD_6_KEY;
394 KeyTable[SDLK_KP7] = MAC_NUMPAD_7_KEY;
395 KeyTable[SDLK_KP8] = MAC_NUMPAD_8_KEY;
396 KeyTable[SDLK_KP9] = MAC_NUMPAD_9_KEY;
397 KeyTable[SDLK_KP_MULTIPLY] = MAC_NUMPAD_ASTERISK_KEY;
398 KeyTable[SDLK_KP_PLUS] = MAC_NUMPAD_PLUS_KEY;
399 KeyTable[SDLK_KP_ENTER] = MAC_NUMPAD_ENTER_KEY;
400 KeyTable[SDLK_KP_MINUS] = MAC_NUMPAD_MINUS_KEY;
401 KeyTable[SDLK_KP_PERIOD] = MAC_NUMPAD_PERIOD_KEY;
402 KeyTable[SDLK_KP_DIVIDE] = MAC_NUMPAD_SLASH_KEY;
403 KeyTable[SDLK_F1] = MAC_F1_KEY;
404 KeyTable[SDLK_F2] = MAC_F2_KEY;
405 KeyTable[SDLK_F3] = MAC_F3_KEY;
406 KeyTable[SDLK_F4] = MAC_F4_KEY;
407 KeyTable[SDLK_F5] = MAC_F5_KEY;
408 KeyTable[SDLK_F6] = MAC_F6_KEY;
409 KeyTable[SDLK_F7] = MAC_F7_KEY;
410 KeyTable[SDLK_F8] = MAC_F8_KEY;
411 KeyTable[SDLK_F9] = MAC_F9_KEY;
412 KeyTable[SDLK_F10] = MAC_F10_KEY;
413 KeyTable[SDLK_F11] = MAC_F11_KEY;
414 KeyTable[SDLK_F12] = MAC_F12_KEY;
415 KeyTable[SDLK_SEMICOLON] = MAC_SEMICOLON_KEY;
416 KeyTable[SDLK_PLUS] = MAC_PLUS_KEY;
417 KeyTable[SDLK_COMMA] = MAC_COMMA_KEY;
418 KeyTable[SDLK_MINUS] = MAC_MINUS_KEY;
419 KeyTable[SDLK_PERIOD] = MAC_PERIOD_KEY;
420 KeyTable[SDLK_SLASH] = MAC_SLASH_KEY;
421 KeyTable[SDLK_BACKQUOTE] = MAC_TILDE_KEY;
422 KeyTable[SDLK_LEFTBRACKET] = MAC_LEFTBRACKET_KEY;
423 KeyTable[SDLK_BACKSLASH] = MAC_BACKSLASH_KEY;
424 KeyTable[SDLK_RIGHTBRACKET] = MAC_RIGHTBRACKET_KEY;
425 KeyTable[SDLK_QUOTE] = MAC_APOSTROPHE_KEY;
428 static inline int clamp_sdl_mouse_button(Uint8 button)
430 if ((button >= 1) && (button <= 3))
435 static void sdlEventProc(const SDL_Event &e)
440 case SDL_MOUSEBUTTONDOWN:
442 val = clamp_sdl_mouse_button(e.button.button);
448 SetKey(MAC_MOUSEBUTTON1);
452 SetKey(MAC_MOUSEBUTTON2);
459 case SDL_MOUSEBUTTONUP:
461 val = clamp_sdl_mouse_button(e.button.button);
467 ClearKey(MAC_MOUSEBUTTON1);
471 ClearKey(MAC_MOUSEBUTTON2);
473 buttons[val] = false;
479 if (e.key.keysym.sym == SDLK_g)
481 if (e.key.keysym.mod & KMOD_CTRL)
482 gMouseGrabbed = !gMouseGrabbed;
485 else if (e.key.keysym.sym == SDLK_RETURN)
487 if (e.key.keysym.mod & KMOD_ALT)
488 SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
491 if (e.key.keysym.sym < SDLK_LAST)
493 if (KeyTable[e.key.keysym.sym] != 0xffff)
494 SetKey(KeyTable[e.key.keysym.sym]);
497 if (e.key.keysym.mod & KMOD_CTRL)
498 SetKey(MAC_CONTROL_KEY);
499 if (e.key.keysym.mod & KMOD_ALT)
500 SetKey(MAC_OPTION_KEY);
501 if (e.key.keysym.mod & KMOD_SHIFT)
502 SetKey(MAC_SHIFT_KEY);
503 if (e.key.keysym.mod & KMOD_CAPS)
504 SetKey(MAC_CAPS_LOCK_KEY);
509 if (e.key.keysym.sym < SDLK_LAST)
511 if (KeyTable[e.key.keysym.sym] != 0xffff)
512 ClearKey(KeyTable[e.key.keysym.sym]);
515 if (e.key.keysym.mod & KMOD_CTRL)
516 ClearKey(MAC_CONTROL_KEY);
517 if (e.key.keysym.mod & KMOD_ALT)
518 ClearKey(MAC_OPTION_KEY);
519 if (e.key.keysym.mod & KMOD_SHIFT)
520 ClearKey(MAC_SHIFT_KEY);
521 if (e.key.keysym.mod & KMOD_CAPS)
522 ClearKey(MAC_CAPS_LOCK_KEY);
528 // --------------------------------------------------------------------------
530 static Point gMidPoint;
532 Boolean SetUp (Game & game)
538 randSeed = UpTime().lo;
541 // ifstream ipstream(":Data:config.txt", std::ios::in /*| std::ios::nocreate*/);
542 ifstream ipstream("./Data/config.txt", std::ios::in /*| std::ios::nocreate*/);
545 usermousesensitivity=1;
574 game.crouchkey=MAC_SHIFT_KEY;
575 game.jumpkey=MAC_SPACE_KEY;
576 game.leftkey=MAC_A_KEY;
577 game.forwardkey=MAC_W_KEY;
578 game.backkey=MAC_S_KEY;
579 game.rightkey=MAC_D_KEY;
580 game.drawkey=MAC_E_KEY;
581 game.throwkey=MAC_Q_KEY;
582 game.attackkey=MAC_MOUSEBUTTON1;
583 game.chatkey=MAC_T_KEY;
589 selectDetail(kContextWidth, kContextHeight, kBitsPerPixel, detail);
592 //ofstream opstream(":Data:config.txt");
593 ofstream opstream("./Data/config.txt");
594 opstream << "Screenwidth:\n";
595 opstream << kContextWidth;
596 opstream << "\nScreenheight:\n";
597 opstream << kContextHeight;
598 opstream << "\nMouse sensitivity:\n";
599 opstream << usermousesensitivity;
600 opstream << "\nBlur(0,1):\n";
601 opstream << ismotionblur;
602 opstream << "\nOverall Detail(0,1,2) higher=better:\n";
604 opstream << "\nFloating jump:\n";
605 opstream << floatjump;
606 opstream << "\nMouse jump:\n";
607 opstream << mousejump;
608 opstream << "\nAmbient sound:\n";
609 opstream << ambientsound;
610 opstream << "\nBlood (0,1,2):\n";
611 opstream << bloodtoggle;
612 opstream << "\nAuto slomo:\n";
613 opstream << autoslomo;
614 opstream << "\nFoliage:\n";
616 opstream << "\nMusic:\n";
617 opstream << musictoggle;
618 opstream << "\nTrilinear:\n";
619 opstream << trilinear;
620 opstream << "\nDecals(shadows,blood puddles,etc):\n";
622 opstream << "\nInvert mouse:\n";
623 opstream << invertmouse;
624 opstream << "\nGamespeed:\n";
625 opstream << gamespeed;
626 opstream << "\nDifficulty(0,1,2) higher=harder:\n";
627 opstream << difficulty;
628 opstream << "\nDamage effects(blackout, doublevision):\n";
629 opstream << damageeffects;
630 opstream << "\nText:\n";
631 opstream << texttoggle;
632 opstream << "\nDebug:\n";
633 opstream << debugmode;
634 opstream << "\nVBL Sync:\n";
636 opstream << "\nShow Points:\n";
637 opstream << showpoints;
638 opstream << "\nAlways Blur:\n";
639 opstream << alwaysblur;
640 opstream << "\nImmediate mode (turn on on G5):\n";
641 opstream << immediate;
642 opstream << "\nVelocity blur:\n";
643 opstream << velocityblur;
644 opstream << "\nVolume:\n";
646 opstream << "\nForward key:\n";
647 opstream << KeyToChar(game.forwardkey);
648 opstream << "\nBack key:\n";
649 opstream << KeyToChar(game.backkey);
650 opstream << "\nLeft key:\n";
651 opstream << KeyToChar(game.leftkey);
652 opstream << "\nRight key:\n";
653 opstream << KeyToChar(game.rightkey);
654 opstream << "\nJump key:\n";
655 opstream << KeyToChar(game.jumpkey);
656 opstream << "\nCrouch key:\n";
657 opstream << KeyToChar(game.crouchkey);
658 opstream << "\nDraw key:\n";
659 opstream << KeyToChar(game.drawkey);
660 opstream << "\nThrow key:\n";
661 opstream << KeyToChar(game.throwkey);
662 opstream << "\nAttack key:\n";
663 opstream << KeyToChar(game.attackkey);
664 opstream << "\nChat key:\n";
665 opstream << KeyToChar(game.chatkey);
670 ipstream.ignore(256,'\n');
671 ipstream >> kContextWidth;
672 ipstream.ignore(256,'\n');
673 ipstream.ignore(256,'\n');
674 ipstream >> kContextHeight;
675 ipstream.ignore(256,'\n');
676 ipstream.ignore(256,'\n');
677 ipstream >> usermousesensitivity;
678 ipstream.ignore(256,'\n');
679 ipstream.ignore(256,'\n');
681 ismotionblur = (i != 0);
682 ipstream.ignore(256,'\n');
683 ipstream.ignore(256,'\n');
685 if(detail!=0)kBitsPerPixel=32;
686 else kBitsPerPixel=16;
687 ipstream.ignore(256,'\n');
688 ipstream.ignore(256,'\n');
690 floatjump = (i != 0);
691 ipstream.ignore(256,'\n');
692 ipstream.ignore(256,'\n');
694 mousejump = (i != 0);
695 ipstream.ignore(256,'\n');
696 ipstream.ignore(256,'\n');
698 ambientsound = (i != 0);
699 ipstream.ignore(256,'\n');
700 ipstream.ignore(256,'\n');
701 ipstream >> bloodtoggle;
702 ipstream.ignore(256,'\n');
703 ipstream.ignore(256,'\n');
705 autoslomo = (i != 0);
706 ipstream.ignore(256,'\n');
707 ipstream.ignore(256,'\n');
710 ipstream.ignore(256,'\n');
711 ipstream.ignore(256,'\n');
713 musictoggle = (i != 0);
714 ipstream.ignore(256,'\n');
715 ipstream.ignore(256,'\n');
717 trilinear = (i != 0);
718 ipstream.ignore(256,'\n');
719 ipstream.ignore(256,'\n');
722 ipstream.ignore(256,'\n');
723 ipstream.ignore(256,'\n');
725 invertmouse = (i != 0);
726 ipstream.ignore(256,'\n');
727 ipstream.ignore(256,'\n');
728 ipstream >> gamespeed;
729 oldgamespeed=gamespeed;
734 ipstream.ignore(256,'\n');
735 ipstream.ignore(256,'\n');
736 ipstream >> difficulty;
737 ipstream.ignore(256,'\n');
738 ipstream.ignore(256,'\n');
740 damageeffects = (i != 0);
741 ipstream.ignore(256,'\n');
742 ipstream.ignore(256,'\n');
744 texttoggle = (i != 0);
745 ipstream.ignore(256,'\n');
746 ipstream.ignore(256,'\n');
748 debugmode = (i != 0);
749 ipstream.ignore(256,'\n');
750 ipstream.ignore(256,'\n');
753 ipstream.ignore(256,'\n');
754 ipstream.ignore(256,'\n');
756 showpoints = (i != 0);
757 ipstream.ignore(256,'\n');
758 ipstream.ignore(256,'\n');
760 alwaysblur = (i != 0);
761 ipstream.ignore(256,'\n');
762 ipstream.ignore(256,'\n');
764 immediate = (i != 0);
765 ipstream.ignore(256,'\n');
766 ipstream.ignore(256,'\n');
768 velocityblur = (i != 0);
769 ipstream.ignore(256,'\n');
770 ipstream.ignore(256,'\n');
772 ipstream.ignore(256,'\n');
773 ipstream.ignore(256,'\n');
775 game.forwardkey=CharToKey(string);
776 ipstream.ignore(256,'\n');
777 ipstream.ignore(256,'\n');
779 game.backkey=CharToKey(string);
780 ipstream.ignore(256,'\n');
781 ipstream.ignore(256,'\n');
783 game.leftkey=CharToKey(string);
784 ipstream.ignore(256,'\n');
785 ipstream.ignore(256,'\n');
787 game.rightkey=CharToKey(string);
788 ipstream.ignore(256,'\n');
789 ipstream.ignore(256,'\n');
791 game.jumpkey=CharToKey(string);
792 ipstream.ignore(256,'\n');
793 ipstream.ignore(256,'\n');
795 game.crouchkey=CharToKey(string);
796 ipstream.ignore(256,'\n');
797 ipstream.ignore(256,'\n');
799 game.drawkey=CharToKey(string);
800 ipstream.ignore(256,'\n');
801 ipstream.ignore(256,'\n');
803 game.throwkey=CharToKey(string);
804 ipstream.ignore(256,'\n');
805 ipstream.ignore(256,'\n');
807 game.attackkey=CharToKey(string);
808 ipstream.ignore(256,'\n');
809 ipstream.ignore(256,'\n');
811 game.chatkey=CharToKey(string);
814 if(detail>2)detail=2;
815 if(detail<0)detail=0;
816 if(screenwidth>3000)screenwidth=640;
817 if(screenwidth<0)screenwidth=640;
818 if(screenheight>3000)screenheight=480;
819 if(screenheight<0)screenheight=480;
821 if(kBitsPerPixel!=32&&kBitsPerPixel!=16){
826 selectDetail(kContextWidth, kContextHeight, kBitsPerPixel, detail);
828 SetupDSpFullScreen();
831 if (!SDL_WasInit(SDL_INIT_VIDEO))
833 if (SDL_Init(SDL_INIT_VIDEO) == -1)
835 fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
839 const char *libname = "libGL.so.1"; // !!! FIXME: Linux specific!
840 if (SDL_GL_LoadLibrary(libname) == -1)
842 fprintf(stderr, "SDL_GL_LoadLibrary(\"%s\") failed: %s\n",
843 libname, SDL_GetError());
847 if (!lookup_all_glsyms(libname))
851 Uint32 sdlflags = SDL_OPENGL; // !!! FIXME: SDL_FULLSCREEN?
852 SDL_WM_SetCaption("Lugaru", "lugaru");
854 if (SDL_SetVideoMode(kContextWidth, kContextHeight, 0, sdlflags) == NULL)
856 fprintf(stderr, "SDL_SetVideoMode() failed: %s\n", SDL_GetError());
862 #elif (defined WIN32)
863 //------------------------------------------------------------------
866 RECT r = {0, 0, kContextWidth-1, kContextHeight-1};
867 DWORD dwStyle = WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE;
868 DWORD dwExStyle = WS_EX_APPWINDOW;
877 dwStyle |= WS_OVERLAPPEDWINDOW;
878 dwExStyle |= WS_EX_WINDOWEDGE;
881 AdjustWindowRectEx(&r, dwStyle, FALSE, dwExStyle);
885 x = (GetSystemMetrics(SM_CXSCREEN) >> 1) - ((r.right - r.left + 1) >> 1);
886 y = (GetSystemMetrics(SM_CYSCREEN) >> 1) - ((r.bottom - r.top + 1) >> 1);
889 g_windowHandle=CreateWindowEx(
891 g_wndClassName, "Lugaru", dwStyle,
893 // kContextWidth, kContextHeight,
894 r.right - r.left + 1, r.bottom - r.top + 1,
895 NULL,NULL,g_appInstance,NULL );
898 ReportError("Could not create window");
902 //------------------------------------------------------------------
905 static PIXELFORMATDESCRIPTOR pfd =
907 sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
909 PFD_DRAW_TO_WINDOW | // Format Must Support Window
910 PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
911 PFD_DOUBLEBUFFER, // Must Support Double Buffering
912 PFD_TYPE_RGBA, // Request An RGBA Format
913 kBitsPerPixel, // Select Our Color Depth
914 0, 0, 0, 0, 0, 0, // Color Bits Ignored
915 0, // No Alpha Buffer
916 0, // Shift Bit Ignored
917 0, // No Accumulation Buffer
918 0, 0, 0, 0, // Accumulation Bits Ignored
919 16, // 16Bit Z-Buffer (Depth Buffer)
920 0, // No Stencil Buffer
921 0, // No Auxiliary Buffer
922 PFD_MAIN_PLANE, // Main Drawing Layer
924 0, 0, 0 // Layer Masks Ignored
927 if (!(hDC = GetDC( g_windowHandle)))
928 ReportError( "Could not get device context");
931 if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
933 ReportError( "Could not find appropriate pixel format");
937 if (!DescribePixelFormat(hDC, PixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd))
939 ReportError( "Could not retrieve pixel format");
943 if (!SetPixelFormat( hDC, PixelFormat, &pfd))
945 ReportError( "Could not set pixel format");
949 if (!(hRC = wglCreateContext(hDC)))
951 ReportError( "Could not create rendering context");
955 if (!wglMakeCurrent(hDC, hRC))
957 ReportError( "Could not activate rendering context");
963 // Place the window above all topmost windows
964 SetWindowPos( g_windowHandle, HWND_TOPMOST, 0,0,0,0,
965 SWP_NOMOVE | SWP_NOSIZE );
968 SetForegroundWindow(g_windowHandle);
969 SetFocus(g_windowHandle);
972 glClear( GL_COLOR_BUFFER_BIT );
976 glDisable( GL_ALPHA_TEST);
977 glDisable( GL_BLEND);
978 glDisable( GL_DEPTH_TEST);
979 // glDisable( GL_DITHER);
981 glDisable( GL_LIGHTING);
982 glDisable( GL_LOGIC_OP);
983 glDisable( GL_STENCIL_TEST);
984 glDisable( GL_TEXTURE_1D);
985 glDisable( GL_TEXTURE_2D);
986 glPixelTransferi( GL_MAP_COLOR, GL_FALSE);
987 glPixelTransferi( GL_RED_SCALE, 1);
988 glPixelTransferi( GL_RED_BIAS, 0);
989 glPixelTransferi( GL_GREEN_SCALE, 1);
990 glPixelTransferi( GL_GREEN_BIAS, 0);
991 glPixelTransferi( GL_BLUE_SCALE, 1);
992 glPixelTransferi( GL_BLUE_BIAS, 0);
993 glPixelTransferi( GL_ALPHA_SCALE, 1);
994 glPixelTransferi( GL_ALPHA_BIAS, 0);
996 // set initial rendering states
997 glShadeModel( GL_SMOOTH);
999 glDepthFunc( GL_LEQUAL);
1000 glDepthMask( GL_TRUE);
1001 // glDepthRange( FRONT_CLIP, BACK_CLIP);
1002 glEnable( GL_DEPTH_TEST);
1003 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
1004 glCullFace( GL_FRONT);
1005 glEnable( GL_CULL_FACE);
1006 glEnable( GL_LIGHTING);
1007 // glEnable( GL_LIGHT_MODEL_AMBIENT);
1008 glEnable( GL_DITHER);
1009 glEnable( GL_COLOR_MATERIAL);
1010 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1011 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1012 glAlphaFunc( GL_GREATER, 0.5f);
1015 if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION ||
1016 iluGetInteger(ILU_VERSION_NUM) < ILU_VERSION ||
1017 ilutGetInteger(ILUT_VERSION_NUM) < ILUT_VERSION)
1019 ReportError("DevIL version is different...exiting!\n");
1027 ilutRenderer(ILUT_OPENGL);
1029 ilEnable(IL_ORIGIN_SET);
1030 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
1033 GLint width = kContextWidth;
1034 GLint height = kContextHeight;
1035 gMidPoint.h = width / 2;
1036 gMidPoint.v = height / 2;
1038 screenheight=height;
1040 game.newdetail=detail;
1041 game.newscreenwidth=screenwidth;
1042 game.newscreenheight=screenheight;
1050 static void DoMouse(Game & game)
1052 static Point lastMouse = {-1,-1};
1057 ScreenToClient(g_windowHandle, &pos);
1058 globalMouse.h = pos.x;
1059 globalMouse.v = pos.y;
1061 if (lastMouse.h == globalMouse.h && lastMouse.v == globalMouse.v)
1068 static Point virtualMouse = {0,0};
1069 delta = globalMouse;
1071 delta.h -= lastMouse.h;
1072 delta.v -= lastMouse.v;
1073 lastMouse.h = pos.x;
1074 lastMouse.v = pos.y;
1076 if(mainmenu||(abs(delta.h)<10*realmultiplier*1000&&abs(delta.v)<10*realmultiplier*1000)){
1077 game.deltah=delta.h*usermousesensitivity;
1078 game.deltav=delta.v*usermousesensitivity;
1079 game.mousecoordh=globalMouse.h;
1080 game.mousecoordv=globalMouse.v;
1083 if((!mainmenu)&&(gMouseGrabbed))
1085 if(lastMouse.h>gMidPoint.h+100||lastMouse.h<gMidPoint.h-100||lastMouse.v>gMidPoint.v+100||lastMouse.v<gMidPoint.v-100){
1086 pos.x = gMidPoint.h;
1087 pos.y = gMidPoint.v;
1088 ClientToScreen(g_windowHandle, &pos);
1089 //SetCursorPos( gMidPoint.h,gMidPoint.v);
1090 SetCursorPos(pos.x, pos.y);
1091 lastMouse = gMidPoint;
1099 // --------------------------------------------------------------------------
1101 void DoKey (SInt8 theKey, SInt8 theCode)
1106 // --------------------------------------------------------------------------
1110 void DoFrameRate (int update)
1112 static long frames = 0;
1114 static AbsoluteTime time = {0,0};
1115 static AbsoluteTime frametime = {0,0};
1116 AbsoluteTime currTime = UpTime ();
1117 double deltaTime = (float) AbsoluteDeltaToDuration (currTime, frametime);
1119 if (0 > deltaTime) // if negative microseconds
1120 deltaTime /= -1000000.0;
1121 else // else milliseconds
1122 deltaTime /= 1000.0;
1124 multiplier=deltaTime;
1125 if(multiplier<.001)multiplier=.001;
1126 if(multiplier>10)multiplier=10;
1127 if(update)frametime = currTime; // reset for next time interval
1129 deltaTime = (float) AbsoluteDeltaToDuration (currTime, time);
1131 if (0 > deltaTime) // if negative microseconds
1132 deltaTime /= -1000000.0;
1133 else // else milliseconds
1134 deltaTime /= 1000.0;
1136 if (0.001 <= deltaTime) // has update interval passed
1139 time = currTime; // reset for next time interval
1146 void DoUpdate (Game & game)
1148 static float sps=200;
1150 static float oldmult;
1153 if(multiplier>.6)multiplier=.6;
1155 game.fps=1/multiplier;
1157 count = multiplier*sps;
1159 //if(count>10)count=10;
1161 realmultiplier=multiplier;
1162 multiplier*=gamespeed;
1163 if(difficulty==1)multiplier*=.9;
1164 if(difficulty==0)multiplier*=.8;
1166 if(game.loading==4)multiplier*=.00001;
1168 if(slomo&&!mainmenu)multiplier*=slomospeed;
1169 //if(freeze)multiplier*=0.00001;
1171 multiplier/=(float)count;
1177 for(int i=0;i<count;i++)
1183 game.TickOnceAfter();
1184 /* - Debug code to test how many channels were active on average per frame
1185 static long frames = 0;
1187 static AbsoluteTime start = {0,0};
1188 AbsoluteTime currTime = UpTime ();
1189 static int num_channels = 0;
1191 num_channels += FSOUND_GetChannelsPlaying();
1192 double deltaTime = (float) AbsoluteDeltaToDuration (currTime, start);
1194 if (0 > deltaTime) // if negative microseconds
1195 deltaTime /= -1000000.0;
1196 else // else milliseconds
1197 deltaTime /= 1000.0;
1204 float avg_channels = (float)num_channels / (float)frames;
1206 ofstream opstream("log.txt",ios::app);
1207 opstream << "Average frame count: ";
1209 opstream << " frames - ";
1210 opstream << avg_channels;
1211 opstream << " per frame.\n";
1221 // --------------------------------------------------------------------------
1236 #define GL_FUNC(ret,fn,params,call,rt) p##fn = NULL;
1237 #include "glstubs.h"
1239 // cheat here...static destructors are calling glDeleteTexture() after
1240 // the context is destroyed and libGL unloaded by SDL_Quit().
1241 pglDeleteTextures = glDeleteTextures_doNothing;
1243 #elif (defined WIN32)
1246 wglMakeCurrent( NULL, NULL);
1247 wglDeleteContext( hRC);
1253 ReleaseDC( g_windowHandle, hDC);
1259 ShowWindow( g_windowHandle, SW_HIDE );
1260 DestroyWindow( g_windowHandle);
1261 g_windowHandle = NULL;
1269 // --------------------------------------------------------------------------
1271 static bool g_focused = true;
1274 static bool IsFocused()
1280 if (GetActiveWindow() != g_windowHandle)
1283 if (IsIconic( g_windowHandle))
1294 #ifndef WIN32 // this is in WinMain, too.
1296 memset( &g_theKeys, 0, sizeof( KeyMap));
1303 bool regnow = false;
1308 //ofstream os("error.txt");
1310 //ofstream os("log.txt");
1316 while (!gDone&&!game.quit&&(!game.tryquit||!game.registered))
1322 // check windows messages
1326 while( SDL_PollEvent( &e ) )
1328 if( e.type == SDL_QUIT )
1335 #elif (defined WIN32)
1338 while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE | PM_NOYIELD ) )
1340 if( msg.message == WM_QUIT )
1347 TranslateMessage( &msg );
1348 DispatchMessage( &msg );
1360 // allow game chance to pause
1361 gameFocused = false;
1365 // game is not in focus, give CPU time to other apps by waiting for messages instead of 'peeking'
1369 //if (GetMessage( &msg, g_windowHandle, 0, 0 ))
1370 /*if (GetMessage( &msg, NULL, 0, 0 ))
1372 TranslateMessage(&msg);
1373 DispatchMessage(&msg);
1375 if ( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
1379 // handle the error and possibly exit
1384 TranslateMessage(&msg);
1385 DispatchMessage(&msg);
1389 STUBBED("give up CPU but sniff the event queue");
1394 regnow = game.registernow;
1399 // if(game.registernow){
1403 STUBBED("launch a web browser");
1406 sprintf(url,"http://www.wolfire.com/registerpc.html");
1408 ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);
1413 catch (const std::exception& error)
1417 std::string e = "Caught exception: ";
1420 LOG(e, Logger::LOG_ERR);
1422 MessageBox(g_windowHandle, error.what(), "ERROR", MB_OK | MB_ICONEXCLAMATION);
1432 // --------------------------------------------------------------------------
1435 #define MAX_WINKEYS 256
1436 static unsigned short KeyTable[MAX_WINKEYS]=
1439 MAC_MOUSEBUTTON1, // VK_LBUTTON (1)
1440 MAC_MOUSEBUTTON2, // VK_RBUTTON (2)
1441 0xffff, // VK_CANCEL (3)
1442 0xffff, // VK_MBUTTON (4)
1446 MAC_DELETE_KEY, // VK_BACK (8)
1447 MAC_TAB_KEY, // VK_TAB (9)
1450 0xffff, // VK_CLEAR (12)
1451 MAC_RETURN_KEY, // VK_RETURN (13)
1454 MAC_SHIFT_KEY, // VK_SHIFT (16)
1455 MAC_CONTROL_KEY, // VK_CONTROL (17)
1456 MAC_OPTION_KEY, // VK_MENU (18)
1457 0xffff, // VK_PAUSE (19)
1458 MAC_CAPS_LOCK_KEY, // #define VK_CAPITAL (20)
1465 MAC_ESCAPE_KEY, // VK_ESCAPE (27)
1470 MAC_SPACE_KEY, // VK_SPACE (32)
1471 MAC_PAGE_UP_KEY, // VK_PRIOR (33)
1472 MAC_PAGE_DOWN_KEY, // VK_NEXT (34)
1473 MAC_END_KEY, // VK_END (35)
1474 MAC_HOME_KEY, // VK_HOME (36)
1475 MAC_ARROW_LEFT_KEY, // VK_LEFT (37)
1476 MAC_ARROW_UP_KEY, // VK_UP (38)
1477 MAC_ARROW_RIGHT_KEY, // VK_RIGHT (39)
1478 MAC_ARROW_DOWN_KEY, // VK_DOWN (40)
1479 0xffff, // VK_SELECT (41)
1480 0xffff, // VK_PRINT (42)
1481 0xffff, // VK_EXECUTE (43)
1482 0xffff, // VK_SNAPSHOT (44)
1483 MAC_INSERT_KEY, // VK_INSERT (45)
1484 MAC_DEL_KEY, // VK_DELETE (46)
1485 0xffff, // VK_HELP (47)
1486 MAC_0_KEY, // VK_0 (48)
1487 MAC_1_KEY, // VK_1 (49)
1488 MAC_2_KEY, // VK_2 (50)
1489 MAC_3_KEY, // VK_3 (51)
1490 MAC_4_KEY, // VK_4 (52)
1491 MAC_5_KEY, // VK_5 (53)
1492 MAC_6_KEY, // VK_6 (54)
1493 MAC_7_KEY, // VK_7 (55)
1494 MAC_8_KEY, // VK_8 (56)
1495 MAC_9_KEY, // VK_9 (57)
1503 MAC_A_KEY, // VK_A (65)
1504 MAC_B_KEY, // VK_B (66)
1505 MAC_C_KEY, // VK_C (67)
1506 MAC_D_KEY, // VK_D (68)
1507 MAC_E_KEY, // VK_E (69)
1508 MAC_F_KEY, // VK_F (70)
1509 MAC_G_KEY, // VK_G (71)
1510 MAC_H_KEY, // VK_H (72)
1511 MAC_I_KEY, // VK_I (73)
1512 MAC_J_KEY, // VK_J (74)
1513 MAC_K_KEY, // VK_K (75)
1514 MAC_L_KEY, // VK_L (76)
1515 MAC_M_KEY, // VK_M (77)
1516 MAC_N_KEY, // VK_N (78)
1517 MAC_O_KEY, // VK_O (79)
1518 MAC_P_KEY, // VK_P (80)
1519 MAC_Q_KEY, // VK_Q (81)
1520 MAC_R_KEY, // VK_R (82)
1521 MAC_S_KEY, // VK_S (83)
1522 MAC_T_KEY, // VK_T (84)
1523 MAC_U_KEY, // VK_U (85)
1524 MAC_V_KEY, // VK_V (86)
1525 MAC_W_KEY, // VK_W (87)
1526 MAC_X_KEY, // VK_X (88)
1527 MAC_Y_KEY, // VK_Y (89)
1528 MAC_Z_KEY, // VK_Z (90)
1534 MAC_NUMPAD_0_KEY, // VK_NUMPAD0 (96)
1535 MAC_NUMPAD_1_KEY, // VK_NUMPAD1 (97)
1536 MAC_NUMPAD_2_KEY, // VK_NUMPAD2 (98)
1537 MAC_NUMPAD_3_KEY, // VK_NUMPAD3 (99)
1538 MAC_NUMPAD_4_KEY, // VK_NUMPAD4 (100)
1539 MAC_NUMPAD_5_KEY, // VK_NUMPAD5 (101)
1540 MAC_NUMPAD_6_KEY, // VK_NUMPAD6 (102)
1541 MAC_NUMPAD_7_KEY, // VK_NUMPAD7 (103)
1542 MAC_NUMPAD_8_KEY, // VK_NUMPAD8 (104)
1543 MAC_NUMPAD_9_KEY, // VK_NUMPAD9 (105)
1544 MAC_NUMPAD_ASTERISK_KEY, // VK_MULTIPLY (106)
1545 MAC_NUMPAD_PLUS_KEY, // VK_ADD (107)
1546 MAC_NUMPAD_ENTER_KEY, // VK_SEPARATOR (108)
1547 MAC_NUMPAD_MINUS_KEY, // VK_SUBTRACT (109)
1548 MAC_NUMPAD_PERIOD_KEY, // VK_DECIMAL (110)
1549 MAC_NUMPAD_SLASH_KEY, // VK_DIVIDE (111)
1550 MAC_F1_KEY, // VK_F1 (112)
1551 MAC_F2_KEY, // VK_F2 (113)
1552 MAC_F3_KEY, // VK_F3 (114)
1553 MAC_F4_KEY, // VK_F4 (115)
1554 MAC_F5_KEY, // VK_F5 (116)
1555 MAC_F6_KEY, // VK_F6 (117)
1556 MAC_F7_KEY, // VK_F7 (118)
1557 MAC_F8_KEY, // VK_F8 (119)
1558 MAC_F9_KEY, // VK_F9 (120)
1559 MAC_F10_KEY, // VK_F10 (121)
1560 MAC_F11_KEY, // VK_F11 (122)
1561 MAC_F12_KEY, // VK_F12 (123)
1582 0xffff, // VK_NUMLOCK (144)
1583 0xffff, // VK_SCROLL (145)
1598 MAC_SHIFT_KEY, // VK_LSHIFT (160)
1599 MAC_SHIFT_KEY, // VK_RSHIFT (161)
1600 MAC_CONTROL_KEY, // VK_LCONTROL (162)
1601 MAC_CONTROL_KEY, // VK_RCONTROL (163)
1602 MAC_OPTION_KEY, // VK_LMENU (164)
1603 MAC_OPTION_KEY, // VK_RMENU (165)
1624 MAC_SEMICOLON_KEY, // (186)
1625 MAC_PLUS_KEY, // (187)
1626 MAC_COMMA_KEY, // (188)
1627 MAC_MINUS_KEY, // (189)
1628 MAC_PERIOD_KEY, // (190)
1629 MAC_SLASH_KEY, // (191)
1630 MAC_TILDE_KEY, // (192)
1657 MAC_LEFTBRACKET_KEY, // (219)
1658 MAC_BACKSLASH_KEY, // (220)
1659 MAC_RIGHTBRACKET_KEY, // (221)
1660 MAC_APOSTROPHE_KEY, // (222)
1696 void ClipMouseToWindow(HWND window)
1700 GetClientRect(window, &wRect);
1702 ClientToScreen(window, (LPPOINT)&wRect.left);
1703 ClientToScreen(window, (LPPOINT)&wRect.right);
1710 LRESULT FAR PASCAL AppWndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
1712 /* this is where we receive all messages concerning this window
1713 * we can either process a message or pass it on to the default
1714 * message handler of windows */
1716 static PAINTSTRUCT ps;
1720 case WM_ACTIVATE: // Watch For Window Activate Message
1722 // Check Minimization State
1723 BOOL iconified = HIWORD(wParam) ? TRUE : FALSE;
1725 if (LOWORD(wParam) == WA_INACTIVE)
1734 CloseWindow( hWnd );
1736 // The window is now iconified
1737 iconified = GL_TRUE;
1743 g_focused=false; // Program Is Active
1747 SetupDSpFullScreen();
1754 // The window is now iconified
1755 iconified = GL_FALSE;
1758 ShowWindow( hWnd, SW_SHOW );
1759 SetForegroundWindow( hWnd );
1763 ClipMouseToWindow(hWnd);
1764 g_focused=true; // Program Is No Longer Active
1767 return 0; // Return To The Message Loop
1773 // check for Alt-F4 (exit hotkey)
1774 if (wParam == VK_F4)
1776 if (GetKeyState( VK_MENU) & 0x8080)
1782 if (wParam < MAX_WINKEYS)
1784 if (KeyTable[wParam] != 0xffff)
1785 SetKey( KeyTable[wParam]);
1793 if (wParam < MAX_WINKEYS)
1794 if (KeyTable[wParam] != 0xffff)
1795 ClearKey( KeyTable[wParam]);
1802 case WM_SYSDEADCHAR:
1805 case WM_NCLBUTTONDOWN:
1806 case WM_LBUTTONDOWN:
1813 case WM_NCRBUTTONDOWN:
1814 case WM_RBUTTONDOWN:
1820 case WM_NCMBUTTONDOWN:
1821 case WM_MBUTTONDOWN:
1827 case WM_NCLBUTTONUP:
1831 buttons[ 0] = false;
1835 case WM_NCRBUTTONUP:
1838 buttons[ 1] = false;
1842 case WM_NCMBUTTONUP:
1845 buttons[ 2] = false;
1849 case WM_NCLBUTTONDBLCLK:
1850 case WM_NCRBUTTONDBLCLK:
1851 case WM_NCMBUTTONDBLCLK:
1852 case WM_LBUTTONDBLCLK:
1854 case WM_RBUTTONDBLCLK:
1855 case WM_MBUTTONDBLCLK:
1858 case WM_NCMOUSEMOVE:
1860 /* ((WindowInfo *)g_lastWindow->GetInfo())->m_mouseX = (signed short)(lParam & 0xffff);
1861 ((WindowInfo *)g_lastWindow->GetInfo())->m_mouseY = (signed short)(lParam >> 16);
1862 if (g_lastWindow->m_mouseCallbacksEnabled) g_lastWindow->MouseMoveCallback();
1863 */// goto winmessage;
1866 case WM_SYSCOMMAND: // Intercept System Commands
1868 switch (wParam) // Check System Calls
1870 case SC_SCREENSAVE: // Screensaver Trying To Start?
1871 case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
1872 return 0; // Prevent From Happening
1874 // User trying to access application menu using ALT?
1883 // ReleaseCapture();
1884 // ClipMouseToWindow(hWnd);
1901 PostQuitMessage(0); /* Terminate Application */
1909 // BeginPaint( g_windowHandle,&ps);
1910 // EndPaint( g_windowHandle,&ps);
1917 /* We processed the message and there
1918 * is no processing by Windows necessary */
1920 /* We didn't process the message so let Windows do it */
1921 return DefWindowProc(hWnd,msg,wParam,lParam);
1925 static BOOL RegisterWindowClasses(HINSTANCE hFirstInstance)
1928 memset( &wc, 0, sizeof( wc));
1930 /* Register the window class. */
1931 wc.cbSize = sizeof(wc);
1933 wc.style = (CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_OWNDC); /* Combination of Class Styles */
1934 wc.lpfnWndProc = AppWndProc; /* Adress of Window Procedure */
1935 wc.cbClsExtra = 0; /* Extra Bytes allocated for this Class */
1936 wc.cbWndExtra = 0; /* Extra Bytes allocated for each Window */
1937 wc.hInstance = hFirstInstance; /* Handle of program instance */
1938 wc.hIcon = LoadIcon( hFirstInstance, MAKEINTRESOURCE(IDI_LUGARU) );
1939 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
1940 wc.hbrBackground = NULL;
1941 wc.lpszMenuName = NULL;
1942 wc.lpszClassName = g_wndClassName; /* Name of the Window Class */
1943 wc.hIconSm = LoadIcon( hFirstInstance, MAKEINTRESOURCE(IDI_LUGARU) );
1945 if (!RegisterClassEx(&wc)) return FALSE; /* Register Class failed */
1951 int resolutionID(int width, int height)
1955 if(width==640 && height==480)whichres=0;
1956 if(width==800 && height==600)whichres=1;
1957 if(width==1024 && height==768)whichres=2;
1958 if(width==1280 && height==1024)whichres=3;
1959 if(width==1600 && height==1200)whichres=4;
1960 if(width==840 && height==524)whichres=5;
1961 if(width==1024 && height==640)whichres=6;
1962 if(width==1344 && height==840)whichres=7;
1967 int closestResolution(int width, int height)
1971 if(width>=640 && height>=480)whichres=0;
1972 if(width>=800 && height>=600)whichres=1;
1973 if(width>=1024 && height>=768)whichres=2;
1974 if(width>=1280 && height>=1024)whichres=3;
1975 if(width>=1600 && height>=1200)whichres=4;
1976 if(width==840 && height==524)whichres=5;
1977 if(width==1024 && height==640)whichres=6;
1978 if(width==1344 && height==840)whichres=7;
1983 bool selectDetail(int & width, int & height, int & bpp, int & detail)
1986 int whichres = closestResolution(width, height);
1990 if(whichres<=0 || whichres>7){
2024 if ((detail != 0) && (resolutionDepths[whichres][1] != 0))
2028 else if ((detail == 0) && (resolutionDepths[whichres][0] != 0))
2032 else if ((detail != 0) && (resolutionDepths[whichres][0] != 0))
2038 else if (0 == whichres)
2046 bpp = resolutionDepths[whichres][(detail != 0)];
2052 int __stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
2055 LPWSTR * cl = CommandLineToArgvW(GetCommandLineW(), &argc);
2058 if (0 == _wcsicmp(cl[1], L"-windowed"))
2066 memset( &g_theKeys, 0, sizeof( KeyMap));
2070 memset(&mode, 0, sizeof(mode));
2071 mode.dmSize = sizeof(mode);
2072 while (EnumDisplaySettings(NULL, i++, &mode))
2074 if (mode.dmBitsPerPel < 16)
2079 int res = resolutionID(mode.dmPelsWidth, mode.dmPelsHeight);
2081 if (res > -1 && res < 8)
2083 if (DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettings(&mode, CDS_TEST))
2088 switch(mode.dmBitsPerPel)
2092 resolutionDepths[res][1] = mode.dmBitsPerPel;
2095 resolutionDepths[res][0] = mode.dmBitsPerPel;
2101 /* if there is no Instance of our program in memory then register the window class */
2102 if (hPrevInstance == NULL && !RegisterWindowClasses(hInstance))
2103 return FALSE; /* registration failed! */
2105 g_appInstance=hInstance;
2109 UnregisterClass( g_wndClassName, hInstance);
2116 extern int channels[100];
2117 extern FSOUND_SAMPLE * samp[100];
2118 extern FSOUND_STREAM * strm[10];
2120 extern "C" void PlaySoundEx(int chan, FSOUND_SAMPLE *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
2122 const FSOUND_SAMPLE * currSample = FSOUND_GetCurrentSample(channels[chan]);
2123 if (currSample && currSample == samp[chan])
2125 if (FSOUND_GetPaused(channels[chan]))
2127 FSOUND_StopSound(channels[chan]);
2128 channels[chan] = FSOUND_FREE;
2130 else if (FSOUND_IsPlaying(channels[chan]))
2132 int loop_mode = FSOUND_GetLoopMode(channels[chan]);
2133 if (loop_mode & FSOUND_LOOP_OFF)
2135 channels[chan] = FSOUND_FREE;
2141 channels[chan] = FSOUND_FREE;
2144 channels[chan] = FSOUND_PlaySoundEx(channels[chan], sptr, dsp, startpaused);
2145 if (channels[chan] < 0)
2147 channels[chan] = FSOUND_PlaySoundEx(FSOUND_FREE, sptr, dsp, startpaused);
2151 extern "C" void PlayStreamEx(int chan, FSOUND_STREAM *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
2153 const FSOUND_SAMPLE * currSample = FSOUND_GetCurrentSample(channels[chan]);
2154 if (currSample && currSample == FSOUND_Stream_GetSample(sptr))
2156 FSOUND_StopSound(channels[chan]);
2157 FSOUND_Stream_Stop(sptr);
2161 FSOUND_Stream_Stop(sptr);
2162 channels[chan] = FSOUND_FREE;
2165 channels[chan] = FSOUND_Stream_PlayEx(channels[chan], sptr, dsp, startpaused);
2166 if (channels[chan] < 0)
2168 channels[chan] = FSOUND_Stream_PlayEx(FSOUND_FREE, sptr, dsp, startpaused);
2172 bool LoadImage(const char * fname, TGAImageRec & tex)
2176 if ( tex.data == NULL )
2182 ILstring f = strdup(ConvertFileName(fname));
2189 ilGenImages(1, &iid);
2194 tex.sizeX = ilGetInteger(IL_IMAGE_WIDTH);
2195 tex.sizeY = ilGetInteger(IL_IMAGE_HEIGHT);
2196 tex.bpp = ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);
2197 ILuint Bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL),
2198 imageSize = tex.sizeX * tex.sizeY * Bpp;
2199 ILubyte *Data = ilGetData();
2200 memcpy(tex.data, Data, imageSize);
2202 // Truvision Targa files are stored as BGR colors
2203 // We want RGB so Blue and Red bytes are switched
2204 if (IL_TGA == ilGetInteger(IL_IMAGE_FORMAT))
2206 // Loop Through The Image Data
2207 for (GLuint i = 0; i < int(imageSize); i += Bpp)
2209 // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
2210 GLbyte temp; // Temporary Variable
2211 temp = tex.data[i]; // Temporarily Store The Value At Image Data 'i'
2212 tex.data[i] = tex.data[i + 2]; // Set The 1st Byte To The Value Of The 3rd Byte
2213 tex.data[i + 2] = temp; // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
2221 ilDeleteImages(1, &iid);
2225 GLuint texid = ilutGLLoadImage(f);
2230 ilutGLBuildMipmaps()
2239 res = load_image(fname, tex);
2240 //if (!res) printf("failed to load %s\n", fname);
2246 void ScreenShot(const char * fname)
2249 ILstring f = strdup(fname);
2256 ilGenImages(1, &iid);
2262 ilDeleteImages(1, &iid);
2272 static bool load_image(const char *file_name, TGAImageRec &tex)
2274 char *ptr = strrchr(file_name, '.');
2277 if (stricmp(ptr+1, "png") == 0)
2278 return load_png(file_name, tex);
2279 else if (stricmp(ptr+1, "jpg") == 0)
2280 return load_jpg(file_name, tex);
2283 STUBBED("Unsupported image type");
2288 struct my_error_mgr {
2289 struct jpeg_error_mgr pub; /* "public" fields */
2290 jmp_buf setjmp_buffer; /* for return to caller */
2292 typedef struct my_error_mgr * my_error_ptr;
2295 static void my_error_exit(j_common_ptr cinfo)
2297 struct my_error_mgr *err = (struct my_error_mgr *)cinfo->err;
2298 longjmp(err->setjmp_buffer, 1);
2301 /* stolen from public domain example.c code in libjpg distribution. */
2302 static bool load_jpg(const char *file_name, TGAImageRec &tex)
2304 struct jpeg_decompress_struct cinfo;
2305 struct my_error_mgr jerr;
2306 JSAMPROW buffer[1]; /* Output row buffer */
2307 int row_stride; /* physical row width in output buffer */
2308 FILE *infile = fopen(file_name, "rb");
2313 cinfo.err = jpeg_std_error(&jerr.pub);
2314 jerr.pub.error_exit = my_error_exit;
2315 if (setjmp(jerr.setjmp_buffer)) {
2316 jpeg_destroy_decompress(&cinfo);
2321 jpeg_create_decompress(&cinfo);
2322 jpeg_stdio_src(&cinfo, infile);
2323 (void) jpeg_read_header(&cinfo, TRUE);
2325 cinfo.out_color_space = JCS_RGB;
2326 cinfo.quantize_colors = 0;
2327 (void) jpeg_calc_output_dimensions(&cinfo);
2328 (void) jpeg_start_decompress(&cinfo);
2330 row_stride = cinfo.output_width * cinfo.output_components;
2331 tex.sizeX = cinfo.output_width;
2332 tex.sizeY = cinfo.output_height;
2335 while (cinfo.output_scanline < cinfo.output_height) {
2336 buffer[0] = (JSAMPROW)(char *)tex.data +
2337 ((cinfo.output_height-1) - cinfo.output_scanline) * row_stride;
2338 (void) jpeg_read_scanlines(&cinfo, buffer, 1);
2341 (void) jpeg_finish_decompress(&cinfo);
2342 jpeg_destroy_decompress(&cinfo);
2349 /* stolen from public domain example.c code in libpng distribution. */
2350 static bool load_png(const char *file_name, TGAImageRec &tex)
2352 bool hasalpha = false;
2353 png_structp png_ptr = NULL;
2354 png_infop info_ptr = NULL;
2355 png_uint_32 width, height;
2356 int bit_depth, color_type, interlace_type;
2357 png_byte **rows = NULL;
2358 bool retval = false;
2359 png_byte **row_pointers = NULL;
2360 FILE *fp = fopen(file_name, "rb");
2365 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2366 if (png_ptr == NULL)
2369 info_ptr = png_create_info_struct(png_ptr);
2370 if (info_ptr == NULL)
2373 if (setjmp(png_jmpbuf(png_ptr)))
2376 png_init_io(png_ptr, fp);
2377 png_read_png(png_ptr, info_ptr,
2378 PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING,
2380 png_get_IHDR(png_ptr, info_ptr, &width, &height,
2381 &bit_depth, &color_type, &interlace_type, NULL, NULL);
2383 if (bit_depth != 8) // transform SHOULD handle this...
2386 if (color_type & PNG_COLOR_MASK_PALETTE) // !!! FIXME?
2389 if ((color_type & PNG_COLOR_MASK_COLOR) == 0) // !!! FIXME?
2392 hasalpha = ((color_type & PNG_COLOR_MASK_ALPHA) != 0);
2393 row_pointers = png_get_rows(png_ptr, info_ptr);
2397 retval = malloc(width * height * 4);
2403 png_byte *dst = tex.data;
2404 for (int i = height-1; i >= 0; i--)
2406 png_byte *src = row_pointers[i];
2407 for (int j = 0; j < width; j++)
2421 png_byte *dst = tex.data;
2422 int pitch = width * 4;
2423 for (int i = height-1; i >= 0; i--, dst += pitch)
2424 memcpy(dst, row_pointers[i], pitch);
2433 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
2440 static bool save_image(const char *file_name)
2442 char *ptr = strrchr(file_name, '.');
2445 if (stricmp(ptr+1, "png") == 0)
2446 return save_png(file_name);
2449 STUBBED("Unsupported image type");
2454 static bool save_png(const char *file_name)
2457 png_structp png_ptr = NULL;
2458 png_infop info_ptr = NULL;
2459 bool retval = false;
2461 fp = fopen(file_name, "wb");
2465 png_bytep *row_pointers = new png_bytep[kContextHeight];
2466 png_bytep screenshot = new png_byte[kContextWidth * kContextHeight * 3];
2467 if ((!screenshot) || (!row_pointers))
2471 glReadPixels(0, 0, kContextWidth, kContextHeight,
2472 GL_RGB, GL_UNSIGNED_BYTE, screenshot);
2473 if (glGetError() != GL_NO_ERROR)
2476 for (int i = 0; i < kContextHeight; i++)
2477 row_pointers[i] = screenshot + ((kContextWidth * ((kContextHeight-1) - i)) * 3);
2479 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2480 if (png_ptr == NULL)
2483 info_ptr = png_create_info_struct(png_ptr);
2484 if (info_ptr == NULL)
2487 if (setjmp(png_jmpbuf(png_ptr)))
2490 png_init_io(png_ptr, fp);
2492 if (setjmp(png_jmpbuf(png_ptr)))
2495 png_set_IHDR(png_ptr, info_ptr, kContextWidth, kContextHeight,
2496 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
2497 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2499 png_write_info(png_ptr, info_ptr);
2501 if (setjmp(png_jmpbuf(png_ptr)))
2504 png_write_image(png_ptr, row_pointers);
2506 if (setjmp(png_jmpbuf(png_ptr)))
2509 png_write_end(png_ptr, NULL);
2513 png_destroy_write_struct(&png_ptr, &info_ptr);
2514 delete[] screenshot;
2515 delete[] row_pointers;
2519 unlink(ConvertFileName(file_name));