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