diff --git a/source/graphics/GameView.cpp b/source/graphics/GameView.cpp index 057cb44666..7059d287ee 100644 --- a/source/graphics/GameView.cpp +++ b/source/graphics/GameView.cpp @@ -3,6 +3,7 @@ #include "GameView.h" #include "graphics/Camera.h" +#include "graphics/CinemaTrack.h" #include "graphics/HFTracer.h" #include "graphics/LightEnv.h" #include "graphics/Model.h" @@ -32,6 +33,7 @@ #include "renderer/Renderer.h" #include "renderer/SkyManager.h" #include "renderer/WaterManager.h" +#include "scripting/ScriptableObject.h" #include "simulation/Entity.h" #include "simulation/EntityOrders.h" #include "simulation/LOSManager.h" @@ -51,45 +53,132 @@ const float CGameView::defaultFOV = DEGTORAD(20.f); const float CGameView::defaultNear = 1.f; const float CGameView::defaultFar = 5000.f; +class CGameViewImpl : public CJSObject +{ +public: + CGameViewImpl(CGame* game) + : Game(game), MeshManager(), ObjectManager(MeshManager), + ViewCamera(), + CullCamera(), + LockCullCamera(false), + Culling(true), + ViewScrollSpeed(60), + ViewRotateSensitivity(0.002f), + ViewRotateSensitivityKeyboard(1.0f), + ViewRotateAboutTargetSensitivity(0.010f), + ViewRotateAboutTargetSensitivityKeyboard(2.0f), + ViewDragSensitivity(0.5f), + ViewZoomSensitivityWheel(16.0f), + ViewZoomSensitivity(256.0f), + ViewZoomSmoothness(0.02f), + ViewSnapSmoothness(0.02f), + CameraDelta(), + CameraPivot(), + ZoomDelta(0) + { + } + + CGame* Game; + CMeshManager MeshManager; + CObjectManager ObjectManager; + + /** + * this camera controls the eye position when rendering + */ + CCamera ViewCamera; + + /** + * this camera controls the frustum that is used for culling + * and shadow calculations + * + * Note that all code that works with camera movements should only change + * m_ViewCamera. The render functions automatically sync the cull camera to + * the view camera depending on the value of m_LockCullCamera. + */ + CCamera CullCamera; + + /** + * When @c true, the cull camera is locked in place. + * When @c false, the cull camera follows the view camera. + * + * Exposed to JS as gameView.lockCullCamera + */ + bool LockCullCamera; + + /** + * When @c true, culling is enabled so that only models that have a chance of + * being visible are sent to the renderer. + * Otherwise, the entire world is sent to the renderer. + * + * Exposed to JS as gameView.culling + */ + bool Culling; + + /** + * Cache global lighting environment. This is used to check whether the + * environment has changed during the last frame, so that vertex data can be updated etc. + */ + CLightEnv CachedLightEnv; + + CCinemaManager TrackManager; + CCinemaTrack TestTrack; + + //////////////////////////////////////// + // Settings + float ViewScrollSpeed; + float ViewRotateSensitivity; + float ViewRotateSensitivityKeyboard; + float ViewRotateAboutTargetSensitivity; + float ViewRotateAboutTargetSensitivityKeyboard; + float ViewDragSensitivity; + float ViewZoomSensitivityWheel; + float ViewZoomSensitivity; + float ViewZoomSmoothness; // 0.0 = instantaneous zooming, 1.0 = so slow it never moves + float ViewSnapSmoothness; // Just the same. + + + //////////////////////////////////////// + // Camera Controls State + CVector3D CameraDelta; + CVector3D CameraPivot; + + CEntity* UnitView; + CModel* UnitViewProp; + CEntity* UnitAttach; + //float m_CameraZoom; + std::vector CameraTargets; + + // Accumulate zooming changes across frames for smoothness + float ZoomDelta; + + // JS Interface + bool JSI_StartCustomSelection(JSContext *cx, uintN argc, jsval *argv); + bool JSI_EndCustomSelection(JSContext *cx, uintN argc, jsval *argv); + + static void ScriptingInit(); +}; + CGameView::CGameView(CGame *pGame): - m_pGame(pGame), - m_pWorld(pGame->GetWorld()), - m_ViewCamera(), - m_CullCamera(), - m_LockCullCamera(false), - m_Culling(true), - m_ViewScrollSpeed(60), - m_ViewRotateSensitivity(0.002f), - m_ViewRotateSensitivityKeyboard(1.0f), - m_ViewRotateAboutTargetSensitivity(0.010f), - m_ViewRotateAboutTargetSensitivityKeyboard(2.0f), - m_ViewDragSensitivity(0.5f), - m_ViewZoomSensitivityWheel(16.0f), - m_ViewZoomSensitivity(256.0f), - m_ViewZoomSmoothness(0.02f), - m_ViewSnapSmoothness(0.02f), - m_CameraDelta(), - m_CameraPivot(), - m_ZoomDelta(0) + m(new CGameViewImpl(pGame)) { SViewPort vp; vp.m_X=0; vp.m_Y=0; vp.m_Width=g_xres; vp.m_Height=g_yres; - m_ViewCamera.SetViewPort(&vp); + m->ViewCamera.SetViewPort(&vp); - m_ViewCamera.SetProjection (defaultNear, defaultFar, defaultFOV); - m_ViewCamera.m_Orientation.SetXRotation(DEGTORAD(30)); - m_ViewCamera.m_Orientation.RotateY(DEGTORAD(0)); - m_ViewCamera.m_Orientation.Translate (100, 150, -100); - m_CullCamera = m_ViewCamera; - g_Renderer.SetSceneCamera(m_ViewCamera, m_CullCamera); + m->ViewCamera.SetProjection (defaultNear, defaultFar, defaultFOV); + m->ViewCamera.m_Orientation.SetXRotation(DEGTORAD(30)); + m->ViewCamera.m_Orientation.RotateY(DEGTORAD(0)); + m->ViewCamera.m_Orientation.Translate (100, 150, -100); + m->CullCamera = m->ViewCamera; + g_Renderer.SetSceneCamera(m->ViewCamera, m->CullCamera); - m_UnitView=NULL; - m_UnitAttach=NULL; + m->UnitView=NULL; + m->UnitAttach=NULL; - ONCE( ScriptingInit(); ); + ONCE( m->ScriptingInit(); ); } CGameView::~CGameView() @@ -98,46 +187,79 @@ CGameView::~CGameView() g_Mouseover.clear(); g_BuildingPlacer.deactivate(); UnloadResources(); + + delete m; } -void CGameView::ScriptingInit() +CObjectManager& CGameView::GetObjectManager() const { - AddMethod("startCustomSelection", 0); - AddMethod("endCustomSelection", 0); - AddProperty(L"culling", &CGameView::m_Culling); - AddProperty(L"lockCullCamera", &CGameView::m_LockCullCamera); + return m->ObjectManager; +} - CJSObject::ScriptingInit("GameView"); +JSObject* CGameView::GetScript() +{ + return m->GetScript(); +} + +CCamera* CGameView::GetCamera() +{ + return &m->ViewCamera; +} + +CCinemaManager* CGameView::GetCinema() +{ + return &m->TrackManager; +}; + +void CGameView::AttachToUnit(CEntity* target) +{ + m->UnitAttach = target; +} + +bool CGameView::IsAttached() +{ + return (m->UnitAttach != NULL); +} + +bool CGameView::IsUnitView() +{ + return (m->UnitView != NULL); +} + + +void CGameViewImpl::ScriptingInit() +{ + AddMethod("startCustomSelection", 0); + AddMethod("endCustomSelection", 0); + AddProperty(L"culling", &CGameViewImpl::Culling); + AddProperty(L"lockCullCamera", &CGameViewImpl::LockCullCamera); + + CJSObject::ScriptingInit("GameView"); } int CGameView::Initialize(CGameAttributes* UNUSED(pAttribs)) { - CFG_GET_SYS_VAL( "view.scroll.speed", Float, m_ViewScrollSpeed ); - CFG_GET_SYS_VAL( "view.rotate.speed", Float, m_ViewRotateSensitivity ); - CFG_GET_SYS_VAL( "view.rotate.keyboard.speed", Float, m_ViewRotateSensitivityKeyboard ); - CFG_GET_SYS_VAL( "view.rotate.abouttarget.speed", Float, m_ViewRotateAboutTargetSensitivity ); - CFG_GET_SYS_VAL( "view.rotate.keyboard.abouttarget.speed", Float, m_ViewRotateAboutTargetSensitivityKeyboard ); - CFG_GET_SYS_VAL( "view.drag.speed", Float, m_ViewDragSensitivity ); - CFG_GET_SYS_VAL( "view.zoom.speed", Float, m_ViewZoomSensitivity ); - CFG_GET_SYS_VAL( "view.zoom.wheel.speed", Float, m_ViewZoomSensitivityWheel ); - CFG_GET_SYS_VAL( "view.zoom.smoothness", Float, m_ViewZoomSmoothness ); - CFG_GET_SYS_VAL( "view.snap.smoothness", Float, m_ViewSnapSmoothness ); + CFG_GET_SYS_VAL( "view.scroll.speed", Float, m->ViewScrollSpeed ); + CFG_GET_SYS_VAL( "view.rotate.speed", Float, m->ViewRotateSensitivity ); + CFG_GET_SYS_VAL( "view.rotate.keyboard.speed", Float, m->ViewRotateSensitivityKeyboard ); + CFG_GET_SYS_VAL( "view.rotate.abouttarget.speed", Float, m->ViewRotateAboutTargetSensitivity ); + CFG_GET_SYS_VAL( "view.rotate.keyboard.abouttarget.speed", Float, m->ViewRotateAboutTargetSensitivityKeyboard ); + CFG_GET_SYS_VAL( "view.drag.speed", Float, m->ViewDragSensitivity ); + CFG_GET_SYS_VAL( "view.zoom.speed", Float, m->ViewZoomSensitivity ); + CFG_GET_SYS_VAL( "view.zoom.wheel.speed", Float, m->ViewZoomSensitivityWheel ); + CFG_GET_SYS_VAL( "view.zoom.smoothness", Float, m->ViewZoomSmoothness ); + CFG_GET_SYS_VAL( "view.snap.smoothness", Float, m->ViewSnapSmoothness ); - if( ( m_ViewZoomSmoothness < 0.0f ) || ( m_ViewZoomSmoothness > 1.0f ) ) - m_ViewZoomSmoothness = 0.02f; - if( ( m_ViewSnapSmoothness < 0.0f ) || ( m_ViewSnapSmoothness > 1.0f ) ) - m_ViewSnapSmoothness = 0.02f; + if( ( m->ViewZoomSmoothness < 0.0f ) || ( m->ViewZoomSmoothness > 1.0f ) ) + m->ViewZoomSmoothness = 0.02f; + if( ( m->ViewSnapSmoothness < 0.0f ) || ( m->ViewSnapSmoothness > 1.0f ) ) + m->ViewSnapSmoothness = 0.02f; return 0; } - - - - - void CGameView::RegisterInit(CGameAttributes *pAttribs) { // CGameView init @@ -153,17 +275,17 @@ void CGameView::RegisterInit(CGameAttributes *pAttribs) void CGameView::Render() { - if (m_LockCullCamera == false) + if (m->LockCullCamera == false) { // Set up cull camera - m_CullCamera = m_ViewCamera; + m->CullCamera = m->ViewCamera; // This can be uncommented to try getting a bigger frustum.. // but then it makes shadow maps too low-detail. //m_CullCamera.SetProjection(1.0f, 10000.0f, DEGTORAD(30)); //m_CullCamera.UpdateFrustum(); } - g_Renderer.SetSceneCamera(m_ViewCamera, m_CullCamera); + g_Renderer.SetSceneCamera(m->ViewCamera, m->CullCamera); CheckLightEnv(); @@ -176,8 +298,8 @@ void CGameView::Render() void CGameView::EnumerateObjects(const CFrustum& frustum, SceneCollector* c) { PROFILE_START( "submit terrain" ); - CTerrain* pTerrain = m_pWorld->GetTerrain(); - u32 patchesPerSide=pTerrain->GetPatchesPerSide(); + CTerrain* pTerrain = m->Game->GetWorld()->GetTerrain(); + u32 patchesPerSide = pTerrain->GetPatchesPerSide(); for (uint j=0; jGetPatch(i,j); @@ -189,7 +311,7 @@ void CGameView::EnumerateObjects(const CFrustum& frustum, SceneCollector* c) bounds[1].Y = waterHeight; } - if (!m_Culling || frustum.IsBoxVisible (CVector3D(0,0,0), bounds)) { + if (!m->Culling || frustum.IsBoxVisible (CVector3D(0,0,0), bounds)) { c->Submit(patch); } } @@ -197,11 +319,12 @@ void CGameView::EnumerateObjects(const CFrustum& frustum, SceneCollector* c) PROFILE_END( "submit terrain" ); PROFILE_START( "submit models" ); - CUnitManager* pUnitMan = m_pWorld->GetUnitManager(); - CProjectileManager* pProjectileMan = m_pWorld->GetProjectileManager(); - CLOSManager* losMgr = m_pWorld->GetLOSManager(); + CWorld* world = m->Game->GetWorld(); + CUnitManager& unitMan = world->GetUnitManager(); + CProjectileManager& pProjectileMan = world->GetProjectileManager(); + CLOSManager* losMgr = world->GetLOSManager(); - const std::vector& units=pUnitMan->GetUnits(); + const std::vector& units = unitMan.GetUnits(); for (uint i=0;iGetEntity(); @@ -214,7 +337,7 @@ void CGameView::EnumerateObjects(const CFrustum& frustum, SceneCollector* c) model->ValidatePosition(); if (status != UNIT_HIDDEN && - (!m_Culling || frustum.IsBoxVisible(CVector3D(0,0,0), model->GetBounds()))) + (!m->Culling || frustum.IsBoxVisible(CVector3D(0,0,0), model->GetBounds()))) { if(units[i] != g_BuildingPlacer.m_actor) { @@ -235,8 +358,8 @@ void CGameView::EnumerateObjects(const CFrustum& frustum, SceneCollector* c) } } - const std::vector& projectiles=pProjectileMan->GetProjectiles(); - for (uint i=0;i& projectiles = pProjectileMan.GetProjectiles(); + for (uint i = 0; i < projectiles.size(); ++i) { CModel* model = projectiles[i]->GetModel(); @@ -246,7 +369,7 @@ void CGameView::EnumerateObjects(const CFrustum& frustum, SceneCollector* c) CVector3D centre; bound.GetCentre(centre); - if ((!m_Culling || frustum.IsBoxVisible(CVector3D(0,0,0), bound)) + if ((!m->Culling || frustum.IsBoxVisible(CVector3D(0,0,0), bound)) && losMgr->GetStatus(centre.X, centre.Z, g_Game->GetLocalPlayer()) & LOS_VISIBLE) { PROFILE( "submit projectiles" ); @@ -258,46 +381,28 @@ void CGameView::EnumerateObjects(const CFrustum& frustum, SceneCollector* c) //locks the camera in place -void CGameView::CameraLock(CVector3D Trans, bool smooth) +void CGameView::CameraLock(const CVector3D& Trans, bool smooth) { - CTerrain* pTerrain = m_pWorld->GetTerrain(); - float height=pTerrain->getExactGroundLevel( - m_ViewCamera.m_Orientation._14 + Trans.X, m_ViewCamera.m_Orientation._34 + Trans.Z) + - g_YMinOffset; - //is requested position within limits? - if (m_ViewCamera.m_Orientation._24 + Trans.Y <= g_MaxZoomHeight) - { - if( m_ViewCamera.m_Orientation._24 + Trans.Y >= height) - { - m_ViewCamera.m_Orientation.Translate(Trans); - } - else if (m_ViewCamera.m_Orientation._24 + Trans.Y < height && smooth == true) - { - m_ViewCamera.m_Orientation.Translate(Trans); - m_ViewCamera.m_Orientation._24=height; - } - - - } + CameraLock(Trans.X, Trans.Y, Trans.Z, smooth); } void CGameView::CameraLock(float x, float y, float z, bool smooth) { - CTerrain* pTerrain = m_pWorld->GetTerrain(); + CTerrain* pTerrain = m->Game->GetWorld()->GetTerrain(); float height = pTerrain->getExactGroundLevel( - m_ViewCamera.m_Orientation._14 + x, m_ViewCamera.m_Orientation._34 + z) + + m->ViewCamera.m_Orientation._14 + x, m->ViewCamera.m_Orientation._34 + z) + g_YMinOffset; //is requested position within limits? - if (m_ViewCamera.m_Orientation._24 + y <= g_MaxZoomHeight) + if (m->ViewCamera.m_Orientation._24 + y <= g_MaxZoomHeight) { - if( m_ViewCamera.m_Orientation._24 + y >= height) + if( m->ViewCamera.m_Orientation._24 + y >= height) { - m_ViewCamera.m_Orientation.Translate(x, y, z); + m->ViewCamera.m_Orientation.Translate(x, y, z); } - else if (m_ViewCamera.m_Orientation._24 + y < height && smooth == true) + else if (m->ViewCamera.m_Orientation._24 + y < height && smooth == true) { - m_ViewCamera.m_Orientation.Translate(x, y, z); - m_ViewCamera.m_Orientation._24=height; + m->ViewCamera.m_Orientation.Translate(x, y, z); + m->ViewCamera.m_Orientation._24=height; } } } @@ -315,11 +420,11 @@ static void MarkUpdateColorRecursive(CModel* model) void CGameView::CheckLightEnv() { - if (m_cachedLightEnv == g_LightEnv) + if (m->CachedLightEnv == g_LightEnv) return; - m_cachedLightEnv = g_LightEnv; - CTerrain* pTerrain = m_pWorld->GetTerrain(); + m->CachedLightEnv = g_LightEnv; + CTerrain* pTerrain = m->Game->GetWorld()->GetTerrain(); if (!pTerrain) return; @@ -327,7 +432,7 @@ void CGameView::CheckLightEnv() PROFILE("update light env"); pTerrain->MakeDirty(RENDERDATA_UPDATE_COLOR); - const std::vector& units = m_pWorld->GetUnitManager()->GetUnits(); + const std::vector& units = m->Game->GetWorld()->GetUnitManager().GetUnits(); for(size_t i = 0; i < units.size(); ++i) { MarkUpdateColorRecursive(units[i]->GetModel()); } @@ -336,7 +441,6 @@ void CGameView::CheckLightEnv() void CGameView::UnloadResources() { - g_ObjMan.UnloadObjects(); g_TexMan.UnloadTerrainTextures(); g_Renderer.UnloadAlphaMaps(); g_Renderer.GetWaterManager()->UnloadWaterTextures(); @@ -346,34 +450,34 @@ void CGameView::UnloadResources() void CGameView::ResetCamera() { // quick hack to return camera home, for screenshots (after alt+tabbing) - m_ViewCamera.SetProjection (1, 5000, DEGTORAD(20)); - m_ViewCamera.m_Orientation.SetXRotation(DEGTORAD(30)); - m_ViewCamera.m_Orientation.RotateY(DEGTORAD(-45)); - m_ViewCamera.m_Orientation.Translate (100, 150, -100); + m->ViewCamera.SetProjection (1, 5000, DEGTORAD(20)); + m->ViewCamera.m_Orientation.SetXRotation(DEGTORAD(30)); + m->ViewCamera.m_Orientation.RotateY(DEGTORAD(-45)); + m->ViewCamera.m_Orientation.Translate (100, 150, -100); } void CGameView::ResetCameraOrientation() { - CVector3D origin = m_ViewCamera.m_Orientation.GetTranslation(); - CVector3D dir = m_ViewCamera.m_Orientation.GetIn(); + CVector3D origin = m->ViewCamera.m_Orientation.GetTranslation(); + CVector3D dir = m->ViewCamera.m_Orientation.GetIn(); CVector3D target = origin + dir * ( ( 50.0f - origin.Y ) / dir.Y ); target -= CVector3D( -22.474480f, 50.0f, 22.474480f ); - m_ViewCamera.SetProjection (1, 5000, DEGTORAD(20)); - m_ViewCamera.m_Orientation.SetXRotation(DEGTORAD(30)); - m_ViewCamera.m_Orientation.RotateY(DEGTORAD(-45)); + m->ViewCamera.SetProjection (1, 5000, DEGTORAD(20)); + m->ViewCamera.m_Orientation.SetXRotation(DEGTORAD(30)); + m->ViewCamera.m_Orientation.RotateY(DEGTORAD(-45)); target += CVector3D( 100.0f, 150.0f, -100.0f ); - m_ViewCamera.m_Orientation.Translate( target ); + m->ViewCamera.m_Orientation.Translate( target ); } void CGameView::RotateAboutTarget() { - m_CameraPivot = m_ViewCamera.GetWorldCoordinates(true); + m->CameraPivot = m->ViewCamera.GetWorldCoordinates(true); } void CGameView::Update(float DeltaTime) @@ -381,36 +485,33 @@ void CGameView::Update(float DeltaTime) if (!g_app_has_focus) return; - if (m_UnitView) + if (m->UnitView) { - m_ViewCamera.m_Orientation.SetYRotation(m_UnitView->m_orientation.Y); - m_ViewCamera.m_Orientation.Translate(m_UnitViewProp->GetTransform().GetTranslation()); - m_ViewCamera.UpdateFrustum(); + m->ViewCamera.m_Orientation.SetYRotation(m->UnitView->m_orientation.Y); + m->ViewCamera.m_Orientation.Translate(m->UnitViewProp->GetTransform().GetTranslation()); + m->ViewCamera.UpdateFrustum(); return; } - if (m_UnitAttach) + if (m->UnitAttach) { - CVector3D ToMove = m_UnitAttach->m_position - m_ViewCamera.GetFocus(); - m_ViewCamera.m_Orientation._14 += ToMove.X; - m_ViewCamera.m_Orientation._34 += ToMove.Z; - m_ViewCamera.UpdateFrustum(); + CVector3D ToMove = m->UnitAttach->m_position - m->ViewCamera.GetFocus(); + m->ViewCamera.m_Orientation._14 += ToMove.X; + m->ViewCamera.m_Orientation._34 += ToMove.Z; + m->ViewCamera.UpdateFrustum(); return; } - if (m_TrackManager.IsActive()) + if (m->TrackManager.IsActive() && m->TrackManager.IsPlaying()) { - if (m_TrackManager.IsPlaying()) - { - if(!m_TrackManager.Update(DeltaTime)) - ResetCamera(); - return; - } + if (! m->TrackManager.Update(DeltaTime)) + ResetCamera(); + return; } - float delta = powf( m_ViewSnapSmoothness, DeltaTime ); - m_ViewCamera.m_Orientation.Translate( m_CameraDelta * ( 1.0f - delta ) ); - m_CameraDelta *= delta; + float delta = powf( m->ViewSnapSmoothness, DeltaTime ); + m->ViewCamera.m_Orientation.Translate( m->CameraDelta * ( 1.0f - delta ) ); + m->CameraDelta *= delta; // This could be rewritten much more reliably, so it doesn't e.g. accidentally tilt @@ -426,8 +527,8 @@ void CGameView::Update(float DeltaTime) mouse_last_y = g_mouse_y; // Miscellaneous vectors - CVector3D forwards = m_ViewCamera.m_Orientation.GetIn(); - CVector3D rightwards = m_ViewCamera.m_Orientation.GetLeft() * -1.0f; // upwards.Cross(forwards); + CVector3D forwards = m->ViewCamera.m_Orientation.GetIn(); + CVector3D rightwards = m->ViewCamera.m_Orientation.GetLeft() * -1.0f; // upwards.Cross(forwards); CVector3D upwards( 0.0f, 1.0f, 0.0f ); // rightwards.Normalize(); @@ -440,62 +541,62 @@ void CGameView::Update(float DeltaTime) // Ctrl + middle-drag or left-and-right-drag to rotate view // Untranslate the camera, so it rotates around the correct point - CVector3D position = m_ViewCamera.m_Orientation.GetTranslation(); - m_ViewCamera.m_Orientation.Translate(position*-1); + CVector3D position = m->ViewCamera.m_Orientation.GetTranslation(); + m->ViewCamera.m_Orientation.Translate(position*-1); // Sideways rotation float rightways = 0.0f; if( hotkeys[HOTKEY_CAMERA_ROTATE] ) - rightways = (float)mouse_dx * m_ViewRotateSensitivity; + rightways = (float)mouse_dx * m->ViewRotateSensitivity; if( hotkeys[HOTKEY_CAMERA_ROTATE_KEYBOARD] ) { if( hotkeys[HOTKEY_CAMERA_LEFT] ) - rightways -= m_ViewRotateSensitivityKeyboard * DeltaTime; + rightways -= m->ViewRotateSensitivityKeyboard * DeltaTime; if( hotkeys[HOTKEY_CAMERA_RIGHT] ) - rightways += m_ViewRotateSensitivityKeyboard * DeltaTime; + rightways += m->ViewRotateSensitivityKeyboard * DeltaTime; } - m_ViewCamera.m_Orientation.RotateY( rightways ); + m->ViewCamera.m_Orientation.RotateY( rightways ); // Up/down rotation float upways = 0.0f; if( hotkeys[HOTKEY_CAMERA_ROTATE] ) - upways = (float)mouse_dy * m_ViewRotateSensitivity; + upways = (float)mouse_dy * m->ViewRotateSensitivity; if( hotkeys[HOTKEY_CAMERA_ROTATE_KEYBOARD] ) { if( hotkeys[HOTKEY_CAMERA_UP] ) - upways -= m_ViewRotateSensitivityKeyboard * DeltaTime; + upways -= m->ViewRotateSensitivityKeyboard * DeltaTime; if( hotkeys[HOTKEY_CAMERA_DOWN] ) - upways += m_ViewRotateSensitivityKeyboard * DeltaTime; + upways += m->ViewRotateSensitivityKeyboard * DeltaTime; } CQuaternion temp; temp.FromAxisAngle(rightwards, upways); - m_ViewCamera.m_Orientation.Rotate(temp); + m->ViewCamera.m_Orientation.Rotate(temp); // Retranslate back to the right position - m_ViewCamera.m_Orientation.Translate(position); + m->ViewCamera.m_Orientation.Translate(position); } else if( hotkeys[HOTKEY_CAMERA_ROTATE_ABOUT_TARGET] ) { - CVector3D origin = m_ViewCamera.m_Orientation.GetTranslation(); - CVector3D delta = origin - m_CameraPivot; + CVector3D origin = m->ViewCamera.m_Orientation.GetTranslation(); + CVector3D delta = origin - m->CameraPivot; CQuaternion rotateH, rotateV; CMatrix3D rotateM; // Sideways rotation - float rightways = (float)mouse_dx * m_ViewRotateAboutTargetSensitivity; + float rightways = (float)mouse_dx * m->ViewRotateAboutTargetSensitivity; rotateH.FromAxisAngle( upwards, rightways ); // Up/down rotation - float upways = (float)mouse_dy * m_ViewRotateAboutTargetSensitivity; + float upways = (float)mouse_dy * m->ViewRotateAboutTargetSensitivity; rotateV.FromAxisAngle( rightwards, upways ); rotateH *= rotateV; @@ -509,20 +610,20 @@ void CGameView::Update(float DeltaTime) if( ( scan >= 0.5f ) ) { // Move the camera to the origin (in preparation for rotation ) - m_ViewCamera.m_Orientation.Translate( origin * -1.0f ); + m->ViewCamera.m_Orientation.Translate( origin * -1.0f ); - m_ViewCamera.m_Orientation.Rotate( rotateH ); + m->ViewCamera.m_Orientation.Rotate( rotateH ); // Move the camera back to where it belongs - m_ViewCamera.m_Orientation.Translate( m_CameraPivot + delta ); + m->ViewCamera.m_Orientation.Translate( m->CameraPivot + delta ); } } else if( hotkeys[HOTKEY_CAMERA_ROTATE_ABOUT_TARGET_KEYBOARD] ) { // Split up because the keyboard controls use the centre of the screen, not the mouse position. - CVector3D origin = m_ViewCamera.m_Orientation.GetTranslation(); - CVector3D pivot = m_ViewCamera.GetFocus(); + CVector3D origin = m->ViewCamera.m_Orientation.GetTranslation(); + CVector3D pivot = m->ViewCamera.GetFocus(); CVector3D delta = origin - pivot; CQuaternion rotateH, rotateV; CMatrix3D rotateM; @@ -531,9 +632,9 @@ void CGameView::Update(float DeltaTime) float rightways = 0.0f; if( hotkeys[HOTKEY_CAMERA_LEFT] ) - rightways -= m_ViewRotateAboutTargetSensitivityKeyboard * DeltaTime; + rightways -= m->ViewRotateAboutTargetSensitivityKeyboard * DeltaTime; if( hotkeys[HOTKEY_CAMERA_RIGHT] ) - rightways += m_ViewRotateAboutTargetSensitivityKeyboard * DeltaTime; + rightways += m->ViewRotateAboutTargetSensitivityKeyboard * DeltaTime; rotateH.FromAxisAngle( upwards, rightways ); @@ -541,9 +642,9 @@ void CGameView::Update(float DeltaTime) float upways = 0.0f; if( hotkeys[HOTKEY_CAMERA_UP] ) - upways -= m_ViewRotateAboutTargetSensitivityKeyboard * DeltaTime; + upways -= m->ViewRotateAboutTargetSensitivityKeyboard * DeltaTime; if( hotkeys[HOTKEY_CAMERA_DOWN] ) - upways += m_ViewRotateAboutTargetSensitivityKeyboard * DeltaTime; + upways += m->ViewRotateAboutTargetSensitivityKeyboard * DeltaTime; rotateV.FromAxisAngle( rightwards, upways ); @@ -558,12 +659,12 @@ void CGameView::Update(float DeltaTime) if( ( scan >= 0.5f ) ) { // Move the camera to the origin (in preparation for rotation ) - m_ViewCamera.m_Orientation.Translate( origin * -1.0f ); + m->ViewCamera.m_Orientation.Translate( origin * -1.0f ); - m_ViewCamera.m_Orientation.Rotate( rotateH ); + m->ViewCamera.m_Orientation.Rotate( rotateH ); // Move the camera back to where it belongs - m_ViewCamera.m_Orientation.Translate( pivot + delta ); + m->ViewCamera.m_Orientation.Translate( pivot + delta ); } } @@ -571,22 +672,22 @@ void CGameView::Update(float DeltaTime) { // Middle-drag to pan //keep camera in bounds - CameraLock(rightwards * (m_ViewDragSensitivity * mouse_dx)); - CameraLock(forwards_horizontal * (-m_ViewDragSensitivity * mouse_dy)); + CameraLock(rightwards * (m->ViewDragSensitivity * mouse_dx)); + CameraLock(forwards_horizontal * (-m->ViewDragSensitivity * mouse_dy)); } // Mouse movement if( !hotkeys[HOTKEY_CAMERA_ROTATE] && !hotkeys[HOTKEY_CAMERA_ROTATE_ABOUT_TARGET] ) { if (g_mouse_x >= g_xres-2 && g_mouse_x < g_xres) - CameraLock(rightwards * (m_ViewScrollSpeed * DeltaTime)); + CameraLock(rightwards * (m->ViewScrollSpeed * DeltaTime)); else if (g_mouse_x <= 3 && g_mouse_x >= 0) - CameraLock(-rightwards * (m_ViewScrollSpeed * DeltaTime)); + CameraLock(-rightwards * (m->ViewScrollSpeed * DeltaTime)); if (g_mouse_y >= g_yres-2 && g_mouse_y < g_yres) - CameraLock(-forwards_horizontal * (m_ViewScrollSpeed * DeltaTime)); + CameraLock(-forwards_horizontal * (m->ViewScrollSpeed * DeltaTime)); else if (g_mouse_y <= 3 && g_mouse_y >= 0) - CameraLock(forwards_horizontal * (m_ViewScrollSpeed * DeltaTime)); + CameraLock(forwards_horizontal * (m->ViewScrollSpeed * DeltaTime)); } @@ -595,14 +696,14 @@ void CGameView::Update(float DeltaTime) if( hotkeys[HOTKEY_CAMERA_PAN_KEYBOARD] ) { if( hotkeys[HOTKEY_CAMERA_RIGHT] ) - CameraLock(rightwards * (m_ViewScrollSpeed * DeltaTime)); + CameraLock(rightwards * (m->ViewScrollSpeed * DeltaTime)); if( hotkeys[HOTKEY_CAMERA_LEFT] ) - CameraLock(-rightwards * (m_ViewScrollSpeed * DeltaTime)); + CameraLock(-rightwards * (m->ViewScrollSpeed * DeltaTime)); if( hotkeys[HOTKEY_CAMERA_DOWN] ) - CameraLock(-forwards_horizontal * (m_ViewScrollSpeed * DeltaTime)); + CameraLock(-forwards_horizontal * (m->ViewScrollSpeed * DeltaTime)); if( hotkeys[HOTKEY_CAMERA_UP] ) - CameraLock(forwards_horizontal * (m_ViewScrollSpeed * DeltaTime)); + CameraLock(forwards_horizontal * (m->ViewScrollSpeed * DeltaTime)); } @@ -610,18 +711,18 @@ void CGameView::Update(float DeltaTime) // Note that scroll wheel zooming is event-based and handled in game_view_handler if( hotkeys[HOTKEY_CAMERA_ZOOM_IN] ) - m_ZoomDelta += m_ViewZoomSensitivity*DeltaTime; + m->ZoomDelta += m->ViewZoomSensitivity*DeltaTime; else if( hotkeys[HOTKEY_CAMERA_ZOOM_OUT] ) - m_ZoomDelta -= m_ViewZoomSensitivity*DeltaTime; + m->ZoomDelta -= m->ViewZoomSensitivity*DeltaTime; - if (fabsf(m_ZoomDelta) > 0.1f) // use a fairly high limit to avoid nasty flickering when zooming + if (fabsf(m->ZoomDelta) > 0.1f) // use a fairly high limit to avoid nasty flickering when zooming { - float zoom_proportion = powf(m_ViewZoomSmoothness, DeltaTime); - CameraLock(forwards * (m_ZoomDelta * (1.0f-zoom_proportion)), false); - m_ZoomDelta *= zoom_proportion; + float zoom_proportion = powf(m->ViewZoomSmoothness, DeltaTime); + CameraLock(forwards * (m->ZoomDelta * (1.0f-zoom_proportion)), false); + m->ZoomDelta *= zoom_proportion; } - m_ViewCamera.UpdateFrustum (); + m->ViewCamera.UpdateFrustum (); } void CGameView::ToUnitView(CEntity* target, CModel* prop) @@ -629,18 +730,18 @@ void CGameView::ToUnitView(CEntity* target, CModel* prop) if( !target ) { //prevent previous zooming - m_ZoomDelta = 0.0f; + m->ZoomDelta = 0.0f; ResetCamera(); - SetCameraTarget( m_UnitView->m_position ); + SetCameraTarget( m->UnitView->m_position ); } - m_UnitView = target; - m_UnitViewProp = prop; + m->UnitView = target; + m->UnitViewProp = prop; } void CGameView::PushCameraTarget( const CVector3D& target ) { // Save the current position - m_CameraTargets.push_back( m_ViewCamera.m_Orientation.GetTranslation() ); + m->CameraTargets.push_back( m->ViewCamera.m_Orientation.GetTranslation() ); // And set the camera SetCameraTarget( target ); } @@ -652,14 +753,14 @@ void CGameView::SetCameraTarget( const CVector3D& target ) // the difference between that position and the camera point, and restoring // that difference to our new target) - CVector3D CurrentTarget = m_ViewCamera.GetFocus(); - m_CameraDelta = target - CurrentTarget; + CVector3D CurrentTarget = m->ViewCamera.GetFocus(); + m->CameraDelta = target - CurrentTarget; } void CGameView::PopCameraTarget() { - m_CameraDelta = m_CameraTargets.back() - m_ViewCamera.m_Orientation.GetTranslation(); - m_CameraTargets.pop_back(); + m->CameraDelta = m->CameraTargets.back() - m->ViewCamera.m_Orientation.GetTranslation(); + m->CameraTargets.pop_back(); } InReaction game_view_handler(const SDL_Event_* ev) @@ -716,11 +817,11 @@ InReaction CGameView::HandleEvent(const SDL_Event_* ev) // because SDL auto-generates a sequence of mousedown/mouseup events // and we never get to see the "down" state inside Update(). case HOTKEY_CAMERA_ZOOM_WHEEL_IN: - m_ZoomDelta += m_ViewZoomSensitivityWheel; + m->ZoomDelta += m->ViewZoomSensitivityWheel; return( IN_HANDLED ); case HOTKEY_CAMERA_ZOOM_WHEEL_OUT: - m_ZoomDelta -= m_ViewZoomSensitivityWheel; + m->ZoomDelta -= m->ViewZoomSensitivityWheel; return( IN_HANDLED ); default: @@ -770,14 +871,14 @@ InReaction CGameView::HandleEvent(const SDL_Event_* ev) return IN_PASS; } -bool CGameView::JSI_StartCustomSelection( +bool CGameViewImpl::JSI_StartCustomSelection( JSContext* UNUSED(context), uint UNUSED(argc), jsval* UNUSED(argv)) { StartCustomSelection(); return true; } -bool CGameView::JSI_EndCustomSelection( +bool CGameViewImpl::JSI_EndCustomSelection( JSContext* UNUSED(context), uint UNUSED(argc), jsval* UNUSED(argv)) { ResetInteraction(); diff --git a/source/graphics/GameView.h b/source/graphics/GameView.h index b319811970..12dc097d7f 100644 --- a/source/graphics/GameView.h +++ b/source/graphics/GameView.h @@ -5,102 +5,29 @@ extern float g_MaxZoomHeight; //note: Max terrain height is this minus YMinOffset extern float g_YMinOffset; -#include "Camera.h" -#include "CinemaTrack.h" -#include "maths/Vector3D.h" -#include "LightEnv.h" -#include "scripting/ScriptableObject.h" - -#include "lib/input.h" - #include "renderer/Scene.h" class CGame; class CGameAttributes; -class CWorld; -class CTerrain; -class CUnitManager; -class CProjectileManager; -class CModel; +class CObjectManager; +class CCamera; +class CCinemaManager; +class CVector3D; class CEntity; -class CGameView : public CJSObject, private Scene +struct JSObject; +enum InReaction; +struct SDL_Event_; + +class CGameViewImpl; + +class CGameView : private Scene { public: static const float defaultFOV, defaultNear, defaultFar; private: - CGame *m_pGame; - CWorld *m_pWorld; - - /** - * m_ViewCamera: this camera controls the eye position when rendering - */ - CCamera m_ViewCamera; - - /** - * m_CullCamera: this camera controls the frustum that is used for culling - * and shadow calculations - * - * Note that all code that works with camera movements should only change - * m_ViewCamera. The render functions automatically sync the cull camera to - * the view camera depending on the value of m_LockCullCamera. - */ - CCamera m_CullCamera; - - /** - * m_LockCullCamera: When @c true, the cull camera is locked in place. - * When @c false, the cull camera follows the view camera. - * - * Exposed to JS as gameView.lockCullCamera - */ - bool m_LockCullCamera; - - /** - * When @c true, culling is enabled so that only models that have a chance of - * being visible are sent to the renderer. - * Otherwise, the entire world is sent to the renderer. - * - * Exposed to JS as gameView.culling - */ - bool m_Culling; - - /** - * m_cachedLightEnv: Cache global lighting environment. This is used to check whether the - * environment has changed during the last frame, so that vertex data can be updated etc. - */ - CLightEnv m_cachedLightEnv; - - CCinemaManager m_TrackManager; - CCinemaTrack m_TestTrack; - - //////////////////////////////////////// - // Settings - float m_ViewScrollSpeed; - float m_ViewRotateSensitivity; - float m_ViewRotateSensitivityKeyboard; - float m_ViewRotateAboutTargetSensitivity; - float m_ViewRotateAboutTargetSensitivityKeyboard; - float m_ViewDragSensitivity; - float m_ViewZoomSensitivityWheel; - float m_ViewZoomSensitivity; - float m_ViewZoomSmoothness; // 0.0 = instantaneous zooming, 1.0 = so slow it never moves - float m_ViewSnapSmoothness; // Just the same. - - - //////////////////////////////////////// - // Camera Controls State - CVector3D m_CameraDelta; - CVector3D m_CameraPivot; - - CEntity* m_UnitView; - CModel* m_UnitViewProp; - CEntity* m_UnitAttach; - //float m_CameraZoom; - std::vector m_CameraTargets; - - // Accumulate zooming changes across frames for smoothness - float m_ZoomDelta; + CGameViewImpl* m; // Check whether lighting environment has changed and update vertex data if necessary void CheckLightEnv(); @@ -116,12 +43,6 @@ private: // UnloadResources(): Unload all graphics resources loaded by InitResources void UnloadResources(); - // JS Interface - bool JSI_StartCustomSelection(JSContext *cx, uintN argc, jsval *argv); - bool JSI_EndCustomSelection(JSContext *cx, uintN argc, jsval *argv); - - static void ScriptingInit(); - public: CGameView(CGame *pGame); ~CGameView(); @@ -129,6 +50,8 @@ public: void RegisterInit(CGameAttributes *pAttribs); int Initialize(CGameAttributes *pGameAttributes); + CObjectManager& GetObjectManager() const; + // Update: Update all the view information (i.e. rotate camera, scroll, // whatever). This will *not* change any World information - only the // *presentation* @@ -141,7 +64,7 @@ public: //Keep the camera in between boundaries/smooth camera scrolling/translating //Should call this whenever moving (translating) the camera - void CameraLock(CVector3D Trans, bool smooth=true); + void CameraLock(const CVector3D& Trans, bool smooth=true); void CameraLock(float x, float y, float z, bool smooth=true); // Camera Control Functions (used by input handler) @@ -156,15 +79,15 @@ public: //First person camera attachment (through the eyes of the unit) void ToUnitView(CEntity* target, CModel* prop); //Keep view the same but follow the unit - void AttachToUnit(CEntity* target) { m_UnitAttach = target; } + void AttachToUnit(CEntity* target); - bool IsAttached () { if( m_UnitAttach ) { return true; } return false; } - bool IsUnitView () { if( m_UnitView ) { return true; } return false; } + bool IsAttached(); + bool IsUnitView(); - inline CCamera *GetCamera() - { return &m_ViewCamera; } - inline CCinemaManager* GetCinema() - { return &m_TrackManager; }; + CCamera *GetCamera(); + CCinemaManager* GetCinema(); + + JSObject* GetScript(); }; extern InReaction game_view_handler(const SDL_Event_* ev); diff --git a/source/graphics/MapReader.cpp b/source/graphics/MapReader.cpp index 329fc09e0b..458f12f1e2 100644 --- a/source/graphics/MapReader.cpp +++ b/source/graphics/MapReader.cpp @@ -62,7 +62,7 @@ void CMapReader::LoadMap(const char* filename, CTerrain *pTerrain_, g_EntityManager.deleteAll(); // delete all remaining non-entity units pUnitMan->DeleteAll(); - g_UnitMan.SetNextID(0); + pUnitMan->SetNextID(0); // unpack the data RegMemFun(this, &CMapReader::UnpackMap, L"CMapReader::UnpackMap", 1200); @@ -269,7 +269,7 @@ int CMapReader::ApplyData() } std::set selections; // TODO: read from file - CUnit* unit = g_UnitMan.CreateUnit(m_ObjectTypes.at(m_Objects[i].m_ObjectIndex), NULL, selections); + CUnit* unit = pUnitMan->CreateUnit(m_ObjectTypes.at(m_Objects[i].m_ObjectIndex), NULL, selections); if (unit) { @@ -889,7 +889,7 @@ int CXMLReader::ReadEntities(XMBElement parent, double end_time) // TODO: save object IDs in the map file, and load them again, // so that triggers have a persistent identifier for objects - ent->m_actor->SetID(g_UnitMan.GetNewID()); + ent->m_actor->SetID(m_MapReader.pUnitMan->GetNewID()); } } @@ -945,7 +945,7 @@ int CXMLReader::ReadNonEntities(XMBElement parent, double end_time) std::set selections; // TODO: read from file - CUnit* unit = g_UnitMan.CreateUnit(ActorName, NULL, selections); + CUnit* unit = m_MapReader.pUnitMan->CreateUnit(ActorName, NULL, selections); if (unit) { @@ -961,7 +961,7 @@ int CXMLReader::ReadNonEntities(XMBElement parent, double end_time) // TODO: save object IDs in the map file, and load them again, // so that triggers have a persistent identifier for objects - unit->SetID(g_UnitMan.GetNewID()); + unit->SetID(m_MapReader.pUnitMan->GetNewID()); } completed_jobs++; diff --git a/source/graphics/MeshManager.h b/source/graphics/MeshManager.h index 2e941f62a4..a56735d306 100644 --- a/source/graphics/MeshManager.h +++ b/source/graphics/MeshManager.h @@ -1,8 +1,6 @@ #ifndef __H_MESHMANAGER_H__ #define __H_MESHMANAGER_H__ -#include "ps/Singleton.h" - #include class CModelDef; @@ -10,11 +8,9 @@ typedef boost::shared_ptr CModelDefPtr; class CStr8; -#define g_MeshManager CMeshManager::GetSingleton() - class CMeshManagerImpl; -class CMeshManager : public Singleton +class CMeshManager { public: CMeshManager(); diff --git a/source/graphics/ObjectBase.cpp b/source/graphics/ObjectBase.cpp index 30aefd7d5b..c2f8aaa481 100644 --- a/source/graphics/ObjectBase.cpp +++ b/source/graphics/ObjectBase.cpp @@ -12,7 +12,8 @@ #define LOG_CATEGORY "graphics" -CObjectBase::CObjectBase() +CObjectBase::CObjectBase(CObjectManager& objectManager) +: m_ObjectManager(objectManager) { m_Properties.m_CastShadows = true; m_Properties.m_AutoFlatten = false; @@ -303,7 +304,7 @@ std::vector CObjectBase::CalculateVariationKey(const std::vector::iterator it = chosenProps.begin(); it != chosenProps.end(); ++it) { - CObjectBase* prop = g_ObjMan.FindObjectBase(it->second); + CObjectBase* prop = m_ObjectManager.FindObjectBase(it->second); if (prop) { std::vector propChoices = prop->CalculateVariationKey(selections); @@ -479,7 +480,7 @@ std::set CObjectBase::CalculateRandomVariation(const std::set& initi // Load each prop, and add their required selections to ours: for (std::map::iterator it = chosenProps.begin(); it != chosenProps.end(); ++it) { - CObjectBase* prop = g_ObjMan.FindObjectBase(it->second); + CObjectBase* prop = m_ObjectManager.FindObjectBase(it->second); if (prop) { std::set propSelections = prop->CalculateRandomVariation(selections); @@ -553,7 +554,7 @@ std::vector > CObjectBase::GetVariantGroups() const { if (props[k].m_ModelName.Length()) { - CObjectBase* prop = g_ObjMan.FindObjectBase(props[k].m_ModelName); + CObjectBase* prop = m_ObjectManager.FindObjectBase(props[k].m_ModelName); if (prop) objectsQueue.push(prop); } diff --git a/source/graphics/ObjectBase.h b/source/graphics/ObjectBase.h index 72c0375435..2247976aec 100644 --- a/source/graphics/ObjectBase.h +++ b/source/graphics/ObjectBase.h @@ -3,6 +3,7 @@ class CModel; class CSkeletonAnim; +class CObjectManager; #include #include @@ -58,7 +59,7 @@ public: std::multimap anims; }; - CObjectBase(); + CObjectBase(CObjectManager& objectManager); // Get the variation key (indices of chosen variants from each group) // based on the selection strings @@ -99,6 +100,9 @@ public: private: std::vector< std::vector > m_VariantGroups; + CObjectManager& m_ObjectManager; + + NO_COPY_CTOR(CObjectBase); }; diff --git a/source/graphics/ObjectEntry.cpp b/source/graphics/ObjectEntry.cpp index 62527a7645..a6d8347d9d 100644 --- a/source/graphics/ObjectEntry.cpp +++ b/source/graphics/ObjectEntry.cpp @@ -38,7 +38,9 @@ CObjectEntry::~CObjectEntry() } -bool CObjectEntry::BuildVariation(const std::vector >& selections, const std::vector& variationKey) +bool CObjectEntry::BuildVariation(const std::vector >& selections, + const std::vector& variationKey, + CObjectManager& objectManager) { CObjectBase::Variation variation = m_Base->BuildVariation(variationKey); @@ -69,7 +71,7 @@ bool CObjectEntry::BuildVariation(const std::vector >& selection CModelDefPtr oldmodeldef = m_Model ? m_Model->GetModelDef() : CModelDefPtr(); // try and create a model - CModelDefPtr modeldef (g_MeshManager.GetMesh(m_ModelName)); + CModelDefPtr modeldef (objectManager.GetMeshManager().GetMesh(m_ModelName)); if (!modeldef) { LOG(ERROR, LOG_CATEGORY, "CObjectEntry::BuildModel(): Model %s failed to load", m_ModelName.c_str()); @@ -133,7 +135,7 @@ bool CObjectEntry::BuildVariation(const std::vector >& selection { const CObjectBase::Prop& prop = props[p]; - CObjectEntry* oe = g_ObjMan.FindObjectVariation(prop.m_ModelName, selections); + CObjectEntry* oe = objectManager.FindObjectVariation(prop.m_ModelName, selections); if (!oe) { LOG(ERROR, LOG_CATEGORY, "Failed to build prop model \"%s\" on actor \"%s\"", (const char*)prop.m_ModelName, (const char*)m_Base->m_ShortName); diff --git a/source/graphics/ObjectEntry.h b/source/graphics/ObjectEntry.h index a3a6295611..8ded245b8b 100644 --- a/source/graphics/ObjectEntry.h +++ b/source/graphics/ObjectEntry.h @@ -4,6 +4,7 @@ class CModel; class CSkeletonAnim; class CObjectBase; +class CMeshManager; struct SPropPoint; #include @@ -13,6 +14,8 @@ struct SPropPoint; #include "ps/CStr.h" #include "ps/Overlay.h" +class CObjectManager; + class CObjectEntry { public: @@ -20,7 +23,8 @@ public: ~CObjectEntry(); // Construct this actor, using the specified variation selections - bool BuildVariation(const std::vector >& selections, const std::vector& variationKey); + bool BuildVariation(const std::vector >& selections, + const std::vector& variationKey, CObjectManager& objectManager); // Base actor. Contains all the things that don't change between // different variations of the actor. diff --git a/source/graphics/ObjectManager.cpp b/source/graphics/ObjectManager.cpp index 440a98be96..8e14b1d389 100644 --- a/source/graphics/ObjectManager.cpp +++ b/source/graphics/ObjectManager.cpp @@ -43,7 +43,8 @@ bool operator< (const CObjectManager::ObjectKey& a, const CObjectManager::Object return a.ActorVariation < b.ActorVariation; } -CObjectManager::CObjectManager() +CObjectManager::CObjectManager(CMeshManager& meshManager) +: m_MeshManager(meshManager) { } @@ -65,7 +66,7 @@ CObjectBase* CObjectManager::FindObjectBase(const char* objectname) // Not already loaded, so try to load it: - CObjectBase* obj = new CObjectBase(); + CObjectBase* obj = new CObjectBase(*this); if (obj->Load(objectname)) { @@ -117,7 +118,7 @@ CObjectEntry* CObjectManager::FindObjectVariation(CObjectBase* base, const std:: // which has already worked out what to do for props, instead of passing the // selections into BuildVariation and having it recalculate the props' choices. - if (! obj->BuildVariation(selections, choices)) + if (! obj->BuildVariation(selections, choices, *this)) { DeleteObject(obj); return NULL; diff --git a/source/graphics/ObjectManager.h b/source/graphics/ObjectManager.h index 1ce691ea23..083dacf6dc 100644 --- a/source/graphics/ObjectManager.h +++ b/source/graphics/ObjectManager.h @@ -3,20 +3,17 @@ #include #include -#include "ps/Singleton.h" #include "ps/CStr.h" #include "ObjectBase.h" class CObjectEntry; class CEntityTemplate; class CMatrix3D; - -// access to sole CObjectManager object -#define g_ObjMan CObjectManager::GetSingleton() +class CMeshManager; /////////////////////////////////////////////////////////////////////////////////////////// // CObjectManager: manager class for all possible actor types -class CObjectManager : public Singleton +class CObjectManager { public: struct ObjectKey @@ -32,9 +29,11 @@ public: public: // constructor, destructor - CObjectManager(); + CObjectManager(CMeshManager& meshManager); ~CObjectManager(); + CMeshManager& GetMeshManager() const { return m_MeshManager; } + void UnloadObjects(); CObjectEntry* FindObject(const char* objname); @@ -45,12 +44,17 @@ public: CObjectEntry* FindObjectVariation(const char* objname, const std::vector >& selections); CObjectEntry* FindObjectVariation(CObjectBase* base, const std::vector >& selections); - // Get all names, quite slowly. (Intended only for ScEd.) - void GetAllObjectNames(std::vector& names); - void GetPropObjectNames(std::vector& names); + // Get all names, quite slowly. (Intended only for Atlas.) + static void GetAllObjectNames(std::vector& names); + static void GetPropObjectNames(std::vector& names); + +private: + CMeshManager& m_MeshManager; std::map m_Objects; std::map m_ObjectBases; + + NO_COPY_CTOR(CObjectManager); }; diff --git a/source/graphics/Unit.cpp b/source/graphics/Unit.cpp index 1997a4964e..f34a80a4f0 100644 --- a/source/graphics/Unit.cpp +++ b/source/graphics/Unit.cpp @@ -10,9 +10,11 @@ #include "ps/Game.h" #include "simulation/Entity.h" -CUnit::CUnit(CObjectEntry* object, CEntity* entity, const std::set& actorSelections) +CUnit::CUnit(CObjectEntry* object, CEntity* entity, CObjectManager& objectManager, + const std::set& actorSelections) : m_Object(object), m_Model(object->m_Model->Clone()), m_Entity(entity), - m_ID(-1), m_ActorSelections(actorSelections), m_PlayerID(-1) + m_ID(-1), m_ActorSelections(actorSelections), m_PlayerID(-1), + m_ObjectManager(objectManager) { } @@ -21,9 +23,10 @@ CUnit::~CUnit() delete m_Model; } -CUnit* CUnit::Create(const CStr& actorName, CEntity* entity, const std::set& selections) +CUnit* CUnit::Create(const CStr& actorName, CEntity* entity, + const std::set& selections, CObjectManager& objectManager) { - CObjectBase* base = g_ObjMan.FindObjectBase(actorName); + CObjectBase* base = objectManager.FindObjectBase(actorName); if (! base) return NULL; @@ -33,12 +36,12 @@ CUnit* CUnit::Create(const CStr& actorName, CEntity* entity, const std::set > selectionsVec; selectionsVec.push_back(actorSelections); - CObjectEntry* obj = g_ObjMan.FindObjectVariation(base, selectionsVec); + CObjectEntry* obj = objectManager.FindObjectVariation(base, selectionsVec); if (! obj) return NULL; - return new CUnit(obj, entity, actorSelections); + return new CUnit(obj, entity, objectManager, actorSelections); } void CUnit::ShowAmmunition() @@ -169,7 +172,7 @@ void CUnit::ReloadObject() selections.push_back(m_ActorSelections); // If these selections give a different object, change this unit to use it - CObjectEntry* newObject = g_ObjMan.FindObjectVariation(m_Object->m_Base, selections); + CObjectEntry* newObject = m_ObjectManager.FindObjectVariation(m_Object->m_Base, selections); if (newObject != m_Object) { // Clone the new object's base (non-instance) model diff --git a/source/graphics/Unit.h b/source/graphics/Unit.h index 2519fe6422..04da0203fa 100644 --- a/source/graphics/Unit.h +++ b/source/graphics/Unit.h @@ -7,6 +7,7 @@ class CModel; class CObjectEntry; +class CObjectManager; class CEntity; class CSkeletonAnim; class CStrW; @@ -17,13 +18,15 @@ class CUnit { private: // Private constructor. Needs complete list of selections for the variation. - CUnit(CObjectEntry* object, CEntity* entity, const std::set& actorSelections); + CUnit(CObjectEntry* object, CEntity* entity, CObjectManager& objectManager, + const std::set& actorSelections); public: // Attempt to create a unit with the given actor, attached to an entity // (or NULL), with a set of suggested selections (with the rest being randomised). // Returns NULL on failure. - static CUnit* Create(const CStr& actorName, CEntity* entity, const std::set& selections); + static CUnit* Create(const CStr& actorName, CEntity* entity, + const std::set& selections, CObjectManager& objectManager); // destructor ~CUnit(); @@ -88,7 +91,12 @@ private: // entity-level selections for this unit std::set m_EntitySelections; + // object manager which looks after this unit's objectentry + CObjectManager& m_ObjectManager; + void ReloadObject(); + + NO_COPY_CTOR(CUnit); }; #endif diff --git a/source/graphics/UnitManager.cpp b/source/graphics/UnitManager.cpp index 3e54298fc2..e50b3c3713 100644 --- a/source/graphics/UnitManager.cpp +++ b/source/graphics/UnitManager.cpp @@ -125,7 +125,10 @@ CUnit* CUnitManager::PickUnit(const CVector3D& origin, const CVector3D& dir, boo // CreateUnit: create a new unit and add it to the world CUnit* CUnitManager::CreateUnit(const CStr& actorName, CEntity* entity, const std::set& selections) { - CUnit* unit = CUnit::Create(actorName, entity, selections); + if (! m_ObjectManager) + return NULL; + + CUnit* unit = CUnit::Create(actorName, entity, selections, *m_ObjectManager); if (unit) AddUnit(unit); return unit; diff --git a/source/graphics/UnitManager.h b/source/graphics/UnitManager.h index b37a5ffd80..b7c9f200a3 100644 --- a/source/graphics/UnitManager.h +++ b/source/graphics/UnitManager.h @@ -11,20 +11,17 @@ #include #include -#include "ps/Singleton.h" class CUnit; class CModel; class CVector3D; class CEntity; class CStr; - -// access to sole CUnitManager object -#define g_UnitMan CUnitManager::GetSingleton() +class CObjectManager; /////////////////////////////////////////////////////////////////////////////// // CUnitManager: simple container class holding all units within the world -class CUnitManager : public Singleton +class CUnitManager { public: // constructor, destructor @@ -56,11 +53,15 @@ public: void SetNextID(int n) { m_NextID = n; } + void SetObjectManager(CObjectManager& objectManager) { m_ObjectManager = &objectManager; } + private: // list of all known units std::vector m_Units; // next ID number to be assigned to a unit created in the editor int m_NextID; + // graphical object manager; may be NULL if not set up + CObjectManager* m_ObjectManager; }; #endif diff --git a/source/graphics/scripting/JSInterface_Camera.cpp b/source/graphics/scripting/JSInterface_Camera.cpp index 98747c8603..93e9e3c0ec 100644 --- a/source/graphics/scripting/JSInterface_Camera.cpp +++ b/source/graphics/scripting/JSInterface_Camera.cpp @@ -1,6 +1,8 @@ #include "precompiled.h" #include "JSInterface_Camera.h" + +#include "scripting/JSConversions.h" #include "maths/scripting/JSInterface_Vector3D.h" #include "graphics/Camera.h" #include "maths/Vector3D.h" diff --git a/source/gui/MiniMap.cpp b/source/gui/MiniMap.cpp index f16ea9afba..0b53aa6522 100644 --- a/source/gui/MiniMap.cpp +++ b/source/gui/MiniMap.cpp @@ -237,7 +237,7 @@ void CMiniMap::Draw() // Set our globals in case they hadn't been set before m_Camera = g_Game->GetView()->GetCamera(); m_Terrain = g_Game->GetWorld()->GetTerrain(); - m_UnitManager = g_Game->GetWorld()->GetUnitManager(); + m_UnitManager = &g_Game->GetWorld()->GetUnitManager(); m_Width = (u32)(m_CachedActualSize.right - m_CachedActualSize.left); m_Height = (u32)(m_CachedActualSize.bottom - m_CachedActualSize.top); m_MapSize = m_Terrain->GetVerticesPerSide(); diff --git a/source/lib/secure_crt.cpp b/source/lib/secure_crt.cpp index acb4f61413..04ab9cd55a 100644 --- a/source/lib/secure_crt.cpp +++ b/source/lib/secure_crt.cpp @@ -5,6 +5,7 @@ #include "secure_crt.h" +#if !HAVE_SECURE_CRT int sprintf_s(char* buf, size_t max_chars, const char* fmt, ...) { @@ -25,3 +26,5 @@ errno_t fopen_s(FILE** pfile, const char* filename, const char* mode) *pfile = file; return 0; } + +#endif // #if !HAVE_SECURE_CRT diff --git a/source/main.cpp b/source/main.cpp index 5f53e60737..0471e383f1 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -34,6 +34,7 @@ that of Atlas depending on commandline parameters. #include "ps/Interact.h" #include "network/SessionManager.h" #include "simulation/TriggerManager.h" +#include "graphics/Camera.h" #include "graphics/GameView.h" #include "simulation/Scheduler.h" #include "sound/CMusicPlayer.h" diff --git a/source/ps/Game.cpp b/source/ps/Game.cpp index 399d512541..a774f45c0b 100644 --- a/source/ps/Game.cpp +++ b/source/ps/Game.cpp @@ -1,24 +1,25 @@ #include "precompiled.h" #include "Game.h" -#include "GameAttributes.h" -#include "CLogger.h" + +#include "graphics/GameView.h" +#include "graphics/UnitManager.h" +#include "lib/timer.h" +#include "network/Client.h" +#include "ps/CConsole.h" +#include "ps/CLogger.h" +#include "ps/CStr.h" +#include "ps/GameAttributes.h" +#include "ps/Loader.h" +#include "ps/Profile.h" +#include "ps/World.h" +#include "simulation/Entity.h" +#include "simulation/EntityManager.h" +#include "simulation/Simulation.h" + #ifndef NO_GUI #include "gui/CGUI.h" #endif -#include "lib/timer.h" -#include "Profile.h" -#include "Loader.h" -#include "CStr.h" -#include "simulation/EntityManager.h" -#include "simulation/Entity.h" -#include "CConsole.h" - -#include "ps/World.h" -#include "simulation/Simulation.h" -#include "graphics/GameView.h" - -#include "network/Client.h" class CNetServer; extern CNetServer *g_NetServer; @@ -48,7 +49,7 @@ CGame::CGame(): m_Time(0), m_SimRate(1.0f) { - debug_printf("CGame::CGame(): Game object CREATED; initializing..\n"); + m_World->GetUnitManager().SetObjectManager(m_GameView->GetObjectManager()); } #if MSC_VERSION @@ -63,8 +64,6 @@ CGame::~CGame() delete m_GameView; delete m_Simulation; delete m_World; - - debug_printf("CGame::~CGame(): Game object DESTROYED\n"); } diff --git a/source/ps/GameSetup/GameSetup.cpp b/source/ps/GameSetup/GameSetup.cpp index cbadf50911..16b0af09b3 100644 --- a/source/ps/GameSetup/GameSetup.cpp +++ b/source/ps/GameSetup/GameSetup.cpp @@ -714,12 +714,9 @@ static void InitRenderer() // create the material manager new CMaterialManager; - new CMeshManager; // create actor related stuff new CSkeletonAnimManager; - new CObjectManager; - new CUnitManager; MICROLOG(L"init renderer"); g_Renderer.Open(g_xres,g_yres,g_bpp); @@ -819,12 +816,9 @@ void Shutdown(uint flags) // destroy actor related stuff TIMER_BEGIN("shutdown actor stuff"); - delete &g_UnitMan; - delete &g_ObjMan; delete &g_SkelAnimMan; delete &g_MaterialManager; - delete &g_MeshManager; TIMER_END("shutdown actor stuff"); // destroy terrain related stuff diff --git a/source/ps/Interact.cpp b/source/ps/Interact.cpp index cb4875007e..2ef84175ec 100644 --- a/source/ps/Interact.cpp +++ b/source/ps/Interact.cpp @@ -644,7 +644,7 @@ void CMouseoverEntities::update( float timestep ) CVector3D origin, dir; pCamera->BuildCameraRay( origin, dir ); - CUnit* hit = g_UnitMan.PickUnit( origin, dir, true ); + CUnit* hit = g_Game->GetWorld()->GetUnitManager().PickUnit( origin, dir, true ); m_worldposition = pCamera->GetWorldCoordinates(true); @@ -1375,7 +1375,7 @@ bool CBuildingPlacer::activate(CStrW& templateName) CStr actorName ( m_template->m_actorName ); // convert CStrW->CStr8 std::set selections; - m_actor = g_UnitMan.CreateUnit( actorName, 0, selections ); + m_actor = g_Game->GetWorld()->GetUnitManager().CreateUnit( actorName, 0, selections ); m_actor->SetPlayerID( g_Game->GetLocalPlayer()->GetPlayerID() ); // m_bounds @@ -1426,7 +1426,7 @@ void CBuildingPlacer::mouseReleased() void CBuildingPlacer::deactivate() { m_active = false; - g_UnitMan.RemoveUnit( m_actor ); + g_Game->GetWorld()->GetUnitManager().RemoveUnit( m_actor ); delete m_actor; m_actor = 0; delete m_bounds; diff --git a/source/ps/World.cpp b/source/ps/World.cpp index dcca5bcff6..3863c0db53 100644 --- a/source/ps/World.cpp +++ b/source/ps/World.cpp @@ -34,12 +34,13 @@ CLightEnv g_LightEnv; CWorld::CWorld(CGame *pGame): m_pGame(pGame), m_Terrain(new CTerrain()), - m_UnitManager(&g_UnitMan), + m_UnitManager(new CUnitManager()), m_EntityManager(new CEntityManager()), m_ProjectileManager(new CProjectileManager()), m_LOSManager(new CLOSManager()), m_TerritoryManager(new CTerritoryManager()) -{} +{ +} void CWorld::Initialize(CGameAttributes *pAttribs) { @@ -79,6 +80,7 @@ CWorld::~CWorld() { delete m_Terrain; delete m_EntityManager; + delete m_UnitManager; // must come after deleting EntityManager delete m_ProjectileManager; delete m_LOSManager; delete m_TerritoryManager; diff --git a/source/ps/World.h b/source/ps/World.h index df27bc4af0..68072d8189 100644 --- a/source/ps/World.h +++ b/source/ps/World.h @@ -46,12 +46,12 @@ public: inline CTerrain *GetTerrain() { return m_Terrain; } - inline CUnitManager *GetUnitManager() - { return m_UnitManager; } + inline CUnitManager &GetUnitManager() + { return *m_UnitManager; } inline CEntityManager *GetEntityManager() { return m_EntityManager; } - inline CProjectileManager *GetProjectileManager() - { return m_ProjectileManager; } + inline CProjectileManager &GetProjectileManager() + { return *m_ProjectileManager; } inline CLOSManager *GetLOSManager() { return m_LOSManager; } inline CTerritoryManager *GetTerritoryManager() diff --git a/source/simulation/Entity.cpp b/source/simulation/Entity.cpp index b2ec32bac6..8c980673cf 100644 --- a/source/simulation/Entity.cpp +++ b/source/simulation/Entity.cpp @@ -118,7 +118,7 @@ CEntity::~CEntity() { if( m_actor ) { - g_UnitMan.RemoveUnit( m_actor ); + g_Game->GetWorld()->GetUnitManager().RemoveUnit( m_actor ); delete( m_actor ); } @@ -150,7 +150,7 @@ void CEntity::loadBase() { if( m_actor ) { - g_UnitMan.RemoveUnit( m_actor ); + g_Game->GetWorld()->GetUnitManager().RemoveUnit( m_actor ); delete( m_actor ); m_actor = NULL; } @@ -162,7 +162,7 @@ void CEntity::loadBase() CStr actorName ( m_base->m_actorName ); // convert CStrW->CStr8 - m_actor = g_UnitMan.CreateUnit( actorName, this, m_actorSelections ); + m_actor = g_Game->GetWorld()->GetUnitManager().CreateUnit( actorName, this, m_actorSelections ); // Set up our instance data @@ -239,7 +239,7 @@ void CEntity::kill() if( m_actor ) { - g_UnitMan.RemoveUnit( m_actor ); + g_Game->GetWorld()->GetUnitManager().RemoveUnit( m_actor ); delete( m_actor ); m_actor = NULL; } diff --git a/source/simulation/EntityScriptInterface.cpp b/source/simulation/EntityScriptInterface.cpp index bbe5b7d26e..a199150835 100644 --- a/source/simulation/EntityScriptInterface.cpp +++ b/source/simulation/EntityScriptInterface.cpp @@ -405,7 +405,7 @@ bool CEntity::Kill( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(arg } else { - g_UnitMan.RemoveUnit( m_actor ); + g_Game->GetWorld()->GetUnitManager().RemoveUnit( m_actor ); delete( m_actor ); m_actor = NULL; } diff --git a/source/simulation/Projectile.cpp b/source/simulation/Projectile.cpp index 8da1381c01..99873c6966 100644 --- a/source/simulation/Projectile.cpp +++ b/source/simulation/Projectile.cpp @@ -1,17 +1,19 @@ #include "precompiled.h" #include "Projectile.h" -#include "Entity.h" + +#include "ScriptObject.h" +#include "graphics/GameView.h" #include "graphics/Model.h" +#include "graphics/ObjectEntry.h" +#include "graphics/ObjectManager.h" +#include "graphics/Terrain.h" #include "graphics/Unit.h" #include "maths/Matrix3D.h" -#include "ScriptObject.h" -#include "ps/Game.h" -#include "Collision.h" -#include "graphics/ObjectManager.h" -#include "graphics/ObjectEntry.h" -#include "graphics/Terrain.h" #include "ps/CLogger.h" +#include "ps/Game.h" +#include "simulation/Collision.h" +#include "simulation/Entity.h" const double GRAVITY = 0.00001; const double GRAVITY_2 = GRAVITY * 0.5; @@ -45,11 +47,12 @@ CProjectile::CProjectile( const CModel* Actor, const CVector3D& Position, const CProjectile::~CProjectile() { + CProjectileManager& projectileManager = g_Game->GetWorld()->GetProjectileManager(); std::vector::iterator it; - for( it = g_ProjectileManager.m_Projectiles.begin(); it != g_ProjectileManager.m_Projectiles.end(); ++it ) + for( it = projectileManager.m_Projectiles.begin(); it != projectileManager.m_Projectiles.end(); ++it ) if( *it == this ) { - g_ProjectileManager.m_Projectiles.erase( it ); + projectileManager.m_Projectiles.erase( it ); break; } delete( m_Actor ); @@ -149,7 +152,7 @@ JSBool CProjectile::Construct( JSContext* cx, JSObject* UNUSED(obj), uint argc, goto fail; } } - else if( !ToPrimitive( cx, argv[0], ModelString ) || NULL == ( oe = g_ObjMan.FindObject( ModelString ) ) || NULL == ( Model = oe->m_Model ) ) + else if( !ToPrimitive( cx, argv[0], ModelString ) || NULL == ( oe = g_Game->GetView()->GetObjectManager().FindObject( ModelString ) ) || NULL == ( Model = oe->m_Model ) ) { err = "Invalid actor"; goto fail; @@ -202,7 +205,8 @@ JSBool CProjectile::Construct( JSContext* cx, JSObject* UNUSED(obj), uint argc, Miss = argv[6]; // Script to run on impact with the floor. { - CProjectile* p = g_ProjectileManager.AddProjectile( Model, Here, There, Speed / 1000.0f, Originator, Impact, Miss ); + CProjectile* p = g_Game->GetWorld()->GetProjectileManager() + .AddProjectile( Model, Here, There, Speed / 1000.0f, Originator, Impact, Miss ); *rval = ToJSVal( *p ); return( JS_TRUE ); @@ -235,14 +239,12 @@ CEventProjectileMiss::CEventProjectileMiss( CEntity* Originator, const CVector3D CProjectileManager::CProjectileManager() { m_LastTurnLength = 0; - debug_printf( "CProjectileManager CREATED\n" ); } CProjectileManager::~CProjectileManager() { while( m_Projectiles.size() ) delete( m_Projectiles[0] ); - debug_printf( "CProjectileManager DESTROYED\n" ); } CProjectile* CProjectileManager::AddProjectile( const CModel* Actor, const CVector3D& Position, const CVector3D& Target, float Speed, CEntity* Originator, const CScriptObject& ImpactScript, const CScriptObject& MissScript ) diff --git a/source/simulation/Projectile.h b/source/simulation/Projectile.h index b9a7d4ff9d..3a93603916 100644 --- a/source/simulation/Projectile.h +++ b/source/simulation/Projectile.h @@ -104,6 +104,4 @@ private: std::vector m_Projectiles; }; -#define g_ProjectileManager (*(g_Game->GetWorld()->GetProjectileManager())) - #endif diff --git a/source/simulation/Simulation.cpp b/source/simulation/Simulation.cpp index 79e15555cf..eb805f6fd1 100644 --- a/source/simulation/Simulation.cpp +++ b/source/simulation/Simulation.cpp @@ -102,12 +102,12 @@ void CSimulation::Update(double frameTime) void CSimulation::Interpolate(double frameTime, double offset) { - const std::vector& units=m_pWorld->GetUnitManager()->GetUnits(); + const std::vector& units=m_pWorld->GetUnitManager().GetUnits(); for (uint i=0;iGetModel()->Update((float)frameTime); g_EntityManager.interpolateAll( (float)offset ); - g_ProjectileManager.InterpolateAll( (float)offset ); + m_pWorld->GetProjectileManager().InterpolateAll( (float)offset ); g_Renderer.GetWaterManager()->m_WaterTexTimer += frameTime; } @@ -122,7 +122,7 @@ void CSimulation::Simulate() PROFILE_END( "entity updates" ); PROFILE_START( "projectile updates" ); - g_ProjectileManager.UpdateAll( m_pTurnManager->GetTurnLength() ); + m_pWorld->GetProjectileManager().UpdateAll( m_pTurnManager->GetTurnLength() ); PROFILE_END( "projectile updates" ); PROFILE_START( "los update" ); diff --git a/source/simulation/TerritoryManager.cpp b/source/simulation/TerritoryManager.cpp index 131ba10559..3576aea189 100644 --- a/source/simulation/TerritoryManager.cpp +++ b/source/simulation/TerritoryManager.cpp @@ -2,22 +2,24 @@ #include "TerritoryManager.h" +#include "graphics/Frustum.h" +#include "graphics/Camera.h" +#include "graphics/GameView.h" +#include "graphics/Model.h" +#include "graphics/Terrain.h" +#include "graphics/Unit.h" +#include "lib/allocators.h" +#include "lib/ogl.h" +#include "lib/timer.h" +#include "maths/Bound.h" #include "ps/Game.h" #include "ps/Player.h" #include "ps/Profile.h" -#include "graphics/Terrain.h" -#include "graphics/GameView.h" -#include "Entity.h" -#include "EntityManager.h" -#include "LOSManager.h" -#include "graphics/Unit.h" -#include "maths/Bound.h" -#include "graphics/Model.h" -#include "lib/allocators.h" -#include "lib/timer.h" -#include "lib/ogl.h" -#include "EntityManager.h" -#include "EntityTemplate.h" +#include "simulation/Entity.h" +#include "simulation/EntityManager.h" +#include "simulation/EntityManager.h" +#include "simulation/EntityTemplate.h" +#include "simulation/LOSManager.h" CTerritoryManager::CTerritoryManager() { diff --git a/source/tools/atlas/GameInterface/ActorViewer.cpp b/source/tools/atlas/GameInterface/ActorViewer.cpp index 07d04eb80e..e9414c98e4 100644 --- a/source/tools/atlas/GameInterface/ActorViewer.cpp +++ b/source/tools/atlas/GameInterface/ActorViewer.cpp @@ -24,6 +24,11 @@ struct ActorViewerImpl : public Scene { + ActorViewerImpl() + : Unit(NULL), MeshManager(), ObjectManager(MeshManager) + { + } + CUnit* Unit; CStrW CurrentUnitID; CStrW CurrentUnitAnim; @@ -36,6 +41,9 @@ struct ActorViewerImpl : public Scene CTerrain Terrain; + CMeshManager MeshManager; + CObjectManager ObjectManager; + // Simplistic implementation of the Scene interface void EnumerateObjects(const CFrustum& UNUSED(frustum), SceneCollector* c) { @@ -45,6 +53,8 @@ struct ActorViewerImpl : public Scene if (Unit) c->SubmitRecursive(Unit->GetModel()); } + + NO_COPY_CTOR(ActorViewerImpl); }; ActorViewer::ActorViewer() @@ -105,7 +115,7 @@ void ActorViewer::SetActor(const CStrW& id, const CStrW& animation) if (id.empty()) return; - m.Unit = CUnit::Create((CStr)id, NULL, std::set()); + m.Unit = CUnit::Create((CStr)id, NULL, std::set(), m.ObjectManager); if (! m.Unit) return; diff --git a/source/tools/atlas/GameInterface/Handlers/CameraCtrlHandlers.cpp b/source/tools/atlas/GameInterface/Handlers/CameraCtrlHandlers.cpp index 294568a02b..f97ef0275b 100644 --- a/source/tools/atlas/GameInterface/Handlers/CameraCtrlHandlers.cpp +++ b/source/tools/atlas/GameInterface/Handlers/CameraCtrlHandlers.cpp @@ -9,6 +9,7 @@ #include "ps/Game.h" #include "renderer/Renderer.h" #include "graphics/GameView.h" +#include "graphics/CinemaTrack.h" #include diff --git a/source/tools/atlas/GameInterface/Handlers/CinemaHandler.cpp b/source/tools/atlas/GameInterface/Handlers/CinemaHandler.cpp index ee581a4bfd..5d6ee3f528 100644 --- a/source/tools/atlas/GameInterface/Handlers/CinemaHandler.cpp +++ b/source/tools/atlas/GameInterface/Handlers/CinemaHandler.cpp @@ -2,6 +2,8 @@ #include "MessageHandler.h" #include "../CommandProc.h" +#include "graphics/Camera.h" +#include "graphics/CinemaTrack.h" #include "graphics/GameView.h" #include "ps/Game.h" #include "ps/CStr.h" diff --git a/source/tools/atlas/GameInterface/Handlers/GraphicsSetupHandlers.cpp b/source/tools/atlas/GameInterface/Handlers/GraphicsSetupHandlers.cpp index 8b9ef35a16..3dd548b811 100644 --- a/source/tools/atlas/GameInterface/Handlers/GraphicsSetupHandlers.cpp +++ b/source/tools/atlas/GameInterface/Handlers/GraphicsSetupHandlers.cpp @@ -117,7 +117,7 @@ MESSAGEHANDLER(SetActorViewer) // Should replace this with proper actor hot-loading system, or something. View::GetView_Actor()->GetActorViewer().SetActor(L"", L""); - g_ObjMan.UnloadObjects(); + g_Game->GetView()->GetObjectManager().UnloadObjects(); vfs_reload_changed_files(); } View::GetView_Actor()->SetSpeedMultiplier(msg->speed); diff --git a/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp b/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp index dc61dd0e47..b4a2e1537c 100644 --- a/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp +++ b/source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp @@ -35,7 +35,7 @@ static void InitGame(std::wstring map) g_GameAttributes.m_FogOfWar = false; // Disable unit AI (and other things that may interfere with making things look nice) - g_GameAttributes.m_ScreenshotMode = true; + g_GameAttributes.m_ScreenshotMode = false; // Initialise the game: g_Game = new CGame(); @@ -110,7 +110,7 @@ MESSAGEHANDLER(SaveMap) { CMapWriter writer; writer.SaveMap(CStr(L"maps/scenarios/" + *msg->filename), - g_Game->GetWorld()->GetTerrain(), g_Game->GetWorld()->GetUnitManager(), + g_Game->GetWorld()->GetTerrain(), &g_Game->GetWorld()->GetUnitManager(), g_Renderer.GetWaterManager(), g_Renderer.GetSkyManager(), &g_LightEnv, g_Game->GetView()->GetCamera(), g_Game->GetView()->GetCinema()); } diff --git a/source/tools/atlas/GameInterface/Handlers/ObjectHandlers.cpp b/source/tools/atlas/GameInterface/Handlers/ObjectHandlers.cpp index 5781622145..36d139b808 100644 --- a/source/tools/atlas/GameInterface/Handlers/ObjectHandlers.cpp +++ b/source/tools/atlas/GameInterface/Handlers/ObjectHandlers.cpp @@ -31,23 +31,30 @@ namespace AtlasMessage { -static bool SortObjectsList(const sObjectsListItem& a, const sObjectsListItem& b) +namespace { - return wcscmp(a.name.c_str(), b.name.c_str()) < 0; + bool SortObjectsList(const sObjectsListItem& a, const sObjectsListItem& b) + { + return wcscmp(a.name.c_str(), b.name.c_str()) < 0; + } + + bool IsFloating(const CUnit* unit) + { + if (! unit) + return false; + + if (unit->GetEntity()) + return (unit->GetEntity()->m_base->m_anchorType != L"Ground"); + else + return unit->GetObject()->m_Base->m_Properties.m_FloatOnWater; + } + + CUnitManager& GetUnitManager() + { + return g_Game->GetWorld()->GetUnitManager(); + } } -static bool IsFloating(const CUnit* unit) -{ - if (! unit) - return false; - - if (unit->GetEntity()) - return (unit->GetEntity()->m_base->m_anchorType != L"Ground"); - else - return unit->GetObject()->m_Base->m_Properties.m_FloatOnWater; -} - - QUERYHANDLER(GetObjectsList) { std::vector objects; @@ -69,8 +76,8 @@ QUERYHANDLER(GetObjectsList) { std::vector names; - //g_ObjMan.GetPropObjectNames(names); - g_ObjMan.GetAllObjectNames(names); + //CObjectManager::GetPropObjectNames(names); + CObjectManager::GetAllObjectNames(names); for (std::vector::iterator it = names.begin(); it != names.end(); ++it) { sObjectsListItem e; @@ -91,7 +98,7 @@ void AtlasRenderSelection() glDisable(GL_DEPTH_TEST); for (size_t i = 0; i < g_Selection.size(); ++i) { - CUnit* unit = g_UnitMan.FindByID(g_Selection[i]); + CUnit* unit = GetUnitManager().FindByID(g_Selection[i]); if (unit) { if (unit->GetEntity()) @@ -288,7 +295,7 @@ MESSAGEHANDLER(ObjectPreview) // Delete old unit if (g_PreviewUnit) { - g_UnitMan.RemoveUnit(g_PreviewUnit); + GetUnitManager().RemoveUnit(g_PreviewUnit); delete g_PreviewUnit; g_PreviewUnit = NULL; } @@ -305,14 +312,14 @@ MESSAGEHANDLER(ObjectPreview) CEntityTemplate* base = g_EntityTemplateCollection.getTemplate(name); if (base) // (ignore errors) { - g_PreviewUnit = g_UnitMan.CreateUnit(base->m_actorName, NULL, selections); + g_PreviewUnit = GetUnitManager().CreateUnit(base->m_actorName, NULL, selections); g_PreviewUnitFloating = (base->m_anchorType != L"Ground"); // TODO: variations } } else { - g_PreviewUnit = g_UnitMan.CreateUnit(CStr(name), NULL, selections); + g_PreviewUnit = GetUnitManager().CreateUnit(CStr(name), NULL, selections); g_PreviewUnitFloating = IsFloating(g_PreviewUnit); } } @@ -384,7 +391,7 @@ BEGIN_COMMAND(CreateObject) m_Player = msg->settings->player; // Get a new ID, for future reference to this unit - m_ID = g_UnitMan.GetNewID(); + m_ID = GetUnitManager().GetNewID(); Redo(); } @@ -429,7 +436,7 @@ BEGIN_COMMAND(CreateObject) } else { - CUnit* unit = g_UnitMan.CreateUnit(CStr(name), NULL, selections); + CUnit* unit = GetUnitManager().CreateUnit(CStr(name), NULL, selections); if (! unit) { LOG(ERROR, LOG_CATEGORY, "Failed to load nonentity actor '%ls'", name.c_str()); @@ -456,7 +463,7 @@ BEGIN_COMMAND(CreateObject) void Undo() { - CUnit* unit = g_UnitMan.FindByID(m_ID); + CUnit* unit = GetUnitManager().FindByID(m_ID); if (unit) { if (unit->GetEntity()) @@ -470,7 +477,7 @@ BEGIN_COMMAND(CreateObject) } else { - g_UnitMan.RemoveUnit(unit); + GetUnitManager().RemoveUnit(unit); delete unit; } } @@ -487,7 +494,7 @@ QUERYHANDLER(PickObject) CVector3D rayorigin, raydir; g_Game->GetView()->GetCamera()->BuildCameraRay((int)x, (int)y, rayorigin, raydir); - CUnit* target = g_UnitMan.PickUnit(rayorigin, raydir, false); + CUnit* target = GetUnitManager().PickUnit(rayorigin, raydir, false); if (target) msg->id = target->GetID(); @@ -526,7 +533,7 @@ BEGIN_COMMAND(MoveObject) void Do() { - CUnit* unit = g_UnitMan.FindByID(msg->id); + CUnit* unit = GetUnitManager().FindByID(msg->id); if (! unit) return; m_PosNew = GetUnitPos(msg->pos, IsFloating(unit)); @@ -546,7 +553,7 @@ BEGIN_COMMAND(MoveObject) void SetPos(CVector3D& pos) { - CUnit* unit = g_UnitMan.FindByID(msg->id); + CUnit* unit = GetUnitManager().FindByID(msg->id); if (! unit) return; if (unit->GetEntity()) @@ -591,7 +598,7 @@ BEGIN_COMMAND(RotateObject) void Do() { - CUnit* unit = g_UnitMan.FindByID(msg->id); + CUnit* unit = GetUnitManager().FindByID(msg->id); if (! unit) return; if (unit->GetEntity()) @@ -641,7 +648,7 @@ BEGIN_COMMAND(RotateObject) void SetAngle(float angle, CMatrix3D& transform) { - CUnit* unit = g_UnitMan.FindByID(msg->id); + CUnit* unit = GetUnitManager().FindByID(msg->id); if (! unit) return; if (unit->GetEntity()) @@ -702,7 +709,7 @@ BEGIN_COMMAND(DeleteObject) void Redo() { - CUnit* unit = g_UnitMan.FindByID(msg->id); + CUnit* unit = GetUnitManager().FindByID(msg->id); if (! unit) return; if (unit->GetEntity()) @@ -715,7 +722,7 @@ BEGIN_COMMAND(DeleteObject) g_Game->GetWorld()->GetTerritoryManager()->DelayedRecalculate(); } - g_UnitMan.RemoveUnit(unit); + GetUnitManager().RemoveUnit(unit); m_UnitInLimbo = unit; } @@ -729,7 +736,7 @@ BEGIN_COMMAND(DeleteObject) g_Game->GetWorld()->GetTerritoryManager()->DelayedRecalculate(); } - g_UnitMan.AddUnit(m_UnitInLimbo); + GetUnitManager().AddUnit(m_UnitInLimbo); m_UnitInLimbo = NULL; } }; diff --git a/source/tools/atlas/GameInterface/Handlers/TriggerHandler.cpp b/source/tools/atlas/GameInterface/Handlers/TriggerHandler.cpp index 5417c3ed18..33d8be2e9b 100644 --- a/source/tools/atlas/GameInterface/Handlers/TriggerHandler.cpp +++ b/source/tools/atlas/GameInterface/Handlers/TriggerHandler.cpp @@ -7,7 +7,7 @@ #include "simulation/TriggerManager.h" #include "ps/Game.h" #include "graphics/GameView.h" - +#include "graphics/CinemaTrack.h" namespace AtlasMessage { diff --git a/source/tools/atlas/GameInterface/Misc.cpp b/source/tools/atlas/GameInterface/Misc.cpp index cb14318c75..0e468ac041 100644 --- a/source/tools/atlas/GameInterface/Misc.cpp +++ b/source/tools/atlas/GameInterface/Misc.cpp @@ -7,6 +7,7 @@ #include "maths/Vector3D.h" #include "ps/Game.h" #include "graphics/GameView.h" +#include "graphics/Camera.h" CVector3D AtlasMessage::Position::GetWorldSpace(bool floating) const { diff --git a/source/tools/atlas/GameInterface/View.cpp b/source/tools/atlas/GameInterface/View.cpp index 7ce805aa0b..700080581d 100644 --- a/source/tools/atlas/GameInterface/View.cpp +++ b/source/tools/atlas/GameInterface/View.cpp @@ -6,6 +6,7 @@ #include "GameLoop.h" #include "Messages.h" +#include "graphics/CinemaTrack.h" #include "graphics/GameView.h" #include "graphics/SColor.h" #include "graphics/UnitManager.h" @@ -150,7 +151,7 @@ CCamera& ViewGame::GetCamera() CUnit* ViewGame::GetUnit(AtlasMessage::ObjectID id) { - return g_UnitMan.FindByID(id); + return g_Game->GetWorld()->GetUnitManager().FindByID(id); } bool ViewGame::WantsHighFramerate()