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