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