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