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 // --------------------------------------------------------------------------
228 void ReportError (char * strError)
230 #ifdef WIN32 // !!! FIXME. --ryan.
231 throw std::exception( strError);
234 /* char errMsgCStr [256];
237 sprintf (errMsgCStr, "%s", strError);
239 // out as debug string
240 CToPStr (strErr, errMsgCStr);
245 void SetupDSpFullScreen ()
252 DEVMODE dmScreenSettings;
253 memset( &dmScreenSettings, 0, sizeof( dmScreenSettings));
254 dmScreenSettings.dmSize = sizeof( dmScreenSettings);
255 dmScreenSettings.dmPelsWidth = kContextWidth;
256 dmScreenSettings.dmPelsHeight = kContextHeight;
257 dmScreenSettings.dmBitsPerPel = kBitsPerPixel;
258 dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
261 if (ChangeDisplaySettings( &dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
263 ReportError( "Could not set display mode");
280 ChangeDisplaySettings( NULL, 0);
288 //-----------------------------------------------------------------------------------------------------------------------
292 void DrawGL (Game & game)
303 static KeyMap g_theKeys;
305 void SetKey( int key)
307 g_theKeys[ key >> 3] |= (1 << (key & 7));
310 void ClearKey( int key)
312 g_theKeys[ key >> 3] &= (0xff ^ (1 << (key & 7)));
315 void GetKeys( unsigned char theKeys[16])
317 memcpy( theKeys, &g_theKeys, 16);
326 static void initSDLKeyTable(void) {}
328 #define MAX_SDLKEYS SDLK_LAST
329 static unsigned short KeyTable[MAX_SDLKEYS];
331 static void initSDLKeyTable(void)
333 memset(KeyTable, 0xFF, sizeof (KeyTable));
334 KeyTable[SDLK_BACKSPACE] = MAC_DELETE_KEY;
335 KeyTable[SDLK_TAB] = MAC_TAB_KEY;
336 KeyTable[SDLK_RETURN] = MAC_RETURN_KEY;
337 KeyTable[SDLK_ESCAPE] = MAC_ESCAPE_KEY;
338 KeyTable[SDLK_SPACE] = MAC_SPACE_KEY;
339 KeyTable[SDLK_PAGEUP] = MAC_PAGE_UP_KEY;
340 KeyTable[SDLK_PAGEDOWN] = MAC_PAGE_DOWN_KEY;
341 KeyTable[SDLK_END] = MAC_END_KEY;
342 KeyTable[SDLK_HOME] = MAC_HOME_KEY;
343 KeyTable[SDLK_LEFT] = MAC_ARROW_LEFT_KEY;
344 KeyTable[SDLK_UP] = MAC_ARROW_UP_KEY;
345 KeyTable[SDLK_RIGHT] = MAC_ARROW_RIGHT_KEY;
346 KeyTable[SDLK_DOWN] = MAC_ARROW_DOWN_KEY;
347 KeyTable[SDLK_INSERT] = MAC_INSERT_KEY;
348 KeyTable[SDLK_DELETE] = MAC_DEL_KEY;
349 KeyTable[SDLK_0] = MAC_0_KEY;
350 KeyTable[SDLK_1] = MAC_1_KEY;
351 KeyTable[SDLK_2] = MAC_2_KEY;
352 KeyTable[SDLK_3] = MAC_3_KEY;
353 KeyTable[SDLK_4] = MAC_4_KEY;
354 KeyTable[SDLK_5] = MAC_5_KEY;
355 KeyTable[SDLK_6] = MAC_6_KEY;
356 KeyTable[SDLK_7] = MAC_7_KEY;
357 KeyTable[SDLK_8] = MAC_8_KEY;
358 KeyTable[SDLK_9] = MAC_9_KEY;
359 KeyTable[SDLK_a] = MAC_A_KEY;
360 KeyTable[SDLK_b] = MAC_B_KEY;
361 KeyTable[SDLK_c] = MAC_C_KEY;
362 KeyTable[SDLK_d] = MAC_D_KEY;
363 KeyTable[SDLK_e] = MAC_E_KEY;
364 KeyTable[SDLK_f] = MAC_F_KEY;
365 KeyTable[SDLK_g] = MAC_G_KEY;
366 KeyTable[SDLK_h] = MAC_H_KEY;
367 KeyTable[SDLK_i] = MAC_I_KEY;
368 KeyTable[SDLK_j] = MAC_J_KEY;
369 KeyTable[SDLK_k] = MAC_K_KEY;
370 KeyTable[SDLK_l] = MAC_L_KEY;
371 KeyTable[SDLK_m] = MAC_M_KEY;
372 KeyTable[SDLK_n] = MAC_N_KEY;
373 KeyTable[SDLK_o] = MAC_O_KEY;
374 KeyTable[SDLK_p] = MAC_P_KEY;
375 KeyTable[SDLK_q] = MAC_Q_KEY;
376 KeyTable[SDLK_r] = MAC_R_KEY;
377 KeyTable[SDLK_s] = MAC_S_KEY;
378 KeyTable[SDLK_t] = MAC_T_KEY;
379 KeyTable[SDLK_u] = MAC_U_KEY;
380 KeyTable[SDLK_v] = MAC_V_KEY;
381 KeyTable[SDLK_w] = MAC_W_KEY;
382 KeyTable[SDLK_x] = MAC_X_KEY;
383 KeyTable[SDLK_y] = MAC_Y_KEY;
384 KeyTable[SDLK_z] = MAC_Z_KEY;
385 KeyTable[SDLK_KP0] = MAC_NUMPAD_0_KEY;
386 KeyTable[SDLK_KP1] = MAC_NUMPAD_1_KEY;
387 KeyTable[SDLK_KP2] = MAC_NUMPAD_2_KEY;
388 KeyTable[SDLK_KP3] = MAC_NUMPAD_3_KEY;
389 KeyTable[SDLK_KP4] = MAC_NUMPAD_4_KEY;
390 KeyTable[SDLK_KP5] = MAC_NUMPAD_5_KEY;
391 KeyTable[SDLK_KP6] = MAC_NUMPAD_6_KEY;
392 KeyTable[SDLK_KP7] = MAC_NUMPAD_7_KEY;
393 KeyTable[SDLK_KP8] = MAC_NUMPAD_8_KEY;
394 KeyTable[SDLK_KP9] = MAC_NUMPAD_9_KEY;
395 KeyTable[SDLK_KP_MULTIPLY] = MAC_NUMPAD_ASTERISK_KEY;
396 KeyTable[SDLK_KP_PLUS] = MAC_NUMPAD_PLUS_KEY;
397 KeyTable[SDLK_KP_ENTER] = MAC_NUMPAD_ENTER_KEY;
398 KeyTable[SDLK_KP_MINUS] = MAC_NUMPAD_MINUS_KEY;
399 KeyTable[SDLK_KP_PERIOD] = MAC_NUMPAD_PERIOD_KEY;
400 KeyTable[SDLK_KP_DIVIDE] = MAC_NUMPAD_SLASH_KEY;
401 KeyTable[SDLK_F1] = MAC_F1_KEY;
402 KeyTable[SDLK_F2] = MAC_F2_KEY;
403 KeyTable[SDLK_F3] = MAC_F3_KEY;
404 KeyTable[SDLK_F4] = MAC_F4_KEY;
405 KeyTable[SDLK_F5] = MAC_F5_KEY;
406 KeyTable[SDLK_F6] = MAC_F6_KEY;
407 KeyTable[SDLK_F7] = MAC_F7_KEY;
408 KeyTable[SDLK_F8] = MAC_F8_KEY;
409 KeyTable[SDLK_F9] = MAC_F9_KEY;
410 KeyTable[SDLK_F10] = MAC_F10_KEY;
411 KeyTable[SDLK_F11] = MAC_F11_KEY;
412 KeyTable[SDLK_F12] = MAC_F12_KEY;
413 KeyTable[SDLK_SEMICOLON] = MAC_SEMICOLON_KEY;
414 KeyTable[SDLK_PLUS] = MAC_PLUS_KEY;
415 KeyTable[SDLK_COMMA] = MAC_COMMA_KEY;
416 KeyTable[SDLK_MINUS] = MAC_MINUS_KEY;
417 KeyTable[SDLK_PERIOD] = MAC_PERIOD_KEY;
418 KeyTable[SDLK_SLASH] = MAC_SLASH_KEY;
419 KeyTable[SDLK_BACKQUOTE] = MAC_TILDE_KEY;
420 KeyTable[SDLK_LEFTBRACKET] = MAC_LEFTBRACKET_KEY;
421 KeyTable[SDLK_BACKSLASH] = MAC_BACKSLASH_KEY;
422 KeyTable[SDLK_RIGHTBRACKET] = MAC_RIGHTBRACKET_KEY;
423 KeyTable[SDLK_QUOTE] = MAC_APOSTROPHE_KEY;
426 static inline int clamp_sdl_mouse_button(Uint8 button)
428 if (button == 2) // right mouse button is button 3 in SDL.
430 else if (button == 3)
433 if ((button >= 1) && (button <= 3))
438 static void sdlEventProc(const SDL_Event &e, Game &game)
443 case SDL_MOUSEMOTION:
444 game.deltah += e.motion.xrel;
445 game.deltav += e.motion.yrel;
448 case SDL_MOUSEBUTTONDOWN:
450 val = clamp_sdl_mouse_button(e.button.button);
456 SetKey(MAC_MOUSEBUTTON1);
460 SetKey(MAC_MOUSEBUTTON2);
467 case SDL_MOUSEBUTTONUP:
469 val = clamp_sdl_mouse_button(e.button.button);
475 ClearKey(MAC_MOUSEBUTTON1);
479 ClearKey(MAC_MOUSEBUTTON2);
481 buttons[val] = false;
487 if (e.key.keysym.sym == SDLK_g)
489 if (e.key.keysym.mod & KMOD_CTRL)
491 SDL_GrabMode mode = SDL_GRAB_ON;
492 if ((SDL_GetVideoSurface()->flags & SDL_FULLSCREEN) == 0)
494 mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
495 mode = (mode==SDL_GRAB_ON) ? SDL_GRAB_OFF:SDL_GRAB_ON;
497 SDL_WM_GrabInput(mode);
501 else if (e.key.keysym.sym == SDLK_RETURN)
503 if (e.key.keysym.mod & KMOD_ALT)
504 SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
507 if (e.key.keysym.sym < SDLK_LAST)
509 if (KeyTable[e.key.keysym.sym] != 0xffff)
510 SetKey(KeyTable[e.key.keysym.sym]);
513 if (e.key.keysym.mod & KMOD_CTRL)
514 SetKey(MAC_CONTROL_KEY);
515 if (e.key.keysym.mod & KMOD_ALT)
516 SetKey(MAC_OPTION_KEY);
517 if (e.key.keysym.mod & KMOD_SHIFT)
518 SetKey(MAC_SHIFT_KEY);
519 if (e.key.keysym.mod & KMOD_CAPS)
520 SetKey(MAC_CAPS_LOCK_KEY);
525 if (e.key.keysym.sym < SDLK_LAST)
527 if (KeyTable[e.key.keysym.sym] != 0xffff)
528 ClearKey(KeyTable[e.key.keysym.sym]);
531 if (e.key.keysym.mod & KMOD_CTRL)
532 ClearKey(MAC_CONTROL_KEY);
533 if (e.key.keysym.mod & KMOD_ALT)
534 ClearKey(MAC_OPTION_KEY);
535 if (e.key.keysym.mod & KMOD_SHIFT)
536 ClearKey(MAC_SHIFT_KEY);
537 if (e.key.keysym.mod & KMOD_CAPS)
538 ClearKey(MAC_CAPS_LOCK_KEY);
544 // --------------------------------------------------------------------------
546 static Point gMidPoint;
548 Boolean SetUp (Game & game)
554 randSeed = UpTime().lo;
557 ifstream ipstream(ConvertFileName(":Data:config.txt"), std::ios::in /*| std::ios::nocreate*/);
560 usermousesensitivity=1;
589 game.crouchkey=MAC_SHIFT_KEY;
590 game.jumpkey=MAC_SPACE_KEY;
591 game.leftkey=MAC_A_KEY;
592 game.forwardkey=MAC_W_KEY;
593 game.backkey=MAC_S_KEY;
594 game.rightkey=MAC_D_KEY;
595 game.drawkey=MAC_E_KEY;
596 game.throwkey=MAC_Q_KEY;
597 game.attackkey=MAC_MOUSEBUTTON1;
598 game.chatkey=MAC_T_KEY;
604 selectDetail(kContextWidth, kContextHeight, kBitsPerPixel, detail);
607 ofstream opstream(ConvertFileName(":Data:config.txt", "w"));
608 opstream << "Screenwidth:\n";
609 opstream << kContextWidth;
610 opstream << "\nScreenheight:\n";
611 opstream << kContextHeight;
612 opstream << "\nMouse sensitivity:\n";
613 opstream << usermousesensitivity;
614 opstream << "\nBlur(0,1):\n";
615 opstream << ismotionblur;
616 opstream << "\nOverall Detail(0,1,2) higher=better:\n";
618 opstream << "\nFloating jump:\n";
619 opstream << floatjump;
620 opstream << "\nMouse jump:\n";
621 opstream << mousejump;
622 opstream << "\nAmbient sound:\n";
623 opstream << ambientsound;
624 opstream << "\nBlood (0,1,2):\n";
625 opstream << bloodtoggle;
626 opstream << "\nAuto slomo:\n";
627 opstream << autoslomo;
628 opstream << "\nFoliage:\n";
630 opstream << "\nMusic:\n";
631 opstream << musictoggle;
632 opstream << "\nTrilinear:\n";
633 opstream << trilinear;
634 opstream << "\nDecals(shadows,blood puddles,etc):\n";
636 opstream << "\nInvert mouse:\n";
637 opstream << invertmouse;
638 opstream << "\nGamespeed:\n";
639 opstream << gamespeed;
640 opstream << "\nDifficulty(0,1,2) higher=harder:\n";
641 opstream << difficulty;
642 opstream << "\nDamage effects(blackout, doublevision):\n";
643 opstream << damageeffects;
644 opstream << "\nText:\n";
645 opstream << texttoggle;
646 opstream << "\nDebug:\n";
647 opstream << debugmode;
648 opstream << "\nVBL Sync:\n";
650 opstream << "\nShow Points:\n";
651 opstream << showpoints;
652 opstream << "\nAlways Blur:\n";
653 opstream << alwaysblur;
654 opstream << "\nImmediate mode (turn on on G5):\n";
655 opstream << immediate;
656 opstream << "\nVelocity blur:\n";
657 opstream << velocityblur;
658 opstream << "\nVolume:\n";
660 opstream << "\nForward key:\n";
661 opstream << KeyToChar(game.forwardkey);
662 opstream << "\nBack key:\n";
663 opstream << KeyToChar(game.backkey);
664 opstream << "\nLeft key:\n";
665 opstream << KeyToChar(game.leftkey);
666 opstream << "\nRight key:\n";
667 opstream << KeyToChar(game.rightkey);
668 opstream << "\nJump key:\n";
669 opstream << KeyToChar(game.jumpkey);
670 opstream << "\nCrouch key:\n";
671 opstream << KeyToChar(game.crouchkey);
672 opstream << "\nDraw key:\n";
673 opstream << KeyToChar(game.drawkey);
674 opstream << "\nThrow key:\n";
675 opstream << KeyToChar(game.throwkey);
676 opstream << "\nAttack key:\n";
677 opstream << KeyToChar(game.attackkey);
678 opstream << "\nChat key:\n";
679 opstream << KeyToChar(game.chatkey);
684 ipstream.ignore(256,'\n');
685 ipstream >> kContextWidth;
686 ipstream.ignore(256,'\n');
687 ipstream.ignore(256,'\n');
688 ipstream >> kContextHeight;
689 ipstream.ignore(256,'\n');
690 ipstream.ignore(256,'\n');
691 ipstream >> usermousesensitivity;
692 ipstream.ignore(256,'\n');
693 ipstream.ignore(256,'\n');
695 ismotionblur = (i != 0);
696 ipstream.ignore(256,'\n');
697 ipstream.ignore(256,'\n');
699 if(detail!=0)kBitsPerPixel=32;
700 else kBitsPerPixel=16;
701 ipstream.ignore(256,'\n');
702 ipstream.ignore(256,'\n');
704 floatjump = (i != 0);
705 ipstream.ignore(256,'\n');
706 ipstream.ignore(256,'\n');
708 mousejump = (i != 0);
709 ipstream.ignore(256,'\n');
710 ipstream.ignore(256,'\n');
712 ambientsound = (i != 0);
713 ipstream.ignore(256,'\n');
714 ipstream.ignore(256,'\n');
715 ipstream >> bloodtoggle;
716 ipstream.ignore(256,'\n');
717 ipstream.ignore(256,'\n');
719 autoslomo = (i != 0);
720 ipstream.ignore(256,'\n');
721 ipstream.ignore(256,'\n');
724 ipstream.ignore(256,'\n');
725 ipstream.ignore(256,'\n');
727 musictoggle = (i != 0);
728 ipstream.ignore(256,'\n');
729 ipstream.ignore(256,'\n');
731 trilinear = (i != 0);
732 ipstream.ignore(256,'\n');
733 ipstream.ignore(256,'\n');
736 ipstream.ignore(256,'\n');
737 ipstream.ignore(256,'\n');
739 invertmouse = (i != 0);
740 ipstream.ignore(256,'\n');
741 ipstream.ignore(256,'\n');
742 ipstream >> gamespeed;
743 oldgamespeed=gamespeed;
748 ipstream.ignore(256,'\n');
749 ipstream.ignore(256,'\n');
750 ipstream >> difficulty;
751 ipstream.ignore(256,'\n');
752 ipstream.ignore(256,'\n');
754 damageeffects = (i != 0);
755 ipstream.ignore(256,'\n');
756 ipstream.ignore(256,'\n');
758 texttoggle = (i != 0);
759 ipstream.ignore(256,'\n');
760 ipstream.ignore(256,'\n');
762 debugmode = (i != 0);
763 ipstream.ignore(256,'\n');
764 ipstream.ignore(256,'\n');
767 ipstream.ignore(256,'\n');
768 ipstream.ignore(256,'\n');
770 showpoints = (i != 0);
771 ipstream.ignore(256,'\n');
772 ipstream.ignore(256,'\n');
774 alwaysblur = (i != 0);
775 ipstream.ignore(256,'\n');
776 ipstream.ignore(256,'\n');
778 immediate = (i != 0);
779 ipstream.ignore(256,'\n');
780 ipstream.ignore(256,'\n');
782 velocityblur = (i != 0);
783 ipstream.ignore(256,'\n');
784 ipstream.ignore(256,'\n');
786 ipstream.ignore(256,'\n');
787 ipstream.ignore(256,'\n');
789 game.forwardkey=CharToKey(string);
790 ipstream.ignore(256,'\n');
791 ipstream.ignore(256,'\n');
793 game.backkey=CharToKey(string);
794 ipstream.ignore(256,'\n');
795 ipstream.ignore(256,'\n');
797 game.leftkey=CharToKey(string);
798 ipstream.ignore(256,'\n');
799 ipstream.ignore(256,'\n');
801 game.rightkey=CharToKey(string);
802 ipstream.ignore(256,'\n');
803 ipstream.ignore(256,'\n');
805 game.jumpkey=CharToKey(string);
806 ipstream.ignore(256,'\n');
807 ipstream.ignore(256,'\n');
809 game.crouchkey=CharToKey(string);
810 ipstream.ignore(256,'\n');
811 ipstream.ignore(256,'\n');
813 game.drawkey=CharToKey(string);
814 ipstream.ignore(256,'\n');
815 ipstream.ignore(256,'\n');
817 game.throwkey=CharToKey(string);
818 ipstream.ignore(256,'\n');
819 ipstream.ignore(256,'\n');
821 game.attackkey=CharToKey(string);
822 ipstream.ignore(256,'\n');
823 ipstream.ignore(256,'\n');
825 game.chatkey=CharToKey(string);
828 if(detail>2)detail=2;
829 if(detail<0)detail=0;
830 if(screenwidth>3000)screenwidth=640;
831 if(screenwidth<0)screenwidth=640;
832 if(screenheight>3000)screenheight=480;
833 if(screenheight<0)screenheight=480;
835 if(kBitsPerPixel!=32&&kBitsPerPixel!=16){
840 selectDetail(kContextWidth, kContextHeight, kBitsPerPixel, detail);
842 SetupDSpFullScreen();
845 if (!SDL_WasInit(SDL_INIT_VIDEO))
847 if (SDL_Init(SDL_INIT_VIDEO) == -1)
849 fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
853 const char *libname = "libGL.so.1"; // !!! FIXME: Linux specific!
854 if (SDL_GL_LoadLibrary(libname) == -1)
856 fprintf(stderr, "SDL_GL_LoadLibrary(\"%s\") failed: %s\n",
857 libname, SDL_GetError());
861 if (!lookup_all_glsyms(libname))
865 Uint32 sdlflags = SDL_OPENGL; // !!! FIXME: SDL_FULLSCREEN?
866 SDL_WM_SetCaption("Lugaru", "lugaru");
868 if (SDL_SetVideoMode(kContextWidth, kContextHeight, 0, sdlflags) == NULL)
870 fprintf(stderr, "SDL_SetVideoMode() failed: %s\n", SDL_GetError());
876 #elif (defined WIN32)
877 //------------------------------------------------------------------
880 RECT r = {0, 0, kContextWidth-1, kContextHeight-1};
881 DWORD dwStyle = WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE;
882 DWORD dwExStyle = WS_EX_APPWINDOW;
891 dwStyle |= WS_OVERLAPPEDWINDOW;
892 dwExStyle |= WS_EX_WINDOWEDGE;
895 AdjustWindowRectEx(&r, dwStyle, FALSE, dwExStyle);
899 x = (GetSystemMetrics(SM_CXSCREEN) >> 1) - ((r.right - r.left + 1) >> 1);
900 y = (GetSystemMetrics(SM_CYSCREEN) >> 1) - ((r.bottom - r.top + 1) >> 1);
903 g_windowHandle=CreateWindowEx(
905 g_wndClassName, "Lugaru", dwStyle,
907 // kContextWidth, kContextHeight,
908 r.right - r.left + 1, r.bottom - r.top + 1,
909 NULL,NULL,g_appInstance,NULL );
912 ReportError("Could not create window");
916 //------------------------------------------------------------------
919 static PIXELFORMATDESCRIPTOR pfd =
921 sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
923 PFD_DRAW_TO_WINDOW | // Format Must Support Window
924 PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
925 PFD_DOUBLEBUFFER, // Must Support Double Buffering
926 PFD_TYPE_RGBA, // Request An RGBA Format
927 kBitsPerPixel, // Select Our Color Depth
928 0, 0, 0, 0, 0, 0, // Color Bits Ignored
929 0, // No Alpha Buffer
930 0, // Shift Bit Ignored
931 0, // No Accumulation Buffer
932 0, 0, 0, 0, // Accumulation Bits Ignored
933 16, // 16Bit Z-Buffer (Depth Buffer)
934 0, // No Stencil Buffer
935 0, // No Auxiliary Buffer
936 PFD_MAIN_PLANE, // Main Drawing Layer
938 0, 0, 0 // Layer Masks Ignored
941 if (!(hDC = GetDC( g_windowHandle)))
942 ReportError( "Could not get device context");
945 if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
947 ReportError( "Could not find appropriate pixel format");
951 if (!DescribePixelFormat(hDC, PixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd))
953 ReportError( "Could not retrieve pixel format");
957 if (!SetPixelFormat( hDC, PixelFormat, &pfd))
959 ReportError( "Could not set pixel format");
963 if (!(hRC = wglCreateContext(hDC)))
965 ReportError( "Could not create rendering context");
969 if (!wglMakeCurrent(hDC, hRC))
971 ReportError( "Could not activate rendering context");
977 // Place the window above all topmost windows
978 SetWindowPos( g_windowHandle, HWND_TOPMOST, 0,0,0,0,
979 SWP_NOMOVE | SWP_NOSIZE );
982 SetForegroundWindow(g_windowHandle);
983 SetFocus(g_windowHandle);
986 glClear( GL_COLOR_BUFFER_BIT );
990 glDisable( GL_ALPHA_TEST);
991 glDisable( GL_BLEND);
992 glDisable( GL_DEPTH_TEST);
993 // glDisable( GL_DITHER);
995 glDisable( GL_LIGHTING);
996 glDisable( GL_LOGIC_OP);
997 glDisable( GL_STENCIL_TEST);
998 glDisable( GL_TEXTURE_1D);
999 glDisable( GL_TEXTURE_2D);
1000 glPixelTransferi( GL_MAP_COLOR, GL_FALSE);
1001 glPixelTransferi( GL_RED_SCALE, 1);
1002 glPixelTransferi( GL_RED_BIAS, 0);
1003 glPixelTransferi( GL_GREEN_SCALE, 1);
1004 glPixelTransferi( GL_GREEN_BIAS, 0);
1005 glPixelTransferi( GL_BLUE_SCALE, 1);
1006 glPixelTransferi( GL_BLUE_BIAS, 0);
1007 glPixelTransferi( GL_ALPHA_SCALE, 1);
1008 glPixelTransferi( GL_ALPHA_BIAS, 0);
1010 // set initial rendering states
1011 glShadeModel( GL_SMOOTH);
1012 glClearDepth( 1.0f);
1013 glDepthFunc( GL_LEQUAL);
1014 glDepthMask( GL_TRUE);
1015 // glDepthRange( FRONT_CLIP, BACK_CLIP);
1016 glEnable( GL_DEPTH_TEST);
1017 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
1018 glCullFace( GL_FRONT);
1019 glEnable( GL_CULL_FACE);
1020 glEnable( GL_LIGHTING);
1021 // glEnable( GL_LIGHT_MODEL_AMBIENT);
1022 glEnable( GL_DITHER);
1023 glEnable( GL_COLOR_MATERIAL);
1024 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1025 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1026 glAlphaFunc( GL_GREATER, 0.5f);
1029 if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION ||
1030 iluGetInteger(ILU_VERSION_NUM) < ILU_VERSION ||
1031 ilutGetInteger(ILUT_VERSION_NUM) < ILUT_VERSION)
1033 ReportError("DevIL version is different...exiting!\n");
1041 ilutRenderer(ILUT_OPENGL);
1043 ilEnable(IL_ORIGIN_SET);
1044 ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
1047 GLint width = kContextWidth;
1048 GLint height = kContextHeight;
1049 gMidPoint.h = width / 2;
1050 gMidPoint.v = height / 2;
1052 screenheight=height;
1054 game.newdetail=detail;
1055 game.newscreenwidth=screenwidth;
1056 game.newscreenheight=screenheight;
1064 static void DoMouse(Game & game)
1067 if(mainmenu||(abs(game.deltah)<10*realmultiplier*1000&&abs(game.deltav)<10*realmultiplier*1000))
1069 game.deltah *= usermousesensitivity;
1070 game.deltav *= usermousesensitivity;
1071 game.mousecoordh += game.deltah;
1072 game.mousecoordv += game.deltav;
1073 if (game.mousecoordh < 0)
1074 game.mousecoordh = 0;
1075 else if (game.mousecoordh >= kContextWidth)
1076 game.mousecoordh = kContextWidth - 1;
1077 if (game.mousecoordv < 0)
1078 game.mousecoordv = 0;
1079 else if (game.mousecoordv >= kContextHeight)
1080 game.mousecoordv = kContextHeight - 1;
1083 static Point lastMouse = {-1,-1};
1088 ScreenToClient(g_windowHandle, &pos);
1089 globalMouse.h = pos.x;
1090 globalMouse.v = pos.y;
1092 if (lastMouse.h == globalMouse.h && lastMouse.v == globalMouse.v)
1099 static Point virtualMouse = {0,0};
1100 delta = globalMouse;
1102 delta.h -= lastMouse.h;
1103 delta.v -= lastMouse.v;
1104 lastMouse.h = pos.x;
1105 lastMouse.v = pos.y;
1107 if(mainmenu||(abs(delta.h)<10*realmultiplier*1000&&abs(delta.v)<10*realmultiplier*1000)){
1108 game.deltah=delta.h*usermousesensitivity;
1109 game.deltav=delta.v*usermousesensitivity;
1110 game.mousecoordh=globalMouse.h;
1111 game.mousecoordv=globalMouse.v;
1116 if(lastMouse.h>gMidPoint.h+100||lastMouse.h<gMidPoint.h-100||lastMouse.v>gMidPoint.v+100||lastMouse.v<gMidPoint.v-100){
1117 pos.x = gMidPoint.h;
1118 pos.y = gMidPoint.v;
1119 ClientToScreen(g_windowHandle, &pos);
1120 //SetCursorPos( gMidPoint.h,gMidPoint.v);
1121 SetCursorPos(pos.x, pos.y);
1122 lastMouse = gMidPoint;
1131 // --------------------------------------------------------------------------
1133 void DoKey (SInt8 theKey, SInt8 theCode)
1138 // --------------------------------------------------------------------------
1142 void DoFrameRate (int update)
1144 static long frames = 0;
1146 static AbsoluteTime time = {0,0};
1147 static AbsoluteTime frametime = {0,0};
1148 AbsoluteTime currTime = UpTime ();
1149 double deltaTime = (float) AbsoluteDeltaToDuration (currTime, frametime);
1151 if (0 > deltaTime) // if negative microseconds
1152 deltaTime /= -1000000.0;
1153 else // else milliseconds
1154 deltaTime /= 1000.0;
1156 multiplier=deltaTime;
1157 if(multiplier<.001)multiplier=.001;
1158 if(multiplier>10)multiplier=10;
1159 if(update)frametime = currTime; // reset for next time interval
1161 deltaTime = (float) AbsoluteDeltaToDuration (currTime, time);
1163 if (0 > deltaTime) // if negative microseconds
1164 deltaTime /= -1000000.0;
1165 else // else milliseconds
1166 deltaTime /= 1000.0;
1168 if (0.001 <= deltaTime) // has update interval passed
1171 time = currTime; // reset for next time interval
1178 void DoUpdate (Game & game)
1180 static float sps=200;
1182 static float oldmult;
1185 if(multiplier>.6)multiplier=.6;
1187 game.fps=1/multiplier;
1189 count = multiplier*sps;
1191 //if(count>10)count=10;
1193 realmultiplier=multiplier;
1194 multiplier*=gamespeed;
1195 if(difficulty==1)multiplier*=.9;
1196 if(difficulty==0)multiplier*=.8;
1198 if(game.loading==4)multiplier*=.00001;
1200 if(slomo&&!mainmenu)multiplier*=slomospeed;
1201 //if(freeze)multiplier*=0.00001;
1203 multiplier/=(float)count;
1209 for(int i=0;i<count;i++)
1215 game.TickOnceAfter();
1216 /* - Debug code to test how many channels were active on average per frame
1217 static long frames = 0;
1219 static AbsoluteTime start = {0,0};
1220 AbsoluteTime currTime = UpTime ();
1221 static int num_channels = 0;
1223 num_channels += FSOUND_GetChannelsPlaying();
1224 double deltaTime = (float) AbsoluteDeltaToDuration (currTime, start);
1226 if (0 > deltaTime) // if negative microseconds
1227 deltaTime /= -1000000.0;
1228 else // else milliseconds
1229 deltaTime /= 1000.0;
1236 float avg_channels = (float)num_channels / (float)frames;
1238 ofstream opstream("log.txt",ios::app);
1239 opstream << "Average frame count: ";
1241 opstream << " frames - ";
1242 opstream << avg_channels;
1243 opstream << " per frame.\n";
1253 // --------------------------------------------------------------------------
1268 #define GL_FUNC(ret,fn,params,call,rt) p##fn = NULL;
1269 #include "glstubs.h"
1271 // cheat here...static destructors are calling glDeleteTexture() after
1272 // the context is destroyed and libGL unloaded by SDL_Quit().
1273 pglDeleteTextures = glDeleteTextures_doNothing;
1275 #elif (defined WIN32)
1278 wglMakeCurrent( NULL, NULL);
1279 wglDeleteContext( hRC);
1285 ReleaseDC( g_windowHandle, hDC);
1291 ShowWindow( g_windowHandle, SW_HIDE );
1292 DestroyWindow( g_windowHandle);
1293 g_windowHandle = NULL;
1301 // --------------------------------------------------------------------------
1303 static bool g_focused = true;
1306 static bool IsFocused()
1312 if (GetActiveWindow() != g_windowHandle)
1315 if (IsIconic( g_windowHandle))
1327 static bool try_launch_browser(const char *browser, const char *url)
1329 // make sure string isn't empty...
1330 while ( (*browser) && (isspace(*browser)) ) browser++;
1331 if (*browser == '\0') return false;
1333 bool seenurl = false;
1334 char buf[4096]; // !!! FIXME: we aren't checking for overflow here!
1338 char ch = *(browser++);
1344 else if (ch == 's') // "%s" == insert URL here.
1352 // (not %% or %s? Drop the char.)
1366 return(system(buf) == 0);
1371 static void launch_web_browser(const char *url)
1374 ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);
1376 // lousy linux doesn't have a clean way to do this, but you can point people
1377 // to docs on the BROWSER variable:
1378 // http://www.catb.org/~esr/BROWSER/
1379 #elif PLATFORM_LINUX
1380 if (strchr(url, '\'') != NULL) // Prevent simple shell injection.
1383 const char *envr = getenv("BROWSER");
1384 if (envr == NULL) // not specified? We'll try a pseudo-sane list...
1385 envr = "opera:mozilla:konqueror:firefox:netscape:xterm -e links:xterm -e lynx:";
1387 char *ptr = (char *) alloca(strlen(envr) + 1);
1393 while ((ptr = strchr(envr, ':')) != NULL)
1396 if (try_launch_browser(envr, url))
1401 try_launch_browser(envr, url);
1409 #ifndef WIN32 // this is in WinMain, too.
1411 memset( &g_theKeys, 0, sizeof( KeyMap));
1418 bool regnow = false;
1423 //ofstream os("error.txt");
1425 //ofstream os("log.txt");
1431 while (!gDone&&!game.quit&&(!game.tryquit||!game.registered))
1437 // check windows messages
1443 while( SDL_PollEvent( &e ) )
1445 if( e.type == SDL_QUIT )
1450 sdlEventProc(e, game);
1453 #elif (defined WIN32)
1456 while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE | PM_NOYIELD ) )
1458 if( msg.message == WM_QUIT )
1465 TranslateMessage( &msg );
1466 DispatchMessage( &msg );
1478 // allow game chance to pause
1479 gameFocused = false;
1483 // game is not in focus, give CPU time to other apps by waiting for messages instead of 'peeking'
1487 //if (GetMessage( &msg, g_windowHandle, 0, 0 ))
1488 /*if (GetMessage( &msg, NULL, 0, 0 ))
1490 TranslateMessage(&msg);
1491 DispatchMessage(&msg);
1493 if ( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
1497 // handle the error and possibly exit
1502 TranslateMessage(&msg);
1503 DispatchMessage(&msg);
1507 STUBBED("give up CPU but sniff the event queue");
1512 regnow = game.registernow;
1517 // if(game.registernow){
1520 launch_web_browser("http://www.wolfire.com/registerpc.html");
1524 catch (const std::exception& error)
1528 std::string e = "Caught exception: ";
1531 LOG(e, Logger::LOG_ERR);
1533 MessageBox(g_windowHandle, error.what(), "ERROR", MB_OK | MB_ICONEXCLAMATION);
1543 // --------------------------------------------------------------------------
1546 #define MAX_WINKEYS 256
1547 static unsigned short KeyTable[MAX_WINKEYS]=
1550 MAC_MOUSEBUTTON1, // VK_LBUTTON (1)
1551 MAC_MOUSEBUTTON2, // VK_RBUTTON (2)
1552 0xffff, // VK_CANCEL (3)
1553 0xffff, // VK_MBUTTON (4)
1557 MAC_DELETE_KEY, // VK_BACK (8)
1558 MAC_TAB_KEY, // VK_TAB (9)
1561 0xffff, // VK_CLEAR (12)
1562 MAC_RETURN_KEY, // VK_RETURN (13)
1565 MAC_SHIFT_KEY, // VK_SHIFT (16)
1566 MAC_CONTROL_KEY, // VK_CONTROL (17)
1567 MAC_OPTION_KEY, // VK_MENU (18)
1568 0xffff, // VK_PAUSE (19)
1569 MAC_CAPS_LOCK_KEY, // #define VK_CAPITAL (20)
1576 MAC_ESCAPE_KEY, // VK_ESCAPE (27)
1581 MAC_SPACE_KEY, // VK_SPACE (32)
1582 MAC_PAGE_UP_KEY, // VK_PRIOR (33)
1583 MAC_PAGE_DOWN_KEY, // VK_NEXT (34)
1584 MAC_END_KEY, // VK_END (35)
1585 MAC_HOME_KEY, // VK_HOME (36)
1586 MAC_ARROW_LEFT_KEY, // VK_LEFT (37)
1587 MAC_ARROW_UP_KEY, // VK_UP (38)
1588 MAC_ARROW_RIGHT_KEY, // VK_RIGHT (39)
1589 MAC_ARROW_DOWN_KEY, // VK_DOWN (40)
1590 0xffff, // VK_SELECT (41)
1591 0xffff, // VK_PRINT (42)
1592 0xffff, // VK_EXECUTE (43)
1593 0xffff, // VK_SNAPSHOT (44)
1594 MAC_INSERT_KEY, // VK_INSERT (45)
1595 MAC_DEL_KEY, // VK_DELETE (46)
1596 0xffff, // VK_HELP (47)
1597 MAC_0_KEY, // VK_0 (48)
1598 MAC_1_KEY, // VK_1 (49)
1599 MAC_2_KEY, // VK_2 (50)
1600 MAC_3_KEY, // VK_3 (51)
1601 MAC_4_KEY, // VK_4 (52)
1602 MAC_5_KEY, // VK_5 (53)
1603 MAC_6_KEY, // VK_6 (54)
1604 MAC_7_KEY, // VK_7 (55)
1605 MAC_8_KEY, // VK_8 (56)
1606 MAC_9_KEY, // VK_9 (57)
1614 MAC_A_KEY, // VK_A (65)
1615 MAC_B_KEY, // VK_B (66)
1616 MAC_C_KEY, // VK_C (67)
1617 MAC_D_KEY, // VK_D (68)
1618 MAC_E_KEY, // VK_E (69)
1619 MAC_F_KEY, // VK_F (70)
1620 MAC_G_KEY, // VK_G (71)
1621 MAC_H_KEY, // VK_H (72)
1622 MAC_I_KEY, // VK_I (73)
1623 MAC_J_KEY, // VK_J (74)
1624 MAC_K_KEY, // VK_K (75)
1625 MAC_L_KEY, // VK_L (76)
1626 MAC_M_KEY, // VK_M (77)
1627 MAC_N_KEY, // VK_N (78)
1628 MAC_O_KEY, // VK_O (79)
1629 MAC_P_KEY, // VK_P (80)
1630 MAC_Q_KEY, // VK_Q (81)
1631 MAC_R_KEY, // VK_R (82)
1632 MAC_S_KEY, // VK_S (83)
1633 MAC_T_KEY, // VK_T (84)
1634 MAC_U_KEY, // VK_U (85)
1635 MAC_V_KEY, // VK_V (86)
1636 MAC_W_KEY, // VK_W (87)
1637 MAC_X_KEY, // VK_X (88)
1638 MAC_Y_KEY, // VK_Y (89)
1639 MAC_Z_KEY, // VK_Z (90)
1645 MAC_NUMPAD_0_KEY, // VK_NUMPAD0 (96)
1646 MAC_NUMPAD_1_KEY, // VK_NUMPAD1 (97)
1647 MAC_NUMPAD_2_KEY, // VK_NUMPAD2 (98)
1648 MAC_NUMPAD_3_KEY, // VK_NUMPAD3 (99)
1649 MAC_NUMPAD_4_KEY, // VK_NUMPAD4 (100)
1650 MAC_NUMPAD_5_KEY, // VK_NUMPAD5 (101)
1651 MAC_NUMPAD_6_KEY, // VK_NUMPAD6 (102)
1652 MAC_NUMPAD_7_KEY, // VK_NUMPAD7 (103)
1653 MAC_NUMPAD_8_KEY, // VK_NUMPAD8 (104)
1654 MAC_NUMPAD_9_KEY, // VK_NUMPAD9 (105)
1655 MAC_NUMPAD_ASTERISK_KEY, // VK_MULTIPLY (106)
1656 MAC_NUMPAD_PLUS_KEY, // VK_ADD (107)
1657 MAC_NUMPAD_ENTER_KEY, // VK_SEPARATOR (108)
1658 MAC_NUMPAD_MINUS_KEY, // VK_SUBTRACT (109)
1659 MAC_NUMPAD_PERIOD_KEY, // VK_DECIMAL (110)
1660 MAC_NUMPAD_SLASH_KEY, // VK_DIVIDE (111)
1661 MAC_F1_KEY, // VK_F1 (112)
1662 MAC_F2_KEY, // VK_F2 (113)
1663 MAC_F3_KEY, // VK_F3 (114)
1664 MAC_F4_KEY, // VK_F4 (115)
1665 MAC_F5_KEY, // VK_F5 (116)
1666 MAC_F6_KEY, // VK_F6 (117)
1667 MAC_F7_KEY, // VK_F7 (118)
1668 MAC_F8_KEY, // VK_F8 (119)
1669 MAC_F9_KEY, // VK_F9 (120)
1670 MAC_F10_KEY, // VK_F10 (121)
1671 MAC_F11_KEY, // VK_F11 (122)
1672 MAC_F12_KEY, // VK_F12 (123)
1693 0xffff, // VK_NUMLOCK (144)
1694 0xffff, // VK_SCROLL (145)
1709 MAC_SHIFT_KEY, // VK_LSHIFT (160)
1710 MAC_SHIFT_KEY, // VK_RSHIFT (161)
1711 MAC_CONTROL_KEY, // VK_LCONTROL (162)
1712 MAC_CONTROL_KEY, // VK_RCONTROL (163)
1713 MAC_OPTION_KEY, // VK_LMENU (164)
1714 MAC_OPTION_KEY, // VK_RMENU (165)
1735 MAC_SEMICOLON_KEY, // (186)
1736 MAC_PLUS_KEY, // (187)
1737 MAC_COMMA_KEY, // (188)
1738 MAC_MINUS_KEY, // (189)
1739 MAC_PERIOD_KEY, // (190)
1740 MAC_SLASH_KEY, // (191)
1741 MAC_TILDE_KEY, // (192)
1768 MAC_LEFTBRACKET_KEY, // (219)
1769 MAC_BACKSLASH_KEY, // (220)
1770 MAC_RIGHTBRACKET_KEY, // (221)
1771 MAC_APOSTROPHE_KEY, // (222)
1807 void ClipMouseToWindow(HWND window)
1811 GetClientRect(window, &wRect);
1813 ClientToScreen(window, (LPPOINT)&wRect.left);
1814 ClientToScreen(window, (LPPOINT)&wRect.right);
1821 LRESULT FAR PASCAL AppWndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
1823 /* this is where we receive all messages concerning this window
1824 * we can either process a message or pass it on to the default
1825 * message handler of windows */
1827 static PAINTSTRUCT ps;
1831 case WM_ACTIVATE: // Watch For Window Activate Message
1833 // Check Minimization State
1834 BOOL iconified = HIWORD(wParam) ? TRUE : FALSE;
1836 if (LOWORD(wParam) == WA_INACTIVE)
1845 CloseWindow( hWnd );
1847 // The window is now iconified
1848 iconified = GL_TRUE;
1854 g_focused=false; // Program Is Active
1858 SetupDSpFullScreen();
1865 // The window is now iconified
1866 iconified = GL_FALSE;
1869 ShowWindow( hWnd, SW_SHOW );
1870 SetForegroundWindow( hWnd );
1874 ClipMouseToWindow(hWnd);
1875 g_focused=true; // Program Is No Longer Active
1878 return 0; // Return To The Message Loop
1884 // check for Alt-F4 (exit hotkey)
1885 if (wParam == VK_F4)
1887 if (GetKeyState( VK_MENU) & 0x8080)
1893 if (wParam < MAX_WINKEYS)
1895 if (KeyTable[wParam] != 0xffff)
1896 SetKey( KeyTable[wParam]);
1904 if (wParam < MAX_WINKEYS)
1905 if (KeyTable[wParam] != 0xffff)
1906 ClearKey( KeyTable[wParam]);
1913 case WM_SYSDEADCHAR:
1916 case WM_NCLBUTTONDOWN:
1917 case WM_LBUTTONDOWN:
1924 case WM_NCRBUTTONDOWN:
1925 case WM_RBUTTONDOWN:
1931 case WM_NCMBUTTONDOWN:
1932 case WM_MBUTTONDOWN:
1938 case WM_NCLBUTTONUP:
1942 buttons[ 0] = false;
1946 case WM_NCRBUTTONUP:
1949 buttons[ 1] = false;
1953 case WM_NCMBUTTONUP:
1956 buttons[ 2] = false;
1960 case WM_NCLBUTTONDBLCLK:
1961 case WM_NCRBUTTONDBLCLK:
1962 case WM_NCMBUTTONDBLCLK:
1963 case WM_LBUTTONDBLCLK:
1965 case WM_RBUTTONDBLCLK:
1966 case WM_MBUTTONDBLCLK:
1969 case WM_NCMOUSEMOVE:
1971 /* ((WindowInfo *)g_lastWindow->GetInfo())->m_mouseX = (signed short)(lParam & 0xffff);
1972 ((WindowInfo *)g_lastWindow->GetInfo())->m_mouseY = (signed short)(lParam >> 16);
1973 if (g_lastWindow->m_mouseCallbacksEnabled) g_lastWindow->MouseMoveCallback();
1974 */// goto winmessage;
1977 case WM_SYSCOMMAND: // Intercept System Commands
1979 switch (wParam) // Check System Calls
1981 case SC_SCREENSAVE: // Screensaver Trying To Start?
1982 case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
1983 return 0; // Prevent From Happening
1985 // User trying to access application menu using ALT?
1994 // ReleaseCapture();
1995 // ClipMouseToWindow(hWnd);
2012 PostQuitMessage(0); /* Terminate Application */
2020 // BeginPaint( g_windowHandle,&ps);
2021 // EndPaint( g_windowHandle,&ps);
2028 /* We processed the message and there
2029 * is no processing by Windows necessary */
2031 /* We didn't process the message so let Windows do it */
2032 return DefWindowProc(hWnd,msg,wParam,lParam);
2036 static BOOL RegisterWindowClasses(HINSTANCE hFirstInstance)
2039 memset( &wc, 0, sizeof( wc));
2041 /* Register the window class. */
2042 wc.cbSize = sizeof(wc);
2044 wc.style = (CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_OWNDC); /* Combination of Class Styles */
2045 wc.lpfnWndProc = AppWndProc; /* Adress of Window Procedure */
2046 wc.cbClsExtra = 0; /* Extra Bytes allocated for this Class */
2047 wc.cbWndExtra = 0; /* Extra Bytes allocated for each Window */
2048 wc.hInstance = hFirstInstance; /* Handle of program instance */
2049 wc.hIcon = LoadIcon( hFirstInstance, MAKEINTRESOURCE(IDI_LUGARU) );
2050 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
2051 wc.hbrBackground = NULL;
2052 wc.lpszMenuName = NULL;
2053 wc.lpszClassName = g_wndClassName; /* Name of the Window Class */
2054 wc.hIconSm = LoadIcon( hFirstInstance, MAKEINTRESOURCE(IDI_LUGARU) );
2056 if (!RegisterClassEx(&wc)) return FALSE; /* Register Class failed */
2062 int resolutionID(int width, int height)
2066 if(width==640 && height==480)whichres=0;
2067 if(width==800 && height==600)whichres=1;
2068 if(width==1024 && height==768)whichres=2;
2069 if(width==1280 && height==1024)whichres=3;
2070 if(width==1600 && height==1200)whichres=4;
2071 if(width==840 && height==524)whichres=5;
2072 if(width==1024 && height==640)whichres=6;
2073 if(width==1344 && height==840)whichres=7;
2078 int closestResolution(int width, int height)
2082 if(width>=640 && height>=480)whichres=0;
2083 if(width>=800 && height>=600)whichres=1;
2084 if(width>=1024 && height>=768)whichres=2;
2085 if(width>=1280 && height>=1024)whichres=3;
2086 if(width>=1600 && height>=1200)whichres=4;
2087 if(width==840 && height==524)whichres=5;
2088 if(width==1024 && height==640)whichres=6;
2089 if(width==1344 && height==840)whichres=7;
2094 bool selectDetail(int & width, int & height, int & bpp, int & detail)
2097 int whichres = closestResolution(width, height);
2101 if(whichres<=0 || whichres>7){
2135 if ((detail != 0) && (resolutionDepths[whichres][1] != 0))
2139 else if ((detail == 0) && (resolutionDepths[whichres][0] != 0))
2143 else if ((detail != 0) && (resolutionDepths[whichres][0] != 0))
2149 else if (0 == whichres)
2157 bpp = resolutionDepths[whichres][(detail != 0)];
2163 int __stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
2166 LPWSTR * cl = CommandLineToArgvW(GetCommandLineW(), &argc);
2169 if (0 == _wcsicmp(cl[1], L"-windowed"))
2177 memset( &g_theKeys, 0, sizeof( KeyMap));
2181 memset(&mode, 0, sizeof(mode));
2182 mode.dmSize = sizeof(mode);
2183 while (EnumDisplaySettings(NULL, i++, &mode))
2185 if (mode.dmBitsPerPel < 16)
2190 int res = resolutionID(mode.dmPelsWidth, mode.dmPelsHeight);
2192 if (res > -1 && res < 8)
2194 if (DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettings(&mode, CDS_TEST))
2199 switch(mode.dmBitsPerPel)
2203 resolutionDepths[res][1] = mode.dmBitsPerPel;
2206 resolutionDepths[res][0] = mode.dmBitsPerPel;
2212 /* if there is no Instance of our program in memory then register the window class */
2213 if (hPrevInstance == NULL && !RegisterWindowClasses(hInstance))
2214 return FALSE; /* registration failed! */
2216 g_appInstance=hInstance;
2220 UnregisterClass( g_wndClassName, hInstance);
2227 extern int channels[100];
2228 extern FSOUND_SAMPLE * samp[100];
2229 extern FSOUND_STREAM * strm[10];
2231 extern "C" void PlaySoundEx(int chan, FSOUND_SAMPLE *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
2233 const FSOUND_SAMPLE * currSample = FSOUND_GetCurrentSample(channels[chan]);
2234 if (currSample && currSample == samp[chan])
2236 if (FSOUND_GetPaused(channels[chan]))
2238 FSOUND_StopSound(channels[chan]);
2239 channels[chan] = FSOUND_FREE;
2241 else if (FSOUND_IsPlaying(channels[chan]))
2243 int loop_mode = FSOUND_GetLoopMode(channels[chan]);
2244 if (loop_mode & FSOUND_LOOP_OFF)
2246 channels[chan] = FSOUND_FREE;
2252 channels[chan] = FSOUND_FREE;
2255 channels[chan] = FSOUND_PlaySoundEx(channels[chan], sptr, dsp, startpaused);
2256 if (channels[chan] < 0)
2258 channels[chan] = FSOUND_PlaySoundEx(FSOUND_FREE, sptr, dsp, startpaused);
2262 extern "C" void PlayStreamEx(int chan, FSOUND_STREAM *sptr, FSOUND_DSPUNIT *dsp, signed char startpaused)
2264 const FSOUND_SAMPLE * currSample = FSOUND_GetCurrentSample(channels[chan]);
2265 if (currSample && currSample == FSOUND_Stream_GetSample(sptr))
2267 FSOUND_StopSound(channels[chan]);
2268 FSOUND_Stream_Stop(sptr);
2272 FSOUND_Stream_Stop(sptr);
2273 channels[chan] = FSOUND_FREE;
2276 channels[chan] = FSOUND_Stream_PlayEx(channels[chan], sptr, dsp, startpaused);
2277 if (channels[chan] < 0)
2279 channels[chan] = FSOUND_Stream_PlayEx(FSOUND_FREE, sptr, dsp, startpaused);
2283 bool LoadImage(const char * fname, TGAImageRec & tex)
2287 if ( tex.data == NULL )
2293 ILstring f = strdup(ConvertFileName(fname));
2300 ilGenImages(1, &iid);
2305 tex.sizeX = ilGetInteger(IL_IMAGE_WIDTH);
2306 tex.sizeY = ilGetInteger(IL_IMAGE_HEIGHT);
2307 tex.bpp = ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);
2308 ILuint Bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL),
2309 imageSize = tex.sizeX * tex.sizeY * Bpp;
2310 ILubyte *Data = ilGetData();
2311 memcpy(tex.data, Data, imageSize);
2313 // Truvision Targa files are stored as BGR colors
2314 // We want RGB so Blue and Red bytes are switched
2315 if (IL_TGA == ilGetInteger(IL_IMAGE_FORMAT))
2317 // Loop Through The Image Data
2318 for (GLuint i = 0; i < int(imageSize); i += Bpp)
2320 // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
2321 GLbyte temp; // Temporary Variable
2322 temp = tex.data[i]; // Temporarily Store The Value At Image Data 'i'
2323 tex.data[i] = tex.data[i + 2]; // Set The 1st Byte To The Value Of The 3rd Byte
2324 tex.data[i + 2] = temp; // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
2332 ilDeleteImages(1, &iid);
2336 GLuint texid = ilutGLLoadImage(f);
2341 ilutGLBuildMipmaps()
2350 res = load_image(fname, tex);
2351 //if (!res) printf("failed to load %s\n", fname);
2357 void ScreenShot(const char * fname)
2360 ILstring f = strdup(fname);
2367 ilGenImages(1, &iid);
2373 ilDeleteImages(1, &iid);
2383 static bool load_image(const char *file_name, TGAImageRec &tex)
2385 char *ptr = strrchr(file_name, '.');
2388 if (stricmp(ptr+1, "png") == 0)
2389 return load_png(file_name, tex);
2390 else if (stricmp(ptr+1, "jpg") == 0)
2391 return load_jpg(file_name, tex);
2394 STUBBED("Unsupported image type");
2399 struct my_error_mgr {
2400 struct jpeg_error_mgr pub; /* "public" fields */
2401 jmp_buf setjmp_buffer; /* for return to caller */
2403 typedef struct my_error_mgr * my_error_ptr;
2406 static void my_error_exit(j_common_ptr cinfo)
2408 struct my_error_mgr *err = (struct my_error_mgr *)cinfo->err;
2409 longjmp(err->setjmp_buffer, 1);
2412 /* stolen from public domain example.c code in libjpg distribution. */
2413 static bool load_jpg(const char *file_name, TGAImageRec &tex)
2415 struct jpeg_decompress_struct cinfo;
2416 struct my_error_mgr jerr;
2417 JSAMPROW buffer[1]; /* Output row buffer */
2418 int row_stride; /* physical row width in output buffer */
2419 FILE *infile = fopen(file_name, "rb");
2424 cinfo.err = jpeg_std_error(&jerr.pub);
2425 jerr.pub.error_exit = my_error_exit;
2426 if (setjmp(jerr.setjmp_buffer)) {
2427 jpeg_destroy_decompress(&cinfo);
2432 jpeg_create_decompress(&cinfo);
2433 jpeg_stdio_src(&cinfo, infile);
2434 (void) jpeg_read_header(&cinfo, TRUE);
2436 cinfo.out_color_space = JCS_RGB;
2437 cinfo.quantize_colors = 0;
2438 (void) jpeg_calc_output_dimensions(&cinfo);
2439 (void) jpeg_start_decompress(&cinfo);
2441 row_stride = cinfo.output_width * cinfo.output_components;
2442 tex.sizeX = cinfo.output_width;
2443 tex.sizeY = cinfo.output_height;
2446 while (cinfo.output_scanline < cinfo.output_height) {
2447 buffer[0] = (JSAMPROW)(char *)tex.data +
2448 ((cinfo.output_height-1) - cinfo.output_scanline) * row_stride;
2449 (void) jpeg_read_scanlines(&cinfo, buffer, 1);
2452 (void) jpeg_finish_decompress(&cinfo);
2453 jpeg_destroy_decompress(&cinfo);
2460 /* stolen from public domain example.c code in libpng distribution. */
2461 static bool load_png(const char *file_name, TGAImageRec &tex)
2463 bool hasalpha = false;
2464 png_structp png_ptr = NULL;
2465 png_infop info_ptr = NULL;
2466 png_uint_32 width, height;
2467 int bit_depth, color_type, interlace_type;
2468 png_byte **rows = NULL;
2469 bool retval = false;
2470 png_byte **row_pointers = NULL;
2471 FILE *fp = fopen(file_name, "rb");
2476 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2477 if (png_ptr == NULL)
2480 info_ptr = png_create_info_struct(png_ptr);
2481 if (info_ptr == NULL)
2484 if (setjmp(png_jmpbuf(png_ptr)))
2487 png_init_io(png_ptr, fp);
2488 png_read_png(png_ptr, info_ptr,
2489 PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING,
2491 png_get_IHDR(png_ptr, info_ptr, &width, &height,
2492 &bit_depth, &color_type, &interlace_type, NULL, NULL);
2494 if (bit_depth != 8) // transform SHOULD handle this...
2497 if (color_type & PNG_COLOR_MASK_PALETTE) // !!! FIXME?
2500 if ((color_type & PNG_COLOR_MASK_COLOR) == 0) // !!! FIXME?
2503 hasalpha = ((color_type & PNG_COLOR_MASK_ALPHA) != 0);
2504 row_pointers = png_get_rows(png_ptr, info_ptr);
2508 retval = malloc(width * height * 4);
2514 png_byte *dst = tex.data;
2515 for (int i = height-1; i >= 0; i--)
2517 png_byte *src = row_pointers[i];
2518 for (int j = 0; j < width; j++)
2532 png_byte *dst = tex.data;
2533 int pitch = width * 4;
2534 for (int i = height-1; i >= 0; i--, dst += pitch)
2535 memcpy(dst, row_pointers[i], pitch);
2544 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
2551 static bool save_image(const char *file_name)
2553 char *ptr = strrchr(file_name, '.');
2556 if (stricmp(ptr+1, "png") == 0)
2557 return save_png(file_name);
2560 STUBBED("Unsupported image type");
2565 static bool save_png(const char *file_name)
2568 png_structp png_ptr = NULL;
2569 png_infop info_ptr = NULL;
2570 bool retval = false;
2572 fp = fopen(file_name, "wb");
2576 png_bytep *row_pointers = new png_bytep[kContextHeight];
2577 png_bytep screenshot = new png_byte[kContextWidth * kContextHeight * 3];
2578 if ((!screenshot) || (!row_pointers))
2582 glReadPixels(0, 0, kContextWidth, kContextHeight,
2583 GL_RGB, GL_UNSIGNED_BYTE, screenshot);
2584 if (glGetError() != GL_NO_ERROR)
2587 for (int i = 0; i < kContextHeight; i++)
2588 row_pointers[i] = screenshot + ((kContextWidth * ((kContextHeight-1) - i)) * 3);
2590 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2591 if (png_ptr == NULL)
2594 info_ptr = png_create_info_struct(png_ptr);
2595 if (info_ptr == NULL)
2598 if (setjmp(png_jmpbuf(png_ptr)))
2601 png_init_io(png_ptr, fp);
2603 if (setjmp(png_jmpbuf(png_ptr)))
2606 png_set_IHDR(png_ptr, info_ptr, kContextWidth, kContextHeight,
2607 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
2608 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2610 png_write_info(png_ptr, info_ptr);
2612 if (setjmp(png_jmpbuf(png_ptr)))
2615 png_write_image(png_ptr, row_pointers);
2617 if (setjmp(png_jmpbuf(png_ptr)))
2620 png_write_end(png_ptr, NULL);
2624 png_destroy_write_struct(&png_ptr, &info_ptr);
2625 delete[] screenshot;
2626 delete[] row_pointers;
2630 unlink(ConvertFileName(file_name));