Game player: preserve material, debug settings, between .blend files.
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include <iostream>
32 #include <math.h>
33
34 #ifdef __linux__
35 #ifdef __alpha__
36 #include <signal.h>
37 #endif /* __alpha__ */
38 #endif /* __linux__ */
39
40 #ifdef __APPLE__
41 // Can't use Carbon right now because of double defined type ID (In Carbon.h and DNA_ID.h, sigh)
42 //#include <Carbon/Carbon.h>
43 //#include <CFBundle.h>
44 #endif // __APPLE__
45 #include "GEN_messaging.h"
46 #include "KX_KetsjiEngine.h"
47
48 /**********************************
49 * Begin Blender include block
50 **********************************/
51 #ifdef __cplusplus
52 extern "C"
53 {
54 #endif  // __cplusplus
55 #include "MEM_guardedalloc.h"
56 #include "BKE_blender.h"        
57 #include "BKE_global.h" 
58 #include "BKE_icons.h"  
59 #include "BKE_node.h"   
60 #include "BLI_blenlib.h"
61 #include "DNA_scene_types.h"
62 #include "BLO_readfile.h"
63 #include "BLO_readblenfile.h"
64 #include "IMB_imbuf.h"
65         
66         int GHOST_HACK_getFirstFile(char buf[]);
67         
68 #ifdef __cplusplus
69 }
70 #endif // __cplusplus
71
72 #include "GPU_draw.h"
73
74 /**********************************
75 * End Blender include block
76 **********************************/
77
78 #include "SYS_System.h"
79 #include "GPG_Application.h"
80
81 #include "GHOST_ISystem.h"
82 #include "RAS_IRasterizer.h"
83
84 #include "BKE_main.h"
85 #include "BKE_utildefines.h"
86
87 #ifdef WIN32
88 #include <windows.h>
89 #ifdef NDEBUG
90 #include <wincon.h>
91 #endif // NDEBUG
92 #endif // WIN32
93
94 const int kMinWindowWidth = 100;
95 const int kMinWindowHeight = 100;
96
97 char bprogname[FILE_MAXDIR+FILE_MAXFILE];
98
99 #ifdef WIN32
100 typedef enum 
101 {
102   SCREEN_SAVER_MODE_NONE = 0,
103   SCREEN_SAVER_MODE_PREVIEW,
104   SCREEN_SAVER_MODE_SAVER,
105   SCREEN_SAVER_MODE_CONFIGURATION,
106   SCREEN_SAVER_MODE_PASSWORD,
107 } ScreenSaverMode;
108
109 static ScreenSaverMode scr_saver_mode = SCREEN_SAVER_MODE_NONE;
110 static HWND scr_saver_hwnd = NULL;
111
112 static BOOL scr_saver_init(int argc, char **argv) 
113 {
114         scr_saver_mode = SCREEN_SAVER_MODE_NONE;
115         scr_saver_hwnd = NULL;
116         BOOL ret = FALSE;
117
118         int len = ::strlen(argv[0]);
119         if (len > 4 && !::stricmp(".scr", argv[0] + len - 4))
120         {
121                 scr_saver_mode = SCREEN_SAVER_MODE_CONFIGURATION;
122                 ret = TRUE;
123                 if (argc >= 2)
124                 {
125                         if (argc >= 3)
126                         {
127                                 scr_saver_hwnd = (HWND) ::atoi(argv[2]);
128                         }
129                         if (!::stricmp("/c", argv[1]))
130                         {
131                                 scr_saver_mode = SCREEN_SAVER_MODE_CONFIGURATION;
132                                 if (scr_saver_hwnd == NULL)
133                                         scr_saver_hwnd = ::GetForegroundWindow();
134                         }
135                         else if (!::stricmp("/s", argv[1]))
136                         {
137                                 scr_saver_mode = SCREEN_SAVER_MODE_SAVER;
138                         }
139                         else if (!::stricmp("/a", argv[1]))
140                         {
141                                 scr_saver_mode = SCREEN_SAVER_MODE_PASSWORD;
142                         }
143                         else if (!::stricmp("/p", argv[1])
144                                  || !::stricmp("/l", argv[1]))
145                         {
146                                 scr_saver_mode = SCREEN_SAVER_MODE_PREVIEW;
147                         }
148                 }
149         }
150         return ret;
151 }
152
153 #endif /* WIN32 */
154
155 void usage(const char* program)
156 {
157         const char * consoleoption;
158 #ifdef _WIN32
159         consoleoption = "-c ";
160 #else
161         consoleoption = "";
162 #endif
163         
164         printf("usage:   %s [-w [-p l t w h]] %s[-g gamengineoptions] "
165                 "[-s stereomode] filename.blend\n", program, consoleoption);
166         printf("  -h: Prints this command summary\n");
167         printf("  -w: display in a window\n");
168         printf("  -p: specify window position\n");
169         printf("       l = window left coordinate\n");
170         printf("       t = window top coordinate\n");
171         printf("       w = window width\n");
172         printf("       h = window height\n");
173         printf("  -f: start game in full screen mode\n");
174         printf("       fw = full screen mode pixel width\n");
175         printf("       fh = full screen mode pixel height\n");
176         printf("       fb = full screen mode bits per pixel\n");
177         printf("       ff = full screen mode frequency\n");
178         printf("  -s: start player in stereo\n");
179         printf("       stereomode: hwpageflip       (Quad buffered shutter glasses)\n");
180         printf("                   syncdoubling     (Above Below)\n");
181         printf("                   sidebyside       (Left Right)\n");
182         printf("                   anaglyph         (Red-Blue glasses)\n");
183         printf("                   vinterlace       (Vertical interlace for autostereo display)\n");
184         printf("                             depending on the type of stereo you want\n");
185 #ifndef _WIN32
186         printf("  -i: parent windows ID \n");
187 #endif
188 #ifdef _WIN32
189         printf("  -c: keep console window open\n");
190 #endif
191         printf("  -d: turn debugging on\n");
192         printf("  -g: game engine options:\n");
193         printf("       Name            Default      Description\n");
194         printf("       ----------------------------------------\n");
195         printf("       fixedtime          0         Do the same timestep each frame \"Enable all frames\"\n");
196         printf("       nomipmap           0         Disable mipmaps\n");
197         printf("       show_framerate     0         Show the frame rate\n");
198         printf("       show_properties    0         Show debug properties\n");
199         printf("       show_profile       0         Show profiling information\n");
200         printf("       blender_material   0         Enable material settings\n");
201         printf("\n");
202         printf("example: %s -p 10 10 320 200 -g noaudio c:\\loadtest.blend\n", program);
203         printf("example: %s -g show_framerate = 0 c:\\loadtest.blend\n", program);
204 }
205
206 static void get_filename(int argc, char **argv, char *filename)
207 {
208 #ifdef __APPLE__
209 /* On Mac we park the game file (called game.blend) in the application bundle.
210 * The executable is located in the bundle as well.
211 * Therefore, we can locate the game relative to the executable.
212         */
213         int srclen = ::strlen(argv[0]);
214         int len = 0;
215         char *gamefile = NULL;
216         
217         filename[0] = '\0';
218
219         if (argc > 1) {
220                 if (BLI_exists(argv[argc-1])) {
221                         BLI_strncpy(filename, argv[argc-1], FILE_MAXDIR + FILE_MAXFILE);
222                 }
223                 if (::strncmp(argv[argc-1], "-psn_", 5)==0) {
224                         static char firstfilebuf[512];
225                         if (GHOST_HACK_getFirstFile(firstfilebuf)) {
226                                 BLI_strncpy(filename, firstfilebuf, FILE_MAXDIR + FILE_MAXFILE);
227                         }
228                 }                        
229         }
230         
231         srclen -= ::strlen("MacOS/blenderplayer");
232         if (srclen > 0) {
233                 len = srclen + ::strlen("Resources/game.blend"); 
234                 gamefile = new char [len + 1];
235                 ::strcpy(gamefile, argv[0]);
236                 ::strcpy(gamefile + srclen, "Resources/game.blend");
237                 //::printf("looking for file: %s\n", filename);
238                 
239                 if (BLI_exists(gamefile))
240                         BLI_strncpy(filename, gamefile, FILE_MAXDIR + FILE_MAXFILE);
241
242                 delete gamefile;
243         }
244         
245 #else
246         filename[0] = '\0';
247
248         if(argc > 1)
249                 BLI_strncpy(filename, argv[argc-1], FILE_MAXDIR + FILE_MAXFILE);
250 #endif // !_APPLE
251 }
252
253 static BlendFileData *load_game_data(char *progname, char *filename = NULL, char *relativename = NULL) {
254         BlendReadError error;
255         BlendFileData *bfd = NULL;
256         
257         /* try to load ourself, will only work if we are a runtime */
258         if (blo_is_a_runtime(progname)) {
259                 bfd= blo_read_runtime(progname, &error);
260                 if (bfd) {
261                         bfd->type= BLENFILETYPE_RUNTIME;
262                         strcpy(bfd->main->name, progname);
263                 }
264         } else {
265                 bfd= BLO_read_from_file(progname, &error);
266         }
267         
268         /*
269         if (bfd && bfd->type == BLENFILETYPE_BLEND) {
270                 BLO_blendfiledata_free(bfd);
271                 bfd = NULL;
272                 error = BRE_NOT_A_PUBFILE;
273         }
274         */
275         
276         if (!bfd && filename) {
277                 bfd = load_game_data(filename);
278                 if (!bfd) {
279                         printf("Loading %s failed: %s\n", filename, BLO_bre_as_string(error));
280                 }
281         }
282         
283         return bfd;
284 }
285
286 int main(int argc, char** argv)
287 {
288         int i;
289         bool error = false;
290         SYS_SystemHandle syshandle = SYS_GetSystem();
291         bool fullScreen = false;
292         bool fullScreenParFound = false;
293         bool windowParFound = false;
294         bool closeConsole = true;
295         RAS_IRasterizer::StereoMode stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO;
296         bool stereoWindow = false;
297         bool stereoParFound = false;
298         int windowLeft = 100;
299         int windowTop = 100;
300         int windowWidth = 640;
301         int windowHeight = 480;
302         GHOST_TUns32 fullScreenWidth = 0;
303         GHOST_TUns32 fullScreenHeight= 0;
304         int fullScreenBpp = 32;
305         int fullScreenFrequency = 60;
306         GHOST_TEmbedderWindowID parentWindow = 0;
307
308
309         
310 #ifdef __linux__
311 #ifdef __alpha__
312         signal (SIGFPE, SIG_IGN);
313 #endif /* __alpha__ */
314 #endif /* __linux__ */
315         BLI_where_am_i(bprogname, argv[0]);
316 #ifdef __APPLE__
317     // Can't use Carbon right now because of double defined type ID (In Carbon.h and DNA_ID.h, sigh)
318     /*
319     IBNibRef            nibRef;
320     WindowRef           window;
321     OSStatus            err;
322         
323           // Create a Nib reference passing the name of the nib file (without the .nib extension)
324           // CreateNibReference only searches into the application bundle.
325           err = ::CreateNibReference(CFSTR("main"), &nibRef);
326           if (err) return -1;
327           
328                 // Once the nib reference is created, set the menu bar. "MainMenu" is the name of the menu bar
329                 // object. This name is set in InterfaceBuilder when the nib is created.
330                 err = ::SetMenuBarFromNib(nibRef, CFSTR("MenuBar"));
331                 if (err) return -1;
332                 
333                   // We don't need the nib reference anymore.
334                   ::DisposeNibReference(nibRef);
335     */
336 #endif // __APPLE__
337
338         init_nodesystem();
339         
340         initglobals();
341
342         GEN_init_messaging_system();
343
344 #ifdef WITH_QUICKTIME
345         quicktime_init();
346 #endif
347
348         libtiff_init();
349  
350         // Parse command line options
351 #ifndef NDEBUG
352         printf("argv[0] = '%s'\n", argv[0]);
353 #endif
354
355 #ifdef WIN32
356         if (scr_saver_init(argc, argv))
357         {
358                 switch (scr_saver_mode)
359                 {
360                 case SCREEN_SAVER_MODE_CONFIGURATION:
361                         MessageBox(scr_saver_hwnd, "This screen saver has no options that you can set", "Screen Saver", MB_OK);
362                         break;
363                 case SCREEN_SAVER_MODE_PASSWORD:
364                         /* This is W95 only, which we currently do not support.
365                            Fall-back to normal screen saver behaviour in that case... */
366                 case SCREEN_SAVER_MODE_SAVER:
367                         fullScreen = true;
368                         fullScreenParFound = true;
369                         break;
370
371                 case SCREEN_SAVER_MODE_PREVIEW:
372                         /* This will actually be handled somewhere below... */
373                         break;
374                 }
375         }
376 #endif
377         for (i = 1; (i < argc) && !error 
378 #ifdef WIN32
379                 && scr_saver_mode == SCREEN_SAVER_MODE_NONE
380 #endif
381                 ;)
382
383         {
384 #ifndef NDEBUG
385                 printf("argv[%d] = '%s'   , %i\n", i, argv[i],argc);
386 #endif
387                 if (argv[i][0] == '-')
388                 {
389                         switch (argv[i][1])
390                         {
391                         case 'g':
392                                 // Parse game options
393                                 {
394                                         i++;
395                                         if (i < argc)
396                                         {
397                                                 char* paramname = argv[i];
398                                                 // Check for single value versus assignment
399                                                 if (i+1 < argc && (*(argv[i+1]) == '='))
400                                                 {
401                                                         i++;
402                                                         if (i + 1 < argc)
403                                                         {
404                                                                 i++;
405                                                                 // Assignment
406                                                                 SYS_WriteCommandLineInt(syshandle, paramname, atoi(argv[i]));
407                                                                 SYS_WriteCommandLineFloat(syshandle, paramname, atof(argv[i]));
408                                                                 SYS_WriteCommandLineString(syshandle, paramname, argv[i]);
409 #ifndef NDEBUG
410                                                                 printf("%s = '%s'\n", paramname, argv[i]);
411 #endif
412                                                                 i++;
413                                                         }
414                                                         else
415                                                         {
416                                                                 error = true;
417                                                                 printf("error: argument assignment %s without value.\n", paramname);
418                                                         }
419                                                 }
420                                                 else
421                                                 {
422 //                                                      SYS_WriteCommandLineInt(syshandle, argv[i++], 1);
423                                                 }
424                                         }
425                                 }
426                                 break;
427
428                         case 'd':
429                                 i++;
430                                 G.f |= G_DEBUG;     /* std output printf's */
431                                 MEM_set_memory_debug();
432                                 break;
433                                 
434                         case 'p':
435                                 // Parse window position and size options
436                                 if (argv[i][2] == 0) {
437                                         i++;
438                                         if ((i + 4) < argc)
439                                         {
440                                                 windowLeft = atoi(argv[i++]);
441                                                 windowTop = atoi(argv[i++]);
442                                                 windowWidth = atoi(argv[i++]);
443                                                 windowHeight = atoi(argv[i++]);
444                                                 windowParFound = true;
445                                         }
446                                         else
447                                         {
448                                                 error = true;
449                                                 printf("error: too few options for window argument.\n");
450                                         }
451                                 } else { /* mac specific */
452                                 
453                     if (strncmp(argv[i], "-psn_", 5)==0) 
454                         i++; /* skip process serial number */
455                                 }
456                                 break;
457                         case 'f':
458                                 i++;
459                                 fullScreen = true;
460                                 fullScreenParFound = true;
461                                 if ((i + 2) < argc && argv[i][0] != '-' && argv[i+1][0] != '-')
462                                 {
463                                         fullScreenWidth = atoi(argv[i++]);
464                                         fullScreenHeight = atoi(argv[i++]);
465                                         if ((i + 1) < argc && argv[i][0] != '-')
466                                         {
467                                                 fullScreenBpp = atoi(argv[i++]);
468                                                 if ((i + 1) < argc && argv[i][0] != '-')
469                                                         fullScreenFrequency = atoi(argv[i++]);
470                                         }
471                                 }
472                                 break;
473                         case 'w':
474                                 // Parse window position and size options
475                                 {
476                                         fullScreen = false;
477                                         fullScreenParFound = true;
478                                         i++;
479                                 }
480                                 break;
481                         case 'h':
482                                 usage(argv[0]);
483                                 return 0;
484                                 break;
485 #ifndef _WIN32
486                         case 'i':
487                                 i++;
488                                 if ( (i + 1) < argc )
489                                         parentWindow = atoi(argv[i++]);                                         
490 #ifndef NDEBUG
491                                 printf("XWindows ID = %d\n", parentWindow);
492 #endif //NDEBUG
493
494 #endif  // _WIN32                       
495                         case 'c':
496                                 i++;
497                                 closeConsole = false;
498                                 break;
499                         case 's':  // stereo
500                                 i++;
501                                 if ((i + 1) < argc)
502                                 {
503                                         stereomode = (RAS_IRasterizer::StereoMode) atoi(argv[i]);
504                                         if (stereomode < RAS_IRasterizer::RAS_STEREO_NOSTEREO || stereomode >= RAS_IRasterizer::RAS_STEREO_MAXSTEREO)
505                                                 stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO;
506                                         
507                                         if(!strcmp(argv[i], "nostereo"))  // ok, redundant but clear
508                                                 stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO;
509                                         
510                                         // only the hardware pageflip method needs a stereo window
511                                         if(!strcmp(argv[i], "hwpageflip")) {
512                                                 stereomode = RAS_IRasterizer::RAS_STEREO_QUADBUFFERED;
513                                                 stereoWindow = true;
514                                         }
515                                         if(!strcmp(argv[i], "syncdoubling"))
516                                                 stereomode = RAS_IRasterizer::RAS_STEREO_ABOVEBELOW;
517                                         
518                                         if(!strcmp(argv[i], "anaglyph"))
519                                                 stereomode = RAS_IRasterizer::RAS_STEREO_ANAGLYPH;
520                                         
521                                         if(!strcmp(argv[i], "sidebyside"))
522                                                 stereomode = RAS_IRasterizer::RAS_STEREO_SIDEBYSIDE;
523                                         
524                                         if(!strcmp(argv[i], "vinterlace"))
525                                                 stereomode = RAS_IRasterizer::RAS_STEREO_VINTERLACE;
526                                         
527 #if 0
528                                         // future stuff
529                                         if(strcmp(argv[i], "stencil")
530                                                 stereomode = RAS_STEREO_STENCIL;
531 #endif
532                                         
533                                         i++;
534                                         stereoParFound = true;
535                                 }
536                                 else
537                                 {
538                                         error = true;
539                                         printf("error: too few options for stereo argument.\n");
540                                 }
541                                 break;
542                         default:
543                                 printf("Unkown argument: %s\n", argv[i++]);
544                                 break;
545                         }
546                 }
547                 else
548                 {
549                         i++;
550                 }
551         }
552         
553         if ((windowWidth < kMinWindowWidth) || (windowHeight < kMinWindowHeight))
554         {
555                 error = true;
556                 printf("error: window size too small.\n");
557         }
558         
559         if (error )
560         {
561                 usage(argv[0]);
562                 return 0;
563         }
564
565 #ifdef WIN32
566         if (scr_saver_mode != SCREEN_SAVER_MODE_CONFIGURATION)
567 #endif
568         {
569 #ifdef __APPLE__
570                 //SYS_WriteCommandLineInt(syshandle, "show_framerate", 1);
571                 //SYS_WriteCommandLineInt(syshandle, "nomipmap", 1);
572                 //fullScreen = false;           // Can't use full screen
573 #endif
574
575                 if (SYS_GetCommandLineInt(syshandle, "nomipmap", 0))
576                 {
577                         GPU_set_mipmap(0);
578                 }
579                 
580                 // Create the system
581                 if (GHOST_ISystem::createSystem() == GHOST_kSuccess)
582                 {
583                         GHOST_ISystem* system = GHOST_ISystem::getSystem();
584                         assertd(system);
585                         
586                         if (!fullScreenWidth || !fullScreenHeight)
587                                 system->getMainDisplayDimensions(fullScreenWidth, fullScreenHeight);
588                         // process first batch of events. If the user
589                         // drops a file on top off the blenderplayer icon, we 
590                         // recieve an event with the filename
591                         
592                         system->processEvents(0);
593                         
594                         // this bracket is needed for app (see below) to get out
595                         // of scope before GHOST_ISystem::disposeSystem() is called.
596                         {
597                                 int exitcode = KX_EXIT_REQUEST_NO_REQUEST;
598                                 STR_String exitstring = "";
599                                 GPG_Application app(system);
600                                 bool firstTimeRunning = true;
601                                 char filename[FILE_MAXDIR + FILE_MAXFILE];
602                                 char *titlename;
603                                 char pathname[160];
604
605                                 get_filename(argc, argv, filename);
606                                 if(filename[0])
607                                         BLI_convertstringcwd(filename);
608                                 
609                                 do
610                                 {
611                                         // Read the Blender file
612                                         BlendFileData *bfd;
613                                         
614                                         // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
615                                         if (exitcode == KX_EXIT_REQUEST_START_OTHER_GAME)
616                                         {
617                                                 char basedpath[240];
618                                                 
619                                                 // base the actuator filename with respect
620                                                 // to the original file working directory
621                                                 strcpy(basedpath, exitstring.Ptr());
622                                                 BLI_convertstringcode(basedpath, pathname);
623                                                 
624                                                 bfd = load_game_data(basedpath);
625
626                                                 if (!bfd)
627                                                 {
628                                                         // just add "//" in front of it
629                                                         char temppath[242];
630                                                         strcpy(temppath, "//");
631                                                         strcat(temppath, basedpath);
632                                 
633                                                         BLI_convertstringcode(temppath, pathname);
634                                                         bfd = load_game_data(temppath);
635                                                 }
636                                         }
637                                         else
638                                         {
639                                                 bfd = load_game_data(bprogname, filename[0]? filename: NULL);
640                                         }
641                                         
642                                         //::printf("game data loaded from %s\n", filename);
643                                         
644                                         if (!bfd) {
645                                                 usage(argv[0]);
646                                                 error = true;
647                                                 exitcode = KX_EXIT_REQUEST_QUIT_GAME;
648                                         } 
649                                         else 
650                                         {
651 #ifdef WIN32
652 #ifdef NDEBUG
653                                                 if (closeConsole)
654                                                 {
655                                                         //::FreeConsole();    // Close a console window
656                                                 }
657 #endif // NDEBUG
658 #endif // WIN32
659                                                 Main *maggie = bfd->main;
660                                                 Scene *scene = bfd->curscene;
661                                                 G.main = maggie;
662
663                                                 if (firstTimeRunning)
664                                                         G.fileflags  = bfd->fileflags;
665
666                                                 //Seg Fault; icon.c gIcons == 0
667                                                 BKE_icons_init(1);
668                                                 
669                                                 titlename = maggie->name;
670                                                 
671                                                 // Check whether the game should be displayed full-screen
672                                                 if ((!fullScreenParFound) && (!windowParFound))
673                                                 {
674                                                         // Only use file settings when command line did not override
675                                                         if (scene->r.fullscreen) {
676                                                                 //printf("fullscreen option found in Blender file\n");
677                                                                 fullScreen = true;
678                                                                 fullScreenWidth= scene->r.xplay;
679                                                                 fullScreenHeight= scene->r.yplay;
680                                                                 fullScreenFrequency= scene->r.freqplay;
681                                                                 fullScreenBpp = scene->r.depth;
682                                                         }
683                                                         else
684                                                         {
685                                                                 fullScreen = false;
686                                                                 windowWidth = scene->r.xplay;
687                                                                 windowHeight = scene->r.yplay;
688                                                         }
689                                                 }
690                                                 
691                                                 
692                                                 // Check whether the game should be displayed in stereo
693                                                 if (!stereoParFound)
694                                                 {
695                                                         stereomode = (RAS_IRasterizer::StereoMode) scene->r.stereomode;
696                                                         if (stereomode == RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
697                                                                 stereoWindow = true;
698                                                 }
699                                                 
700                                                 //                                      GPG_Application app (system, maggie, startscenename);
701                                                 app.SetGameEngineData(maggie, scene);
702                                                 
703                                                 if (firstTimeRunning)
704                                                 {
705                                                         firstTimeRunning = false;
706
707                                                         // set the filename only the first time as in KetsjiEmbedded
708                                                         strcpy (pathname, maggie->name);
709                                                         // also copy here (used by GameLogic.getBaseDirectory)
710                                                         strcpy (G.sce, maggie->name);
711
712                                                         if (fullScreen)
713                                                         {
714 #ifdef WIN32
715                                                                 if (scr_saver_mode == SCREEN_SAVER_MODE_SAVER)
716                                                                 {
717                                                                         app.startScreenSaverFullScreen(fullScreenWidth, fullScreenHeight, fullScreenBpp, fullScreenFrequency,
718                                                                                 stereoWindow, stereomode);
719                                                                 }
720                                                                 else
721 #endif
722                                                                 {
723                                                                         app.startFullScreen(fullScreenWidth, fullScreenHeight, fullScreenBpp, fullScreenFrequency,
724                                                                                 stereoWindow, stereomode);
725                                                                 }
726                                                         }
727                                                         else
728                                                         {
729 #ifdef __APPLE__
730                                                                 // on Mac's we'll show the executable name instead of the 'game.blend' name
731                                                                 char tempname[1024], *appstring;
732                                                                 ::strcpy(tempname, titlename);
733                                                                 
734                                                                 appstring = strstr(tempname, ".app/");
735                                                                 if (appstring) {
736                                                                         appstring[2] = 0;
737                                                                         titlename = &tempname[0];
738                                                                 }
739 #endif
740                                                                 // Strip the path so that we have the name of the game file
741                                                                 STR_String path = titlename;
742 #ifndef WIN32
743                                                                 vector<STR_String> parts = path.Explode('/');
744 #else  // WIN32
745                                                                 vector<STR_String> parts = path.Explode('\\');
746 #endif // WIN32                        
747                                                                 STR_String title;
748                                                                 if (parts.size())
749                                                                 {
750                                                                         title = parts[parts.size()-1];
751                                                                         parts = title.Explode('.');
752                                                                         if (parts.size() > 1)
753                                                                         {
754                                                                                 title = parts[0];
755                                                                         }
756                                                                 }
757                                                                 else
758                                                                 {
759                                                                         title = "blenderplayer";
760                                                                 }
761 #ifdef WIN32
762                                                                 if (scr_saver_mode == SCREEN_SAVER_MODE_PREVIEW)
763                                                                 {
764                                                                         app.startScreenSaverPreview(scr_saver_hwnd, stereoWindow, stereomode);
765                                                                 }
766                                                                 else
767 #endif
768                                                                 {
769                                                                                                                                                                                                                 if (parentWindow != 0)
770                                                                                 app.startEmbeddedWindow(title, parentWindow, stereoWindow, stereomode);
771                                                                         else
772                                                                                 app.startWindow(title, windowLeft, windowTop, windowWidth, windowHeight,
773                                                                                 stereoWindow, stereomode);
774                                                                 }
775                                                         }
776                                                 }
777                                                 else
778                                                 {
779                                                         app.StartGameEngine(stereomode);
780                                                         exitcode = KX_EXIT_REQUEST_NO_REQUEST;
781                                                 }
782                                                 
783                                                 // Add the application as event consumer
784                                                 system->addEventConsumer(&app);
785                                                 
786                                                 // Enter main loop
787                                                 bool run = true;
788                                                 while (run)
789                                                 {
790                                                         system->processEvents(false);
791                                                         system->dispatchEvents();
792                                                         if ((exitcode = app.getExitRequested()))
793                                                         {
794                                                                 run = false;
795                                                                 exitstring = app.getExitString();
796                                                         }
797                                                 }
798                                                 app.StopGameEngine();
799
800                                                 BLO_blendfiledata_free(bfd);
801                                         }
802                                 } while (exitcode == KX_EXIT_REQUEST_RESTART_GAME || exitcode == KX_EXIT_REQUEST_START_OTHER_GAME);
803                         }
804
805                         // Seg Fault; icon.c gIcons == 0
806                         BKE_icons_free();
807
808                         // Dispose the system
809                         GHOST_ISystem::disposeSystem();
810                 } else {
811                         error = true;
812                         printf("error: couldn't create a system.\n");
813                 }
814         }
815
816         free_nodesystem();
817
818         return error ? -1 : 0;
819 }
820
821