Cycles: svn merge -r41225:41232 ^/trunk/blender
[blender.git] / source / gameengine / GamePlayer / ghost / GPG_ghost.cpp
index d1039d5c01c1c58400487207e8118d389f7b5e76..fb07918ec5c3237a7a5386a7db175aa9b4d913f0 100644 (file)
@@ -1,6 +1,4 @@
-/**
-* $Id$
-*
+/*
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * Contributor(s): none yet.
  *
  * ***** END GPL LICENSE BLOCK *****
-* Start up of the Blender Player on GHOST.
-*/
+ * Start up of the Blender Player on GHOST.
+ */
+
+/** \file gameengine/GamePlayer/ghost/GPG_ghost.cpp
+ *  \ingroup player
+ */
+
 
 #include <iostream>
 #include <math.h>
@@ -42,7 +45,6 @@
 //#include <Carbon/Carbon.h>
 //#include <CFBundle.h>
 #endif // __APPLE__
-#include "GEN_messaging.h"
 #include "KX_KetsjiEngine.h"
 #include "KX_PythonInit.h"
 
@@ -58,22 +60,23 @@ extern "C"
 #include "BKE_global.h"        
 #include "BKE_icons.h" 
 #include "BKE_node.h"  
-#include "BKE_report.h"        
+#include "BKE_report.h"
+#include "BKE_library.h"
+#include "BLI_threads.h"
 #include "BLI_blenlib.h"
 #include "DNA_scene_types.h"
 #include "DNA_userdef_types.h"
 #include "BLO_readfile.h"
-#include "BLO_readblenfile.h"
+#include "BLO_runtime.h"
 #include "IMB_imbuf.h"
 #include "BKE_text.h"
+#include "BKE_sound.h"
        
        int GHOST_HACK_getFirstFile(char buf[]);
        
-extern char bprogname[];       /* holds a copy of argv[0], from creator.c */
-extern char btempdir[];                /* use this to store a valid temp directory */
-
 // For BLF
 #include "BLF_api.h"
+#include "BLF_translation.h"
 extern int datatoc_bfont_ttf_size;
 extern char datatoc_bfont_ttf[];
 
@@ -87,7 +90,7 @@ extern char datatoc_bfont_ttf[];
 * End Blender include block
 **********************************/
 
-#include "SYS_System.h"
+#include "BL_System.h"
 #include "GPG_Application.h"
 
 #include "GHOST_ISystem.h"
@@ -108,7 +111,11 @@ extern char datatoc_bfont_ttf[];
 const int kMinWindowWidth = 100;
 const int kMinWindowHeight = 100;
 
-char bprogname[FILE_MAX];
+static void mem_error_cb(const char *errorStr)
+{
+       fprintf(stderr, "%s", errorStr);
+       fflush(stderr);
+}
 
 #ifdef WIN32
 typedef enum 
@@ -188,7 +195,7 @@ void usage(const char* program, bool isBlenderPlayer)
        }
        
        printf("usage:   %s [-w [w h l t]] [-f [fw fh fb ff]] %s[-g gamengineoptions] "
-               "[-s stereomode] %s\n", program, consoleoption, filename);
+               "[-s stereomode] [-m aasamples] %s\n", program, consoleoption, filename);
        printf("  -h: Prints this command summary\n\n");
        printf("  -w: display in a window\n");
        printf("       --Optional parameters--\n"); 
@@ -224,9 +231,8 @@ void usage(const char* program, bool isBlenderPlayer)
        printf("             cubemap                (Cube Map)\n");
        printf("             sphericalpanoramic     (Spherical Panoramic)\n");
        printf("                             depending on the type of dome you are using\n\n");
-#ifndef _WIN32
+       printf("  -m: maximum anti-aliasing (eg. 2,4,8,16)\n\n");
        printf("  -i: parent windows ID \n\n");
-#endif
 #ifdef _WIN32
        printf("  -c: keep console window open\n\n");
 #endif
@@ -246,6 +252,7 @@ void usage(const char* program, bool isBlenderPlayer)
        printf("\n");
        printf("example: %s -w 320 200 10 10 -g noaudio%s%s\n", program, pathname, filename);
        printf("example: %s -g show_framerate = 0 %s%s\n", program, pathname, filename);
+       printf("example: %s -i 232421 -m 16 %s%s\n\n", program, pathname, filename);
 }
 
 static void get_filename(int argc, char **argv, char *filename)
@@ -295,7 +302,7 @@ static void get_filename(int argc, char **argv, char *filename)
 #endif // !_APPLE
 }
 
-static BlendFileData *load_game_data(char *progname, char *filename = NULL, char *relativename = NULL)
+static BlendFileData *load_game_data(const char *progname, char *filename = NULL, char *relativename = NULL)
 {
        ReportList reports;
        BlendFileData *bfd = NULL;
@@ -303,11 +310,11 @@ static BlendFileData *load_game_data(char *progname, char *filename = NULL, char
        BKE_reports_init(&reports, RPT_STORE);
        
        /* try to load ourself, will only work if we are a runtime */
-       if (blo_is_a_runtime(progname)) {
-               bfd= blo_read_runtime(progname, &reports);
+       if (BLO_is_a_runtime(progname)) {
+               bfd= BLO_read_runtime(progname, &reports);
                if (bfd) {
                        bfd->type= BLENFILETYPE_RUNTIME;
-                       strcpy(bfd->main->name, progname);
+                       BLI_strncpy(bfd->main->name, progname, sizeof(bfd->main->name));
                }
        } else {
                bfd= BLO_read_from_file(progname, &reports);
@@ -335,7 +342,9 @@ int main(int argc, char** argv)
        bool fullScreen = false;
        bool fullScreenParFound = false;
        bool windowParFound = false;
+#ifdef WIN32
        bool closeConsole = true;
+#endif
        RAS_IRasterizer::StereoMode stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO;
        bool stereoWindow = false;
        bool stereoParFound = false;
@@ -356,20 +365,22 @@ int main(int argc, char** argv)
        GHOST_TEmbedderWindowID parentWindow = 0;
        bool isBlenderPlayer = false;
        int validArguments=0;
+       GHOST_TUns16 aasamples = 0;
        
 #ifdef __linux__
 #ifdef __alpha__
        signal (SIGFPE, SIG_IGN);
 #endif /* __alpha__ */
 #endif /* __linux__ */
-       BLI_where_am_i(bprogname, sizeof(bprogname), argv[0]);
+       BLI_init_program_path(argv[0]);
+       BLI_init_temporary_dir(NULL);
 #ifdef __APPLE__
-    // Can't use Carbon right now because of double defined type ID (In Carbon.h and DNA_ID.h, sigh)
-    /*
-    IBNibRef           nibRef;
-    WindowRef          window;
-    OSStatus           err;
-       
+       // Can't use Carbon right now because of double defined type ID (In Carbon.h and DNA_ID.h, sigh)
+       /*
+       IBNibRef                nibRef;
+       WindowRef               window;
+       OSStatus                err;
+
          // Create a Nib reference passing the name of the nib file (without the .nib extension)
          // CreateNibReference only searches into the application bundle.
          err = ::CreateNibReference(CFSTR("main"), &nibRef);
@@ -382,8 +393,12 @@ int main(int argc, char** argv)
                
                  // We don't need the nib reference anymore.
                  ::DisposeNibReference(nibRef);
-    */
+       */
 #endif // __APPLE__
+       
+       // We don't use threads directly in the BGE, but we need to call this so things like
+       // freeing up GPU_Textures works correctly.
+       BLI_threadapi_init();
 
        RNA_init();
 
@@ -391,15 +406,20 @@ int main(int argc, char** argv)
        
        initglobals();
 
-       GEN_init_messaging_system();
+       // We load our own G.main, so free the one that initglobals() gives us
+       free_main(G.main);
+       G.main = NULL;
 
        IMB_init();
 
        // Setup builtin font for BLF (mostly copied from creator.c, wm_init_exit.c and interface_style.c)
        BLF_init(11, U.dpi);
        BLF_lang_init();
+       BLF_lang_encoding("");
+       BLF_lang_set("");
+
        BLF_load_mem("default", (unsigned char*)datatoc_bfont_ttf, datatoc_bfont_ttf_size);
+
        // Parse command line options
 #if defined(DEBUG)
        printf("argv[0] = '%s'\n", argv[0]);
@@ -434,8 +454,13 @@ int main(int argc, char** argv)
        U.audioformat = 0x24;
        U.audiochannels = 2;
 
+       // XXX this one too
+       U.anisotropic_filter = 2;
+
+       sound_init_once();
+
        /* if running blenderplayer the last argument can't be parsed since it has to be the filename. */
-       isBlenderPlayer = !blo_is_a_runtime(argv[0]);
+       isBlenderPlayer = !BLO_is_a_runtime(argv[0]);
        if (isBlenderPlayer)
                validArguments = argc - 1;
        else
@@ -542,7 +567,6 @@ int main(int argc, char** argv)
                                usage(argv[0], isBlenderPlayer);
                                return 0;
                                break;
-#ifndef _WIN32
                        case 'i':
                                i++;
                                if ( (i + 1) <= validArguments )
@@ -551,15 +575,20 @@ int main(int argc, char** argv)
                                        error = true;
                                        printf("error: too few options for parent window argument.\n");
                                }
-
 #if defined(DEBUG)
                                printf("XWindows ID = %d\n", parentWindow);
 #endif // defined(DEBUG)
-
-#endif  // _WIN32                      
+                               break;
+                       case 'm':
+                               i++;
+                               if ((i+1) <= validArguments )
+                               aasamples = atoi(argv[i++]);
+                               break;
                        case 'c':
                                i++;
+#ifdef WIN32
                                closeConsole = false;
+#endif
                                break;
                        case 's':  // stereo
                                i++;
@@ -683,6 +712,8 @@ int main(int argc, char** argv)
                {
                        GPU_set_mipmap(0);
                }
+
+               GPU_set_anisotropic(U.anisotropic_filter);
                
                // Create the system
                if (GHOST_ISystem::createSystem() == GHOST_kSuccess)
@@ -713,6 +744,11 @@ int main(int argc, char** argv)
                                if(filename[0])
                                        BLI_path_cwd(filename);
                                
+
+                               // fill the GlobalSettings with the first scene files
+                               // those may change during the game and persist after using Game Actuator
+                               GlobalSettings gs;
+
                                do
                                {
                                        // Read the Blender file
@@ -724,7 +760,7 @@ int main(int argc, char** argv)
                                                char basedpath[240];
                                                
                                                // base the actuator filename relative to the last file
-                                               strcpy(basedpath, exitstring.Ptr());
+                                               BLI_strncpy(basedpath, exitstring.Ptr(), sizeof(basedpath));
                                                BLI_path_abs(basedpath, pathname);
                                                
                                                bfd = load_game_data(basedpath);
@@ -742,7 +778,7 @@ int main(int argc, char** argv)
                                        }
                                        else
                                        {
-                                               bfd = load_game_data(bprogname, filename[0]? filename: NULL);
+                                               bfd = load_game_data(BLI_program_path(), filename[0]? filename: NULL);
                                        }
                                        
                                        //::printf("game data loaded from %s\n", filename);
@@ -766,9 +802,13 @@ int main(int argc, char** argv)
                                                Scene *scene = bfd->curscene;
                                                G.main = maggie;
 
-                                               if (firstTimeRunning)
+                                               if (firstTimeRunning) {
                                                        G.fileflags  = bfd->fileflags;
 
+                                                       gs.matmode= scene->gm.matmode;
+                                                       gs.glslflag= scene->gm.flag;
+                                               }
+
                                                //Seg Fault; icon.c gIcons == 0
                                                BKE_icons_init(1);
                                                
@@ -828,10 +868,11 @@ int main(int argc, char** argv)
                                                }
                                                
                                                //                                      GPG_Application app (system, maggie, startscenename);
-                                               app.SetGameEngineData(maggie, scene, argc, argv); /* this argc cant be argc_py_clamped, since python uses it */
-                                               
+                                               app.SetGameEngineData(maggie, scene, &gs, argc, argv); /* this argc cant be argc_py_clamped, since python uses it */
                                                BLI_strncpy(pathname, maggie->name, sizeof(pathname));
-                                               BLI_strncpy(G.main->name, maggie->name, sizeof(G.main->name));
+                                               if(G.main != maggie) {
+                                                       BLI_strncpy(G.main->name, maggie->name, sizeof(G.main->name));
+                                               }
 #ifdef WITH_PYTHON
                                                setGamePythonPath(G.main->name);
 #endif
@@ -845,13 +886,13 @@ int main(int argc, char** argv)
                                                                if (scr_saver_mode == SCREEN_SAVER_MODE_SAVER)
                                                                {
                                                                        app.startScreenSaverFullScreen(fullScreenWidth, fullScreenHeight, fullScreenBpp, fullScreenFrequency,
-                                                                               stereoWindow, stereomode);
+                                                                               stereoWindow, stereomode, aasamples);
                                                                }
                                                                else
 #endif
                                                                {
                                                                        app.startFullScreen(fullScreenWidth, fullScreenHeight, fullScreenBpp, fullScreenFrequency,
-                                                                               stereoWindow, stereomode);
+                                                                               stereoWindow, stereomode, aasamples);
                                                                }
                                                        }
                                                        else
@@ -891,16 +932,16 @@ int main(int argc, char** argv)
 #ifdef WIN32
                                                                if (scr_saver_mode == SCREEN_SAVER_MODE_PREVIEW)
                                                                {
-                                                                       app.startScreenSaverPreview(scr_saver_hwnd, stereoWindow, stereomode);
+                                                                       app.startScreenSaverPreview(scr_saver_hwnd, stereoWindow, stereomode, aasamples);
                                                                }
                                                                else
 #endif
                                                                {
                                                                                                                                                                                                                if (parentWindow != 0)
-                                                                               app.startEmbeddedWindow(title, parentWindow, stereoWindow, stereomode);
+                                                                               app.startEmbeddedWindow(title, parentWindow, stereoWindow, stereomode, aasamples);
                                                                        else
                                                                                app.startWindow(title, windowLeft, windowTop, windowWidth, windowHeight,
-                                                                               stereoWindow, stereomode);
+                                                                               stereoWindow, stereomode, aasamples);
                                                                }
                                                        }
                                                }
@@ -923,10 +964,15 @@ int main(int argc, char** argv)
                                                        {
                                                                run = false;
                                                                exitstring = app.getExitString();
+                                                               gs = *app.getGlobalSettings();
                                                        }
                                                }
                                                app.StopGameEngine();
 
+                                               /* 'app' is freed automatic when out of scope. 
+                                                * removal is needed else the system will free an already freed value */
+                                               system->removeEventConsumer(&app);
+
                                                BLO_blendfiledata_free(bfd);
                                        }
                                } while (exitcode == KX_EXIT_REQUEST_RESTART_GAME || exitcode == KX_EXIT_REQUEST_START_OTHER_GAME);
@@ -946,11 +992,23 @@ int main(int argc, char** argv)
        // Cleanup
        RNA_exit();
        BLF_exit();
+
+#ifdef WITH_INTERNATIONAL
+       BLF_free_unifont();
+#endif
+
        IMB_exit();
        free_nodesystem();
 
        SYS_DeleteSystem(syshandle);
 
+       int totblock= MEM_get_memory_blocks_in_use();
+       if(totblock!=0) {
+               printf("Error Totblock: %d\n",totblock);
+               MEM_set_error_callback(mem_error_cb);
+               MEM_printmemlist();
+       }
+
        return error ? -1 : 0;
 }