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;
47 CXBApplicationEx::~CXBApplicationEx()
52 bool CXBApplicationEx::Create()
54 // Variables to perform app timing
58 m_ExitCode = EXITCODE_QUIT;
60 // Initialize the app's device-dependent objects
63 CLog::Log(LOGERROR, "XBAppEx: Call to Initialize() failed!" );
71 VOID CXBApplicationEx::Destroy()
73 CLog::Log(LOGNOTICE, "destroy");
74 // Perform app-specific cleanup
78 /* Function that runs the application */
79 INT CXBApplicationEx::Run()
81 CLog::Log(LOGNOTICE, "Running the application..." );
83 unsigned int lastFrameTime = 0;
84 unsigned int frameTime = 0;
85 const unsigned int noRenderFrameTime = 15; // Simulates ~66fps
87 #ifdef XBMC_TRACK_EXCEPTIONS
88 BYTE processExceptionCount = 0;
89 BYTE frameMoveExceptionCount = 0;
90 BYTE renderExceptionCount = 0;
91 const BYTE MAX_EXCEPTION_COUNT = 10;
97 #ifdef HAS_PERFORMANCE_SAMPLE
98 CPerformanceSample sampleLoop("XBApplicationEx-loop");
100 //-----------------------------------------
101 // Animate and render a frame
102 //-----------------------------------------
103 #ifdef XBMC_TRACK_EXCEPTIONS
107 lastFrameTime = XbmcThreads::SystemClockMillis();
109 //reset exception count
110 #ifdef XBMC_TRACK_EXCEPTIONS
111 processExceptionCount = 0;
114 catch (const XbmcCommons::UncheckedException &e)
116 e.LogThrowMessage("CApplication::Process()");
117 processExceptionCount++;
118 //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
119 if (processExceptionCount > MAX_EXCEPTION_COUNT)
121 CLog::Log(LOGERROR, "CApplication::Process(), too many exceptions");
127 CLog::Log(LOGERROR, "exception in CApplication::Process()");
128 processExceptionCount++;
129 //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
130 if (processExceptionCount > MAX_EXCEPTION_COUNT)
132 CLog::Log(LOGERROR, "CApplication::Process(), too many exceptions");
137 // Frame move the scene
138 #ifdef XBMC_TRACK_EXCEPTIONS
142 if (!m_bStop) FrameMove(true, m_renderGUI);
143 //reset exception count
144 #ifdef XBMC_TRACK_EXCEPTIONS
145 frameMoveExceptionCount = 0;
148 catch (const XbmcCommons::UncheckedException &e)
150 e.LogThrowMessage("CApplication::FrameMove()");
151 frameMoveExceptionCount++;
152 //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
153 if (frameMoveExceptionCount > MAX_EXCEPTION_COUNT)
155 CLog::Log(LOGERROR, "CApplication::FrameMove(), too many exceptions");
161 CLog::Log(LOGERROR, "exception in CApplication::FrameMove()");
162 frameMoveExceptionCount++;
163 //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
164 if (frameMoveExceptionCount > MAX_EXCEPTION_COUNT)
166 CLog::Log(LOGERROR, "CApplication::FrameMove(), too many exceptions");
173 #ifdef XBMC_TRACK_EXCEPTIONS
177 if (m_renderGUI && !m_bStop) Render();
178 else if (!m_renderGUI)
180 frameTime = XbmcThreads::SystemClockMillis() - lastFrameTime;
181 if(frameTime < noRenderFrameTime)
182 Sleep(noRenderFrameTime - frameTime);
184 #ifdef XBMC_TRACK_EXCEPTIONS
185 //reset exception count
186 renderExceptionCount = 0;
189 catch (const XbmcCommons::UncheckedException &e)
191 e.LogThrowMessage("CApplication::Render()");
192 renderExceptionCount++;
193 //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
194 if (renderExceptionCount > MAX_EXCEPTION_COUNT)
196 CLog::Log(LOGERROR, "CApplication::Render(), too many exceptions");
202 CLog::Log(LOGERROR, "exception in CApplication::Render()");
203 renderExceptionCount++;
204 //MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
205 if (renderExceptionCount > MAX_EXCEPTION_COUNT)
207 CLog::Log(LOGERROR, "CApplication::Render(), too many exceptions");
212 } // while (!m_bStop)
215 CLog::Log(LOGNOTICE, "application stopped..." );