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