i18n: code clean-up
[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_lang_encoding("");
424         BLF_lang_set("");
425
426         BLF_load_mem("default", (unsigned char*)datatoc_bfont_ttf, datatoc_bfont_ttf_size);
427
428         // Parse command line options
429 #if defined(DEBUG)
430         printf("argv[0] = '%s'\n", argv[0]);
431 #endif
432
433 #ifdef WIN32
434         if (scr_saver_init(argc, argv))
435         {
436                 switch (scr_saver_mode)
437                 {
438                 case SCREEN_SAVER_MODE_CONFIGURATION:
439                         MessageBox(scr_saver_hwnd, "This screen saver has no options that you can set", "Screen Saver", MB_OK);
440                         break;
441                 case SCREEN_SAVER_MODE_PASSWORD:
442                         /* This is W95 only, which we currently do not support.
443                            Fall-back to normal screen saver behaviour in that case... */
444                 case SCREEN_SAVER_MODE_SAVER:
445                         fullScreen = true;
446                         fullScreenParFound = true;
447                         break;
448
449                 case SCREEN_SAVER_MODE_PREVIEW:
450                         /* This will actually be handled somewhere below... */
451                         break;
452                 }
453         }
454 #endif
455         // XXX add the ability to change this values to the command line parsing.
456         U.mixbufsize = 2048;
457         U.audiodevice = 2;
458         U.audiorate = 44100;
459         U.audioformat = 0x24;
460         U.audiochannels = 2;
461
462         // XXX this one too
463         U.anisotropic_filter = 2;
464
465         sound_init_once();
466
467         /* if running blenderplayer the last argument can't be parsed since it has to be the filename. */
468         isBlenderPlayer = !BLO_is_a_runtime(argv[0]);
469         if (isBlenderPlayer)
470                 validArguments = argc - 1;
471         else
472                 validArguments = argc;
473
474         for (i = 1; (i < validArguments) && !error 
475 #ifdef WIN32
476                 && scr_saver_mode == SCREEN_SAVER_MODE_NONE
477 #endif
478                 ;)
479
480         {
481 #if defined(DEBUG)
482                 printf("argv[%d] = '%s'   , %i\n", i, argv[i],argc);
483 #endif
484                 if (argv[i][0] == '-')
485                 {
486                         /* ignore all args after " - ", allow python to have own args */
487                         if (argv[i][1]=='\0') {
488                                 argc_py_clamped= i;
489                                 break;
490                         }
491                         
492                         switch (argv[i][1])
493                         {
494                         case 'g':
495                                 // Parse game options
496                                 {
497                                         i++;
498                                         if (i <= validArguments)
499                                         {
500                                                 char* paramname = argv[i];
501                                                 // Check for single value versus assignment
502                                                 if (i+1 <= validArguments && (*(argv[i+1]) == '='))
503                                                 {
504                                                         i++;
505                                                         if (i + 1 <= validArguments)
506                                                         {
507                                                                 i++;
508                                                                 // Assignment
509                                                                 SYS_WriteCommandLineInt(syshandle, paramname, atoi(argv[i]));
510                                                                 SYS_WriteCommandLineFloat(syshandle, paramname, atof(argv[i]));
511                                                                 SYS_WriteCommandLineString(syshandle, paramname, argv[i]);
512 #if defined(DEBUG)
513                                                                 printf("%s = '%s'\n", paramname, argv[i]);
514 #endif
515                                                                 i++;
516                                                         }
517                                                         else
518                                                         {
519                                                                 error = true;
520                                                                 printf("error: argument assignment %s without value.\n", paramname);
521                                                         }
522                                                 }
523                                                 else
524                                                 {
525 //                                                      SYS_WriteCommandLineInt(syshandle, argv[i++], 1);
526                                                 }
527                                         }
528                                 }
529                                 break;
530
531                         case 'd':
532                                 i++;
533                                 G.f |= G_DEBUG;     /* std output printf's */
534                                 MEM_set_memory_debug();
535                                 break;
536
537                         case 'f':
538                                 i++;
539                                 fullScreen = true;
540                                 fullScreenParFound = true;
541                                 if ((i + 2) <= validArguments && argv[i][0] != '-' && argv[i+1][0] != '-')
542                                 {
543                                         fullScreenWidth = atoi(argv[i++]);
544                                         fullScreenHeight = atoi(argv[i++]);
545                                         if ((i + 1) <= validArguments && argv[i][0] != '-')
546                                         {
547                                                 fullScreenBpp = atoi(argv[i++]);
548                                                 if ((i + 1) <= validArguments && argv[i][0] != '-')
549                                                         fullScreenFrequency = atoi(argv[i++]);
550                                         }
551                                 }
552                                 break;
553                         case 'w':
554                                 // Parse window position and size options
555                                 i++;
556                                 fullScreen = false;
557                                 windowParFound = true;
558
559                                 if ((i + 2) <= validArguments && argv[i][0] != '-' && argv[i+1][0] != '-')
560                                 {
561                                         windowWidth = atoi(argv[i++]);
562                                         windowHeight = atoi(argv[i++]);
563                                         if ((i + 2) <= validArguments && argv[i][0] != '-' && argv[i+1][0] != '-')
564                                         {
565                                                 windowLeft = atoi(argv[i++]);
566                                                 windowTop = atoi(argv[i++]);
567                                         }
568                                 }
569                                 break;
570                                         
571                         case 'h':
572                                 usage(argv[0], isBlenderPlayer);
573                                 return 0;
574                                 break;
575                         case 'i':
576                                 i++;
577                                 if ( (i + 1) <= validArguments )
578                                         parentWindow = atoi(argv[i++]); 
579                                 else {
580                                         error = true;
581                                         printf("error: too few options for parent window argument.\n");
582                                 }
583 #if defined(DEBUG)
584                                 printf("XWindows ID = %d\n", parentWindow);
585 #endif // defined(DEBUG)
586                                 break;
587                         case 'm':
588                                 i++;
589                                 if ((i+1) <= validArguments )
590                                 aasamples = atoi(argv[i++]);
591                                 break;
592                         case 'c':
593                                 i++;
594 #ifdef WIN32
595                                 closeConsole = false;
596 #endif
597                                 break;
598                         case 's':  // stereo
599                                 i++;
600                                 if ((i + 1) <= validArguments)
601                                 {
602                                         stereomode = (RAS_IRasterizer::StereoMode) atoi(argv[i]);
603                                         if (stereomode < RAS_IRasterizer::RAS_STEREO_NOSTEREO || stereomode >= RAS_IRasterizer::RAS_STEREO_MAXSTEREO)
604                                                 stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO;
605                                         
606                                         if(!strcmp(argv[i], "nostereo"))  // ok, redundant but clear
607                                                 stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO;
608                                         
609                                         // only the hardware pageflip method needs a stereo window
610                                         else if(!strcmp(argv[i], "hwpageflip")) {
611                                                 stereomode = RAS_IRasterizer::RAS_STEREO_QUADBUFFERED;
612                                                 stereoWindow = true;
613                                         }
614                                         else if(!strcmp(argv[i], "syncdoubling"))
615                                                 stereomode = RAS_IRasterizer::RAS_STEREO_ABOVEBELOW;
616                                         
617                                         else if(!strcmp(argv[i], "anaglyph"))
618                                                 stereomode = RAS_IRasterizer::RAS_STEREO_ANAGLYPH;
619                                         
620                                         else if(!strcmp(argv[i], "sidebyside"))
621                                                 stereomode = RAS_IRasterizer::RAS_STEREO_SIDEBYSIDE;
622                                         
623                                         else if(!strcmp(argv[i], "vinterlace"))
624                                                 stereomode = RAS_IRasterizer::RAS_STEREO_VINTERLACE;
625                                         
626 #if 0
627                                         // future stuff
628                                         else if(!strcmp(argv[i], "stencil")
629                                                 stereomode = RAS_STEREO_STENCIL;
630 #endif
631                                         
632                                         i++;
633                                         stereoParFound = true;
634                                         stereoFlag = STEREO_ENABLED;
635                                 }
636                                 else
637                                 {
638                                         error = true;
639                                         printf("error: too few options for stereo argument.\n");
640                                 }
641                                 break;
642                         case 'D':
643                                 stereoFlag = STEREO_DOME;
644                                 stereomode = RAS_IRasterizer::RAS_STEREO_DOME;
645                                 i++;
646                                 if ((i + 1) <= validArguments)
647                                 {
648                                         if(!strcmp(argv[i], "angle")){
649                                                 i++;
650                                                 domeFov = atoi(argv[i++]);
651                                         }
652                                         if(!strcmp(argv[i], "tilt")){
653                                                 i++;
654                                                 domeTilt = atoi(argv[i++]);
655                                         }
656                                         if(!strcmp(argv[i], "warpdata")){
657                                                 i++;
658                                                 domeWarp = argv[i++];
659                                         }
660                                         if(!strcmp(argv[i], "mode")){
661                                                 i++;
662                                                 if(!strcmp(argv[i], "fisheye"))
663                                                         domeMode = DOME_FISHEYE;
664                                                         
665                                                 else if(!strcmp(argv[i], "truncatedfront"))
666                                                         domeMode = DOME_TRUNCATED_FRONT;
667                                                         
668                                                 else if(!strcmp(argv[i], "truncatedrear"))
669                                                         domeMode = DOME_TRUNCATED_REAR;
670                                                         
671                                                 else if(!strcmp(argv[i], "cubemap"))
672                                                         domeMode = DOME_ENVMAP;
673                                                         
674                                                 else if(!strcmp(argv[i], "sphericalpanoramic"))
675                                                         domeMode = DOME_PANORAM_SPH;
676
677                                                 else
678                                                         printf("error: %s is not a valid dome mode.\n", argv[i]);
679                                         }
680                                         i++;
681                                 }
682                                 break;
683                         default:
684                                 printf("Unknown argument: %s\n", argv[i++]);
685                                 break;
686                         }
687                 }
688                 else
689                 {
690                         i++;
691                 }
692         }
693         
694         if ((windowWidth < kMinWindowWidth) || (windowHeight < kMinWindowHeight))
695         {
696                 error = true;
697                 printf("error: window size too small.\n");
698         }
699         
700         if (error )
701         {
702                 usage(argv[0], isBlenderPlayer);
703                 return 0;
704         }
705
706 #ifdef WIN32
707         if (scr_saver_mode != SCREEN_SAVER_MODE_CONFIGURATION)
708 #endif
709         {
710 #ifdef __APPLE__
711                 //SYS_WriteCommandLineInt(syshandle, "show_framerate", 1);
712                 //SYS_WriteCommandLineInt(syshandle, "nomipmap", 1);
713                 //fullScreen = false;           // Can't use full screen
714 #endif
715
716                 if (SYS_GetCommandLineInt(syshandle, "nomipmap", 0))
717                 {
718                         GPU_set_mipmap(0);
719                 }
720
721                 GPU_set_anisotropic(U.anisotropic_filter);
722                 
723                 // Create the system
724                 if (GHOST_ISystem::createSystem() == GHOST_kSuccess)
725                 {
726                         GHOST_ISystem* system = GHOST_ISystem::getSystem();
727                         assertd(system);
728                         
729                         if (!fullScreenWidth || !fullScreenHeight)
730                                 system->getMainDisplayDimensions(fullScreenWidth, fullScreenHeight);
731                         // process first batch of events. If the user
732                         // drops a file on top off the blenderplayer icon, we 
733                         // receive an event with the filename
734                         
735                         system->processEvents(0);
736                         
737                         // this bracket is needed for app (see below) to get out
738                         // of scope before GHOST_ISystem::disposeSystem() is called.
739                         {
740                                 int exitcode = KX_EXIT_REQUEST_NO_REQUEST;
741                                 STR_String exitstring = "";
742                                 GPG_Application app(system);
743                                 bool firstTimeRunning = true;
744                                 char filename[FILE_MAXDIR + FILE_MAXFILE];
745                                 char pathname[FILE_MAXDIR + FILE_MAXFILE];
746                                 char *titlename;
747
748                                 get_filename(argc_py_clamped, argv, filename);
749                                 if(filename[0])
750                                         BLI_path_cwd(filename);
751                                 
752
753                                 // fill the GlobalSettings with the first scene files
754                                 // those may change during the game and persist after using Game Actuator
755                                 GlobalSettings gs;
756
757                                 do
758                                 {
759                                         // Read the Blender file
760                                         BlendFileData *bfd;
761                                         
762                                         // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
763                                         if (exitcode == KX_EXIT_REQUEST_START_OTHER_GAME)
764                                         {
765                                                 char basedpath[240];
766                                                 
767                                                 // base the actuator filename relative to the last file
768                                                 strcpy(basedpath, exitstring.Ptr());
769                                                 BLI_path_abs(basedpath, pathname);
770                                                 
771                                                 bfd = load_game_data(basedpath);
772
773                                                 if (!bfd)
774                                                 {
775                                                         // just add "//" in front of it
776                                                         char temppath[242];
777                                                         strcpy(temppath, "//");
778                                                         strcat(temppath, basedpath);
779                                 
780                                                         BLI_path_abs(temppath, pathname);
781                                                         bfd = load_game_data(temppath);
782                                                 }
783                                         }
784                                         else
785                                         {
786                                                 bfd = load_game_data(bprogname, filename[0]? filename: NULL);
787                                         }
788                                         
789                                         //::printf("game data loaded from %s\n", filename);
790                                         
791                                         if (!bfd) {
792                                                 usage(argv[0], isBlenderPlayer);
793                                                 error = true;
794                                                 exitcode = KX_EXIT_REQUEST_QUIT_GAME;
795                                         } 
796                                         else 
797                                         {
798 #ifdef WIN32
799 #if !defined(DEBUG)
800                                                 if (closeConsole)
801                                                 {
802                                                         //::FreeConsole();    // Close a console window
803                                                 }
804 #endif // !defined(DEBUG)
805 #endif // WIN32
806                                                 Main *maggie = bfd->main;
807                                                 Scene *scene = bfd->curscene;
808                                                 G.main = maggie;
809
810                                                 if (firstTimeRunning) {
811                                                         G.fileflags  = bfd->fileflags;
812
813                                                         gs.matmode= scene->gm.matmode;
814                                                         gs.glslflag= scene->gm.flag;
815                                                 }
816
817                                                 //Seg Fault; icon.c gIcons == 0
818                                                 BKE_icons_init(1);
819                                                 
820                                                 titlename = maggie->name;
821                                                 
822                                                 // Check whether the game should be displayed full-screen
823                                                 if ((!fullScreenParFound) && (!windowParFound))
824                                                 {
825                                                         // Only use file settings when command line did not override
826                                                         if (scene->gm.fullscreen) {
827                                                                 //printf("fullscreen option found in Blender file\n");
828                                                                 fullScreen = true;
829                                                                 fullScreenWidth= scene->gm.xplay;
830                                                                 fullScreenHeight= scene->gm.yplay;
831                                                                 fullScreenFrequency= scene->gm.freqplay;
832                                                                 fullScreenBpp = scene->gm.depth;
833                                                         }
834                                                         else
835                                                         {
836                                                                 fullScreen = false;
837                                                                 windowWidth = scene->gm.xplay;
838                                                                 windowHeight = scene->gm.yplay;
839                                                         }
840                                                 }
841                                                 
842                                                 
843                                                 // Check whether the game should be displayed in stereo
844                                                 if (!stereoParFound)
845                                                 {
846                                                         if(scene->gm.stereoflag == STEREO_ENABLED){
847                                                                 stereomode = (RAS_IRasterizer::StereoMode) scene->gm.stereomode;
848                                                                 if (stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
849                                                                         stereoWindow = true;
850                                                         }
851                                                 }
852                                                 else
853                                                         scene->gm.stereoflag = STEREO_ENABLED;
854
855                                                 if (stereoFlag == STEREO_DOME){
856                                                         stereomode = RAS_IRasterizer::RAS_STEREO_DOME;
857                                                         scene->gm.stereoflag = STEREO_DOME;
858                                                         if (domeFov > 89)
859                                                                 scene->gm.dome.angle = domeFov;
860                                                         if (domeTilt > -180)
861                                                                 scene->gm.dome.tilt = domeTilt;
862                                                         if (domeMode > 0)
863                                                                 scene->gm.dome.mode = domeMode;
864                                                         if (domeWarp)
865                                                         {
866                                                                 //XXX to do: convert relative to absolute path
867                                                                 domeText= add_text(domeWarp, "");
868                                                                 if(!domeText)
869                                                                         printf("error: invalid warpdata text file - %s\n", domeWarp);
870                                                                 else
871                                                                         scene->gm.dome.warptext = domeText;
872                                                         }
873                                                 }
874                                                 
875                                                 //                                      GPG_Application app (system, maggie, startscenename);
876                                                 app.SetGameEngineData(maggie, scene, &gs, argc, argv); /* this argc cant be argc_py_clamped, since python uses it */
877                                                 BLI_strncpy(pathname, maggie->name, sizeof(pathname));
878                                                 if(G.main != maggie) {
879                                                         BLI_strncpy(G.main->name, maggie->name, sizeof(G.main->name));
880                                                 }
881 #ifdef WITH_PYTHON
882                                                 setGamePythonPath(G.main->name);
883 #endif
884                                                 if (firstTimeRunning)
885                                                 {
886                                                         firstTimeRunning = false;
887
888                                                         if (fullScreen)
889                                                         {
890 #ifdef WIN32
891                                                                 if (scr_saver_mode == SCREEN_SAVER_MODE_SAVER)
892                                                                 {
893                                                                         app.startScreenSaverFullScreen(fullScreenWidth, fullScreenHeight, fullScreenBpp, fullScreenFrequency,
894                                                                                 stereoWindow, stereomode, aasamples);
895                                                                 }
896                                                                 else
897 #endif
898                                                                 {
899                                                                         app.startFullScreen(fullScreenWidth, fullScreenHeight, fullScreenBpp, fullScreenFrequency,
900                                                                                 stereoWindow, stereomode, aasamples);
901                                                                 }
902                                                         }
903                                                         else
904                                                         {
905 #ifdef __APPLE__
906                                                                 // on Mac's we'll show the executable name instead of the 'game.blend' name
907                                                                 char tempname[1024], *appstring;
908                                                                 ::strcpy(tempname, titlename);
909                                                                 
910                                                                 appstring = strstr(tempname, ".app/");
911                                                                 if (appstring) {
912                                                                         appstring[2] = 0;
913                                                                         titlename = &tempname[0];
914                                                                 }
915 #endif
916                                                                 // Strip the path so that we have the name of the game file
917                                                                 STR_String path = titlename;
918 #ifndef WIN32
919                                                                 vector<STR_String> parts = path.Explode('/');
920 #else  // WIN32
921                                                                 vector<STR_String> parts = path.Explode('\\');
922 #endif // WIN32                        
923                                                                 STR_String title;
924                                                                 if (parts.size())
925                                                                 {
926                                                                         title = parts[parts.size()-1];
927                                                                         parts = title.Explode('.');
928                                                                         if (parts.size() > 1)
929                                                                         {
930                                                                                 title = parts[0];
931                                                                         }
932                                                                 }
933                                                                 else
934                                                                 {
935                                                                         title = "blenderplayer";
936                                                                 }
937 #ifdef WIN32
938                                                                 if (scr_saver_mode == SCREEN_SAVER_MODE_PREVIEW)
939                                                                 {
940                                                                         app.startScreenSaverPreview(scr_saver_hwnd, stereoWindow, stereomode, aasamples);
941                                                                 }
942                                                                 else
943 #endif
944                                                                 {
945                                                                                                                                                                                                                 if (parentWindow != 0)
946                                                                                 app.startEmbeddedWindow(title, parentWindow, stereoWindow, stereomode, aasamples);
947                                                                         else
948                                                                                 app.startWindow(title, windowLeft, windowTop, windowWidth, windowHeight,
949                                                                                 stereoWindow, stereomode, aasamples);
950                                                                 }
951                                                         }
952                                                 }
953                                                 else
954                                                 {
955                                                         app.StartGameEngine(stereomode);
956                                                         exitcode = KX_EXIT_REQUEST_NO_REQUEST;
957                                                 }
958                                                 
959                                                 // Add the application as event consumer
960                                                 system->addEventConsumer(&app);
961                                                 
962                                                 // Enter main loop
963                                                 bool run = true;
964                                                 while (run)
965                                                 {
966                                                         system->processEvents(false);
967                                                         system->dispatchEvents();
968                                                         if ((exitcode = app.getExitRequested()))
969                                                         {
970                                                                 run = false;
971                                                                 exitstring = app.getExitString();
972                                                                 gs = *app.getGlobalSettings();
973                                                         }
974                                                 }
975                                                 app.StopGameEngine();
976
977                                                 /* 'app' is freed automatic when out of scope. 
978                                                  * removal is needed else the system will free an already freed value */
979                                                 system->removeEventConsumer(&app);
980
981                                                 BLO_blendfiledata_free(bfd);
982                                         }
983                                 } while (exitcode == KX_EXIT_REQUEST_RESTART_GAME || exitcode == KX_EXIT_REQUEST_START_OTHER_GAME);
984                         }
985
986                         // Seg Fault; icon.c gIcons == 0
987                         BKE_icons_free();
988
989                         // Dispose the system
990                         GHOST_ISystem::disposeSystem();
991                 } else {
992                         error = true;
993                         printf("error: couldn't create a system.\n");
994                 }
995         }
996
997         // Cleanup
998         RNA_exit();
999         BLF_exit();
1000         BLF_free_unifont();
1001         IMB_exit();
1002         free_nodesystem();
1003
1004         SYS_DeleteSystem(syshandle);
1005
1006         int totblock= MEM_get_memory_blocks_in_use();
1007         if(totblock!=0) {
1008                 printf("Error Totblock: %d\n",totblock);
1009                 MEM_set_error_callback(mem_error_cb);
1010                 MEM_printmemlist();
1011         }
1012
1013         return error ? -1 : 0;
1014 }
1015
1016