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