Filling in branch from trunk
[blender.git] / source / gameengine / GamePlayer / ghost / GPG_ghost.cpp
1 /**
2 * $Id$
3 *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31 * Start up of the Blender Player on GHOST.
32 */
33
34 #include <iostream>
35 #include <math.h>
36
37 #ifdef __linux__
38 #ifdef __alpha__
39 #include <signal.h>
40 #endif /* __alpha__ */
41 #endif /* __linux__ */
42
43 #ifdef __APPLE__
44 // Can't use Carbon right now because of double defined type ID (In Carbon.h and DNA_ID.h, sigh)
45 //#include <Carbon/Carbon.h>
46 //#include <CFBundle.h>
47 #endif // __APPLE__
48 #include "GEN_messaging.h"
49 #include "KX_KetsjiEngine.h"
50
51 /**********************************
52 * Begin Blender include block
53 **********************************/
54 #ifdef __cplusplus
55 extern "C"
56 {
57 #endif  // __cplusplus
58 #include "BKE_global.h" 
59 #include "BKE_icons.h"  
60 #include "BLI_blenlib.h"
61 #include "DNA_scene_types.h"
62 #include "BLO_readfile.h"
63 #include "BLO_readblenfile.h"
64         
65         int GHOST_HACK_getFirstFile(char buf[]);
66         
67 #ifdef __cplusplus
68 }
69 #endif // __cplusplus
70 /**********************************
71 * End Blender include block
72 **********************************/
73
74 #include "SYS_System.h"
75 #include "GPG_Application.h"
76 #include "GPC_PolygonMaterial.h"
77
78 #include "GHOST_ISystem.h"
79 #include "RAS_IRasterizer.h"
80
81 #include "BKE_main.h"
82 #include "BKE_utildefines.h"
83
84 #ifdef WIN32
85 #include <windows.h>
86 #ifdef NDEBUG
87 #include <wincon.h>
88 #endif // NDEBUG
89 #endif // WIN32
90
91 const int kMinWindowWidth = 100;
92 const int kMinWindowHeight = 100;
93
94 char bprogname[FILE_MAXDIR+FILE_MAXFILE];
95
96 #ifdef WIN32
97 typedef enum 
98 {
99   SCREEN_SAVER_MODE_NONE = 0,
100   SCREEN_SAVER_MODE_PREVIEW,
101   SCREEN_SAVER_MODE_SAVER,
102   SCREEN_SAVER_MODE_CONFIGURATION,
103   SCREEN_SAVER_MODE_PASSWORD,
104 } ScreenSaverMode;
105
106 static ScreenSaverMode scr_saver_mode = SCREEN_SAVER_MODE_NONE;
107 static HWND scr_saver_hwnd = NULL;
108
109 static BOOL scr_saver_init(int argc, char **argv) 
110 {
111         scr_saver_mode = SCREEN_SAVER_MODE_NONE;
112         scr_saver_hwnd = NULL;
113         BOOL ret = FALSE;
114
115         int len = ::strlen(argv[0]);
116         if (len > 4 && !::stricmp(".scr", argv[0] + len - 4))
117         {
118                 scr_saver_mode = SCREEN_SAVER_MODE_CONFIGURATION;
119                 ret = TRUE;
120                 if (argc >= 2)
121                 {
122                         if (argc >= 3)
123                         {
124                                 scr_saver_hwnd = (HWND) ::atoi(argv[2]);
125                         }
126                         if (!::stricmp("/c", argv[1]))
127                         {
128                                 scr_saver_mode = SCREEN_SAVER_MODE_CONFIGURATION;
129                                 if (scr_saver_hwnd == NULL)
130                                         scr_saver_hwnd = ::GetForegroundWindow();
131                         }
132                         else if (!::stricmp("/s", argv[1]))
133                         {
134                                 scr_saver_mode = SCREEN_SAVER_MODE_SAVER;
135                         }
136                         else if (!::stricmp("/a", argv[1]))
137                         {
138                                 scr_saver_mode = SCREEN_SAVER_MODE_PASSWORD;
139                         }
140                         else if (!::stricmp("/p", argv[1])
141                                  || !::stricmp("/l", argv[1]))
142                         {
143                                 scr_saver_mode = SCREEN_SAVER_MODE_PREVIEW;
144                         }
145                 }
146         }
147         return ret;
148 }
149
150 #endif /* WIN32 */
151
152 void usage(char* program)
153 {
154         char * consoleoption;
155 #ifdef _WIN32
156         consoleoption = "-c ";
157 #else
158         consoleoption = "";
159 #endif
160         
161         printf("usage:   %s [-w [-p l t w h]] %s[-g gamengineoptions] "
162                 "[-s stereomode] filename.blend\n", program, consoleoption);
163         printf("  -h: Prints this command summary\n");
164         printf("  -w: display in a window\n");
165         printf("  -p: specify window position\n");
166         printf("       l = window left coordinate\n");
167         printf("       t = window top coordinate\n");
168         printf("       w = window width\n");
169         printf("       h = window height\n");
170         printf("  -f: start game in full screen mode\n");
171         printf("       fw = full screen mode pixel width\n");
172         printf("       fh = full screen mode pixel height\n");
173         printf("       fb = full screen mode bits per pixel\n");
174         printf("       ff = full screen mode frequency\n");
175         printf("  -s: start player in stereo\n");
176         printf("       stereomode: hwpageflip       (Quad buffered shutter glasses)\n");
177         printf("                   syncdoubling     (Above Below)\n");
178         printf("                   sidebyside       (Left Right)\n");
179         printf("                   anaglyph         (Red-Blue glasses)\n");
180         printf("                   vinterlace       (Vertical interlace for autostereo display)\n");
181         printf("                             depending on the type of stereo you want\n");
182 #ifdef _WIN32
183         printf("  -c: keep console window open\n");
184 #endif
185         printf("  -g: game engine options:\n");
186         printf("       Name            Default      Description\n");
187         printf("       ----------------------------------------\n");
188         printf("       fixedtime          0         Do the same timestep each frame \"Enable all frames\"\n");
189         printf("       nomipmap           0         Disable mipmaps\n");
190         printf("       show_framerate     0         Show the frame rate\n");
191         printf("       show_properties    0         Show debug properties\n");
192         printf("       show_profile       0         Show profiling information\n");
193         printf("       vertexarrays       1         Enable vertex arrays\n");
194         printf("       blender_material   0         Enable material settings\n");
195         printf("\n");
196         printf("example: %s -p 10 10 320 200 -g noaudio c:\\loadtest.blend\n", program);
197         printf("example: %s -g vertexarrays = 0 c:\\loadtest.blend\n", program);
198 }
199
200 char *get_filename(int argc, char **argv) {
201 #ifdef __APPLE__
202 /* On Mac we park the game file (called game.blend) in the application bundle.
203 * The executable is located in the bundle as well.
204 * Therefore, we can locate the game relative to the executable.
205         */
206         int srclen = ::strlen(argv[0]);
207         int len = 0;
208         char *filename = NULL;
209         
210         if (argc > 1) {
211                 if (BLI_exists(argv[argc-1])) {
212                         len = ::strlen(argv[argc-1]);
213                         filename = new char [len + 1];
214                         ::strcpy(filename, argv[argc-1]);
215                         return(filename);
216                 }
217                 if (::strncmp(argv[argc-1], "-psn_", 5)==0) {
218                         static char firstfilebuf[512];
219                         if (GHOST_HACK_getFirstFile(firstfilebuf)) {
220                                 len = ::strlen(firstfilebuf);
221                                 filename = new char [len + 1];
222                                 ::strcpy(filename, firstfilebuf);
223                                 return(filename);
224                         }
225                 }                        
226         }
227         
228         srclen -= ::strlen("MacOS/blenderplayer");
229         if (srclen > 0) {
230                 len = srclen + ::strlen("Resources/game.blend"); 
231                 filename = new char [len + 1];
232                 ::strcpy(filename, argv[0]);
233                 ::strcpy(filename + srclen, "Resources/game.blend");
234                 //::printf("looking for file: %s\n", filename);
235                 
236                 if (BLI_exists(filename)) {
237                         return (filename);
238                 }
239         }
240         
241         return(NULL);
242 #else
243         return (argc>1)?argv[argc-1]:NULL;
244 #endif // !_APPLE
245 }
246
247 static BlendFileData *load_game_data(char *progname, char *filename = NULL) {
248         BlendReadError error;
249         BlendFileData *bfd = NULL;
250         
251         /* try to load ourself, will only work if we are a runtime */
252         if (blo_is_a_runtime(progname)) {
253                 bfd= blo_read_runtime(progname, &error);
254                 if (bfd) {
255                         bfd->type= BLENFILETYPE_RUNTIME;
256                         strcpy(bfd->main->name, progname);
257                 }
258         } else {
259                 bfd= BLO_read_from_file(progname, &error);
260         }
261         
262         /*
263         if (bfd && bfd->type == BLENFILETYPE_BLEND) {
264                 BLO_blendfiledata_free(bfd);
265                 bfd = NULL;
266                 error = BRE_NOT_A_PUBFILE;
267         }
268         */
269         
270         if (!bfd && filename) {
271                 bfd = load_game_data(filename);
272                 if (!bfd) {
273                         printf("Loading %s failed: %s\n", filename, BLO_bre_as_string(error));
274                 }
275         }
276         
277         return bfd;
278 }
279
280 int main(int argc, char** argv)
281 {
282         int i;
283         bool error = false;
284         SYS_SystemHandle syshandle = SYS_GetSystem();
285         bool fullScreen = false;
286         bool fullScreenParFound = false;
287         bool windowParFound = false;
288         bool closeConsole = true;
289         RAS_IRasterizer::StereoMode stereomode;
290         bool stereoWindow = false;
291         bool stereoParFound = false;
292         int windowLeft = 100;
293         int windowTop = 100;
294         int windowWidth = 640;
295         int windowHeight = 480;
296         GHOST_TUns32 fullScreenWidth = 0;
297         GHOST_TUns32 fullScreenHeight= 0;
298         int fullScreenBpp = 32;
299         int fullScreenFrequency = 60;
300
301 #ifdef __linux__
302 #ifdef __alpha__
303         signal (SIGFPE, SIG_IGN);
304 #endif /* __alpha__ */
305 #endif /* __linux__ */
306         BLI_where_am_i(bprogname, argv[0]);
307         
308 #ifdef __APPLE__
309     // Can't use Carbon right now because of double defined type ID (In Carbon.h and DNA_ID.h, sigh)
310     /*
311     IBNibRef            nibRef;
312     WindowRef           window;
313     OSStatus            err;
314         
315           // Create a Nib reference passing the name of the nib file (without the .nib extension)
316           // CreateNibReference only searches into the application bundle.
317           err = ::CreateNibReference(CFSTR("main"), &nibRef);
318           if (err) return -1;
319           
320                 // Once the nib reference is created, set the menu bar. "MainMenu" is the name of the menu bar
321                 // object. This name is set in InterfaceBuilder when the nib is created.
322                 err = ::SetMenuBarFromNib(nibRef, CFSTR("MenuBar"));
323                 if (err) return -1;
324                 
325                   // We don't need the nib reference anymore.
326                   ::DisposeNibReference(nibRef);
327     */
328 #endif // __APPLE__
329         
330         GEN_init_messaging_system();
331  
332         // Parse command line options
333 #ifndef NDEBUG
334         printf("argv[0] = '%s'\n", argv[0]);
335 #endif
336
337 #ifdef WIN32
338         if (scr_saver_init(argc, argv))
339         {
340                 switch (scr_saver_mode)
341                 {
342                 case SCREEN_SAVER_MODE_CONFIGURATION:
343                         MessageBox(scr_saver_hwnd, "This screen saver has no options that you can set", "Screen Saver", MB_OK);
344                         break;
345                 case SCREEN_SAVER_MODE_PASSWORD:
346                         /* This is W95 only, which we currently do not support.
347                            Fall-back to normal screen saver behaviour in that case... */
348                 case SCREEN_SAVER_MODE_SAVER:
349                         fullScreen = true;
350                         fullScreenParFound = true;
351                         break;
352
353                 case SCREEN_SAVER_MODE_PREVIEW:
354                         /* This will actually be handled somewhere below... */
355                         break;
356                 }
357         }
358 #endif
359         for (i = 1; (i < argc) && !error 
360 #ifdef WIN32
361                 && scr_saver_mode == SCREEN_SAVER_MODE_NONE
362 #endif
363                 ;)
364
365         {
366 #ifndef NDEBUG
367                 printf("argv[%d] = '%s'   , %i\n", i, argv[i],argc);
368 #endif
369                 if (argv[i][0] == '-')
370                 {
371                         switch (argv[i][1])
372                         {
373                         case 'g':
374                                 // Parse game options
375                                 {
376                                         i++;
377                                         if (i < argc)
378                                         {
379                                                 char* paramname = argv[i];
380                                                 // Check for single value versus assignment
381                                                 if (i+1 < argc && (*(argv[i+1]) == '='))
382                                                 {
383                                                         i++;
384                                                         if (i + 1 < argc)
385                                                         {
386                                                                 i++;
387                                                                 // Assignment
388                                                                 SYS_WriteCommandLineInt(syshandle, paramname, atoi(argv[i]));
389                                                                 SYS_WriteCommandLineFloat(syshandle, paramname, atof(argv[i]));
390                                                                 SYS_WriteCommandLineString(syshandle, paramname, argv[i]);
391 #ifndef NDEBUG
392                                                                 printf("%s = '%s'\n", paramname, argv[i]);
393 #endif
394                                                                 i++;
395                                                         }
396                                                         else
397                                                         {
398                                                                 error = true;
399                                                                 printf("error: argument assignment %s without value.\n", paramname);
400                                                         }
401                                                 }
402                                                 else
403                                                 {
404 //                                                      SYS_WriteCommandLineInt(syshandle, argv[i++], 1);
405                                                 }
406                                         }
407                                 }
408                                 break;
409                                 
410                         case 'p':
411                                 // Parse window position and size options
412                                 if (argv[i][2] == 0) {
413                                         i++;
414                                         if ((i + 4) < argc)
415                                         {
416                                                 windowLeft = atoi(argv[i++]);
417                                                 windowTop = atoi(argv[i++]);
418                                                 windowWidth = atoi(argv[i++]);
419                                                 windowHeight = atoi(argv[i++]);
420                                                 windowParFound = true;
421                                         }
422                                         else
423                                         {
424                                                 error = true;
425                                                 printf("error: too few options for window argument.\n");
426                                         }
427                                 } else { /* mac specific */
428                                 
429                     if (strncmp(argv[i], "-psn_", 5)==0) 
430                         i++; /* skip process serial number */
431                                 }
432                                 break;
433                         case 'f':
434                                 i++;
435                                 fullScreen = true;
436                                 fullScreenParFound = true;
437                                 if ((i + 2) < argc && argv[i][0] != '-' && argv[i+1][0] != '-')
438                                 {
439                                         fullScreenWidth = atoi(argv[i++]);
440                                         fullScreenHeight = atoi(argv[i++]);
441                                         if ((i + 1) < argc && argv[i][0] != '-')
442                                         {
443                                                 fullScreenBpp = atoi(argv[i++]);
444                                                 if ((i + 1) < argc && argv[i][0] != '-')
445                                                         fullScreenFrequency = atoi(argv[i++]);
446                                         }
447                                 }
448                                 break;
449                         case 'w':
450                                 // Parse window position and size options
451                                 {
452                                         fullScreen = false;
453                                         fullScreenParFound = true;
454                                         i++;
455                                 }
456                                 break;
457                         case 'h':
458                                 usage(argv[0]);
459                                 return 0;
460                                 break;
461                         case 'c':
462                                 i++;
463                                 closeConsole = false;
464                                 break;
465                         case 's':  // stereo
466                                 i++;
467                                 if ((i + 1) < argc)
468                                 {
469                                         stereomode = (RAS_IRasterizer::StereoMode) atoi(argv[i]);
470                                         if (stereomode < RAS_IRasterizer::RAS_STEREO_NOSTEREO || stereomode >= RAS_IRasterizer::RAS_STEREO_MAXSTEREO)
471                                                 stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO;
472                                         
473                                         if(!strcmp(argv[i], "nostereo"))  // ok, redundant but clear
474                                                 stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO;
475                                         
476                                         // only the hardware pageflip method needs a stereo window
477                                         if(!strcmp(argv[i], "hwpageflip")) {
478                                                 stereomode = RAS_IRasterizer::RAS_STEREO_QUADBUFFERED;
479                                                 stereoWindow = true;
480                                         }
481                                         if(!strcmp(argv[i], "syncdoubling"))
482                                                 stereomode = RAS_IRasterizer::RAS_STEREO_ABOVEBELOW;
483                                         
484                                         if(!strcmp(argv[i], "anaglyph"))
485                                                 stereomode = RAS_IRasterizer::RAS_STEREO_ANAGLYPH;
486                                         
487                                         if(!strcmp(argv[i], "sidebyside"))
488                                                 stereomode = RAS_IRasterizer::RAS_STEREO_SIDEBYSIDE;
489                                         
490                                         if(!strcmp(argv[i], "vinterlace"))
491                                                 stereomode = RAS_IRasterizer::RAS_STEREO_VINTERLACE;
492                                         
493 #if 0
494                                         // future stuff
495                                         if(strcmp(argv[i], "stencil")
496                                                 stereomode = RAS_STEREO_STENCIL;
497 #endif
498                                         
499                                         i++;
500                                         stereoParFound = true;
501                                 }
502                                 else
503                                 {
504                                         error = true;
505                                         printf("error: too few options for stereo argument.\n");
506                                 }
507                                 break;
508                         default:
509                                 printf("Unkown argument: %s\n", argv[i++]);
510                                 break;
511                         }
512                 }
513                 else
514                 {
515                         i++;
516                 }
517         }
518         
519         if ((windowWidth < kMinWindowWidth) || (windowHeight < kMinWindowHeight))
520         {
521                 error = true;
522                 printf("error: window size too small.\n");
523         }
524         
525         if (error )
526         {
527                 usage(argv[0]);
528                 return 0;
529         }
530
531         if (!stereoParFound) stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO;
532
533 #ifdef WIN32
534         if (scr_saver_mode != SCREEN_SAVER_MODE_CONFIGURATION)
535 #endif
536         {
537 #ifdef __APPLE__
538                 //SYS_WriteCommandLineInt(syshandle, "show_framerate", 1);
539                 SYS_WriteCommandLineInt(syshandle, "nomipmap", 1);
540                 //fullScreen = false;           // Can't use full screen
541 #endif
542
543                 if (SYS_GetCommandLineInt(syshandle, "nomipmap", 0))
544                 {
545                         GPC_PolygonMaterial::SetMipMappingEnabled(0);
546                 }
547                 
548                 // Create the system
549                 if (GHOST_ISystem::createSystem() == GHOST_kSuccess)
550                 {
551                         GHOST_ISystem* system = GHOST_ISystem::getSystem();
552                         assertd(system);
553                         
554                         if (!fullScreenWidth || !fullScreenHeight)
555                                 system->getMainDisplayDimensions(fullScreenWidth, fullScreenHeight);
556                         // process first batch of events. If the user
557                         // drops a file on top off the blenderplayer icon, we 
558                         // recieve an event with the filename
559                         
560                         system->processEvents(0);
561                         
562                         // this bracket is needed for app (see below) to get out
563                         // of scope before GHOST_ISystem::disposeSystem() is called.
564                         {
565                                 int exitcode = KX_EXIT_REQUEST_NO_REQUEST;
566                                 STR_String exitstring = "";
567                                 GPG_Application app(system, NULL, exitstring);
568                                 bool firstTimeRunning = true;
569                                 
570                                 do
571                                 {
572                                         // Read the Blender file
573                                         char *filename = get_filename(argc, argv);
574                                         char *titlename;
575                                         char pathname[160];
576                                         BlendFileData *bfd;
577                                         
578                                         // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
579                                         if (exitcode == KX_EXIT_REQUEST_START_OTHER_GAME)
580                                         {
581                                                 char basedpath[160];
582                                                 
583                                                 // base the actuator filename with respect
584                                                 // to the original file working directory
585                                                 strcpy(basedpath, exitstring.Ptr());
586                                                 BLI_convertstringcode(basedpath, pathname, 0);
587                                                 
588                                                 bfd = load_game_data(basedpath);
589                                         }
590                                         else
591                                         {
592                                                 bfd = load_game_data(argv[0], filename);
593                                         }
594                                         
595                                         //::printf("game data loaded from %s\n", filename);
596                                         
597                                         if (!bfd) {
598                                                 usage(argv[0]);
599                                                 error = true;
600                                                 exitcode = KX_EXIT_REQUEST_QUIT_GAME;
601                                         } 
602                                         else 
603                                         {
604 #ifdef WIN32
605 #ifdef NDEBUG
606                                                 if (closeConsole)
607                                                 {
608                                                         //::FreeConsole();    // Close a console window
609                                                 }
610 #endif // NDEBUG
611 #endif // WIN32
612                                                 Main *maggie = bfd->main;
613                                                 Scene *scene = bfd->curscene;
614                                                 strcpy (pathname, maggie->name);
615                                                 char *startscenename = scene->id.name + 2;
616                                                 G.fileflags  = bfd->fileflags;
617
618                                                 //Seg Fault; icon.c gIcons == 0
619                                                 BKE_icons_init(1);
620                                                 
621                                                 titlename = maggie->name;
622                                                 
623                                                 // Check whether the game should be displayed full-screen
624                                                 if ((!fullScreenParFound) && (!windowParFound))
625                                                 {
626                                                         // Only use file settings when command line did not override
627                                                         if (scene->r.fullscreen) {
628                                                                 //printf("fullscreen option found in Blender file\n");
629                                                                 fullScreen = true;
630                                                                 fullScreenWidth= scene->r.xplay;
631                                                                 fullScreenHeight= scene->r.yplay;
632                                                                 fullScreenFrequency= scene->r.freqplay;
633                                                                 fullScreenBpp = scene->r.depth;
634                                                         }
635                                                         else
636                                                         {
637                                                                 fullScreen = false;
638                                                                 windowWidth = scene->r.xplay;
639                                                                 windowHeight = scene->r.yplay;
640                                                         }
641                                                 }
642                                                 
643                                                 
644                                                 // Check whether the game should be displayed in stereo
645                                                 if (!stereoParFound)
646                                                 {
647                                                         stereomode = (RAS_IRasterizer::StereoMode) scene->r.stereomode;
648                                                         if (stereomode == RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
649                                                                 stereoWindow = true;
650                                                 }
651                                                 
652                                                 //                                      GPG_Application app (system, maggie, startscenename);
653                                                 app.SetGameEngineData(maggie, startscenename);
654                                                 
655                                                 if (firstTimeRunning)
656                                                 {
657                                                         firstTimeRunning = false;
658                                                         
659                                                         if (fullScreen)
660                                                         {
661 #ifdef WIN32
662                                                                 if (scr_saver_mode == SCREEN_SAVER_MODE_SAVER)
663                                                                 {
664                                                                         app.startScreenSaverFullScreen(fullScreenWidth, fullScreenHeight, fullScreenBpp, fullScreenFrequency,
665                                                                                 stereoWindow, stereomode);
666                                                                 }
667                                                                 else
668 #endif
669                                                                 {
670                                                                         app.startFullScreen(fullScreenWidth, fullScreenHeight, fullScreenBpp, fullScreenFrequency,
671                                                                                 stereoWindow, stereomode);
672                                                                 }
673                                                         }
674                                                         else
675                                                         {
676 #ifdef __APPLE__
677                                                                 // on Mac's we'll show the executable name instead of the 'game.blend' name
678                                                                 char tempname[1024], *appstring;
679                                                                 ::strcpy(tempname, titlename);
680                                                                 
681                                                                 appstring = strstr(tempname, ".app/");
682                                                                 if (appstring) {
683                                                                         appstring[2] = 0;
684                                                                         titlename = &tempname[0];
685                                                                 }
686 #endif
687                                                                 // Strip the path so that we have the name of the game file
688                                                                 STR_String path = titlename;
689 #ifndef WIN32
690                                                                 vector<STR_String> parts = path.Explode('/');
691 #else  // WIN32
692                                                                 vector<STR_String> parts = path.Explode('\\');
693 #endif // WIN32                        
694                                                                 STR_String title;
695                                                                 if (parts.size())
696                                                                 {
697                                                                         title = parts[parts.size()-1];
698                                                                         parts = title.Explode('.');
699                                                                         if (parts.size() > 1)
700                                                                         {
701                                                                                 title = parts[0];
702                                                                         }
703                                                                 }
704                                                                 else
705                                                                 {
706                                                                         title = "blenderplayer";
707                                                                 }
708 #ifdef WIN32
709                                                                 if (scr_saver_mode == SCREEN_SAVER_MODE_PREVIEW)
710                                                                 {
711                                                                         app.startScreenSaverPreview(scr_saver_hwnd, stereoWindow, stereomode);
712                                                                 }
713                                                                 else
714 #endif
715                                                                 {
716                                                                         app.startWindow(title, windowLeft, windowTop, windowWidth, windowHeight,
717                                                                                 stereoWindow, stereomode);
718                                                                 }
719                                                         }
720                                                 }
721                                                 else
722                                                 {
723                                                         app.StartGameEngine(stereomode);
724                                                         exitcode = KX_EXIT_REQUEST_NO_REQUEST;
725                                                 }
726                                                 
727                                                 // Add the application as event consumer
728                                                 system->addEventConsumer(&app);
729                                                 
730                                                 // Enter main loop
731                                                 bool run = true;
732                                                 while (run)
733                                                 {
734                                                         system->processEvents(false);
735                                                         system->dispatchEvents();
736                                                         if ((exitcode = app.getExitRequested()))
737                                                         {
738                                                                 run = false;
739                                                                 exitstring = app.getExitString();
740                                                         }
741                                                 }
742                                                 app.StopGameEngine();
743                                                 BLO_blendfiledata_free(bfd);
744                                                 
745 #ifdef __APPLE__
746                                                 if (filename) {
747                                                         delete [] filename;
748                                                 }
749 #endif // __APPLE__
750                                         }
751                                 } while (exitcode == KX_EXIT_REQUEST_RESTART_GAME || exitcode == KX_EXIT_REQUEST_START_OTHER_GAME);
752                         }
753
754                         // Seg Fault; icon.c gIcons == 0
755                         BKE_icons_free();
756
757                         // Dispose the system
758                         GHOST_ISystem::disposeSystem();
759                 } else {
760                         error = true;
761                         printf("error: couldn't create a system.\n");
762                 }
763         }
764
765         return error ? -1 : 0;
766 }
767
768