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