diff options
Diffstat (limited to 'src/app')
-rw-r--r-- | src/app/app.cpp | 113 | ||||
-rw-r--r-- | src/app/app.h | 52 |
2 files changed, 151 insertions, 14 deletions
diff --git a/src/app/app.cpp b/src/app/app.cpp index c936ac1..2155cf4 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -26,6 +26,7 @@ #include "common/image.h" #include "common/key.h" +#include "graphics/engine/modelmanager.h" #include "graphics/opengl/gldevice.h" #include "object/robotmain.h" @@ -98,6 +99,7 @@ CApplication::CApplication() m_engine = nullptr; m_device = nullptr; + m_modelManager = nullptr; m_robotMain = nullptr; m_sound = nullptr; @@ -126,6 +128,12 @@ CApplication::CApplication() m_curTimeStamp = CreateTimeStamp(); m_lastTimeStamp = CreateTimeStamp(); + for (int i = 0; i < PCNT_MAX; ++i) + { + m_performanceCounters[i][0] = CreateTimeStamp(); + m_performanceCounters[i][1] = CreateTimeStamp(); + } + m_joystickEnabled = false; m_mouseMode = MOUSE_SYSTEM; @@ -171,6 +179,12 @@ CApplication::~CApplication() DestroyTimeStamp(m_baseTimeStamp); DestroyTimeStamp(m_curTimeStamp); DestroyTimeStamp(m_lastTimeStamp); + + for (int i = 0; i < PCNT_MAX; ++i) + { + DestroyTimeStamp(m_performanceCounters[i][0]); + DestroyTimeStamp(m_performanceCounters[i][1]); + } } ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[]) @@ -250,6 +264,7 @@ ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[]) GetLogger()->Message("\n"); GetLogger()->Message("List of available options:\n"); GetLogger()->Message(" -help this help\n"); + GetLogger()->Message(" -vbo enable OpenGL VBOs\n"); GetLogger()->Message(" -datadir path set custom data directory path\n"); GetLogger()->Message(" -debug enable debug mode (more info printed in logs)\n"); GetLogger()->Message(" -loglevel level set log level to level (one of: trace, debug, info, warn, error, none)\n"); @@ -400,6 +415,9 @@ bool CApplication::Create() return false; } + // Create model manager + m_modelManager = new Gfx::CModelManager(m_engine); + // Create the robot application. m_robotMain = new CRobotMain(m_iMan, this); @@ -479,6 +497,12 @@ void CApplication::Destroy() m_sound = nullptr; } + if (m_modelManager != nullptr) + { + delete m_modelManager; + m_modelManager = nullptr; + } + if (m_engine != nullptr) { m_engine->Destroy(); @@ -693,6 +717,14 @@ int CApplication::Run() while (true) { + ResetPerformanceCounters(); + + if (m_active) + { + StartPerformanceCounter(PCNT_ALL); + StartPerformanceCounter(PCNT_EVENT_PROCESSING); + } + // To be sure no old event remains m_private->currentEvent.type = SDL_NOEVENT; @@ -804,15 +836,38 @@ int CApplication::Run() m_robotMain->EventProcess(event); } + StopPerformanceCounter(PCNT_EVENT_PROCESSING); + + StartPerformanceCounter(PCNT_UPDATE_ALL); + + // Prepare and process step simulation event + event = CreateUpdateEvent(); + if (event.type != EVENT_NULL && m_robotMain != nullptr) + { + StartPerformanceCounter(PCNT_UPDATE_ENGINE); + m_engine->FrameUpdate(); + StopPerformanceCounter(PCNT_UPDATE_ENGINE); + + m_sound->FrameMove(m_relTime); + + StartPerformanceCounter(PCNT_UPDATE_GAME); + m_robotMain->EventProcess(event); + StopPerformanceCounter(PCNT_UPDATE_GAME); + } + + StopPerformanceCounter(PCNT_UPDATE_ALL); + /* Update mouse position explicitly right before rendering * because mouse events are usually way behind */ UpdateMouse(); - // Update game and render a frame during idle time (no messages are waiting) + StartPerformanceCounter(PCNT_RENDER_ALL); Render(); + StopPerformanceCounter(PCNT_RENDER_ALL); - // Update simulation state - StepSimulation(); + StopPerformanceCounter(PCNT_ALL); + + UpdatePerformanceCountersData(); if (m_lowCPU) { @@ -1142,10 +1197,10 @@ void CApplication::SetSimulationSpeed(float speed) GetLogger()->Info("Simulation speed = %.2f\n", speed); } -void CApplication::StepSimulation() +Event CApplication::CreateUpdateEvent() { if (m_simulationSuspended) - return; + return Event(EVENT_NULL); CopyTimeStamp(m_lastTimeStamp, m_curTimeStamp); GetCurrentTimeStamp(m_curTimeStamp); @@ -1160,11 +1215,6 @@ void CApplication::StepSimulation() m_exactRelTime = m_simulationSpeed * m_realRelTime; m_relTime = (m_simulationSpeed * m_realRelTime) / 1e9f; - - m_engine->FrameUpdate(); - m_sound->FrameMove(m_relTime); - - Event frameEvent(EVENT_FRAME); frameEvent.systemEvent = true; frameEvent.trackedKeysState = m_trackedKeys; @@ -1172,7 +1222,8 @@ void CApplication::StepSimulation() frameEvent.mousePos = m_mousePos; frameEvent.mouseButtonsState = m_mouseButtonsState; frameEvent.rTime = m_relTime; - m_eventQueue->AddEvent(frameEvent); + + return frameEvent; } float CApplication::GetSimulationSpeed() @@ -1516,3 +1567,43 @@ bool CApplication::GetLowCPU() { return m_lowCPU; } + +void CApplication::StartPerformanceCounter(PerformanceCounter counter) +{ + GetCurrentTimeStamp(m_performanceCounters[counter][0]); +} + +void CApplication::StopPerformanceCounter(PerformanceCounter counter) +{ + GetCurrentTimeStamp(m_performanceCounters[counter][1]); +} + +float CApplication::GetPerformanceCounterData(PerformanceCounter counter) +{ + return m_performanceCountersData[counter]; +} + +void CApplication::ResetPerformanceCounters() +{ + for (int i = 0; i < PCNT_MAX; ++i) + { + StartPerformanceCounter(static_cast<PerformanceCounter>(i)); + StopPerformanceCounter(static_cast<PerformanceCounter>(i)); + } +} + +void CApplication::UpdatePerformanceCountersData() +{ + long long sum = TimeStampExactDiff(m_performanceCounters[PCNT_ALL][0], + m_performanceCounters[PCNT_ALL][1]); + + for (int i = 0; i < PCNT_MAX; ++i) + { + long long diff = TimeStampExactDiff(m_performanceCounters[i][0], + m_performanceCounters[i][1]); + + m_performanceCountersData[static_cast<PerformanceCounter>(i)] = + static_cast<float>(diff) / static_cast<float>(sum); + } +} + diff --git a/src/app/app.h b/src/app/app.h index 2da20d3..4aa97ce 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -41,6 +41,10 @@ class CEventQueue; class CRobotMain; class CSoundInterface; +namespace Gfx { +class CModelManager; +} + /** * \struct JoystickDevice * \brief Information about a joystick device @@ -112,6 +116,31 @@ enum MouseMode MOUSE_NONE, //! < no cursor visible }; +/** + * \enum PerformanceCounter + * \brief Type of counter testing performance + */ +enum PerformanceCounter +{ + PCNT_EVENT_PROCESSING, //! < event processing (except update events) + + PCNT_UPDATE_ALL, //! < the whole frame update process + PCNT_UPDATE_ENGINE, //! < frame update in CEngine + PCNT_UPDATE_PARTICLE, //! < frame update in CParticle + PCNT_UPDATE_GAME, //! < frame update in CRobotMain + + PCNT_RENDER_ALL, //! < the whole rendering process + PCNT_RENDER_PARTICLE, //! < rendering the particles in 3D + PCNT_RENDER_WATER, //! < rendering the water + PCNT_RENDER_TERRAIN, //! < rendering the terrain + PCNT_RENDER_OBJECTS, //! < rendering the 3D objects + PCNT_RENDER_INTERFACE, //! < rendering 2D interface + + PCNT_ALL, //! < all counters together + + PCNT_MAX +}; + struct ApplicationPrivate; /** @@ -198,9 +227,6 @@ public: //! Returns whether simulation is suspended bool GetSimulationSuspended(); - //! Updates the simulation state - void StepSimulation(); - //@{ //! Management of simulation speed void SetSimulationSpeed(float speed); @@ -304,6 +330,13 @@ public: bool GetLowCPU(); //@} + //! Management of performance counters + //@{ + void StartPerformanceCounter(PerformanceCounter counter); + void StopPerformanceCounter(PerformanceCounter counter); + float GetPerformanceCounterData(PerformanceCounter counter); + //@} + protected: //! Creates the window's SDL_Surface bool CreateVideoSurface(); @@ -312,6 +345,8 @@ protected: Event ProcessSystemEvent(); //! If applicable, creates a virtual event to match the changed state as of new event Event CreateVirtualEvent(const Event& sourceEvent); + //! Prepares a simulation update event + Event CreateUpdateEvent(); //! Handles some incoming events bool ProcessEvent(const Event& event); //! Renders the image in window @@ -322,6 +357,11 @@ protected: //! Closes the joystick device void CloseJoystick(); + //! Resets all performance counters to zero + void ResetPerformanceCounters(); + //! Updates performance counters from gathered timer data + void UpdatePerformanceCountersData(); + protected: //! Instance manager CInstanceManager* m_iMan; @@ -333,10 +373,13 @@ protected: Gfx::CEngine* m_engine; //! Graphics device Gfx::CDevice* m_device; + //! 3D models manager + Gfx::CModelManager* m_modelManager; //! Sound subsystem CSoundInterface* m_sound; //! Main class of the proper game engine CRobotMain* m_robotMain; + //! Profile (INI) reader/writer CProfile* m_profile; //! Code to return at exit @@ -363,6 +406,9 @@ protected: SystemTimeStamp* m_lastTimeStamp; SystemTimeStamp* m_curTimeStamp; + SystemTimeStamp* m_performanceCounters[PCNT_MAX][2]; + float m_performanceCountersData[PCNT_MAX]; + long long m_realAbsTimeBase; long long m_realAbsTime; long long m_realRelTime; |