/
XBApplicationEx.cpp
177 lines (161 loc) · 4.32 KB
/
XBApplicationEx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright (C) 2005-2009 Team XBMC
* http://www.xbmc.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "system.h"
#include "XBApplicationEx.h"
#include "utils/log.h"
#include "threads/SystemClock.h"
#ifdef HAS_PERFORMANCE_SAMPLE
#include "utils/PerformanceSample.h"
#else
#define MEASURE_FUNCTION
#endif
CXBApplicationEx::CXBApplicationEx()
{
// Variables to perform app timing
m_bStop = false;
m_AppActive = true;
m_AppFocused = true;
m_ExitCode = EXITCODE_QUIT;
}
CXBApplicationEx::~CXBApplicationEx()
{
}
/* Create the app */
bool CXBApplicationEx::Create()
{
// Variables to perform app timing
m_bStop = false;
m_AppActive = true;
m_AppFocused = true;
m_ExitCode = EXITCODE_QUIT;
// Initialize the app's device-dependent objects
if (!Initialize())
{
CLog::Log(LOGERROR, "XBAppEx: Call to Initialize() failed!" );
return false;
}
return true;
}
/* Destroy the app */
VOID CXBApplicationEx::Destroy()
{
CLog::Log(LOGNOTICE, "destroy");
// Perform app-specific cleanup
Cleanup();
}
/* Function that runs the application */
INT CXBApplicationEx::Run(bool renderGUI)
{
CLog::Log(LOGNOTICE, "Running the application..." );
unsigned int lastFrameTime = 0;
unsigned int frameTime = 0;
const unsigned int noRenderFrameTime = 15; // Simulates ~66fps
#ifndef _DEBUG
BYTE processExceptionCount = 0;
BYTE frameMoveExceptionCount = 0;
BYTE renderExceptionCount = 0;
const BYTE MAX_EXCEPTION_COUNT = 10;
#endif
// Run xbmc
while (!m_bStop)
{
#ifdef HAS_PERFORMANCE_SAMPLE
CPerformanceSample sampleLoop("XBApplicationEx-loop");
#endif
//-----------------------------------------
// Animate and render a frame
//-----------------------------------------
#ifndef _DEBUG
try
{
#endif
lastFrameTime = XbmcThreads::SystemClockMillis();
Process();
//reset exception count
#ifndef _DEBUG
processExceptionCount = 0;
}
catch (...)
{
CLog::Log(LOGERROR, "exception in CApplication::Process()");
processExceptionCount++;
//MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
if (processExceptionCount > MAX_EXCEPTION_COUNT)
{
CLog::Log(LOGERROR, "CApplication::Process(), too many exceptions");
throw;
}
}
#endif
// Frame move the scene
#ifndef _DEBUG
try
{
#endif
if (!m_bStop) FrameMove(true, renderGUI);
//reset exception count
#ifndef _DEBUG
frameMoveExceptionCount = 0;
}
catch (...)
{
CLog::Log(LOGERROR, "exception in CApplication::FrameMove()");
frameMoveExceptionCount++;
//MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
if (frameMoveExceptionCount > MAX_EXCEPTION_COUNT)
{
CLog::Log(LOGERROR, "CApplication::FrameMove(), too many exceptions");
throw;
}
}
#endif
// Render the scene
#ifndef _DEBUG
try
{
#endif
if (renderGUI && !m_bStop) Render();
else if (!renderGUI)
{
frameTime = XbmcThreads::SystemClockMillis() - lastFrameTime;
if(frameTime < noRenderFrameTime)
Sleep(noRenderFrameTime - frameTime);
}
#ifndef _DEBUG
//reset exception count
renderExceptionCount = 0;
}
catch (...)
{
CLog::Log(LOGERROR, "exception in CApplication::Render()");
renderExceptionCount++;
//MAX_EXCEPTION_COUNT exceptions in a row? -> bail out
if (renderExceptionCount > MAX_EXCEPTION_COUNT)
{
CLog::Log(LOGERROR, "CApplication::Render(), too many exceptions");
throw;
}
}
#endif
} // while (!m_bStop)
Destroy();
CLog::Log(LOGNOTICE, "application stopped..." );
return m_ExitCode;
}