2 * Copyright (C) 2005-2012 Team XBMC
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "XBApplicationEx.h"
23 #include "utils/log.h"
24 #include "threads/SystemClock.h"
25 #ifdef HAS_PERFORMANCE_SAMPLE
26 #include "utils/PerformanceSample.h"
28 #define MEASURE_FUNCTION
30 #include "commons/Exception.h"
32 // Put this here for easy enable and disable
34 #define XBMC_TRACK_EXCEPTIONS
37 CXBApplicationEx::CXBApplicationEx()
39 // Variables to perform app timing
43 m_ExitCode = EXITCODE_QUIT;
46 CXBApplicationEx::~CXBApplicationEx()
51 bool CXBApplicationEx::Create()
53 // Variables to perform app timing
57 m_ExitCode = EXITCODE_QUIT;
59 // Initialize the app's device-dependent objects
62 CLog::Log(LOGERROR, "XBAppEx: Call to Initialize() failed!" );
70 VOID CXBApplicationEx::Destroy()
72 CLog::Log(LOGNOTICE, "destroy");
73 // Perform app-specific cleanup
77 /* Function that runs the application */
78 INT CXBApplicationEx::Run(bool renderGUI)
80 CLog::Log(LOGNOTICE, "Running the application..." );
82 unsigned int lastFrameTime = 0;
83 unsigned int frameTime = 0;
84 const unsigned int noRenderFrameTime = 15; // Simulates ~66fps
86 #ifdef XBMC_TRACK_EXCEPTIONS
87 BYTE processExceptionCount = 0;
88 BYTE frameMoveExceptionCount = 0;
89 BYTE renderExceptionCount = 0;
90 const BYTE MAX_EXCEPTION_COUNT = 10;
96 #ifdef HAS_PERFORMANCE_SAMPLE
97 CPerformanceSample sampleLoop("XBApplicationEx-loop");
99 //-----------------------------------------
100 // Animate and render a frame
101 //-----------------------------------------
102 #ifdef XBMC_TRACK_EXCEPTIONS
106 lastFrameTime = XbmcThreads::SystemClockMillis();
108 //reset exception count
109 #ifdef XBMC_TRACK_EXCEPTIONS
110 processExceptionCount = 0;
113 catch (const XbmcCommons::UncheckedException &e)
115 e.LogThrowMessage("CApplication::Process()");
116 processExceptionCount++;
117 //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
118 if (processExceptionCount > MAX_EXCEPTION_COUNT)
120 CLog::Log(LOGERROR, "CApplication::Process(), too many exceptions");
126 CLog::Log(LOGERROR, "exception in CApplication::Process()");
127 processExceptionCount++;
128 //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
129 if (processExceptionCount > MAX_EXCEPTION_COUNT)
131 CLog::Log(LOGERROR, "CApplication::Process(), too many exceptions");
136 // Frame move the scene
137 #ifdef XBMC_TRACK_EXCEPTIONS
141 if (!m_bStop) FrameMove(true, renderGUI);
142 //reset exception count
143 #ifdef XBMC_TRACK_EXCEPTIONS
144 frameMoveExceptionCount = 0;
147 catch (const XbmcCommons::UncheckedException &e)
149 e.LogThrowMessage("CApplication::FrameMove()");
150 frameMoveExceptionCount++;
151 //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
152 if (frameMoveExceptionCount > MAX_EXCEPTION_COUNT)
154 CLog::Log(LOGERROR, "CApplication::FrameMove(), too many exceptions");
160 CLog::Log(LOGERROR, "exception in CApplication::FrameMove()");
161 frameMoveExceptionCount++;
162 //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
163 if (frameMoveExceptionCount > MAX_EXCEPTION_COUNT)
165 CLog::Log(LOGERROR, "CApplication::FrameMove(), too many exceptions");
172 #ifdef XBMC_TRACK_EXCEPTIONS
176 if (renderGUI && !m_bStop) Render();
179 frameTime = XbmcThreads::SystemClockMillis() - lastFrameTime;
180 if(frameTime < noRenderFrameTime)
181 Sleep(noRenderFrameTime - frameTime);
183 #ifdef XBMC_TRACK_EXCEPTIONS
184 //reset exception count
185 renderExceptionCount = 0;
188 catch (const XbmcCommons::UncheckedException &e)
190 e.LogThrowMessage("CApplication::Render()");
191 renderExceptionCount++;
192 //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
193 if (renderExceptionCount > MAX_EXCEPTION_COUNT)
195 CLog::Log(LOGERROR, "CApplication::Render(), too many exceptions");
201 CLog::Log(LOGERROR, "exception in CApplication::Render()");
202 renderExceptionCount++;
203 //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
204 if (renderExceptionCount > MAX_EXCEPTION_COUNT)
206 CLog::Log(LOGERROR, "CApplication::Render(), too many exceptions");
211 } // while (!m_bStop)
214 CLog::Log(LOGNOTICE, "application stopped..." );