Blender  V2.59
KX_KetsjiEngine.h
Go to the documentation of this file.
00001 /*
00002  * $Id: KX_KetsjiEngine.h 35063 2011-02-22 10:33:14Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00021  * All rights reserved.
00022  *
00023  * The Original Code is: all of this file.
00024  *
00025  * Contributor(s): none yet.
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  *
00029  */
00030 
00035 #ifndef __KX_KETSJI_ENGINE
00036 #define __KX_KETSJI_ENGINE
00037 
00038 #include "MT_CmMatrix4x4.h"
00039 #include "MT_Matrix4x4.h"
00040 #include "STR_String.h"
00041 #include "KX_ISystem.h"
00042 #include "KX_Scene.h"
00043 #include "KX_Python.h"
00044 #include "KX_WorldInfo.h"
00045 #include <vector>
00046 #include <set>
00047 
00048 class KX_TimeCategoryLogger;
00049 
00050 #define LEFT_EYE  1
00051 #define RIGHT_EYE 2
00052 
00053 enum KX_ExitRequestMode
00054 {
00055         KX_EXIT_REQUEST_NO_REQUEST = 0,
00056         KX_EXIT_REQUEST_QUIT_GAME,
00057         KX_EXIT_REQUEST_RESTART_GAME,
00058         KX_EXIT_REQUEST_START_OTHER_GAME,
00059         KX_EXIT_REQUEST_NO_SCENES_LEFT,
00060         KX_EXIT_REQUEST_BLENDER_ESC,
00061         KX_EXIT_REQUEST_OUTSIDE,
00062         KX_EXIT_REQUEST_MAX
00063 };
00064 
00068 class KX_KetsjiEngine
00069 {
00070 
00071 private:
00072         class RAS_ICanvas*                              m_canvas; // 2D Canvas (2D Rendering Device Context)
00073         class RAS_IRasterizer*                          m_rasterizer;  // 3D Rasterizer (3D Rendering)
00074         class KX_ISystem*                               m_kxsystem;
00075         class RAS_IRenderTools*                         m_rendertools;
00076         class KX_ISceneConverter*                       m_sceneconverter;
00077         class NG_NetworkDeviceInterface*                m_networkdevice;
00078 #ifdef WITH_PYTHON
00079         /* borrowed from sys.modules["__main__"], dont manage ref's */
00080         PyObject*                                       m_pythondictionary;
00081 #endif
00082         class SCA_IInputDevice*                         m_keyboarddevice;
00083         class SCA_IInputDevice*                         m_mousedevice;
00084         class KX_Dome*                                          m_dome; // dome stereo mode
00085 
00087         std::set<STR_String> m_removingScenes;
00089         std::set<STR_String> m_addingOverlayScenes;
00091         std::set<STR_String> m_addingBackgroundScenes;
00093         std::set<std::pair<STR_String,STR_String> >     m_replace_scenes;
00094 
00095         /* The current list of scenes. */
00096         KX_SceneList            m_scenes;
00097         /* State variable recording the presence of object debug info in the current scene list. */
00098         bool                            m_propertiesPresent;    
00099 
00100         bool                            m_bInitialized;
00101         int                                     m_activecam;
00102         bool                            m_bFixedTime;
00103         
00104         
00105         bool                            m_firstframe;
00106         int                                     m_currentFrame;
00107 
00108         double                          m_frameTime;//discrete timestamp of the 'game logic frame'
00109         double                          m_clockTime;//current time
00110         double                          m_previousClockTime;//previous clock time
00111         double                          m_remainingTime;
00112 
00113         static int                              m_maxLogicFrame;        /* maximum number of consecutive logic frame */
00114         static int                              m_maxPhysicsFrame;      /* maximum number of consecutive physics frame */
00115         static double                   m_ticrate;
00116         static double                   m_anim_framerate; /* for animation playback only - ipo and action */
00117 
00118         static double                   m_suspendedtime;
00119         static double                   m_suspendeddelta;
00120 
00121         int                                     m_exitcode;
00122         STR_String                      m_exitstring;
00128         int                             m_drawingmode;
00129         float                   m_cameraZoom;
00130         
00131         bool                    m_overrideCam;  
00132         STR_String              m_overrideSceneName;
00133         
00134         bool                    m_overrideCamUseOrtho;
00135         MT_CmMatrix4x4  m_overrideCamProjMat;
00136         MT_CmMatrix4x4  m_overrideCamViewMat;
00137         float                   m_overrideCamNear;
00138         float                   m_overrideCamFar;
00139         float                   m_overrideCamLens;
00140 
00141         bool m_stereo;
00142         int m_curreye;
00143 
00145         typedef enum
00146         {
00147                 tc_first = 0,
00148                 tc_physics = 0,
00149                 tc_logic,
00150                 tc_network,
00151                 tc_scenegraph,
00152                 tc_sound,
00153                 tc_rasterizer,
00154                 tc_services,    // time spend in miscelaneous activities
00155                 tc_overhead,    // profile info drawing overhead
00156                 tc_outside,             // time spend outside main loop
00157                 tc_numCategories
00158         } KX_TimeCategory;
00159 
00161         KX_TimeCategoryLogger*  m_logger;
00162         
00164         static const char               m_profileLabels[tc_numCategories][15];
00166         static double                   m_average_framerate;
00168         bool                                    m_show_framerate;
00170         bool                                    m_show_profile;
00172         bool                                    m_showProperties;
00174         bool                                    m_showBackground;
00175 
00176         bool                                    m_show_debug_properties;
00177 
00179         bool                                    m_animation_record;
00180 
00182         bool                                    m_hideCursor;
00183 
00185         bool                                    m_overrideFrameColor;
00187         float                                   m_overrideFrameColorR;
00189         float                                   m_overrideFrameColorG;
00191         float                                   m_overrideFrameColorB;
00192 
00193         void                                    RenderFrame(KX_Scene* scene, KX_Camera* cam);
00194         void                                    PostRenderScene(KX_Scene* scene);
00195         void                                    RenderDebugProperties();
00196         void                                    RenderShadowBuffers(KX_Scene *scene);
00197         void                                    SetBackGround(KX_WorldInfo* worldinfo);
00198         void                                    DoSound(KX_Scene* scene);
00199         void                                    RenderFonts(KX_Scene* scene);
00200 
00201 public:
00202         KX_KetsjiEngine(class KX_ISystem* system);
00203         virtual ~KX_KetsjiEngine();
00204 
00205         // set the devices and stuff. the client must take care of creating these
00206         void                    SetWorldSettings(KX_WorldInfo* worldinfo);
00207         void                    SetKeyboardDevice(SCA_IInputDevice* keyboarddevice);
00208         void                    SetMouseDevice(SCA_IInputDevice* mousedevice);
00209         void                    SetNetworkDevice(NG_NetworkDeviceInterface* networkdevice);
00210         void                    SetCanvas(RAS_ICanvas* canvas);
00211         void                    SetRenderTools(RAS_IRenderTools* rendertools);
00212         void                    SetRasterizer(RAS_IRasterizer* rasterizer);
00213 #ifdef WITH_PYTHON
00214         void                    SetPyNamespace(PyObject* pythondictionary);
00215         PyObject*               GetPyNamespace(){return m_pythondictionary;};
00216 #endif
00217         void                    SetSceneConverter(KX_ISceneConverter* sceneconverter);
00218         void                    SetAnimRecordMode(bool animation_record, int startFrame);
00219 
00220         RAS_IRasterizer*                GetRasterizer(){return m_rasterizer;};
00221         RAS_ICanvas*                GetCanvas(){return m_canvas;};
00222         RAS_IRenderTools*           GetRenderTools(){return m_rendertools;};
00223         SCA_IInputDevice*               GetKeyboardDevice(){return m_keyboarddevice;};
00224         SCA_IInputDevice*               GetMouseDevice(){return m_mousedevice;};
00225 
00227         void                    InitDome(short res, short mode, short angle, float resbuf, short tilt, struct Text* text); 
00228         void                    EndDome();
00229         void                    RenderDome();
00230         bool                    m_usedome;
00231 
00233         bool                    NextFrame();
00234         void                    Render();
00235         
00236         void                    StartEngine(bool clearIpo);
00237         void                    StopEngine();
00238         void                    Export(const STR_String& filename);
00239 
00240         void                    RequestExit(int exitrequestmode);
00241         void                    SetNameNextGame(const STR_String& nextgame);
00242         int                             GetExitCode();
00243         const STR_String&       GetExitString();
00244 
00245         KX_SceneList*   CurrentScenes();
00246         KX_Scene*       FindScene(const STR_String& scenename);
00247         void                    AddScene(class KX_Scene* scene);
00248         void                    ConvertAndAddScene(const STR_String& scenename,bool overlay);
00249 
00250         void                    RemoveScene(const STR_String& scenename);
00251         void                    ReplaceScene(const STR_String& oldscene,const STR_String& newscene);
00252         void                    SuspendScene(const STR_String& scenename);
00253         void                    ResumeScene(const STR_String& scenename);
00254 
00255         void                    GetSceneViewport(KX_Scene* scene, KX_Camera* cam, RAS_Rect& area, RAS_Rect& viewport);
00256 
00257         void SetDrawType(int drawingtype);
00258         int  GetDrawType(){return m_drawingmode;};
00259 
00260         void SetCameraZoom(float camzoom);
00261         
00262         void EnableCameraOverride(const STR_String& forscene);
00263         
00264         void SetCameraOverrideUseOrtho(bool useOrtho);
00265         void SetCameraOverrideProjectionMatrix(const MT_CmMatrix4x4& mat);
00266         void SetCameraOverrideViewMatrix(const MT_CmMatrix4x4& mat);
00267         void SetCameraOverrideClipping(float near, float far);
00268         void SetCameraOverrideLens(float lens);
00269         
00274         void SetUseFixedTime(bool bUseFixedTime);
00275 
00280         bool GetUseFixedTime(void) const;
00281 
00285         double GetClockTime(void) const;
00289         double GetFrameTime(void) const;
00290 
00291         double GetRealTime(void) const;
00296         static double GetSuspendedDelta();
00297 
00301         static double GetTicRate();
00305         static void SetTicRate(double ticrate);
00309         static int GetMaxLogicFrame();
00313         static void SetMaxLogicFrame(int frame);
00317         static int GetMaxPhysicsFrame();
00321         static void SetMaxPhysicsFrame(int frame);
00322 
00326         static double GetAnimFrameRate();
00330         static void SetAnimFrameRate(double framerate);
00331 
00335         static double GetAverageFrameRate();
00336 
00343         void SetTimingDisplay(bool frameRate, bool profile, bool properties);
00344 
00351         void GetTimingDisplay(bool& frameRate, bool& profile, bool& properties) const;
00352 
00357         void SetHideCursor(bool hideCursor);
00358 
00363         bool GetHideCursor(void) const;
00364 
00369         void SetUseOverrideFrameColor(bool overrideFrameColor);
00370 
00375         bool GetUseOverrideFrameColor(void) const; 
00376 
00383         void SetOverrideFrameColor(float r, float g, float b);
00384 
00391         void GetOverrideFrameColor(float& r, float& g, float& b) const;
00392 
00393         KX_Scene*               CreateScene(const STR_String& scenename);
00394         KX_Scene*               CreateScene(Scene *scene);
00395         
00396 protected:
00403         void                    ProcessScheduledScenes(void);
00404 
00408         void                    SceneListsChanged(void);
00409 
00410         void                    RemoveScheduledScenes(void);
00411         void                    AddScheduledScenes(void);
00412         void                    ReplaceScheduledScenes(void);
00413         void                    PostProcessScene(class KX_Scene* scene);
00414         
00415         bool                    BeginFrame();
00416         void                    ClearFrame();
00417         void                    EndFrame();
00418         
00419         
00420 #ifdef WITH_CXX_GUARDEDALLOC
00421 public:
00422         void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:KX_KetsjiEngine"); }
00423         void operator delete( void *mem ) { MEM_freeN(mem); }
00424 #endif
00425 };
00426 
00427 #endif //__KX_KETSJI_ENGINE
00428 
00429