merge from trunk #37722
[blender-staging.git] / source / gameengine / GamePlayer / ghost / GPG_ghost.cpp
index fb2e1c72a85830817f6cb638cbb41decdc17ca63..0e3cfc58e0937bdd85754a4e060b5b399166b5e4 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
 * $Id$
 *
  * ***** BEGIN GPL LICENSE BLOCK *****
@@ -15,7 +15,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  * All rights reserved.
 * Start up of the Blender Player on GHOST.
 */
 
+/** \file gameengine/GamePlayer/ghost/GPG_ghost.cpp
+ *  \ingroup player
+ */
+
+
 #include <iostream>
 #include <math.h>
 
@@ -42,7 +47,6 @@
 //#include <Carbon/Carbon.h>
 //#include <CFBundle.h>
 #endif // __APPLE__
-#include "GEN_messaging.h"
 #include "KX_KetsjiEngine.h"
 #include "KX_PythonInit.h"
 
@@ -58,19 +62,27 @@ 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_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"
+extern int datatoc_bfont_ttf_size;
+extern char datatoc_bfont_ttf[];
+
 #ifdef __cplusplus
 }
 #endif // __cplusplus
@@ -81,7 +93,7 @@ extern char btempdir[];               /* use this to store a valid temp directory */
 * End Blender include block
 **********************************/
 
-#include "SYS_System.h"
+#include "BL_System.h"
 #include "GPG_Application.h"
 
 #include "GHOST_ISystem.h"
@@ -94,15 +106,21 @@ extern char btempdir[];            /* use this to store a valid temp directory */
 
 #ifdef WIN32
 #include <windows.h>
-#ifdef NDEBUG
+#if !defined(DEBUG)
 #include <wincon.h>
-#endif // NDEBUG
+#endif // !defined(DEBUG)
 #endif // WIN32
 
 const int kMinWindowWidth = 100;
 const int kMinWindowHeight = 100;
 
-char bprogname[FILE_MAXDIR+FILE_MAXFILE];
+char bprogname[FILE_MAX];
+
+static void mem_error_cb(const char *errorStr)
+{
+       fprintf(stderr, "%s", errorStr);
+       fflush(stderr);
+}
 
 #ifdef WIN32
 typedef enum 
@@ -160,17 +178,29 @@ static BOOL scr_saver_init(int argc, char **argv)
 
 #endif /* WIN32 */
 
-void usage(const char* program)
+void usage(const char* program, bool isBlenderPlayer)
 {
        const char * consoleoption;
+       const char * filename = "";
+       const char * pathname = "";
+
 #ifdef _WIN32
        consoleoption = "-c ";
 #else
        consoleoption = "";
 #endif
+
+       if (isBlenderPlayer) {
+               filename = "filename.blend";
+#ifdef _WIN32
+               pathname = "c:\\";
+#else
+               pathname = "//home//user//";
+#endif
+       }
        
        printf("usage:   %s [-w [w h l t]] [-f [fw fh fb ff]] %s[-g gamengineoptions] "
-               "[-s stereomode] filename.blend\n", program, consoleoption);
+               "[-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"); 
@@ -195,9 +225,19 @@ void usage(const char* program)
        printf("                   anaglyph         (Red-Blue glasses)\n");
        printf("                   vinterlace       (Vertical interlace for autostereo display)\n");
        printf("                             depending on the type of stereo you want\n\n");
-#ifndef _WIN32
+       printf("  -D: start player in dome mode\n");
+       printf("       --Optional parameters--\n");
+       printf("       angle    = field of view in degrees\n");
+       printf("       tilt     = tilt angle in degrees\n");
+       printf("       warpdata = a file to use for warping the image (absolute path)\n");      
+       printf("       mode: fisheye                (Fisheye)\n");
+       printf("             truncatedfront         (Front-Truncated)\n");
+       printf("             truncatedrear          (Rear-Truncated)\n");
+       printf("             cubemap                (Cube Map)\n");
+       printf("             sphericalpanoramic     (Spherical Panoramic)\n");
+       printf("                             depending on the type of dome you are using\n\n");
+       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
@@ -215,8 +255,9 @@ void usage(const char* program)
        printf("\n");
        printf("  - : all arguments after this are ignored, allowing python to access them from sys.argv\n");
        printf("\n");
-       printf("example: %s -w 320 200 10 10 -g noaudio c:\\loadtest.blend\n", program);
-       printf("example: %s -g show_framerate = 0 c:\\loadtest.blend\n", program);
+       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)
@@ -274,8 +315,8 @@ 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);
@@ -306,10 +347,18 @@ 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;
+       int stereoFlag = STEREO_NOSTEREO;
+       int domeFov = -1;
+       int domeTilt = -200;
+       int domeMode = 0;
+       char* domeWarp = NULL;
+       Text *domeText  = NULL;
        int windowLeft = 100;
        int windowTop = 100;
        int windowWidth = 640;
@@ -319,15 +368,16 @@ int main(int argc, char** argv)
        int fullScreenBpp = 32;
        int fullScreenFrequency = 60;
        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, argv[0]);
+       BLI_where_am_i(bprogname, sizeof(bprogname), argv[0]);
 #ifdef __APPLE__
     // Can't use Carbon right now because of double defined type ID (In Carbon.h and DNA_ID.h, sigh)
     /*
@@ -350,22 +400,30 @@ int main(int argc, char** argv)
     */
 #endif // __APPLE__
 
+       // 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();
+       // use default settings
+       BLF_lang_encoding("");
+       BLF_lang_set("");
+
        RNA_init();
+       RNA_structs_gettexted()
 
        init_nodesystem();
        
        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;
 
-#ifdef WITH_QUICKTIME
-       quicktime_init();
-#endif
+       IMB_init();
 
-       libtiff_init();
+       BLF_load_mem("default", (unsigned char*)datatoc_bfont_ttf, datatoc_bfont_ttf_size);
  
        // Parse command line options
-#ifndef NDEBUG
+#if defined(DEBUG)
        printf("argv[0] = '%s'\n", argv[0]);
 #endif
 
@@ -398,14 +456,26 @@ int main(int argc, char** argv)
        U.audioformat = 0x24;
        U.audiochannels = 2;
 
-       for (i = 1; (i < argc) && !error 
+       // 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]);
+       if (isBlenderPlayer)
+               validArguments = argc - 1;
+       else
+               validArguments = argc;
+
+       for (i = 1; (i < validArguments) && !error 
 #ifdef WIN32
                && scr_saver_mode == SCREEN_SAVER_MODE_NONE
 #endif
                ;)
 
        {
-#ifndef NDEBUG
+#if defined(DEBUG)
                printf("argv[%d] = '%s'   , %i\n", i, argv[i],argc);
 #endif
                if (argv[i][0] == '-')
@@ -422,21 +492,21 @@ int main(int argc, char** argv)
                                // Parse game options
                                {
                                        i++;
-                                       if (i < argc)
+                                       if (i <= validArguments)
                                        {
                                                char* paramname = argv[i];
                                                // Check for single value versus assignment
-                                               if (i+1 < argc && (*(argv[i+1]) == '='))
+                                               if (i+1 <= validArguments && (*(argv[i+1]) == '='))
                                                {
                                                        i++;
-                                                       if (i + 1 < argc)
+                                                       if (i + 1 <= validArguments)
                                                        {
                                                                i++;
                                                                // Assignment
                                                                SYS_WriteCommandLineInt(syshandle, paramname, atoi(argv[i]));
                                                                SYS_WriteCommandLineFloat(syshandle, paramname, atof(argv[i]));
                                                                SYS_WriteCommandLineString(syshandle, paramname, argv[i]);
-#ifndef NDEBUG
+#if defined(DEBUG)
                                                                printf("%s = '%s'\n", paramname, argv[i]);
 #endif
                                                                i++;
@@ -465,14 +535,14 @@ int main(int argc, char** argv)
                                i++;
                                fullScreen = true;
                                fullScreenParFound = true;
-                               if ((i + 2) <= argc && argv[i][0] != '-' && argv[i+1][0] != '-')
+                               if ((i + 2) <= validArguments && argv[i][0] != '-' && argv[i+1][0] != '-')
                                {
                                        fullScreenWidth = atoi(argv[i++]);
                                        fullScreenHeight = atoi(argv[i++]);
-                                       if ((i + 1) <= argc && argv[i][0] != '-')
+                                       if ((i + 1) <= validArguments && argv[i][0] != '-')
                                        {
                                                fullScreenBpp = atoi(argv[i++]);
-                                               if ((i + 1) <= argc && argv[i][0] != '-')
+                                               if ((i + 1) <= validArguments && argv[i][0] != '-')
                                                        fullScreenFrequency = atoi(argv[i++]);
                                        }
                                }
@@ -483,11 +553,11 @@ int main(int argc, char** argv)
                                fullScreen = false;
                                windowParFound = true;
 
-                               if ((i + 2) <= argc && argv[i][0] != '-' && argv[i+1][0] != '-')
+                               if ((i + 2) <= validArguments && argv[i][0] != '-' && argv[i+1][0] != '-')
                                {
                                        windowWidth = atoi(argv[i++]);
                                        windowHeight = atoi(argv[i++]);
-                                       if ((i +2) <= argc && argv[i][0] != '-' && argv[i+1][0] != '-')
+                                       if ((i + 2) <= validArguments && argv[i][0] != '-' && argv[i+1][0] != '-')
                                        {
                                                windowLeft = atoi(argv[i++]);
                                                windowTop = atoi(argv[i++]);
@@ -496,26 +566,35 @@ int main(int argc, char** argv)
                                break;
                                        
                        case 'h':
-                               usage(argv[0]);
+                               usage(argv[0], isBlenderPlayer);
                                return 0;
                                break;
-#ifndef _WIN32
                        case 'i':
                                i++;
-                               if ( (i + 1) < argc )
-                                       parentWindow = atoi(argv[i++]);                                         
-#ifndef NDEBUG
+                               if ( (i + 1) <= validArguments )
+                                       parentWindow = atoi(argv[i++]); 
+                               else {
+                                       error = true;
+                                       printf("error: too few options for parent window argument.\n");
+                               }
+#if defined(DEBUG)
                                printf("XWindows ID = %d\n", parentWindow);
-#endif //NDEBUG
-
-#endif  // _WIN32                      
+#endif // defined(DEBUG)
+                               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++;
-                               if ((i + 1) < argc)
+                               if ((i + 1) <= validArguments)
                                {
                                        stereomode = (RAS_IRasterizer::StereoMode) atoi(argv[i]);
                                        if (stereomode < RAS_IRasterizer::RAS_STEREO_NOSTEREO || stereomode >= RAS_IRasterizer::RAS_STEREO_MAXSTEREO)
@@ -525,30 +604,31 @@ int main(int argc, char** argv)
                                                stereomode = RAS_IRasterizer::RAS_STEREO_NOSTEREO;
                                        
                                        // only the hardware pageflip method needs a stereo window
-                                       if(!strcmp(argv[i], "hwpageflip")) {
+                                       else if(!strcmp(argv[i], "hwpageflip")) {
                                                stereomode = RAS_IRasterizer::RAS_STEREO_QUADBUFFERED;
                                                stereoWindow = true;
                                        }
-                                       if(!strcmp(argv[i], "syncdoubling"))
+                                       else if(!strcmp(argv[i], "syncdoubling"))
                                                stereomode = RAS_IRasterizer::RAS_STEREO_ABOVEBELOW;
                                        
-                                       if(!strcmp(argv[i], "anaglyph"))
+                                       else if(!strcmp(argv[i], "anaglyph"))
                                                stereomode = RAS_IRasterizer::RAS_STEREO_ANAGLYPH;
                                        
-                                       if(!strcmp(argv[i], "sidebyside"))
+                                       else if(!strcmp(argv[i], "sidebyside"))
                                                stereomode = RAS_IRasterizer::RAS_STEREO_SIDEBYSIDE;
                                        
-                                       if(!strcmp(argv[i], "vinterlace"))
+                                       else if(!strcmp(argv[i], "vinterlace"))
                                                stereomode = RAS_IRasterizer::RAS_STEREO_VINTERLACE;
                                        
 #if 0
                                        // future stuff
-                                       if(strcmp(argv[i], "stencil")
+                                       else if(!strcmp(argv[i], "stencil")
                                                stereomode = RAS_STEREO_STENCIL;
 #endif
                                        
                                        i++;
                                        stereoParFound = true;
+                                       stereoFlag = STEREO_ENABLED;
                                }
                                else
                                {
@@ -556,8 +636,49 @@ int main(int argc, char** argv)
                                        printf("error: too few options for stereo argument.\n");
                                }
                                break;
+                       case 'D':
+                               stereoFlag = STEREO_DOME;
+                               stereomode = RAS_IRasterizer::RAS_STEREO_DOME;
+                               i++;
+                               if ((i + 1) <= validArguments)
+                               {
+                                       if(!strcmp(argv[i], "angle")){
+                                               i++;
+                                               domeFov = atoi(argv[i++]);
+                                       }
+                                       if(!strcmp(argv[i], "tilt")){
+                                               i++;
+                                               domeTilt = atoi(argv[i++]);
+                                       }
+                                       if(!strcmp(argv[i], "warpdata")){
+                                               i++;
+                                               domeWarp = argv[i++];
+                                       }
+                                       if(!strcmp(argv[i], "mode")){
+                                               i++;
+                                               if(!strcmp(argv[i], "fisheye"))
+                                                       domeMode = DOME_FISHEYE;
+                                                       
+                                               else if(!strcmp(argv[i], "truncatedfront"))
+                                                       domeMode = DOME_TRUNCATED_FRONT;
+                                                       
+                                               else if(!strcmp(argv[i], "truncatedrear"))
+                                                       domeMode = DOME_TRUNCATED_REAR;
+                                                       
+                                               else if(!strcmp(argv[i], "cubemap"))
+                                                       domeMode = DOME_ENVMAP;
+                                                       
+                                               else if(!strcmp(argv[i], "sphericalpanoramic"))
+                                                       domeMode = DOME_PANORAM_SPH;
+
+                                               else
+                                                       printf("error: %s is not a valid dome mode.\n", argv[i]);
+                                       }
+                                       i++;
+                               }
+                               break;
                        default:
-                               printf("Unkown argument: %s\n", argv[i++]);
+                               printf("Unknown argument: %s\n", argv[i++]);
                                break;
                        }
                }
@@ -575,7 +696,7 @@ int main(int argc, char** argv)
        
        if (error )
        {
-               usage(argv[0]);
+               usage(argv[0], isBlenderPlayer);
                return 0;
        }
 
@@ -593,6 +714,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)
@@ -604,7 +727,7 @@ int main(int argc, char** argv)
                                system->getMainDisplayDimensions(fullScreenWidth, fullScreenHeight);
                        // process first batch of events. If the user
                        // drops a file on top off the blenderplayer icon, we 
-                       // recieve an event with the filename
+                       // receive an event with the filename
                        
                        system->processEvents(0);
                        
@@ -621,7 +744,7 @@ int main(int argc, char** argv)
 
                                get_filename(argc_py_clamped, argv, filename);
                                if(filename[0])
-                                       BLI_convertstringcwd(filename);
+                                       BLI_path_cwd(filename);
                                
                                do
                                {
@@ -635,7 +758,7 @@ int main(int argc, char** argv)
                                                
                                                // base the actuator filename relative to the last file
                                                strcpy(basedpath, exitstring.Ptr());
-                                               BLI_convertstringcode(basedpath, pathname);
+                                               BLI_path_abs(basedpath, pathname);
                                                
                                                bfd = load_game_data(basedpath);
 
@@ -646,7 +769,7 @@ int main(int argc, char** argv)
                                                        strcpy(temppath, "//");
                                                        strcat(temppath, basedpath);
                                
-                                                       BLI_convertstringcode(temppath, pathname);
+                                                       BLI_path_abs(temppath, pathname);
                                                        bfd = load_game_data(temppath);
                                                }
                                        }
@@ -658,19 +781,19 @@ int main(int argc, char** argv)
                                        //::printf("game data loaded from %s\n", filename);
                                        
                                        if (!bfd) {
-                                               usage(argv[0]);
+                                               usage(argv[0], isBlenderPlayer);
                                                error = true;
                                                exitcode = KX_EXIT_REQUEST_QUIT_GAME;
                                        } 
                                        else 
                                        {
 #ifdef WIN32
-#ifdef NDEBUG
+#if !defined(DEBUG)
                                                if (closeConsole)
                                                {
                                                        //::FreeConsole();    // Close a console window
                                                }
-#endif // NDEBUG
+#endif // !defined(DEBUG)
 #endif // WIN32
                                                Main *maggie = bfd->main;
                                                Scene *scene = bfd->curscene;
@@ -688,19 +811,19 @@ int main(int argc, char** argv)
                                                if ((!fullScreenParFound) && (!windowParFound))
                                                {
                                                        // Only use file settings when command line did not override
-                                                       if (scene->r.fullscreen) {
+                                                       if (scene->gm.fullscreen) {
                                                                //printf("fullscreen option found in Blender file\n");
                                                                fullScreen = true;
-                                                               fullScreenWidth= scene->r.xplay;
-                                                               fullScreenHeight= scene->r.yplay;
-                                                               fullScreenFrequency= scene->r.freqplay;
-                                                               fullScreenBpp = scene->r.depth;
+                                                               fullScreenWidth= scene->gm.xplay;
+                                                               fullScreenHeight= scene->gm.yplay;
+                                                               fullScreenFrequency= scene->gm.freqplay;
+                                                               fullScreenBpp = scene->gm.depth;
                                                        }
                                                        else
                                                        {
                                                                fullScreen = false;
-                                                               windowWidth = scene->r.xplay;
-                                                               windowHeight = scene->r.yplay;
+                                                               windowWidth = scene->gm.xplay;
+                                                               windowHeight = scene->gm.yplay;
                                                        }
                                                }
                                                
@@ -708,18 +831,44 @@ int main(int argc, char** argv)
                                                // Check whether the game should be displayed in stereo
                                                if (!stereoParFound)
                                                {
-                                                       stereomode = (RAS_IRasterizer::StereoMode) scene->r.stereomode;
-                                                       if (stereomode == RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
-                                                               stereoWindow = true;
+                                                       if(scene->gm.stereoflag == STEREO_ENABLED){
+                                                               stereomode = (RAS_IRasterizer::StereoMode) scene->gm.stereomode;
+                                                               if (stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
+                                                                       stereoWindow = true;
+                                                       }
+                                               }
+                                               else
+                                                       scene->gm.stereoflag = STEREO_ENABLED;
+
+                                               if (stereoFlag == STEREO_DOME){
+                                                       stereomode = RAS_IRasterizer::RAS_STEREO_DOME;
+                                                       scene->gm.stereoflag = STEREO_DOME;
+                                                       if (domeFov > 89)
+                                                               scene->gm.dome.angle = domeFov;
+                                                       if (domeTilt > -180)
+                                                               scene->gm.dome.tilt = domeTilt;
+                                                       if (domeMode > 0)
+                                                               scene->gm.dome.mode = domeMode;
+                                                       if (domeWarp)
+                                                       {
+                                                               //XXX to do: convert relative to absolute path
+                                                               domeText= add_text(domeWarp, "");
+                                                               if(!domeText)
+                                                                       printf("error: invalid warpdata text file - %s\n", domeWarp);
+                                                               else
+                                                                       scene->gm.dome.warptext = domeText;
+                                                       }
                                                }
                                                
                                                //                                      GPG_Application app (system, maggie, startscenename);
                                                app.SetGameEngineData(maggie, scene, argc, argv); /* this argc cant be argc_py_clamped, since python uses it */
-                                               
                                                BLI_strncpy(pathname, maggie->name, sizeof(pathname));
-                                               BLI_strncpy(G.sce, maggie->name, sizeof(G.sce));
-                                               setGamePythonPath(G.sce);
-
+                                               if(G.main != maggie) {
+                                                       BLI_strncpy(G.main->name, maggie->name, sizeof(G.main->name));
+                                               }
+#ifdef WITH_PYTHON
+                                               setGamePythonPath(G.main->name);
+#endif
                                                if (firstTimeRunning)
                                                {
                                                        firstTimeRunning = false;
@@ -730,13 +879,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
@@ -776,16 +925,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);
                                                                }
                                                        }
                                                }
@@ -812,6 +961,10 @@ int main(int argc, char** argv)
                                                }
                                                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);
@@ -828,8 +981,21 @@ int main(int argc, char** argv)
                }
        }
 
+       // Cleanup
+       RNA_exit();
+       BLF_exit();
+       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;
 }