@@ -12,6 +12,7 @@ using namespace AppFramework;
12
12
#include < fcntl.h>
13
13
#include < mmsystem.h>
14
14
#include < strsafe.h>
15
+ using namespace std ;
15
16
16
17
namespace AppFramework
17
18
{
@@ -42,7 +43,22 @@ void FrameworkWin::Init(HINSTANCE& hInstance, int& nCmdShow)
42
43
43
44
int FrameworkWin::Run ()
44
45
{
45
- #ifdef _DEBUG
46
+ // TODO: Place code here.
47
+ MSG msg; memset (&msg, 0 , sizeof (msg));
48
+ HACCEL hAccelTable;
49
+
50
+ // Initialize global strings
51
+ LoadString (m_hInstance, IDS_APP_TITLE, m_szTitle, MAX_LOADSTRING);
52
+ LoadString (m_hInstance, IDC_FRAMEWORK, m_szWindowClass, MAX_LOADSTRING);
53
+ MyRegisterClass (m_hInstance);
54
+
55
+ // Perform application initialization:
56
+ if (!InitInstance (m_hInstance, m_nCmdShow))
57
+ {
58
+ return FALSE ;
59
+ }
60
+
61
+ #if (1) // _DEBUG
46
62
// Allocate the console
47
63
AllocConsole ();
48
64
@@ -60,21 +76,6 @@ int FrameworkWin::Run()
60
76
*stdin = *hf_in;
61
77
#endif
62
78
63
- // TODO: Place code here.
64
- MSG msg; memset (&msg, 0 , sizeof (msg));
65
- HACCEL hAccelTable;
66
-
67
- // Initialize global strings
68
- LoadString (m_hInstance, IDS_APP_TITLE, m_szTitle, MAX_LOADSTRING);
69
- LoadString (m_hInstance, IDC_FRAMEWORK, m_szWindowClass, MAX_LOADSTRING);
70
- MyRegisterClass (m_hInstance);
71
-
72
- // Perform application initialization:
73
- if (!InitInstance (m_hInstance, m_nCmdShow))
74
- {
75
- return FALSE ;
76
- }
77
-
78
79
hAccelTable = LoadAccelerators (m_hInstance, MAKEINTRESOURCE (IDC_FRAMEWORK));
79
80
80
81
// Force display and system to be in a working state by resetting
@@ -85,13 +86,14 @@ int FrameworkWin::Run()
85
86
bool bAppRdy = false ; // App can be updated
86
87
PHANDLE hThread = nullptr ; // Array of thread handles
87
88
unsigned int * nThArgs = nullptr ; // Array of thread arguments
89
+ unsigned int startLoadingTicks = 0 ; // Timestamp for timing resource loading
88
90
89
91
// Acquire system information
90
92
SYSTEM_INFO sysinfo;
91
93
GetSystemInfo (&sysinfo);
92
94
93
- // Create one thread per CPU (minus the one for the UI)
94
- DWORD dwCPUCount = sysinfo.dwNumberOfProcessors - 1 ;
95
+ // Create one thread per CPU
96
+ DWORD dwCPUCount = sysinfo.dwNumberOfProcessors ;
95
97
if (dwCPUCount < 1 )
96
98
dwCPUCount = 1 ;
97
99
@@ -104,6 +106,9 @@ int FrameworkWin::Run()
104
106
// Initialize app
105
107
if (AppMain->Init (m_hWnd))
106
108
{
109
+ cout << " Starting " << dwCPUCount << " threads for loading resources." << endl << endl;
110
+ startLoadingTicks = GetTicks ();
111
+
107
112
// Create some threads for loading data. Applications can decide how to schedule
108
113
// their tasks based on provided thread index and total thread count.
109
114
DWORD (WINAPI* pThread)(LPVOID) = &AppMainLoadResources_wrapper;
@@ -119,7 +124,6 @@ int FrameworkWin::Run()
119
124
ErrorExit (TEXT (" CreateThread()" ));
120
125
bExit = true ;
121
126
}
122
- /*
123
127
else
124
128
{
125
129
// Set the loading threads' priorities to above normal
@@ -129,8 +133,10 @@ int FrameworkWin::Run()
129
133
bExit = true ;
130
134
}
131
135
}
132
- */
133
136
}
137
+
138
+ // Reduce this thread's priority since it isn't doing anything important
139
+ SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_BELOW_NORMAL);
134
140
}
135
141
else
136
142
{
@@ -153,12 +159,12 @@ int FrameworkWin::Run()
153
159
DispatchMessage (&msg);
154
160
}
155
161
156
- if (pInputMgr)
162
+ if (pInputMgr && bAppRdy )
157
163
pInputMgr->HandleMessage (msg);
158
164
}
159
165
160
166
// Update input
161
- if (pInputMgr)
167
+ if (pInputMgr && bAppRdy )
162
168
{
163
169
RECT rc;
164
170
RECT wRect;
@@ -200,34 +206,61 @@ int FrameworkWin::Run()
200
206
if (lpExitCode == STILL_ACTIVE)
201
207
{
202
208
bAppRdy = false ;
203
- }
204
- else
205
- {
206
- CloseHandle (hThread[i]);
207
- hThread[i] = nullptr ;
209
+ break ;
208
210
}
209
211
}
210
212
else
211
213
{
212
214
ErrorExit (TEXT (" GetExitCodeThread()" ));
213
215
bExit = true ;
216
+ break ;
214
217
}
215
218
}
216
219
}
220
+
221
+ // Restore thread priority if resources are loaded
222
+ if (bAppRdy)
223
+ {
224
+ cout << endl << " Resources successfully loaded in " << (float )(GetTicks () - startLoadingTicks) / 1000000 .f << " seconds." << endl;
225
+ SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_NORMAL);
226
+ #if !(_DEBUG)
227
+ Sleep (2000 );
228
+ // Free the console
229
+ FreeConsole ();
230
+ #endif
231
+ BringWindowToTop (m_hWnd);
232
+ SetFocus (m_hWnd);
233
+ }
234
+
235
+ // Give some CPU time to other threads
236
+ Sleep (1 );
217
237
}
218
238
else
219
239
bExit = true ;
220
240
}
221
241
}
222
242
243
+ WaitForMultipleObjects (dwCPUCount, hThread, TRUE , INFINITE);
244
+ for (unsigned int i = 0 ; i < dwCPUCount; i++)
245
+ {
246
+ if (hThread[i])
247
+ CloseHandle (hThread[i]);
248
+ hThread[i] = nullptr ;
249
+ }
250
+
223
251
// Reset execution state
224
252
SetThreadExecutionState (ES_CONTINUOUS);
225
253
254
+ // Release app resources
255
+ AppMain->Release ();
256
+
226
257
// Release previously allocated memory
227
258
delete[] nThArgs;
228
259
delete[] hThread;
229
260
230
- #ifdef _DEBUG
261
+ DestroyWindow (m_hWnd);
262
+
263
+ #if _DEBUG
231
264
// Free the console
232
265
FreeConsole ();
233
266
#endif
@@ -452,7 +485,7 @@ unsigned int FrameworkWin::GetTicks()
452
485
now = timeGetTime ();
453
486
}
454
487
455
- return (now - m_nStartTicks);
488
+ return (now - m_nStartTicks) * 1000 ;
456
489
}
457
490
458
491
float FrameworkWin::CalculateDeltaTime ()
@@ -464,6 +497,11 @@ float FrameworkWin::CalculateDeltaTime()
464
497
return (float )deltaTicks / 1000 .f ;
465
498
}
466
499
500
+ void FrameworkWin::Sleep (const unsigned int miliseconds)
501
+ {
502
+ ::Sleep (miliseconds);
503
+ }
504
+
467
505
void FrameworkWin::ErrorExit (LPTSTR lpszFunction)
468
506
{
469
507
// Retrieve the system error message for the last-error code
0 commit comments