diff --git a/source/graphics/Camera.h b/source/graphics/Camera.h index 819eab536b..355588e926 100755 --- a/source/graphics/Camera.h +++ b/source/graphics/Camera.h @@ -43,7 +43,7 @@ class CCamera //everytime the view or projection matrices are //altered. void UpdateFrustum (); - CFrustum GetFustum () { return m_ViewFrustum; } + CFrustum GetFrustum () { return m_ViewFrustum; } void SetViewPort (SViewPort *viewport); SViewPort GetViewPort () { return m_ViewPort; } diff --git a/source/graphics/GameView.cpp b/source/graphics/GameView.cpp new file mode 100755 index 0000000000..65efbba29f --- /dev/null +++ b/source/graphics/GameView.cpp @@ -0,0 +1,83 @@ +#include "precompiled.h" + +#include "Terrain.h" +#include "Renderer.h" +#include "GameView.h" +#include "Game.h" +#include "Camera.h" + +extern CCamera g_Camera; + +CGameView::CGameView(CGame *pGame): + m_pGame(pGame), + m_pWorld(pGame->GetWorld()), + m_pCamera(&g_Camera) +{} + +void CGameView::Initialize(CGameAttributes *pAttribs) +{} + +void CGameView::Render() +{ + MICROLOG(L"render terrain"); + RenderTerrain(m_pWorld->GetTerrain()); + MICROLOG(L"render models"); + RenderModels(m_pWorld->GetUnitManager()); + MICROLOG(L"flush frame"); +} + +void CGameView::RenderTerrain(CTerrain *pTerrain) +{ + CFrustum frustum=m_pCamera->GetFrustum(); + u32 patchesPerSide=pTerrain->GetPatchesPerSide(); + for (uint j=0; jGetPatch(i,j); + if (frustum.IsBoxVisible (CVector3D(0,0,0),patch->GetBounds())) { + g_Renderer.Submit(patch); + } + } + } +} + +void CGameView::RenderModels(CUnitManager *pUnitMan) +{ + CFrustum frustum=m_pCamera->GetFrustum(); + + const std::vector& units=pUnitMan->GetUnits(); + for (uint i=0;iGetModel()->GetBounds())) { + SubmitModelRecursive(units[i]->GetModel()); + } + } +} + +void CGameView::SubmitModelRecursive(CModel* model) +{ + g_Renderer.Submit(model); + + const std::vector& props=model->GetProps(); + for (uint i=0;iGetUnitManager(); + CTerrain *pTerrain=m_pWorld->GetTerrain(); + + uint i,j; + const std::vector& units=pUnitMan->GetUnits(); + for (i=0;iGetModel()); + } + + u32 patchesPerSide=pTerrain->GetPatchesPerSide(); + for (j=0; jGetPatch(i,j); + g_Renderer.Submit(patch); + } + } +} diff --git a/source/graphics/GameView.h b/source/graphics/GameView.h new file mode 100755 index 0000000000..aad92c65e4 --- /dev/null +++ b/source/graphics/GameView.h @@ -0,0 +1,49 @@ +#ifndef _GameView_H +#define _GameView_H + +class CGame; +class CGameAttributes; +class CWorld; +class CTerrain; +class CUnitManager; +class CModel; +class CCamera; + +extern CCamera g_Camera; + +class CGameView +{ + CGame *m_pGame; + CWorld *m_pWorld; + CCamera *m_pCamera; + + // RenderTerrain: iterate through all terrain patches and submit all patches + // in viewing frustum to the renderer + void RenderTerrain(CTerrain *pTerrain); + + // RenderModels: iterate through model list and submit all models in viewing + // frustum to the Renderer + void RenderModels(CUnitManager *pUnitMan); + + // SubmitModelRecursive: recurse down given model, submitting it and all its + // descendents to the renderer + void SubmitModelRecursive(CModel *pModel); +public: + CGameView(CGame *pGame); + + void Initialize(CGameAttributes *pGameAttributes); + + /* + Render the World + */ + void Render(); + + // RenderNoCull: render absolutely everything to a blank frame to force + // renderer to load required assets + void RenderNoCull(); + + inline CCamera *GetCamera() + { return m_pCamera; } +}; + +#endif diff --git a/source/main.cpp b/source/main.cpp index ee1ddfc31d..e6e77c716c 100755 --- a/source/main.cpp +++ b/source/main.cpp @@ -21,6 +21,8 @@ #include "ps/CConsole.h" +#include "ps/Game.h" + #include "Config.h" #include "MapReader.h" #include "Terrain.h" @@ -54,7 +56,6 @@ #include "gui/GUI.h" #endif - CConsole* g_Console = 0; extern int conInputHandler(const SDL_Event* ev); @@ -70,7 +71,6 @@ int g_xres, g_yres; int g_bpp; int g_freq; bool g_active = true; -const int SIM_FRAMERATE = 10; // flag to disable extended GL extensions until fix found - specifically, crashes // using VBOs on laptop Radeon cards @@ -87,14 +87,12 @@ static bool g_EntGraph = false; static float g_Gamma = 1.0f; -// mapfile to load or null for no map (and to use default terrain) -static const char* g_MapFile=0; - +CGameAttributes g_GameAttributes; +CGame *g_Game=NULL; static Handle g_Font_Console; // for the console static Handle g_Font_Misc; // random font for miscellaneous things - extern CCamera g_Camera; extern void terr_init(); @@ -104,24 +102,11 @@ extern int terr_handler(const SDL_Event* ev); extern int allow_reload(); extern int dir_add_watch(const char* const dir, bool watch_subdirs); - - - - extern void sle(int); - - -size_t frameCount=0; -size_t simulationTime = 0; +extern size_t frameCount; static bool quit = false; // break out of main loop - - - - - - const wchar_t* HardcodedErrorString(int err) { #define E(sym) case sym: return L ## #sym; @@ -156,7 +141,6 @@ ERROR_GROUP(System); ERROR_TYPE(System, SDLInitFailed); ERROR_TYPE(System, VmodeFailed); ERROR_TYPE(System, RequiredExtensionsMissing); -ERROR_TYPE(System, MapLoadFailed); void Testing (void) @@ -369,52 +353,6 @@ static int handler(const SDL_Event* ev) return EV_PASS; } - -////////////////////////////////////////////////////////////////////////////////////////////////// -// RenderTerrain: iterate through all terrain patches and submit all patches in viewing frustum to -// the renderer -void RenderTerrain() -{ - CFrustum frustum=g_Camera.GetFustum(); - u32 patchesPerSide=g_Terrain.GetPatchesPerSide(); - for (uint j=0; jGetBounds())) { - g_Renderer.Submit(patch); - } - } - } -} - -////////////////////////////////////////////////////////////////////////////////////////////////// -// SubmitModelRecursive: recurse down given model, submitting it and all its descendents to the -// renderer -void SubmitModelRecursive(CModel* model) -{ - g_Renderer.Submit(model); - - const std::vector& props=model->GetProps(); - for (uint i=0;i& units=g_UnitMan.GetUnits(); - for (uint i=0;iGetModel()->GetBounds())) { - SubmitModelRecursive(units[i]->GetModel()); - } - } -} - ///////////////////////////////////////////////////////////////////////////////////////////// // RenderNoCull: render absolutely everything to a blank frame to force renderer // to load required assets @@ -422,20 +360,8 @@ void RenderNoCull() { g_Renderer.BeginFrame(); g_Renderer.SetCamera(g_Camera); - - uint i,j; - const std::vector& units=g_UnitMan.GetUnits(); - for (i=0;iGetModel()); - } - - u32 patchesPerSide=g_Terrain.GetPatchesPerSide(); - for (j=0; jGetView()->RenderNoCull(); g_Renderer.FlushFrame(); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); @@ -453,11 +379,9 @@ static void Render() // switch on wireframe for terrain if we want it //g_Renderer.SetTerrainRenderMode( SOLID ); // (PT: If this is done here, the W key doesn't work) - MICROLOG(L"render terrain"); - RenderTerrain(); - MICROLOG(L"render models"); - RenderModels(); - MICROLOG(L"flush frame"); + + g_Game->Render(); + g_Renderer.FlushFrame(); glPushAttrib( GL_ENABLE_BIT ); @@ -543,33 +467,6 @@ static void Render() g_Renderer.EndFrame(); } - -////////////////////////////////////////////////////////////////////////////////////////////// -// UpdateWorld: update time dependent data in the simulation to account for changes over -// some fixed simulation timestep. - -void UpdateWorld(float time) -{ - g_GUI.TickObjects(); - g_Scheduler.update(); - simulationTime += (size_t)( time * 1000.0f ); - frameCount++; - g_EntityManager.updateAll( time ); -} - -////////////////////////////////////////////////////////////////////////////////////////////// -// InterpolateWorld: interpolate a data point for rendering between simulation frames. - -void InterpolateWorld( float delta, float offset ) -{ - const std::vector& units=g_UnitMan.GetUnits(); - for (uint i=0;iGetModel()->Update( delta ); - - g_EntityManager.interpolateAll( offset ); -} - - void ParseArgs(int argc, char* argv[]) { for (int i=1;iInitialize(&g_GameAttributes); // Check for heap corruption after every allocation. Very, very slowly. // (And it highlights the allocation just after the one you care about, @@ -1075,8 +961,6 @@ static void Frame() // Non-movie code: static double last_time; - static double delta_time = 0.0; // The amount by which the displayed frame is lagging - // the simulation. const double time = get_time(); const float TimeSinceLastFrame = (float)(time-last_time); last_time = time; @@ -1092,34 +976,14 @@ static void Frame() // TODO: limiter in case simulation can't keep up? // const double TICK_TIME = 30e-3; // [s] - const float SIM_UPDATE_INTERVAL = 1.0f / (float)SIM_FRAMERATE; // Simulation runs at 10 fps. +// const float SIM_UPDATE_INTERVAL = 1.0f / (float)SIM_FRAMERATE; // Simulation runs at 10 fps. MICROLOG(L"input"); in_get_events(); - - delta_time += TimeSinceLastFrame; - - if( delta_time >= 0.0 ) - { - // A new simulation frame is required. - MICROLOG( L"calculate simulation" ); - UpdateWorld( SIM_UPDATE_INTERVAL ); - delta_time -= SIM_UPDATE_INTERVAL; - if( delta_time >= 0.0 ) - { - // The desired sim frame rate can't be achieved. Settle for process & render - // frames as fast as possible. - // g_Console->InsertMessage( L"Can't maintain %d FPS simulation rate!", SIM_FRAMERATE ); - delta_time = 0.0; - } - } - - MICROLOG(L"interpolate frame"); - - InterpolateWorld( TimeSinceLastFrame, (float)( delta_time / (double)SIM_UPDATE_INTERVAL ) + 1.0f ); - + g_Game->Update(TimeSinceLastFrame); + if (!g_FixedFrameTiming) terr_update(float(TimeSinceLastFrame)); @@ -1143,8 +1007,6 @@ static void Frame() mouseButtons[SDL_BUTTON_WHEELUP] = false; mouseButtons[SDL_BUTTON_WHEELDOWN] = false; - - if(g_active) { MICROLOG(L"render"); diff --git a/source/ps/Errors.cpp b/source/ps/Errors.cpp index cffc3184ea..92a0c3538b 100755 --- a/source/ps/Errors.cpp +++ b/source/ps/Errors.cpp @@ -5,16 +5,19 @@ #include "Errors.h" class PSERROR_GUI : public PSERROR {}; +class PSERROR_Game : public PSERROR {}; class PSERROR_Renderer : public PSERROR {}; class PSERROR_Scripting : public PSERROR {}; class PSERROR_System : public PSERROR {}; class PSERROR_Xeromyces : public PSERROR {}; +class PSERROR_Game_World : public PSERROR_Game {}; class PSERROR_Scripting_DefineType : public PSERROR_Scripting {}; class PSERROR_Scripting_LoadFile : public PSERROR_Scripting {}; class PSERROR_GUI_JSOpenFailed : public PSERROR_GUI { public: PSERROR_GUI_JSOpenFailed(); }; class PSERROR_GUI_TextureLoadFailed : public PSERROR_GUI { public: PSERROR_GUI_TextureLoadFailed(); }; +class PSERROR_Game_World_MapLoadFailed : public PSERROR_Game_World { public: PSERROR_Game_World_MapLoadFailed(); }; class PSERROR_Renderer_VBOFailed : public PSERROR_Renderer { public: PSERROR_Renderer_VBOFailed(); }; class PSERROR_Scripting_CallFunctionFailed : public PSERROR_Scripting { public: PSERROR_Scripting_CallFunctionFailed(); }; class PSERROR_Scripting_ContextCreationFailed : public PSERROR_Scripting { public: PSERROR_Scripting_ContextCreationFailed(); }; @@ -31,7 +34,6 @@ class PSERROR_Scripting_RegisterFunctionFailed : public PSERROR_Scripting { publ class PSERROR_Scripting_RuntimeCreationFailed : public PSERROR_Scripting { public: PSERROR_Scripting_RuntimeCreationFailed(); }; class PSERROR_Scripting_StandardClassSetupFailed : public PSERROR_Scripting { public: PSERROR_Scripting_StandardClassSetupFailed(); }; class PSERROR_Scripting_TypeDoesNotExist : public PSERROR_Scripting { public: PSERROR_Scripting_TypeDoesNotExist(); }; -class PSERROR_System_MapLoadFailed : public PSERROR_System { public: PSERROR_System_MapLoadFailed(); }; class PSERROR_System_RequiredExtensionsMissing : public PSERROR_System { public: PSERROR_System_RequiredExtensionsMissing(); }; class PSERROR_System_SDLInitFailed : public PSERROR_System { public: PSERROR_System_SDLInitFailed(); }; class PSERROR_System_VmodeFailed : public PSERROR_System { public: PSERROR_System_VmodeFailed(); }; @@ -40,117 +42,121 @@ class PSERROR_Xeromyces_XMLParseError : public PSERROR_Xeromyces { public: PSERR extern const PSRETURN PSRETURN_GUI_JSOpenFailed = 0x01000001; extern const PSRETURN PSRETURN_GUI_TextureLoadFailed = 0x01000002; -extern const PSRETURN PSRETURN_Renderer_VBOFailed = 0x02000001; -extern const PSRETURN PSRETURN_Scripting_DefineType_AlreadyExists = 0x03010001; -extern const PSRETURN PSRETURN_Scripting_DefineType_CreationFailed = 0x03010002; -extern const PSRETURN PSRETURN_Scripting_LoadFile_EvalErrors = 0x03020001; -extern const PSRETURN PSRETURN_Scripting_LoadFile_OpenFailed = 0x03020002; -extern const PSRETURN PSRETURN_Scripting_CallFunctionFailed = 0x03000001; -extern const PSRETURN PSRETURN_Scripting_ContextCreationFailed = 0x03000002; -extern const PSRETURN PSRETURN_Scripting_ConversionFailed = 0x03000003; -extern const PSRETURN PSRETURN_Scripting_CreateObjectFailed = 0x03000004; -extern const PSRETURN PSRETURN_Scripting_DefineConstantFailed = 0x03000005; -extern const PSRETURN PSRETURN_Scripting_GlobalObjectCreationFailed = 0x03000006; -extern const PSRETURN PSRETURN_Scripting_NativeFunctionSetupFailed = 0x03000007; -extern const PSRETURN PSRETURN_Scripting_RegisterFunctionFailed = 0x03000008; -extern const PSRETURN PSRETURN_Scripting_RuntimeCreationFailed = 0x03000009; -extern const PSRETURN PSRETURN_Scripting_StandardClassSetupFailed = 0x0300000a; -extern const PSRETURN PSRETURN_Scripting_TypeDoesNotExist = 0x0300000b; -extern const PSRETURN PSRETURN_System_MapLoadFailed = 0x04000001; -extern const PSRETURN PSRETURN_System_RequiredExtensionsMissing = 0x04000002; -extern const PSRETURN PSRETURN_System_SDLInitFailed = 0x04000003; -extern const PSRETURN PSRETURN_System_VmodeFailed = 0x04000004; -extern const PSRETURN PSRETURN_Xeromyces_XMLOpenFailed = 0x05000001; -extern const PSRETURN PSRETURN_Xeromyces_XMLParseError = 0x05000002; +extern const PSRETURN PSRETURN_Game_World_MapLoadFailed = 0x02030001; +extern const PSRETURN PSRETURN_Renderer_VBOFailed = 0x03000001; +extern const PSRETURN PSRETURN_Scripting_DefineType_AlreadyExists = 0x04010001; +extern const PSRETURN PSRETURN_Scripting_DefineType_CreationFailed = 0x04010002; +extern const PSRETURN PSRETURN_Scripting_LoadFile_EvalErrors = 0x04020001; +extern const PSRETURN PSRETURN_Scripting_LoadFile_OpenFailed = 0x04020002; +extern const PSRETURN PSRETURN_Scripting_CallFunctionFailed = 0x04000001; +extern const PSRETURN PSRETURN_Scripting_ContextCreationFailed = 0x04000002; +extern const PSRETURN PSRETURN_Scripting_ConversionFailed = 0x04000003; +extern const PSRETURN PSRETURN_Scripting_CreateObjectFailed = 0x04000004; +extern const PSRETURN PSRETURN_Scripting_DefineConstantFailed = 0x04000005; +extern const PSRETURN PSRETURN_Scripting_GlobalObjectCreationFailed = 0x04000006; +extern const PSRETURN PSRETURN_Scripting_NativeFunctionSetupFailed = 0x04000007; +extern const PSRETURN PSRETURN_Scripting_RegisterFunctionFailed = 0x04000008; +extern const PSRETURN PSRETURN_Scripting_RuntimeCreationFailed = 0x04000009; +extern const PSRETURN PSRETURN_Scripting_StandardClassSetupFailed = 0x0400000a; +extern const PSRETURN PSRETURN_Scripting_TypeDoesNotExist = 0x0400000b; +extern const PSRETURN PSRETURN_System_RequiredExtensionsMissing = 0x05000001; +extern const PSRETURN PSRETURN_System_SDLInitFailed = 0x05000002; +extern const PSRETURN PSRETURN_System_VmodeFailed = 0x05000003; +extern const PSRETURN PSRETURN_Xeromyces_XMLOpenFailed = 0x06000001; +extern const PSRETURN PSRETURN_Xeromyces_XMLParseError = 0x06000002; extern const PSRETURN MASK__PSRETURN_GUI = 0xff000000; extern const PSRETURN CODE__PSRETURN_GUI = 0x01000000; +extern const PSRETURN MASK__PSRETURN_Game = 0xff000000; +extern const PSRETURN CODE__PSRETURN_Game = 0x02000000; extern const PSRETURN MASK__PSRETURN_Renderer = 0xff000000; -extern const PSRETURN CODE__PSRETURN_Renderer = 0x02000000; +extern const PSRETURN CODE__PSRETURN_Renderer = 0x03000000; extern const PSRETURN MASK__PSRETURN_Scripting = 0xff000000; -extern const PSRETURN CODE__PSRETURN_Scripting = 0x03000000; +extern const PSRETURN CODE__PSRETURN_Scripting = 0x04000000; extern const PSRETURN MASK__PSRETURN_System = 0xff000000; -extern const PSRETURN CODE__PSRETURN_System = 0x04000000; +extern const PSRETURN CODE__PSRETURN_System = 0x05000000; extern const PSRETURN MASK__PSRETURN_Xeromyces = 0xff000000; -extern const PSRETURN CODE__PSRETURN_Xeromyces = 0x05000000; +extern const PSRETURN CODE__PSRETURN_Xeromyces = 0x06000000; +extern const PSRETURN MASK__PSRETURN_Game_World = 0xffff0000; +extern const PSRETURN CODE__PSRETURN_Game_World = 0x02030000; extern const PSRETURN MASK__PSRETURN_Scripting_DefineType = 0xffff0000; -extern const PSRETURN CODE__PSRETURN_Scripting_DefineType = 0x03010000; +extern const PSRETURN CODE__PSRETURN_Scripting_DefineType = 0x04010000; extern const PSRETURN MASK__PSRETURN_Scripting_LoadFile = 0xffff0000; -extern const PSRETURN CODE__PSRETURN_Scripting_LoadFile = 0x03020000; +extern const PSRETURN CODE__PSRETURN_Scripting_LoadFile = 0x04020000; extern const PSRETURN MASK__PSRETURN_GUI_JSOpenFailed = 0xffffffff; extern const PSRETURN CODE__PSRETURN_GUI_JSOpenFailed = 0x01000001; extern const PSRETURN MASK__PSRETURN_GUI_TextureLoadFailed = 0xffffffff; extern const PSRETURN CODE__PSRETURN_GUI_TextureLoadFailed = 0x01000002; +extern const PSRETURN MASK__PSRETURN_Game_World_MapLoadFailed = 0xffffffff; +extern const PSRETURN CODE__PSRETURN_Game_World_MapLoadFailed = 0x02030001; extern const PSRETURN MASK__PSRETURN_Renderer_VBOFailed = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_Renderer_VBOFailed = 0x02000001; +extern const PSRETURN CODE__PSRETURN_Renderer_VBOFailed = 0x03000001; extern const PSRETURN MASK__PSRETURN_Scripting_DefineType_AlreadyExists = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_Scripting_DefineType_AlreadyExists = 0x03010001; +extern const PSRETURN CODE__PSRETURN_Scripting_DefineType_AlreadyExists = 0x04010001; extern const PSRETURN MASK__PSRETURN_Scripting_DefineType_CreationFailed = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_Scripting_DefineType_CreationFailed = 0x03010002; +extern const PSRETURN CODE__PSRETURN_Scripting_DefineType_CreationFailed = 0x04010002; extern const PSRETURN MASK__PSRETURN_Scripting_LoadFile_EvalErrors = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_Scripting_LoadFile_EvalErrors = 0x03020001; +extern const PSRETURN CODE__PSRETURN_Scripting_LoadFile_EvalErrors = 0x04020001; extern const PSRETURN MASK__PSRETURN_Scripting_LoadFile_OpenFailed = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_Scripting_LoadFile_OpenFailed = 0x03020002; +extern const PSRETURN CODE__PSRETURN_Scripting_LoadFile_OpenFailed = 0x04020002; extern const PSRETURN MASK__PSRETURN_Scripting_CallFunctionFailed = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_Scripting_CallFunctionFailed = 0x03000001; +extern const PSRETURN CODE__PSRETURN_Scripting_CallFunctionFailed = 0x04000001; extern const PSRETURN MASK__PSRETURN_Scripting_ContextCreationFailed = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_Scripting_ContextCreationFailed = 0x03000002; +extern const PSRETURN CODE__PSRETURN_Scripting_ContextCreationFailed = 0x04000002; extern const PSRETURN MASK__PSRETURN_Scripting_ConversionFailed = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_Scripting_ConversionFailed = 0x03000003; +extern const PSRETURN CODE__PSRETURN_Scripting_ConversionFailed = 0x04000003; extern const PSRETURN MASK__PSRETURN_Scripting_CreateObjectFailed = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_Scripting_CreateObjectFailed = 0x03000004; +extern const PSRETURN CODE__PSRETURN_Scripting_CreateObjectFailed = 0x04000004; extern const PSRETURN MASK__PSRETURN_Scripting_DefineConstantFailed = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_Scripting_DefineConstantFailed = 0x03000005; +extern const PSRETURN CODE__PSRETURN_Scripting_DefineConstantFailed = 0x04000005; extern const PSRETURN MASK__PSRETURN_Scripting_GlobalObjectCreationFailed = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_Scripting_GlobalObjectCreationFailed = 0x03000006; +extern const PSRETURN CODE__PSRETURN_Scripting_GlobalObjectCreationFailed = 0x04000006; extern const PSRETURN MASK__PSRETURN_Scripting_NativeFunctionSetupFailed = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_Scripting_NativeFunctionSetupFailed = 0x03000007; +extern const PSRETURN CODE__PSRETURN_Scripting_NativeFunctionSetupFailed = 0x04000007; extern const PSRETURN MASK__PSRETURN_Scripting_RegisterFunctionFailed = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_Scripting_RegisterFunctionFailed = 0x03000008; +extern const PSRETURN CODE__PSRETURN_Scripting_RegisterFunctionFailed = 0x04000008; extern const PSRETURN MASK__PSRETURN_Scripting_RuntimeCreationFailed = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_Scripting_RuntimeCreationFailed = 0x03000009; +extern const PSRETURN CODE__PSRETURN_Scripting_RuntimeCreationFailed = 0x04000009; extern const PSRETURN MASK__PSRETURN_Scripting_StandardClassSetupFailed = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_Scripting_StandardClassSetupFailed = 0x0300000a; +extern const PSRETURN CODE__PSRETURN_Scripting_StandardClassSetupFailed = 0x0400000a; extern const PSRETURN MASK__PSRETURN_Scripting_TypeDoesNotExist = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_Scripting_TypeDoesNotExist = 0x0300000b; -extern const PSRETURN MASK__PSRETURN_System_MapLoadFailed = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_System_MapLoadFailed = 0x04000001; +extern const PSRETURN CODE__PSRETURN_Scripting_TypeDoesNotExist = 0x0400000b; extern const PSRETURN MASK__PSRETURN_System_RequiredExtensionsMissing = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_System_RequiredExtensionsMissing = 0x04000002; +extern const PSRETURN CODE__PSRETURN_System_RequiredExtensionsMissing = 0x05000001; extern const PSRETURN MASK__PSRETURN_System_SDLInitFailed = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_System_SDLInitFailed = 0x04000003; +extern const PSRETURN CODE__PSRETURN_System_SDLInitFailed = 0x05000002; extern const PSRETURN MASK__PSRETURN_System_VmodeFailed = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_System_VmodeFailed = 0x04000004; +extern const PSRETURN CODE__PSRETURN_System_VmodeFailed = 0x05000003; extern const PSRETURN MASK__PSRETURN_Xeromyces_XMLOpenFailed = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_Xeromyces_XMLOpenFailed = 0x05000001; +extern const PSRETURN CODE__PSRETURN_Xeromyces_XMLOpenFailed = 0x06000001; extern const PSRETURN MASK__PSRETURN_Xeromyces_XMLParseError = 0xffffffff; -extern const PSRETURN CODE__PSRETURN_Xeromyces_XMLParseError = 0x05000002; +extern const PSRETURN CODE__PSRETURN_Xeromyces_XMLParseError = 0x06000002; PSERROR_GUI_JSOpenFailed::PSERROR_GUI_JSOpenFailed() { magic=0x45725221; code=0x01000001; } PSERROR_GUI_TextureLoadFailed::PSERROR_GUI_TextureLoadFailed() { magic=0x45725221; code=0x01000002; } -PSERROR_Renderer_VBOFailed::PSERROR_Renderer_VBOFailed() { magic=0x45725221; code=0x02000001; } -PSERROR_Scripting_DefineType_AlreadyExists::PSERROR_Scripting_DefineType_AlreadyExists() { magic=0x45725221; code=0x03010001; } -PSERROR_Scripting_DefineType_CreationFailed::PSERROR_Scripting_DefineType_CreationFailed() { magic=0x45725221; code=0x03010002; } -PSERROR_Scripting_LoadFile_EvalErrors::PSERROR_Scripting_LoadFile_EvalErrors() { magic=0x45725221; code=0x03020001; } -PSERROR_Scripting_LoadFile_OpenFailed::PSERROR_Scripting_LoadFile_OpenFailed() { magic=0x45725221; code=0x03020002; } -PSERROR_Scripting_CallFunctionFailed::PSERROR_Scripting_CallFunctionFailed() { magic=0x45725221; code=0x03000001; } -PSERROR_Scripting_ContextCreationFailed::PSERROR_Scripting_ContextCreationFailed() { magic=0x45725221; code=0x03000002; } -PSERROR_Scripting_ConversionFailed::PSERROR_Scripting_ConversionFailed() { magic=0x45725221; code=0x03000003; } -PSERROR_Scripting_CreateObjectFailed::PSERROR_Scripting_CreateObjectFailed() { magic=0x45725221; code=0x03000004; } -PSERROR_Scripting_DefineConstantFailed::PSERROR_Scripting_DefineConstantFailed() { magic=0x45725221; code=0x03000005; } -PSERROR_Scripting_GlobalObjectCreationFailed::PSERROR_Scripting_GlobalObjectCreationFailed() { magic=0x45725221; code=0x03000006; } -PSERROR_Scripting_NativeFunctionSetupFailed::PSERROR_Scripting_NativeFunctionSetupFailed() { magic=0x45725221; code=0x03000007; } -PSERROR_Scripting_RegisterFunctionFailed::PSERROR_Scripting_RegisterFunctionFailed() { magic=0x45725221; code=0x03000008; } -PSERROR_Scripting_RuntimeCreationFailed::PSERROR_Scripting_RuntimeCreationFailed() { magic=0x45725221; code=0x03000009; } -PSERROR_Scripting_StandardClassSetupFailed::PSERROR_Scripting_StandardClassSetupFailed() { magic=0x45725221; code=0x0300000a; } -PSERROR_Scripting_TypeDoesNotExist::PSERROR_Scripting_TypeDoesNotExist() { magic=0x45725221; code=0x0300000b; } -PSERROR_System_MapLoadFailed::PSERROR_System_MapLoadFailed() { magic=0x45725221; code=0x04000001; } -PSERROR_System_RequiredExtensionsMissing::PSERROR_System_RequiredExtensionsMissing() { magic=0x45725221; code=0x04000002; } -PSERROR_System_SDLInitFailed::PSERROR_System_SDLInitFailed() { magic=0x45725221; code=0x04000003; } -PSERROR_System_VmodeFailed::PSERROR_System_VmodeFailed() { magic=0x45725221; code=0x04000004; } -PSERROR_Xeromyces_XMLOpenFailed::PSERROR_Xeromyces_XMLOpenFailed() { magic=0x45725221; code=0x05000001; } -PSERROR_Xeromyces_XMLParseError::PSERROR_Xeromyces_XMLParseError() { magic=0x45725221; code=0x05000002; } +PSERROR_Game_World_MapLoadFailed::PSERROR_Game_World_MapLoadFailed() { magic=0x45725221; code=0x02030001; } +PSERROR_Renderer_VBOFailed::PSERROR_Renderer_VBOFailed() { magic=0x45725221; code=0x03000001; } +PSERROR_Scripting_DefineType_AlreadyExists::PSERROR_Scripting_DefineType_AlreadyExists() { magic=0x45725221; code=0x04010001; } +PSERROR_Scripting_DefineType_CreationFailed::PSERROR_Scripting_DefineType_CreationFailed() { magic=0x45725221; code=0x04010002; } +PSERROR_Scripting_LoadFile_EvalErrors::PSERROR_Scripting_LoadFile_EvalErrors() { magic=0x45725221; code=0x04020001; } +PSERROR_Scripting_LoadFile_OpenFailed::PSERROR_Scripting_LoadFile_OpenFailed() { magic=0x45725221; code=0x04020002; } +PSERROR_Scripting_CallFunctionFailed::PSERROR_Scripting_CallFunctionFailed() { magic=0x45725221; code=0x04000001; } +PSERROR_Scripting_ContextCreationFailed::PSERROR_Scripting_ContextCreationFailed() { magic=0x45725221; code=0x04000002; } +PSERROR_Scripting_ConversionFailed::PSERROR_Scripting_ConversionFailed() { magic=0x45725221; code=0x04000003; } +PSERROR_Scripting_CreateObjectFailed::PSERROR_Scripting_CreateObjectFailed() { magic=0x45725221; code=0x04000004; } +PSERROR_Scripting_DefineConstantFailed::PSERROR_Scripting_DefineConstantFailed() { magic=0x45725221; code=0x04000005; } +PSERROR_Scripting_GlobalObjectCreationFailed::PSERROR_Scripting_GlobalObjectCreationFailed() { magic=0x45725221; code=0x04000006; } +PSERROR_Scripting_NativeFunctionSetupFailed::PSERROR_Scripting_NativeFunctionSetupFailed() { magic=0x45725221; code=0x04000007; } +PSERROR_Scripting_RegisterFunctionFailed::PSERROR_Scripting_RegisterFunctionFailed() { magic=0x45725221; code=0x04000008; } +PSERROR_Scripting_RuntimeCreationFailed::PSERROR_Scripting_RuntimeCreationFailed() { magic=0x45725221; code=0x04000009; } +PSERROR_Scripting_StandardClassSetupFailed::PSERROR_Scripting_StandardClassSetupFailed() { magic=0x45725221; code=0x0400000a; } +PSERROR_Scripting_TypeDoesNotExist::PSERROR_Scripting_TypeDoesNotExist() { magic=0x45725221; code=0x0400000b; } +PSERROR_System_RequiredExtensionsMissing::PSERROR_System_RequiredExtensionsMissing() { magic=0x45725221; code=0x05000001; } +PSERROR_System_SDLInitFailed::PSERROR_System_SDLInitFailed() { magic=0x45725221; code=0x05000002; } +PSERROR_System_VmodeFailed::PSERROR_System_VmodeFailed() { magic=0x45725221; code=0x05000003; } +PSERROR_Xeromyces_XMLOpenFailed::PSERROR_Xeromyces_XMLOpenFailed() { magic=0x45725221; code=0x06000001; } +PSERROR_Xeromyces_XMLParseError::PSERROR_Xeromyces_XMLParseError() { magic=0x45725221; code=0x06000002; } const wchar_t* GetErrorString(PSRETURN code) { @@ -158,28 +164,28 @@ const wchar_t* GetErrorString(PSRETURN code) { case 0x01000001: return L"GUI_JSOpenFailed"; break; case 0x01000002: return L"GUI_TextureLoadFailed"; break; - case 0x02000001: return L"Renderer_VBOFailed"; break; - case 0x03010001: return L"Scripting_DefineType_AlreadyExists"; break; - case 0x03010002: return L"Scripting_DefineType_CreationFailed"; break; - case 0x03020001: return L"Scripting_LoadFile_EvalErrors"; break; - case 0x03020002: return L"Scripting_LoadFile_OpenFailed"; break; - case 0x03000001: return L"Scripting_CallFunctionFailed"; break; - case 0x03000002: return L"Scripting_ContextCreationFailed"; break; - case 0x03000003: return L"Scripting_ConversionFailed"; break; - case 0x03000004: return L"Scripting_CreateObjectFailed"; break; - case 0x03000005: return L"Scripting_DefineConstantFailed"; break; - case 0x03000006: return L"Scripting_GlobalObjectCreationFailed"; break; - case 0x03000007: return L"Scripting_NativeFunctionSetupFailed"; break; - case 0x03000008: return L"Scripting_RegisterFunctionFailed"; break; - case 0x03000009: return L"Scripting_RuntimeCreationFailed"; break; - case 0x0300000a: return L"Scripting_StandardClassSetupFailed"; break; - case 0x0300000b: return L"Scripting_TypeDoesNotExist"; break; - case 0x04000001: return L"System_MapLoadFailed"; break; - case 0x04000002: return L"System_RequiredExtensionsMissing"; break; - case 0x04000003: return L"System_SDLInitFailed"; break; - case 0x04000004: return L"System_VmodeFailed"; break; - case 0x05000001: return L"Xeromyces_XMLOpenFailed"; break; - case 0x05000002: return L"Xeromyces_XMLParseError"; break; + case 0x02030001: return L"Game_World_MapLoadFailed"; break; + case 0x03000001: return L"Renderer_VBOFailed"; break; + case 0x04010001: return L"Scripting_DefineType_AlreadyExists"; break; + case 0x04010002: return L"Scripting_DefineType_CreationFailed"; break; + case 0x04020001: return L"Scripting_LoadFile_EvalErrors"; break; + case 0x04020002: return L"Scripting_LoadFile_OpenFailed"; break; + case 0x04000001: return L"Scripting_CallFunctionFailed"; break; + case 0x04000002: return L"Scripting_ContextCreationFailed"; break; + case 0x04000003: return L"Scripting_ConversionFailed"; break; + case 0x04000004: return L"Scripting_CreateObjectFailed"; break; + case 0x04000005: return L"Scripting_DefineConstantFailed"; break; + case 0x04000006: return L"Scripting_GlobalObjectCreationFailed"; break; + case 0x04000007: return L"Scripting_NativeFunctionSetupFailed"; break; + case 0x04000008: return L"Scripting_RegisterFunctionFailed"; break; + case 0x04000009: return L"Scripting_RuntimeCreationFailed"; break; + case 0x0400000a: return L"Scripting_StandardClassSetupFailed"; break; + case 0x0400000b: return L"Scripting_TypeDoesNotExist"; break; + case 0x05000001: return L"System_RequiredExtensionsMissing"; break; + case 0x05000002: return L"System_SDLInitFailed"; break; + case 0x05000003: return L"System_VmodeFailed"; break; + case 0x06000001: return L"Xeromyces_XMLOpenFailed"; break; + case 0x06000002: return L"Xeromyces_XMLParseError"; break; } return L"Unrecognised error"; } diff --git a/source/ps/Game.cpp b/source/ps/Game.cpp new file mode 100755 index 0000000000..36b864843e --- /dev/null +++ b/source/ps/Game.cpp @@ -0,0 +1,22 @@ +#include "precompiled.h" + +#include "Game.h" + +void CGame::Initialize(CGameAttributes *pAttribs) +{ + m_World.Initialize(pAttribs); + m_Simulation.Initialize(pAttribs); + m_GameView.Initialize(pAttribs); +} + +void CGame::Render() +{ + m_GameView.Render(); +} + +void CGame::Update(double deltaTime) +{ + m_Simulation.Update(deltaTime); + + // TODO Detect game over and bring up the summary screen or something +} diff --git a/source/ps/Game.h b/source/ps/Game.h new file mode 100755 index 0000000000..70d0d26c20 --- /dev/null +++ b/source/ps/Game.h @@ -0,0 +1,69 @@ +#ifndef _ps_Game_H +#define _ps_Game_H + +// Kludge: Our included headers might want to subgroup the Game group, so do it +// here, before including the other guys +#include "Errors.h" +ERROR_GROUP(Game); + +#include "World.h" +#include "Simulation.h" +#include "Player.h" +#include "GameView.h" + +#define PS_MAX_PLAYERS 6 + +class CGameAttributes +{ +public: + inline CGameAttributes(): + m_MapFile(NULL) + {} + + // The VFS path of the mapfile to load or NULL for no map (and to use + // default terrain) + const char *m_MapFile; +}; + +class CGame +{ + CWorld m_World; + CSimulation m_Simulation; + CGameView m_GameView; + + CPlayer *m_Players[PS_MAX_PLAYERS]; + CPlayer *m_pLocalPlayer; + +public: + inline CGame(): + m_World(this), + m_Simulation(this), + m_GameView(this) + { + // TODO When are players created? + // TODO Probably should at least reset in here though + } + + /* + Initialize all local state and members for playing a game described by + the attribute class. + */ + void Initialize(CGameAttributes *pGameAttributes); + + /* + Perform all per-frame updates + */ + void Update(double deltaTime); + + /* + Render the game + */ + void Render(); + + inline CWorld *GetWorld() + { return &m_World; } + inline CGameView *GetView() + { return &m_GameView; } +}; + +#endif diff --git a/source/ps/Interact.cpp b/source/ps/Interact.cpp index 224b51590f..46f9f03c0d 100755 --- a/source/ps/Interact.cpp +++ b/source/ps/Interact.cpp @@ -855,7 +855,7 @@ int interactInputHandler( const SDL_Event* ev ) bool isOnScreen( CEntity* ev ) { - CFrustum frustum = g_Camera.GetFustum(); + CFrustum frustum = g_Camera.GetFrustum(); return( frustum.IsBoxVisible( CVector3D(), ev->m_actor->GetModel()->GetBounds() ) ); } @@ -901,4 +901,4 @@ void popCameraTarget() { cameraDelta = cameraTargets.back() - g_Camera.m_Orientation.GetTranslation(); cameraTargets.pop_back(); -} \ No newline at end of file +} diff --git a/source/ps/Player.h b/source/ps/Player.h new file mode 100755 index 0000000000..17ba47e8bf --- /dev/null +++ b/source/ps/Player.h @@ -0,0 +1,8 @@ +#ifndef _Player_H +#define _Player_H + +class CPlayer +{ +}; + +#endif diff --git a/source/ps/World.cpp b/source/ps/World.cpp new file mode 100755 index 0000000000..c7d71aa36d --- /dev/null +++ b/source/ps/World.cpp @@ -0,0 +1,28 @@ +#include "precompiled.h" + +#include "CStr.h" +#include "CLogger.h" +#include "Errors.h" + +#include "World.h" +#include "MapReader.h" +#include "Game.h" +#include "Terrain.h" + +CTerrain g_Terrain; + +void CWorld::Initialize(CGameAttributes *pAttribs) +{ + // load a map if we were given one + if (pAttribs->m_MapFile) { + CStr mapfilename("mods/official/maps/scenarios/"); + mapfilename+=pAttribs->m_MapFile; + try { + CMapReader reader; + reader.LoadMap(mapfilename); + } catch (...) { + LOG(ERROR, "Failed to load map %s", mapfilename.c_str()); + throw PSERROR_Game_World_MapLoadFailed(); + } + } +} diff --git a/source/ps/World.h b/source/ps/World.h new file mode 100755 index 0000000000..d401e7eaff --- /dev/null +++ b/source/ps/World.h @@ -0,0 +1,43 @@ +#ifndef _ps_World_H +#define _ps_World_H + +class CGame; +class CGameAttributes; + +class CTerrain; +extern CTerrain g_Terrain; + +#include "UnitManager.h" + +class CWorld +{ + CGame *m_pGame; + + // These both point to the respective g_* globals - the plan is to remove + // the globals and move them into CWorld members as soon as all code has + // been converted + CTerrain *m_pTerrain; + CUnitManager *m_pUnitManager; +public: + inline CWorld(CGame *pGame): + m_pGame(pGame), + m_pTerrain(&g_Terrain), + m_pUnitManager(&g_UnitMan) + {} + + /* + Initialize the World - load the map and all objects + */ + void Initialize(CGameAttributes *pGameAttributes); + + inline CTerrain *GetTerrain() + { return m_pTerrain; } + inline CUnitManager *GetUnitManager() + { return m_pUnitManager; } +}; + +#include "Game.h" +ERROR_SUBGROUP(Game, World); +ERROR_TYPE(Game_World, MapLoadFailed); + +#endif diff --git a/source/simulation/Entity.cpp b/source/simulation/Entity.cpp index 20ce2db358..4327c48f5b 100755 --- a/source/simulation/Entity.cpp +++ b/source/simulation/Entity.cpp @@ -158,7 +158,7 @@ void CEntity::snapToGround() m_graphics_position.Y = g_Terrain.getExactGroundLevel( m_graphics_position.X, m_graphics_position.Z ); } -void CEntity::update( float timestep ) +void CEntity::update( size_t timestep ) { m_position_previous = m_position; m_orientation_previous = m_orientation; diff --git a/source/simulation/Entity.h b/source/simulation/Entity.h index febee27292..906c310a65 100755 --- a/source/simulation/Entity.h +++ b/source/simulation/Entity.h @@ -84,9 +84,9 @@ public: private: CEntity( CBaseEntity* base, CVector3D position, float orientation ); - bool processGotoNoPathing( CEntityOrder* current, float timestep ); - bool processGoto( CEntityOrder* current, float timestep ); - bool processPatrol( CEntityOrder* current, float timestep ); + bool processGotoNoPathing( CEntityOrder* current, size_t timestep_milli ); + bool processGoto( CEntityOrder* current, size_t timestep_milli ); + bool processPatrol( CEntityOrder* current, size_t timestep_milli ); public: ~CEntity(); @@ -95,7 +95,7 @@ public: HEntity me; void dispatch( const CMessage* msg ); - void update( float timestep ); + void update( size_t timestep_millis ); void kill(); void interpolate( float relativeoffset ); diff --git a/source/simulation/EntityManager.cpp b/source/simulation/EntityManager.cpp index 9fd9917568..a99d71f6bb 100755 --- a/source/simulation/EntityManager.cpp +++ b/source/simulation/EntityManager.cpp @@ -86,7 +86,7 @@ void CEntityManager::dispatchAll( CMessage* msg ) m_entities[i].m_entity->dispatch( msg ); } -void CEntityManager::updateAll( float timestep ) +void CEntityManager::updateAll( size_t timestep ) { std::vector::iterator it; for( it = m_reaper.begin(); it < m_reaper.end(); it++ ) diff --git a/source/simulation/EntityManager.h b/source/simulation/EntityManager.h index 5b2e8cc190..68f4f4de44 100755 --- a/source/simulation/EntityManager.h +++ b/source/simulation/EntityManager.h @@ -44,7 +44,7 @@ public: HEntity create( CBaseEntity* base, CVector3D position, float orientation ); HEntity create( CStr templatename, CVector3D position, float orientation ); HEntity* getByHandle( u16 index ); - void updateAll( float timestep ); + void updateAll( size_t timestep ); void interpolateAll( float relativeoffset ); void dispatchAll( CMessage* msg ); void renderAll(); diff --git a/source/simulation/EntityStateProcessing.cpp b/source/simulation/EntityStateProcessing.cpp index 3f5cdb5fd3..b06a34073d 100755 --- a/source/simulation/EntityStateProcessing.cpp +++ b/source/simulation/EntityStateProcessing.cpp @@ -11,8 +11,10 @@ extern CTerrain g_Terrain; -bool CEntity::processGotoNoPathing( CEntityOrder* current, float timestep ) +bool CEntity::processGotoNoPathing( CEntityOrder* current, size_t timestep_millis ) { + float timestep=timestep_millis/1000.0f; + CVector2D delta; delta.x = (float)current->m_data[0].location.x - m_position.X; delta.y = (float)current->m_data[0].location.y - m_position.Z; @@ -213,8 +215,10 @@ bool CEntity::processGotoNoPathing( CEntityOrder* current, float timestep ) return( false ); } -bool CEntity::processGoto( CEntityOrder* current, float timestep ) +bool CEntity::processGoto( CEntityOrder* current, size_t timestep_millis ) { + float timestep=timestep_millis/1000.0f; + CVector2D pos( m_position.X, m_position.Z ); CVector2D path_to = current->m_data[0].location; m_orderQueue.pop_front(); @@ -235,8 +239,10 @@ bool CEntity::processGoto( CEntityOrder* current, float timestep ) return( true ); } -bool CEntity::processPatrol( CEntityOrder* current, float timestep ) +bool CEntity::processPatrol( CEntityOrder* current, size_t timestep_millis ) { + float timestep=timestep_millis/1000.0f; + CEntityOrder this_segment; CEntityOrder repeat_patrol; diff --git a/source/simulation/Scheduler.cpp b/source/simulation/Scheduler.cpp index e89f1bf552..ec586d52fd 100755 --- a/source/simulation/Scheduler.cpp +++ b/source/simulation/Scheduler.cpp @@ -2,8 +2,8 @@ #include "Scheduler.h" #include "Entity.h" -extern size_t simulationTime; -extern size_t frameCount; +size_t simulationTime; +size_t frameCount; void CScheduler::pushTime( size_t delay, const HEntity& destination, const CMessage* message ) { @@ -45,8 +45,11 @@ void CScheduler::pushInterval( size_t first, size_t interval, JSFunction* functi timeFunction.push( SDispatchObjectFunction( function, simulationTime + first, operateOn, interval ) ); } -void CScheduler::update() +void CScheduler::update(size_t simElapsed) { + simulationTime += simElapsed; + frameCount++; + while( !timeMessage.empty() ) { SDispatchObjectMessage top = timeMessage.top(); @@ -105,4 +108,4 @@ void CScheduler::update() jsval rval; JS_CallFunction( g_ScriptingHost.getContext(), top.operateOn, top.function, 0, NULL, &rval ); } -} \ No newline at end of file +} diff --git a/source/simulation/Scheduler.h b/source/simulation/Scheduler.h index 7b10872184..a417480fdf 100755 --- a/source/simulation/Scheduler.h +++ b/source/simulation/Scheduler.h @@ -77,9 +77,9 @@ struct CScheduler : public Singleton void pushTime( size_t delay, JSFunction* function, JSObject* operateOn = NULL ); void pushFrame( size_t delay, JSFunction* function, JSObject* operateOn = NULL ); void pushInterval( size_t first, size_t interval, JSFunction* function, JSObject* operateOn = NULL ); - void update(); + void update(size_t elapsedSimulationTime); }; #define g_Scheduler CScheduler::GetSingleton() -#endif \ No newline at end of file +#endif diff --git a/source/simulation/Simulation.cpp b/source/simulation/Simulation.cpp new file mode 100755 index 0000000000..a722e0615e --- /dev/null +++ b/source/simulation/Simulation.cpp @@ -0,0 +1,57 @@ +#include "precompiled.h" + +#include + +#include "Simulation.h" +#include "Game.h" +#include "EntityManager.h" +#include "Scheduler.h" + +#include "gui/CGUI.h" + +CSimulation::CSimulation(CGame *pGame): + m_pGame(pGame), + m_pWorld(pGame->GetWorld()), + m_DeltaTime(0) +{} + +void CSimulation::Initialize(CGameAttributes *pAttribs) +{} + +void CSimulation::Update(double frameTime) +{ + m_DeltaTime += frameTime; + + if( m_DeltaTime >= 0.0 ) + { + // A new simulation frame is required. + MICROLOG( L"calculate simulation" ); + Simulate(); + m_DeltaTime -= (m_SimUpdateInterval/1000.0); + if( m_DeltaTime >= 0.0 ) + { + // The desired sim frame rate can't be achieved. Settle for process & render + // frames as fast as possible. + // g_Console->InsertMessage( L"Can't maintain %d FPS simulation rate!", SIM_FRAMERATE ); + m_DeltaTime = 0.0; + } + } + + Interpolate(frameTime, ((1000.0*m_DeltaTime) / (float)m_SimUpdateInterval) + 1.0); +} + +void CSimulation::Interpolate(double frameTime, double offset) +{ + const std::vector& units=m_pWorld->GetUnitManager()->GetUnits(); + for (uint i=0;iGetModel()->Update(frameTime); + + g_EntityManager.interpolateAll(offset); +} + +void CSimulation::Simulate() +{ + g_GUI.TickObjects(); + g_Scheduler.update(m_SimUpdateInterval); + g_EntityManager.updateAll( m_SimUpdateInterval ); +} diff --git a/source/simulation/Simulation.h b/source/simulation/Simulation.h new file mode 100755 index 0000000000..d8548ab7cc --- /dev/null +++ b/source/simulation/Simulation.h @@ -0,0 +1,34 @@ +#ifndef _Simulation_H +#define _Simulation_H + +class CGame; +class CGameAttributes; +class CWorld; + +class CSimulation +{ + CGame *m_pGame; + CWorld *m_pWorld; + + double m_DeltaTime; + + // Temp - Should move to a dynamic interval that can adapt to network + // conditions + const static int SIM_FRAMERATE = 10; + const static int m_SimUpdateInterval=1000/SIM_FRAMERATE; + + // Simulate: move the deterministic simulation forward by one interval + void Simulate(); + + // Interpolate: interpolate a data point for rendering between simulation + // frames. + void Interpolate(double frameTime, double offset); +public: + CSimulation(CGame *pGame); + + void Initialize(CGameAttributes *pGameAttributes); + + void Update(double frameTime); +}; + +#endif diff --git a/source/terrain/terrainMain.cpp b/source/terrain/terrainMain.cpp index a843fe879c..fa87273ebe 100755 --- a/source/terrain/terrainMain.cpp +++ b/source/terrain/terrainMain.cpp @@ -19,7 +19,6 @@ void InitScene (); void InitResources (); -void RenderScene (); extern bool keys[512]; // SDL also defines non-ascii keys; 512 should be enough extern bool mouseButtons[5]; @@ -28,7 +27,6 @@ extern bool g_active; CMatrix3D g_WorldMat; -CTerrain g_Terrain; CCamera g_Camera; CLightEnv g_LightEnv; @@ -473,7 +471,6 @@ void InitScene () g_Camera.m_Orientation.RotateY(DEGTORAD(-45)); g_Camera.m_Orientation.Translate (100, 150, -100); - } void InitResources()