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