svn merge -r37276:38555 https://svn.blender.org/svnroot/bf-blender/trunk/blender .
[blender.git] / source / gameengine / GamePlayer / ghost / GPG_ghost.cpp
1 /*
2 * $Id$
3 *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 /** \file gameengine/GamePlayer/ghost/GPG_ghost.cpp
32  *  \ingroup player
33  */
34
35
36 #include <iostream>
37 #include <math.h>
38
39 #ifdef __linux__
40 #ifdef __alpha__
41 #include <signal.h>
42 #endif /* __alpha__ */
43 #endif /* __linux__ */
44
45 #ifdef __APPLE__
46 // Can't use Carbon right now because of double defined type ID (In Carbon.h and DNA_ID.h, sigh)
47 //#include <Carbon/Carbon.h>
48 //#include <CFBundle.h>
49 #endif // __APPLE__
50 #include "KX_KetsjiEngine.h"
51 #include "KX_PythonInit.h"
52
53 /**********************************
54 * Begin Blender include block
55 **********************************/
56 #ifdef __cplusplus
57 extern "C"
58 {
59 #endif  // __cplusplus
60 #include "MEM_guardedalloc.h"
61 #include "BKE_blender.h"        
62 #include "BKE_global.h" 
63 #include "BKE_icons.h"  
64 #include "BKE_node.h"   
65 #include "BKE_report.h"
66 #include "BKE_library.h"
67 #include "BLI_threads.h"
68 #include "BLI_blenlib.h"
69 #include "DNA_scene_types.h"
70 #include "DNA_userdef_types.h"
71 #include "BLO_readfile.h"
72 #include "BLO_runtime.h"
73 #include "IMB_imbuf.h"
74 #include "BKE_text.h"
75 #include "BKE_sound.h"
76         
77         int GHOST_HACK_getFirstFile(char buf[]);
78         
79 extern char bprogname[];        /* holds a copy of argv[0], from creator.c */
80 extern char btempdir[];         /* use this to store a valid temp directory */
81
82 // For BLF
83 #include "BLF_api.h"
84 extern int datatoc_bfont_ttf_size;
85 extern char datatoc_bfont_ttf[];
86
87 #ifdef __cplusplus
88 }
89 #endif // __cplusplus
90
91 #include "GPU_draw.h"
92
93 /**********************************
94 * End Blender include block
95 **********************************/
96
97 #include "BL_System.h"
98 #include "GPG_Application.h"
99
100 #include "GHOST_ISystem.h"
101 #include "RAS_IRasterizer.h"
102
103 #include "BKE_main.h"
104 #include "BKE_utildefines.h"
105
106 #include "RNA_define.h"
107
108 #ifdef WIN32
109 #include <windows.h>
110 #if !defined(DEBUG)
111 #include <wincon.h>
112 #endif // !defined(DEBUG)
113 #endif // WIN32
114
115 const int kMinWindowWidth = 100;
116 const int kMinWindowHeight = 100;
117
118 char bprogname[FILE_MAX];
119
120 static void mem_error_cb(const char *errorStr)
121 {
122         fprintf(stderr, "%s", errorStr);
123         fflush(stderr);
124 }
125
126 #ifdef WIN32
127 typedef enum 
128 {
129   SCREEN_SAVER_MODE_NONE = 0,
130   SCREEN_SAVER_MODE_PREVIEW,
131   SCREEN_SAVER_MODE_SAVER,
132   SCREEN_SAVER_MODE_CONFIGURATION,
133   SCREEN_SAVER_MODE_PASSWORD,
134 } ScreenSaverMode;
135
136 static ScreenSaverMode scr_saver_mode = SCREEN_SAVER_MODE_NONE;
137 static HWND scr_saver_hwnd = NULL;
138
139 static BOOL scr_saver_init(int argc, char **argv) 
140 {
141         scr_saver_mode = SCREEN_SAVER_MODE_NONE;
142         scr_saver_hwnd = NULL;
143         BOOL ret = FALSE;
144
145         int len = ::strlen(argv[0]);
146         if (len > 4 && !::stricmp(".scr", argv[0] + len - 4))
147         {
148                 scr_saver_mode = SCREEN_SAVER_MODE_CONFIGURATION;
149                 ret = TRUE;
150                 if (argc >= 2)
151                 {
152                         if (argc >= 3)
153                         {
154                                 scr_saver_hwnd = (HWND) ::atoi(argv[2]);
155                         }
156                         if (!::stricmp("/c", argv[1]))
157                         {
158                                 scr_saver_mode = SCREEN_SAVER_MODE_CONFIGURATION;
159                                 if (scr_saver_hwnd == NULL)
160                                         scr_saver_hwnd = ::GetForegroundWindow();
161                         }
162                         else if (!::stricmp("/s", argv[1]))
163                         {
164                                 scr_saver_mode = SCREEN_SAVER_MODE_SAVER;
165                         }
166                         else if (!::stricmp("/a", argv[1]))
167                         {
168                                 scr_saver_mode = SCREEN_SAVER_MODE_PASSWORD;
169                         }
170                         else if (!::stricmp("/p", argv[1])
171                                  || !::stricmp("/l", argv[1]))
172                         {
173                                 scr_saver_mode = SCREEN_SAVER_MODE_PREVIEW;
174                         }
175                 }
176         }
177         return ret;
178 }
179
180 #endif /* WIN32 */
181
182 void usage(const char* program, bool isBlenderPlayer)
183 {
184         const char * consoleoption;
185         const char * filename = "";
186         const char * pathname = "";
187
188 #ifdef _WIN32
189         consoleoption = "-c ";
190 #else
191         consoleoption = "";
192 #endif
193
194         if (isBlenderPlayer) {
195                 filename = "filename.blend";
196 #ifdef _WIN32
197                 pathname = "c:\\";
198 #else
199                 pathname = "//home//user//";
200 #endif
201         }
202         
203         printf("usage:   %s [-w [w h l t]] [-f [fw fh fb ff]] %s[-g gamengineoptions] "
204                 "[-s stereomode] [-m aasamples] %s\n", program, consoleoption, filename);
205         printf("  -h: Prints this command summary\n\n");
206         printf("  -w: display in a window\n");
207         printf("       --Optional parameters--\n"); 
208         printf("       w = window width\n");
209         printf("       h = window height\n\n");
210         printf("       l = window left coordinate\n");
211         printf("       t = window top coordinate\n");
212         printf("       Note: If w or h is defined, both must be defined.\n");
213         printf("          Also, if l or t is defined, all options must be used.\n\n");
214         printf("  -f: start game in full screen mode\n");
215         printf("       --Optional parameters--\n");
216         printf("       fw = full screen mode pixel width\n");
217         printf("       fh = full screen mode pixel height\n\n");
218         printf("       fb = full screen mode bits per pixel\n");
219         printf("       ff = full screen mode frequency\n");
220         printf("       Note: If fw or fh is defined, both must be defined.\n");
221         printf("          Also, if fb is used, fw and fh must be used. ff requires all options.\n\n");
222         printf("  -s: start player in stereo\n");
223         printf("       stereomode: hwpageflip       (Quad buffered shutter glasses)\n");
224         printf("                   syncdoubling     (Above Below)\n");
225         printf("                   sidebyside       (Left Right)\n");
226         printf("                   anaglyph         (Red-Blue glasses)\n");
227         printf("                   vinterlace       (Vertical interlace for autostereo display)\n");
228         printf("                             depending on the type of stereo you want\n\n");
229         printf("  -D: start player in dome mode\n");
230         printf("       --Optional parameters--\n");
231         printf("       angle    = field of view in degrees\n");
232         printf("       tilt     = tilt angle in degrees\n");
233         printf("       warpdata = a file to use for warping the image (absolute path)\n");      
234         printf("       mode: fisheye                (Fisheye)\n");
235         printf("             truncatedfront         (Front-Truncated)\n");
236         printf("             truncatedrear          (Rear-Truncated)\n");
237         printf("             cubemap                (Cube Map)\n");
238         printf("             sphericalpanoramic     (Spherical Panoramic)\n");
239         printf("                             depending on the type of dome you are using\n\n");
240         printf("  -m: maximum anti-aliasing (eg. 2,4,8,16)\n\n");
241         printf("  -i: parent windows ID \n\n");
242 #ifdef _WIN32
243         printf("  -c: keep console window open\n\n");
244 #endif
245         printf("  -d: turn debugging on\n\n");
246         printf("  -g: game engine options:\n\n");
247         printf("       Name                       Default      Description\n");
248         printf("       ------------------------------------------------------------------------\n");
249         printf("       fixedtime                      0         \"Enable all frames\"\n");
250         printf("       nomipmap                       0         Disable mipmaps\n");
251         printf("       show_framerate                 0         Show the frame rate\n");
252         printf("       show_properties                0         Show debug properties\n");
253         printf("       show_profile                   0         Show profiling information\n");
254         printf("       blender_material               0         Enable material settings\n");
255         printf("       ignore_deprecation_warnings    1         Ignore deprecation warnings\n");
256         printf("\n");
257         printf("  - : all arguments after this are ignored, allowing python to access them from sys.argv\n");
258         printf("\n");
259         printf("example: %s -w 320 200 10 10 -g noaudio%s%s\n", program, pathname, filename);
260         printf("example: %s -g show_framerate = 0 %s%s\n", program, pathname, filename);
261         printf("example: %s -i 232421 -m 16 %s%s\n\n", program, pathname, filename);
262 }
263
264 static void get_filename(int argc, char **argv, char *filename)
265 {
266 #ifdef __APPLE__
267 /* On Mac we park the game file (called game.blend) in the application bundle.
268 * The executable is located in the bundle as well.
269 * Therefore, we can locate the game relative to the executable.
270         */
271         int srclen = ::strlen(argv[0]);
272         int len = 0;
273         char *gamefile = NULL;
274         
275         filename[0] = '\0';
276
277         if (argc > 1) {
278                 if (BLI_exists(argv[argc-1])) {
279                         BLI_strncpy(filename, argv[argc-1], FILE_MAXDIR + FILE_MAXFILE);
280                 }
281                 if (::strncmp(argv[argc-1], "-psn_", 5)==0) {
282                         static char firstfilebuf[512];
283                         if (GHOST_HACK_getFirstFile(firstfilebuf)) {
284                                 BLI_strncpy(filename, firstfilebuf, FILE_MAXDIR + FILE_MAXFILE);
285                         }
286                 }                        
287         }
288         
289         srclen -= ::strlen("MacOS/blenderplayer");
290         if (srclen > 0) {
291                 len = srclen + ::strlen("Resources/game.blend"); 
292                 gamefile = new char [len + 1];
293                 ::strcpy(gamefile, argv[0]);
294                 ::strcpy(gamefile + srclen, "Resources/game.blend");
295                 //::printf("looking for file: %s\n", filename);
296                 
297                 if (BLI_exists(gamefile))
298                         BLI_strncpy(filename, gamefile, FILE_MAXDIR + FILE_MAXFILE);
299
300                 delete [] gamefile;
301         }
302         
303 #else
304         filename[0] = '\0';
305
306         if(argc > 1)
307                 BLI_strncpy(filename, argv[argc-1], FILE_MAXDIR + FILE_MAXFILE);
308 #endif // !_APPLE
309 }
310
311 static BlendFileData *load_game_data(char *progname, char *filename = NULL, char *relativename = NULL)
312 {
313         ReportList reports;
314         BlendFileData *bfd = NULL;
315
316         BKE_reports_init(&reports, RPT_STORE);
317         
318         /* try to load ourself, will only work if we are a runtime */
319         if (BLO_is_a_runtime(progname)) {
320                 bfd= BLO_read_runtime(progname, &reports);
321                 if (bfd) {
322                         bfd->type= BLENFILETYPE_RUNTIME;
323                         strcpy(bfd->main->name, progname);
324                 }
325         } else {
326                 bfd= BLO_read_from_file(progname, &reports);
327         }
328         
329         if (!bfd && filename) {
330                 bfd = load_game_data(filename);
331                 if (!bfd) {
332                         printf("Loading %s failed: ", filename);
333                         BKE_reports_print(&reports, RPT_ERROR);
334                 }
335         }
336
337         BKE_reports_clear(&reports);
338         
339         return bfd;
340 }
341
342 int main(int argc, char** argv)
343 {
344         int i;
345         int argc_py_clamped= argc; /* use this so python args can be added after ' - ' */
346         bool error = false;
347         SYS_SystemHandle syshandle = SYS_GetSystem();
348         bool fullScreen = false;
349         bool fullScreenParFound = false;
350         bool windowParFound = false;
351 #ifdef WIN32
352         bool closeConsole = true;
353 #endif
354         RAS_IRasterizer::StereoMode stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO;
355         bool stereoWindow = false;
356         bool stereoParFound = false;
357         int stereoFlag = STEREO_NOSTEREO;
358         int domeFov = -1;
359         int domeTilt = -200;
360         int domeMode = 0;
361         char* domeWarp = NULL;
362         Text *domeText  = NULL;
363         int windowLeft = 100;
364         int windowTop = 100;
365         int windowWidth = 640;
366         int windowHeight = 480;
367         GHOST_TUns32 fullScreenWidth = 0;
368         GHOST_TUns32 fullScreenHeight= 0;
369         int fullScreenBpp = 32;
370         int fullScreenFrequency = 60;
371         GHOST_TEmbedderWindowID parentWindow = 0;
372         bool isBlenderPlayer = false;
373         int validArguments=0;
374         GHOST_TUns16 aasamples = 0;
375         
376 #ifdef __linux__
377 #ifdef __alpha__
378         signal (SIGFPE, SIG_IGN);
379 #endif /* __alpha__ */
380 #endif /* __linux__ */
381         BLI_where_am_i(bprogname, sizeof(bprogname), argv[0]);
382 #ifdef __APPLE__
383     // Can't use Carbon right now because of double defined type ID (In Carbon.h and DNA_ID.h, sigh)
384     /*
385     IBNibRef            nibRef;
386     WindowRef           window;
387     OSStatus            err;
388         
389           // Create a Nib reference passing the name of the nib file (without the .nib extension)
390           // CreateNibReference only searches into the application bundle.
391           err = ::CreateNibReference(CFSTR("main"), &nibRef);
392           if (err) return -1;
393           
394                 // Once the nib reference is created, set the menu bar. "MainMenu" is the name of the menu bar
395                 // object. This name is set in InterfaceBuilder when the nib is created.
396                 err = ::SetMenuBarFromNib(nibRef, CFSTR("MenuBar"));
397                 if (err) return -1;
398                 
399                   // We don't need the nib reference anymore.
400                   ::DisposeNibReference(nibRef);
401     */
402 #endif // __APPLE__
403         
404         // We don't use threads directly in the BGE, but we need to call this so things like
405         // freeing up GPU_Textures works correctly.
406         BLI_threadapi_init();
407         
408         RNA_init();
409
410         init_nodesystem();
411         
412         initglobals();
413
414         // We load our own G.main, so free the one that initglobals() gives us
415         free_main(G.main);
416         G.main = NULL;
417
418         IMB_init();
419
420         // Setup builtin font for BLF (mostly copied from creator.c, wm_init_exit.c and interface_style.c)
421         BLF_init(11, U.dpi);
422         BLF_lang_init();
423         BLF_load_mem("default", (unsigned char*)datatoc_bfont_ttf, datatoc_bfont_ttf_size);
424  
425         // Parse command line options
426 #if defined(DEBUG)
427         printf("argv[0] = '%s'\n", argv[0]);
428 #endif
429
430 #ifdef WIN32
431         if (scr_saver_init(argc, argv))
432         {
433                 switch (scr_saver_mode)
434                 {
435                 case SCREEN_SAVER_MODE_CONFIGURATION:
436                         MessageBox(scr_saver_hwnd, "This screen saver has no options that you can set", "Screen Saver", MB_OK);
437                         break;
438                 case SCREEN_SAVER_MODE_PASSWORD:
439                         /* This is W95 only, which we currently do not support.
440                            Fall-back to normal screen saver behaviour in that case... */
441                 case SCREEN_SAVER_MODE_SAVER:
442                         fullScreen = true;
443                         fullScreenParFound = true;
444                         break;
445
446                 case SCREEN_SAVER_MODE_PREVIEW:
447                         /* This will actually be handled somewhere below... */
448                         break;
449                 }
450         }
451 #endif
452         // XXX add the ability to change this values to the command line parsing.
453         U.mixbufsize = 2048;
454         U.audiodevice = 2;
455         U.audiorate = 44100;
456         U.audioformat = 0x24;
457         U.audiochannels = 2;
458
459         // XXX this one too
460         U.anisotropic_filter = 2;
461
462         sound_init_once();
463
464         /* if running blenderplayer the last argument can't be parsed since it has to be the filename. */
465         isBlenderPlayer = !BLO_is_a_runtime(argv[0]);
466         if (isBlenderPlayer)
467                 validArguments = argc - 1;
468         else
469                 validArguments = argc;
470
471         for (i = 1; (i < validArguments) && !error 
472 #ifdef WIN32
473                 && scr_saver_mode == SCREEN_SAVER_MODE_NONE
474 #endif
475                 ;)
476
477         {
478 #if defined(DEBUG)
479                 printf("argv[%d] = '%s'   , %i\n", i, argv[i],argc);
480 #endif
481                 if (argv[i][0] == '-')
482                 {
483                         /* ignore all args after " - ", allow python to have own args */
484                         if (argv[i][1]=='\0') {
485                                 argc_py_clamped= i;
486                                 break;
487                         }
488                         
489                         switch (argv[i][1])
490                         {
491                         case 'g':
492                                 // Parse game options
493                                 {
494                                         i++;
495                                         if (i <= validArguments)
496                                         {
497                                                 char* paramname = argv[i];
498                                                 // Check for single value versus assignment
499                                                 if (i+1 <= validArguments && (*(argv[i+1]) == '='))
500                                                 {
501                                                         i++;
502                                                         if (i + 1 <= validArguments)
503                                                         {
504                                                                 i++;
505                                                                 // Assignment
506                                                                 SYS_WriteCommandLineInt(syshandle, paramname, atoi(argv[i]));
507                                                                 SYS_WriteCommandLineFloat(syshandle, paramname, atof(argv[i]));
508                                                                 SYS_WriteCommandLineString(syshandle, paramname, argv[i]);
509 #if defined(DEBUG)
510                                                                 printf("%s = '%s'\n", paramname, argv[i]);
511 #endif
512                                                                 i++;
513                                                         }
514                                                         else
515                                                         {
516                                                                 error = true;
517                                                                 printf("error: argument assignment %s without value.\n", paramname);
518                                                         }
519                                                 }
520                                                 else
521                                                 {
522 //                                                      SYS_WriteCommandLineInt(syshandle, argv[i++], 1);
523                                                 }
524                                         }
525                                 }
526                                 break;
527
528                         case 'd':
529                                 i++;
530                                 G.f |= G_DEBUG;     /* std output printf's */
531                                 MEM_set_memory_debug();
532                                 break;
533
534                         case 'f':
535                                 i++;
536                                 fullScreen = true;
537                                 fullScreenParFound = true;
538                                 if ((i + 2) <= validArguments && argv[i][0] != '-' && argv[i+1][0] != '-')
539                                 {
540                                         fullScreenWidth = atoi(argv[i++]);
541                                         fullScreenHeight = atoi(argv[i++]);
542                                         if ((i + 1) <= validArguments && argv[i][0] != '-')
543                                         {
544                                                 fullScreenBpp = atoi(argv[i++]);
545                                                 if ((i + 1) <= validArguments && argv[i][0] != '-')
546                                                         fullScreenFrequency = atoi(argv[i++]);
547                                         }
548                                 }
549                                 break;
550                         case 'w':
551                                 // Parse window position and size options
552                                 i++;
553                                 fullScreen = false;
554                                 windowParFound = true;
555
556                                 if ((i + 2) <= validArguments && argv[i][0] != '-' && argv[i+1][0] != '-')
557                                 {
558                                         windowWidth = atoi(argv[i++]);
559                                         windowHeight = atoi(argv[i++]);
560                                         if ((i + 2) <= validArguments && argv[i][0] != '-' && argv[i+1][0] != '-')
561                                         {
562                                                 windowLeft = atoi(argv[i++]);
563                                                 windowTop = atoi(argv[i++]);
564                                         }
565                                 }
566                                 break;
567                                         
568                         case 'h':
569                                 usage(argv[0], isBlenderPlayer);
570                                 return 0;
571                                 break;
572                         case 'i':
573                                 i++;
574                                 if ( (i + 1) <= validArguments )
575                                         parentWindow = atoi(argv[i++]); 
576                                 else {
577                                         error = true;
578                                         printf("error: too few options for parent window argument.\n");
579                                 }
580 #if defined(DEBUG)
581                                 printf("XWindows ID = %d\n", parentWindow);
582 #endif // defined(DEBUG)
583                                 break;
584                         case 'm':
585                                 i++;
586                                 if ((i+1) <= validArguments )
587                                 aasamples = atoi(argv[i++]);
588                                 break;
589                         case 'c':
590                                 i++;
591 #ifdef WIN32
592                                 closeConsole = false;
593 #endif
594                                 break;
595                         case 's':  // stereo
596                                 i++;
597                                 if ((i + 1) <= validArguments)
598                                 {
599                                         stereomode = (RAS_IRasterizer::StereoMode) atoi(argv[i]);
600                                         if (stereomode < RAS_IRasterizer::RAS_STEREO_NOSTEREO || stereomode >= RAS_IRasterizer::RAS_STEREO_MAXSTEREO)
601                                                 stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO;
602                                         
603                                         if(!strcmp(argv[i], "nostereo"))  // ok, redundant but clear
604                                                 stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO;
605                                         
606                                         // only the hardware pageflip method needs a stereo window
607                                         else if(!strcmp(argv[i], "hwpageflip")) {
608                                                 stereomode = RAS_IRasterizer::RAS_STEREO_QUADBUFFERED;
609                                                 stereoWindow = true;
610                                         }
611                                         else if(!strcmp(argv[i], "syncdoubling"))
612                                                 stereomode = RAS_IRasterizer::RAS_STEREO_ABOVEBELOW;
613                                         
614                                         else if(!strcmp(argv[i], "anaglyph"))
615                                                 stereomode = RAS_IRasterizer::RAS_STEREO_ANAGLYPH;
616                                         
617                                         else if(!strcmp(argv[i], "sidebyside"))
618                                                 stereomode = RAS_IRasterizer::RAS_STEREO_SIDEBYSIDE;
619                                         
620                                         else if(!strcmp(argv[i], "vinterlace"))
621                                                 stereomode = RAS_IRasterizer::RAS_STEREO_VINTERLACE;
622                                         
623 #if 0
624                                         // future stuff
625                                         else if(!strcmp(argv[i], "stencil")
626                                                 stereomode = RAS_STEREO_STENCIL;
627 #endif
628                                         
629                                         i++;
630                                         stereoParFound = true;
631                                         stereoFlag = STEREO_ENABLED;
632                                 }
633                                 else
634                                 {
635                                         error = true;
636                                         printf("error: too few options for stereo argument.\n");
637                                 }
638                                 break;
639                         case 'D':
640                                 stereoFlag = STEREO_DOME;
641                                 stereomode = RAS_IRasterizer::RAS_STEREO_DOME;
642                                 i++;
643                                 if ((i + 1) <= validArguments)
644                                 {
645                                         if(!strcmp(argv[i], "angle")){
646                                                 i++;
647                                                 domeFov = atoi(argv[i++]);
648                                         }
649                                         if(!strcmp(argv[i], "tilt")){
650                                                 i++;
651                                                 domeTilt = atoi(argv[i++]);
652                                         }
653                                         if(!strcmp(argv[i], "warpdata")){
654                                                 i++;
655                                                 domeWarp = argv[i++];
656                                         }
657                                         if(!strcmp(argv[i], "mode")){
658                                                 i++;
659                                                 if(!strcmp(argv[i], "fisheye"))
660                                                         domeMode = DOME_FISHEYE;
661                                                         
662                                                 else if(!strcmp(argv[i], "truncatedfront"))
663                                                         domeMode = DOME_TRUNCATED_FRONT;
664                                                         
665                                                 else if(!strcmp(argv[i], "truncatedrear"))
666                                                         domeMode = DOME_TRUNCATED_REAR;
667                                                         
668                                                 else if(!strcmp(argv[i], "cubemap"))
669                                                         domeMode = DOME_ENVMAP;
670                                                         
671                                                 else if(!strcmp(argv[i], "sphericalpanoramic"))
672                                                         domeMode = DOME_PANORAM_SPH;
673
674                                                 else
675                                                         printf("error: %s is not a valid dome mode.\n", argv[i]);
676                                         }
677                                         i++;
678                                 }
679                                 break;
680                         default:
681                                 printf("Unknown argument: %s\n", argv[i++]);
682                                 break;
683                         }
684                 }
685                 else
686                 {
687                         i++;
688                 }
689         }
690         
691         if ((windowWidth < kMinWindowWidth) || (windowHeight < kMinWindowHeight))
692         {
693                 error = true;
694                 printf("error: window size too small.\n");
695         }
696         
697         if (error )
698         {
699                 usage(argv[0], isBlenderPlayer);
700                 return 0;
701         }
702
703 #ifdef WIN32
704         if (scr_saver_mode != SCREEN_SAVER_MODE_CONFIGURATION)
705 #endif
706         {
707 #ifdef __APPLE__
708                 //SYS_WriteCommandLineInt(syshandle, "show_framerate", 1);
709                 //SYS_WriteCommandLineInt(syshandle, "nomipmap", 1);
710                 //fullScreen = false;           // Can't use full screen
711 #endif
712
713                 if (SYS_GetCommandLineInt(syshandle, "nomipmap", 0))
714                 {
715                         GPU_set_mipmap(0);
716                 }
717
718                 GPU_set_anisotropic(U.anisotropic_filter);
719                 
720                 // Create the system
721                 if (GHOST_ISystem::createSystem() == GHOST_kSuccess)
722                 {
723                         GHOST_ISystem* system = GHOST_ISystem::getSystem();
724                         assertd(system);
725                         
726                         if (!fullScreenWidth || !fullScreenHeight)
727                                 system->getMainDisplayDimensions(fullScreenWidth, fullScreenHeight);
728                         // process first batch of events. If the user
729                         // drops a file on top off the blenderplayer icon, we 
730                         // receive an event with the filename
731                         
732                         system->processEvents(0);
733                         
734                         // this bracket is needed for app (see below) to get out
735                         // of scope before GHOST_ISystem::disposeSystem() is called.
736                         {
737                                 int exitcode = KX_EXIT_REQUEST_NO_REQUEST;
738                                 STR_String exitstring = "";
739                                 GPG_Application app(system);
740                                 bool firstTimeRunning = true;
741                                 char filename[FILE_MAXDIR + FILE_MAXFILE];
742                                 char pathname[FILE_MAXDIR + FILE_MAXFILE];
743                                 char *titlename;
744
745                                 get_filename(argc_py_clamped, argv, filename);
746                                 if(filename[0])
747                                         BLI_path_cwd(filename);
748                                 
749                                 do
750                                 {
751                                         // Read the Blender file
752                                         BlendFileData *bfd;
753                                         
754                                         // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
755                                         if (exitcode == KX_EXIT_REQUEST_START_OTHER_GAME)
756                                         {
757                                                 char basedpath[240];
758                                                 
759                                                 // base the actuator filename relative to the last file
760                                                 strcpy(basedpath, exitstring.Ptr());
761                                                 BLI_path_abs(basedpath, pathname);
762                                                 
763                                                 bfd = load_game_data(basedpath);
764
765                                                 if (!bfd)
766                                                 {
767                                                         // just add "//" in front of it
768                                                         char temppath[242];
769                                                         strcpy(temppath, "//");
770                                                         strcat(temppath, basedpath);
771                                 
772                                                         BLI_path_abs(temppath, pathname);
773                                                         bfd = load_game_data(temppath);
774                                                 }
775                                         }
776                                         else
777                                         {
778                                                 bfd = load_game_data(bprogname, filename[0]? filename: NULL);
779                                         }
780                                         
781                                         //::printf("game data loaded from %s\n", filename);
782                                         
783                                         if (!bfd) {
784                                                 usage(argv[0], isBlenderPlayer);
785                                                 error = true;
786                                                 exitcode = KX_EXIT_REQUEST_QUIT_GAME;
787                                         } 
788                                         else 
789                                         {
790 #ifdef WIN32
791 #if !defined(DEBUG)
792                                                 if (closeConsole)
793                                                 {
794                                                         //::FreeConsole();    // Close a console window
795                                                 }
796 #endif // !defined(DEBUG)
797 #endif // WIN32
798                                                 Main *maggie = bfd->main;
799                                                 Scene *scene = bfd->curscene;
800                                                 G.main = maggie;
801
802                                                 if (firstTimeRunning)
803                                                         G.fileflags  = bfd->fileflags;
804
805                                                 //Seg Fault; icon.c gIcons == 0
806                                                 BKE_icons_init(1);
807                                                 
808                                                 titlename = maggie->name;
809                                                 
810                                                 // Check whether the game should be displayed full-screen
811                                                 if ((!fullScreenParFound) && (!windowParFound))
812                                                 {
813                                                         // Only use file settings when command line did not override
814                                                         if (scene->gm.fullscreen) {
815                                                                 //printf("fullscreen option found in Blender file\n");
816                                                                 fullScreen = true;
817                                                                 fullScreenWidth= scene->gm.xplay;
818                                                                 fullScreenHeight= scene->gm.yplay;
819                                                                 fullScreenFrequency= scene->gm.freqplay;
820                                                                 fullScreenBpp = scene->gm.depth;
821                                                         }
822                                                         else
823                                                         {
824                                                                 fullScreen = false;
825                                                                 windowWidth = scene->gm.xplay;
826                                                                 windowHeight = scene->gm.yplay;
827                                                         }
828                                                 }
829                                                 
830                                                 
831                                                 // Check whether the game should be displayed in stereo
832                                                 if (!stereoParFound)
833                                                 {
834                                                         if(scene->gm.stereoflag == STEREO_ENABLED){
835                                                                 stereomode = (RAS_IRasterizer::StereoMode) scene->gm.stereomode;
836                                                                 if (stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
837                                                                         stereoWindow = true;
838                                                         }
839                                                 }
840                                                 else
841                                                         scene->gm.stereoflag = STEREO_ENABLED;
842
843                                                 if (stereoFlag == STEREO_DOME){
844                                                         stereomode = RAS_IRasterizer::RAS_STEREO_DOME;
845                                                         scene->gm.stereoflag = STEREO_DOME;
846                                                         if (domeFov > 89)
847                                                                 scene->gm.dome.angle = domeFov;
848                                                         if (domeTilt > -180)
849                                                                 scene->gm.dome.tilt = domeTilt;
850                                                         if (domeMode > 0)
851                                                                 scene->gm.dome.mode = domeMode;
852                                                         if (domeWarp)
853                                                         {
854                                                                 //XXX to do: convert relative to absolute path
855                                                                 domeText= add_text(domeWarp, "");
856                                                                 if(!domeText)
857                                                                         printf("error: invalid warpdata text file - %s\n", domeWarp);
858                                                                 else
859                                                                         scene->gm.dome.warptext = domeText;
860                                                         }
861                                                 }
862                                                 
863                                                 //                                      GPG_Application app (system, maggie, startscenename);
864                                                 app.SetGameEngineData(maggie, scene, argc, argv); /* this argc cant be argc_py_clamped, since python uses it */
865                                                 BLI_strncpy(pathname, maggie->name, sizeof(pathname));
866                                                 if(G.main != maggie) {
867                                                         BLI_strncpy(G.main->name, maggie->name, sizeof(G.main->name));
868                                                 }
869 #ifdef WITH_PYTHON
870                                                 setGamePythonPath(G.main->name);
871 #endif
872                                                 if (firstTimeRunning)
873                                                 {
874                                                         firstTimeRunning = false;
875
876                                                         if (fullScreen)
877                                                         {
878 #ifdef WIN32
879                                                                 if (scr_saver_mode == SCREEN_SAVER_MODE_SAVER)
880                                                                 {
881                                                                         app.startScreenSaverFullScreen(fullScreenWidth, fullScreenHeight, fullScreenBpp, fullScreenFrequency,
882                                                                                 stereoWindow, stereomode, aasamples);
883                                                                 }
884                                                                 else
885 #endif
886                                                                 {
887                                                                         app.startFullScreen(fullScreenWidth, fullScreenHeight, fullScreenBpp, fullScreenFrequency,
888                                                                                 stereoWindow, stereomode, aasamples);
889                                                                 }
890                                                         }
891                                                         else
892                                                         {
893 #ifdef __APPLE__
894                                                                 // on Mac's we'll show the executable name instead of the 'game.blend' name
895                                                                 char tempname[1024], *appstring;
896                                                                 ::strcpy(tempname, titlename);
897                                                                 
898                                                                 appstring = strstr(tempname, ".app/");
899                                                                 if (appstring) {
900                                                                         appstring[2] = 0;
901                                                                         titlename = &tempname[0];
902                                                                 }
903 #endif
904                                                                 // Strip the path so that we have the name of the game file
905                                                                 STR_String path = titlename;
906 #ifndef WIN32
907                                                                 vector<STR_String> parts = path.Explode('/');
908 #else  // WIN32
909                                                                 vector<STR_String> parts = path.Explode('\\');
910 #endif // WIN32                        
911                                                                 STR_String title;
912                                                                 if (parts.size())
913                                                                 {
914                                                                         title = parts[parts.size()-1];
915                                                                         parts = title.Explode('.');
916                                                                         if (parts.size() > 1)
917                                                                         {
918                                                                                 title = parts[0];
919                                                                         }
920                                                                 }
921                                                                 else
922                                                                 {
923                                                                         title = "blenderplayer";
924                                                                 }
925 #ifdef WIN32
926                                                                 if (scr_saver_mode == SCREEN_SAVER_MODE_PREVIEW)
927                                                                 {
928                                                                         app.startScreenSaverPreview(scr_saver_hwnd, stereoWindow, stereomode, aasamples);
929                                                                 }
930                                                                 else
931 #endif
932                                                                 {
933                                                                                                                                                                                                                 if (parentWindow != 0)
934                                                                                 app.startEmbeddedWindow(title, parentWindow, stereoWindow, stereomode, aasamples);
935                                                                         else
936                                                                                 app.startWindow(title, windowLeft, windowTop, windowWidth, windowHeight,
937                                                                                 stereoWindow, stereomode, aasamples);
938                                                                 }
939                                                         }
940                                                 }
941                                                 else
942                                                 {
943                                                         app.StartGameEngine(stereomode);
944                                                         exitcode = KX_EXIT_REQUEST_NO_REQUEST;
945                                                 }
946                                                 
947                                                 // Add the application as event consumer
948                                                 system->addEventConsumer(&app);
949                                                 
950                                                 // Enter main loop
951                                                 bool run = true;
952                                                 while (run)
953                                                 {
954                                                         system->processEvents(false);
955                                                         system->dispatchEvents();
956                                                         if ((exitcode = app.getExitRequested()))
957                                                         {
958                                                                 run = false;
959                                                                 exitstring = app.getExitString();
960                                                         }
961                                                 }
962                                                 app.StopGameEngine();
963
964                                                 /* 'app' is freed automatic when out of scope. 
965                                                  * removal is needed else the system will free an already freed value */
966                                                 system->removeEventConsumer(&app);
967
968                                                 BLO_blendfiledata_free(bfd);
969                                         }
970                                 } while (exitcode == KX_EXIT_REQUEST_RESTART_GAME || exitcode == KX_EXIT_REQUEST_START_OTHER_GAME);
971                         }
972
973                         // Seg Fault; icon.c gIcons == 0
974                         BKE_icons_free();
975
976                         // Dispose the system
977                         GHOST_ISystem::disposeSystem();
978                 } else {
979                         error = true;
980                         printf("error: couldn't create a system.\n");
981                 }
982         }
983
984         // Cleanup
985         RNA_exit();
986         BLF_exit();
987         IMB_exit();
988         free_nodesystem();
989
990         SYS_DeleteSystem(syshandle);
991
992         int totblock= MEM_get_memory_blocks_in_use();
993         if(totblock!=0) {
994                 printf("Error Totblock: %d\n",totblock);
995                 MEM_set_error_callback(mem_error_cb);
996                 MEM_printmemlist();
997         }
998
999         return error ? -1 : 0;
1000 }
1001
1002