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