svn merge -r 22571:22800 https://svn.blender.org/svnroot/bf-blender/trunk/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 "BKE_report.h" 
62 #include "BLI_blenlib.h"
63 #include "DNA_scene_types.h"
64 #include "DNA_userdef_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         libtiff_init();
366  
367         // Parse command line options
368 #ifndef NDEBUG
369         printf("argv[0] = '%s'\n", argv[0]);
370 #endif
371
372 #ifdef WIN32
373         if (scr_saver_init(argc, argv))
374         {
375                 switch (scr_saver_mode)
376                 {
377                 case SCREEN_SAVER_MODE_CONFIGURATION:
378                         MessageBox(scr_saver_hwnd, "This screen saver has no options that you can set", "Screen Saver", MB_OK);
379                         break;
380                 case SCREEN_SAVER_MODE_PASSWORD:
381                         /* This is W95 only, which we currently do not support.
382                            Fall-back to normal screen saver behaviour in that case... */
383                 case SCREEN_SAVER_MODE_SAVER:
384                         fullScreen = true;
385                         fullScreenParFound = true;
386                         break;
387
388                 case SCREEN_SAVER_MODE_PREVIEW:
389                         /* This will actually be handled somewhere below... */
390                         break;
391                 }
392         }
393 #endif
394         // XXX add the ability to change this values to the command line parsing.
395         U.mixbufsize = 2048;
396         U.audiodevice = 2;
397         U.audiorate = 44100;
398         U.audioformat = 0x24;
399         U.audiochannels = 2;
400
401         for (i = 1; (i < argc) && !error 
402 #ifdef WIN32
403                 && scr_saver_mode == SCREEN_SAVER_MODE_NONE
404 #endif
405                 ;)
406
407         {
408 #ifndef NDEBUG
409                 printf("argv[%d] = '%s'   , %i\n", i, argv[i],argc);
410 #endif
411                 if (argv[i][0] == '-')
412                 {
413                         /* ignore all args after " - ", allow python to have own args */
414                         if (argv[i][1]=='\0') {
415                                 argc_py_clamped= i;
416                                 break;
417                         }
418                         
419                         switch (argv[i][1])
420                         {
421                         case 'g':
422                                 // Parse game options
423                                 {
424                                         i++;
425                                         if (i < argc)
426                                         {
427                                                 char* paramname = argv[i];
428                                                 // Check for single value versus assignment
429                                                 if (i+1 < argc && (*(argv[i+1]) == '='))
430                                                 {
431                                                         i++;
432                                                         if (i + 1 < argc)
433                                                         {
434                                                                 i++;
435                                                                 // Assignment
436                                                                 SYS_WriteCommandLineInt(syshandle, paramname, atoi(argv[i]));
437                                                                 SYS_WriteCommandLineFloat(syshandle, paramname, atof(argv[i]));
438                                                                 SYS_WriteCommandLineString(syshandle, paramname, argv[i]);
439 #ifndef NDEBUG
440                                                                 printf("%s = '%s'\n", paramname, argv[i]);
441 #endif
442                                                                 i++;
443                                                         }
444                                                         else
445                                                         {
446                                                                 error = true;
447                                                                 printf("error: argument assignment %s without value.\n", paramname);
448                                                         }
449                                                 }
450                                                 else
451                                                 {
452 //                                                      SYS_WriteCommandLineInt(syshandle, argv[i++], 1);
453                                                 }
454                                         }
455                                 }
456                                 break;
457
458                         case 'd':
459                                 i++;
460                                 G.f |= G_DEBUG;     /* std output printf's */
461                                 MEM_set_memory_debug();
462                                 break;
463
464                         case 'f':
465                                 i++;
466                                 fullScreen = true;
467                                 fullScreenParFound = true;
468                                 if ((i + 2) <= argc && argv[i][0] != '-' && argv[i+1][0] != '-')
469                                 {
470                                         fullScreenWidth = atoi(argv[i++]);
471                                         fullScreenHeight = atoi(argv[i++]);
472                                         if ((i + 1) <= argc && argv[i][0] != '-')
473                                         {
474                                                 fullScreenBpp = atoi(argv[i++]);
475                                                 if ((i + 1) <= argc && argv[i][0] != '-')
476                                                         fullScreenFrequency = atoi(argv[i++]);
477                                         }
478                                 }
479                                 break;
480                         case 'w':
481                                 // Parse window position and size options
482                                 i++;
483                                 fullScreen = false;
484                                 windowParFound = true;
485
486                                 if ((i + 2) <= argc && argv[i][0] != '-' && argv[i+1][0] != '-')
487                                 {
488                                         windowWidth = atoi(argv[i++]);
489                                         windowHeight = atoi(argv[i++]);
490                                         if ((i +2) <= argc && argv[i][0] != '-' && argv[i+1][0] != '-')
491                                         {
492                                                 windowLeft = atoi(argv[i++]);
493                                                 windowTop = atoi(argv[i++]);
494                                         }
495                                 }
496                                 break;
497                                         
498                         case 'h':
499                                 usage(argv[0]);
500                                 return 0;
501                                 break;
502 #ifndef _WIN32
503                         case 'i':
504                                 i++;
505                                 if ( (i + 1) < argc )
506                                         parentWindow = atoi(argv[i++]);                                         
507 #ifndef NDEBUG
508                                 printf("XWindows ID = %d\n", parentWindow);
509 #endif //NDEBUG
510
511 #endif  // _WIN32                       
512                         case 'c':
513                                 i++;
514                                 closeConsole = false;
515                                 break;
516                         case 's':  // stereo
517                                 i++;
518                                 if ((i + 1) < argc)
519                                 {
520                                         stereomode = (RAS_IRasterizer::StereoMode) atoi(argv[i]);
521                                         if (stereomode < RAS_IRasterizer::RAS_STEREO_NOSTEREO || stereomode >= RAS_IRasterizer::RAS_STEREO_MAXSTEREO)
522                                                 stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO;
523                                         
524                                         if(!strcmp(argv[i], "nostereo"))  // ok, redundant but clear
525                                                 stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO;
526                                         
527                                         // only the hardware pageflip method needs a stereo window
528                                         if(!strcmp(argv[i], "hwpageflip")) {
529                                                 stereomode = RAS_IRasterizer::RAS_STEREO_QUADBUFFERED;
530                                                 stereoWindow = true;
531                                         }
532                                         if(!strcmp(argv[i], "syncdoubling"))
533                                                 stereomode = RAS_IRasterizer::RAS_STEREO_ABOVEBELOW;
534                                         
535                                         if(!strcmp(argv[i], "anaglyph"))
536                                                 stereomode = RAS_IRasterizer::RAS_STEREO_ANAGLYPH;
537                                         
538                                         if(!strcmp(argv[i], "sidebyside"))
539                                                 stereomode = RAS_IRasterizer::RAS_STEREO_SIDEBYSIDE;
540                                         
541                                         if(!strcmp(argv[i], "vinterlace"))
542                                                 stereomode = RAS_IRasterizer::RAS_STEREO_VINTERLACE;
543                                         
544 #if 0
545                                         // future stuff
546                                         if(strcmp(argv[i], "stencil")
547                                                 stereomode = RAS_STEREO_STENCIL;
548 #endif
549                                         
550                                         i++;
551                                         stereoParFound = true;
552                                 }
553                                 else
554                                 {
555                                         error = true;
556                                         printf("error: too few options for stereo argument.\n");
557                                 }
558                                 break;
559                         default:
560                                 printf("Unkown argument: %s\n", argv[i++]);
561                                 break;
562                         }
563                 }
564                 else
565                 {
566                         i++;
567                 }
568         }
569         
570         if ((windowWidth < kMinWindowWidth) || (windowHeight < kMinWindowHeight))
571         {
572                 error = true;
573                 printf("error: window size too small.\n");
574         }
575         
576         if (error )
577         {
578                 usage(argv[0]);
579                 return 0;
580         }
581
582 #ifdef WIN32
583         if (scr_saver_mode != SCREEN_SAVER_MODE_CONFIGURATION)
584 #endif
585         {
586 #ifdef __APPLE__
587                 //SYS_WriteCommandLineInt(syshandle, "show_framerate", 1);
588                 //SYS_WriteCommandLineInt(syshandle, "nomipmap", 1);
589                 //fullScreen = false;           // Can't use full screen
590 #endif
591
592                 if (SYS_GetCommandLineInt(syshandle, "nomipmap", 0))
593                 {
594                         GPU_set_mipmap(0);
595                 }
596                 
597                 // Create the system
598                 if (GHOST_ISystem::createSystem() == GHOST_kSuccess)
599                 {
600                         GHOST_ISystem* system = GHOST_ISystem::getSystem();
601                         assertd(system);
602                         
603                         if (!fullScreenWidth || !fullScreenHeight)
604                                 system->getMainDisplayDimensions(fullScreenWidth, fullScreenHeight);
605                         // process first batch of events. If the user
606                         // drops a file on top off the blenderplayer icon, we 
607                         // recieve an event with the filename
608                         
609                         system->processEvents(0);
610                         
611                         // this bracket is needed for app (see below) to get out
612                         // of scope before GHOST_ISystem::disposeSystem() is called.
613                         {
614                                 int exitcode = KX_EXIT_REQUEST_NO_REQUEST;
615                                 STR_String exitstring = "";
616                                 GPG_Application app(system);
617                                 bool firstTimeRunning = true;
618                                 char filename[FILE_MAXDIR + FILE_MAXFILE];
619                                 char pathname[FILE_MAXDIR + FILE_MAXFILE];
620                                 char *titlename;
621
622                                 get_filename(argc_py_clamped, argv, filename);
623                                 if(filename[0])
624                                         BLI_convertstringcwd(filename);
625                                 
626                                 do
627                                 {
628                                         // Read the Blender file
629                                         BlendFileData *bfd;
630                                         
631                                         // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
632                                         if (exitcode == KX_EXIT_REQUEST_START_OTHER_GAME)
633                                         {
634                                                 char basedpath[240];
635                                                 
636                                                 // base the actuator filename relative to the last file
637                                                 strcpy(basedpath, exitstring.Ptr());
638                                                 BLI_convertstringcode(basedpath, pathname);
639                                                 
640                                                 bfd = load_game_data(basedpath);
641
642                                                 if (!bfd)
643                                                 {
644                                                         // just add "//" in front of it
645                                                         char temppath[242];
646                                                         strcpy(temppath, "//");
647                                                         strcat(temppath, basedpath);
648                                 
649                                                         BLI_convertstringcode(temppath, pathname);
650                                                         bfd = load_game_data(temppath);
651                                                 }
652                                         }
653                                         else
654                                         {
655                                                 bfd = load_game_data(bprogname, filename[0]? filename: NULL);
656                                         }
657                                         
658                                         //::printf("game data loaded from %s\n", filename);
659                                         
660                                         if (!bfd) {
661                                                 usage(argv[0]);
662                                                 error = true;
663                                                 exitcode = KX_EXIT_REQUEST_QUIT_GAME;
664                                         } 
665                                         else 
666                                         {
667 #ifdef WIN32
668 #ifdef NDEBUG
669                                                 if (closeConsole)
670                                                 {
671                                                         //::FreeConsole();    // Close a console window
672                                                 }
673 #endif // NDEBUG
674 #endif // WIN32
675                                                 Main *maggie = bfd->main;
676                                                 Scene *scene = bfd->curscene;
677                                                 G.main = maggie;
678
679                                                 if (firstTimeRunning)
680                                                         G.fileflags  = bfd->fileflags;
681
682                                                 //Seg Fault; icon.c gIcons == 0
683                                                 BKE_icons_init(1);
684                                                 
685                                                 titlename = maggie->name;
686                                                 
687                                                 // Check whether the game should be displayed full-screen
688                                                 if ((!fullScreenParFound) && (!windowParFound))
689                                                 {
690                                                         // Only use file settings when command line did not override
691                                                         if (scene->r.fullscreen) {
692                                                                 //printf("fullscreen option found in Blender file\n");
693                                                                 fullScreen = true;
694                                                                 fullScreenWidth= scene->r.xplay;
695                                                                 fullScreenHeight= scene->r.yplay;
696                                                                 fullScreenFrequency= scene->r.freqplay;
697                                                                 fullScreenBpp = scene->r.depth;
698                                                         }
699                                                         else
700                                                         {
701                                                                 fullScreen = false;
702                                                                 windowWidth = scene->r.xplay;
703                                                                 windowHeight = scene->r.yplay;
704                                                         }
705                                                 }
706                                                 
707                                                 
708                                                 // Check whether the game should be displayed in stereo
709                                                 if (!stereoParFound)
710                                                 {
711                                                         stereomode = (RAS_IRasterizer::StereoMode) scene->r.stereomode;
712                                                         if (stereomode == RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
713                                                                 stereoWindow = true;
714                                                 }
715                                                 
716                                                 //                                      GPG_Application app (system, maggie, startscenename);
717                                                 app.SetGameEngineData(maggie, scene, argc, argv); /* this argc cant be argc_py_clamped, since python uses it */
718                                                 
719                                                 BLI_strncpy(pathname, maggie->name, sizeof(pathname));
720                                                 BLI_strncpy(G.sce, maggie->name, sizeof(G.sce));
721                                                 setGamePythonPath(G.sce);
722
723                                                 if (firstTimeRunning)
724                                                 {
725                                                         firstTimeRunning = false;
726
727                                                         if (fullScreen)
728                                                         {
729 #ifdef WIN32
730                                                                 if (scr_saver_mode == SCREEN_SAVER_MODE_SAVER)
731                                                                 {
732                                                                         app.startScreenSaverFullScreen(fullScreenWidth, fullScreenHeight, fullScreenBpp, fullScreenFrequency,
733                                                                                 stereoWindow, stereomode);
734                                                                 }
735                                                                 else
736 #endif
737                                                                 {
738                                                                         app.startFullScreen(fullScreenWidth, fullScreenHeight, fullScreenBpp, fullScreenFrequency,
739                                                                                 stereoWindow, stereomode);
740                                                                 }
741                                                         }
742                                                         else
743                                                         {
744 #ifdef __APPLE__
745                                                                 // on Mac's we'll show the executable name instead of the 'game.blend' name
746                                                                 char tempname[1024], *appstring;
747                                                                 ::strcpy(tempname, titlename);
748                                                                 
749                                                                 appstring = strstr(tempname, ".app/");
750                                                                 if (appstring) {
751                                                                         appstring[2] = 0;
752                                                                         titlename = &tempname[0];
753                                                                 }
754 #endif
755                                                                 // Strip the path so that we have the name of the game file
756                                                                 STR_String path = titlename;
757 #ifndef WIN32
758                                                                 vector<STR_String> parts = path.Explode('/');
759 #else  // WIN32
760                                                                 vector<STR_String> parts = path.Explode('\\');
761 #endif // WIN32                        
762                                                                 STR_String title;
763                                                                 if (parts.size())
764                                                                 {
765                                                                         title = parts[parts.size()-1];
766                                                                         parts = title.Explode('.');
767                                                                         if (parts.size() > 1)
768                                                                         {
769                                                                                 title = parts[0];
770                                                                         }
771                                                                 }
772                                                                 else
773                                                                 {
774                                                                         title = "blenderplayer";
775                                                                 }
776 #ifdef WIN32
777                                                                 if (scr_saver_mode == SCREEN_SAVER_MODE_PREVIEW)
778                                                                 {
779                                                                         app.startScreenSaverPreview(scr_saver_hwnd, stereoWindow, stereomode);
780                                                                 }
781                                                                 else
782 #endif
783                                                                 {
784                                                                                                                                                                                                                 if (parentWindow != 0)
785                                                                                 app.startEmbeddedWindow(title, parentWindow, stereoWindow, stereomode);
786                                                                         else
787                                                                                 app.startWindow(title, windowLeft, windowTop, windowWidth, windowHeight,
788                                                                                 stereoWindow, stereomode);
789                                                                 }
790                                                         }
791                                                 }
792                                                 else
793                                                 {
794                                                         app.StartGameEngine(stereomode);
795                                                         exitcode = KX_EXIT_REQUEST_NO_REQUEST;
796                                                 }
797                                                 
798                                                 // Add the application as event consumer
799                                                 system->addEventConsumer(&app);
800                                                 
801                                                 // Enter main loop
802                                                 bool run = true;
803                                                 while (run)
804                                                 {
805                                                         system->processEvents(false);
806                                                         system->dispatchEvents();
807                                                         if ((exitcode = app.getExitRequested()))
808                                                         {
809                                                                 run = false;
810                                                                 exitstring = app.getExitString();
811                                                         }
812                                                 }
813                                                 app.StopGameEngine();
814
815                                                 BLO_blendfiledata_free(bfd);
816                                         }
817                                 } while (exitcode == KX_EXIT_REQUEST_RESTART_GAME || exitcode == KX_EXIT_REQUEST_START_OTHER_GAME);
818                         }
819
820                         // Seg Fault; icon.c gIcons == 0
821                         BKE_icons_free();
822
823                         // Dispose the system
824                         GHOST_ISystem::disposeSystem();
825                 } else {
826                         error = true;
827                         printf("error: couldn't create a system.\n");
828                 }
829         }
830
831         free_nodesystem();
832
833         return error ? -1 : 0;
834 }
835
836