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