Engine.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #include "StdH.h"
  13. #include <Engine/Build.h>
  14. #include <Engine/Base/Profiling.h>
  15. #include <Engine/Base/Input.h>
  16. #include <Engine/Base/Protection.h>
  17. #include <Engine/Base/Console.h>
  18. #include <Engine/Base/Console_internal.h>
  19. #include <Engine/Base/Statistics_Internal.h>
  20. #include <Engine/Base/Shell.h>
  21. #include <Engine/Base/CRC.h>
  22. #include <Engine/Base/CRCTable.h>
  23. #include <Engine/Base/ProgressHook.h>
  24. #include <Engine/Sound/SoundListener.h>
  25. #include <Engine/Sound/SoundLibrary.h>
  26. #include <Engine/Graphics/GfxLibrary.h>
  27. #include <Engine/Graphics/Font.h>
  28. #include <Engine/Network/Network.h>
  29. #include <Engine/templates/DynamicContainer.cpp>
  30. #include <Engine/Templates/Stock_CAnimData.h>
  31. #include <Engine/Templates/Stock_CTextureData.h>
  32. #include <Engine/Templates/Stock_CSoundData.h>
  33. #include <Engine/Templates/Stock_CModelData.h>
  34. #include <Engine/Templates/Stock_CEntityClass.h>
  35. #include <Engine/Templates/Stock_CMesh.h>
  36. #include <Engine/Templates/Stock_CSkeleton.h>
  37. #include <Engine/Templates/Stock_CAnimSet.h>
  38. #include <Engine/Templates/Stock_CShader.h>
  39. #include <Engine/Templates/StaticArray.cpp>
  40. #include <Engine/Base/IFeel.h>
  41. // this version string can be referenced from outside the engine
  42. ENGINE_API CTString _strEngineBuild = "";
  43. ENGINE_API ULONG _ulEngineBuildMajor = _SE_BUILD_MAJOR;
  44. ENGINE_API ULONG _ulEngineBuildMinor = _SE_BUILD_MINOR;
  45. ENGINE_API BOOL _bDedicatedServer = FALSE;
  46. ENGINE_API BOOL _bWorldEditorApp = FALSE;
  47. ENGINE_API CTString _strLogFile = "";
  48. // global handle for application window
  49. extern HWND _hwndMain = NULL;
  50. extern BOOL _bFullScreen = FALSE;
  51. // critical section for access to zlib functions
  52. CTCriticalSection zip_csLock;
  53. // to keep system gamma table
  54. static UWORD auwSystemGamma[256*3];
  55. // OS info
  56. static CTString sys_strOS = "";
  57. static INDEX sys_iOSMajor = 0;
  58. static INDEX sys_iOSMinor = 0;
  59. static INDEX sys_iOSBuild = 0;
  60. static CTString sys_strOSMisc = "";
  61. // CPU info
  62. static CTString sys_strCPUVendor = "";
  63. static INDEX sys_iCPUType = 0;
  64. static INDEX sys_iCPUFamily = 0;
  65. static INDEX sys_iCPUModel = 0;
  66. static INDEX sys_iCPUStepping = 0;
  67. static BOOL sys_bCPUHasMMX = 0;
  68. static BOOL sys_bCPUHasCMOV = 0;
  69. static INDEX sys_iCPUMHz = 0;
  70. INDEX sys_iCPUMisc = 0;
  71. // RAM info
  72. static INDEX sys_iRAMPhys = 0;
  73. static INDEX sys_iRAMSwap = 0;
  74. // HDD info
  75. static INDEX sys_iHDDSize = 0;
  76. static INDEX sys_iHDDFree = 0;
  77. static INDEX sys_iHDDMisc = 0;
  78. // MOD info
  79. static CTString sys_strModName = "";
  80. static CTString sys_strModExt = "";
  81. // enables paranoia checks for allocation array
  82. extern BOOL _bAllocationArrayParanoiaCheck = FALSE;
  83. BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  84. {
  85. switch (ul_reason_for_call)
  86. {
  87. case DLL_PROCESS_ATTACH:
  88. break;
  89. case DLL_THREAD_ATTACH:
  90. case DLL_THREAD_DETACH:
  91. case DLL_PROCESS_DETACH:
  92. break;
  93. default:
  94. ASSERT(FALSE);
  95. }
  96. return TRUE;
  97. }
  98. static void DetectCPU(void)
  99. {
  100. char strVendor[12+1];
  101. strVendor[12] = 0;
  102. ULONG ulTFMS;
  103. ULONG ulFeatures;
  104. // test MMX presence and update flag
  105. __asm {
  106. mov eax,0 ;// request for basic id
  107. cpuid
  108. mov dword ptr [strVendor+0], ebx
  109. mov dword ptr [strVendor+4], edx
  110. mov dword ptr [strVendor+8], ecx
  111. mov eax,1 ;// request for TFMS feature flags
  112. cpuid
  113. mov dword ptr [ulTFMS], eax ;// remember type, family, model and stepping
  114. mov dword ptr [ulFeatures], edx
  115. }
  116. INDEX iType = (ulTFMS>>12)&0x3;
  117. INDEX iFamily = (ulTFMS>> 8)&0xF;
  118. INDEX iModel = (ulTFMS>> 4)&0xF;
  119. INDEX iStepping = (ulTFMS>> 0)&0xF;
  120. CPrintF(TRANS(" Vendor: %s\n"), strVendor);
  121. CPrintF(TRANS(" Type: %d, Family: %d, Model: %d, Stepping: %d\n"),
  122. iType, iFamily, iModel, iStepping);
  123. BOOL bMMX = ulFeatures & (1<<23);
  124. BOOL bCMOV = ulFeatures & (1<<15);
  125. CTString strYes = TRANS("Yes");
  126. CTString strNo = TRANS("No");
  127. CPrintF(TRANS(" MMX : %s\n"), bMMX ?strYes:strNo);
  128. CPrintF(TRANS(" CMOV: %s\n"), bCMOV?strYes:strNo);
  129. CPrintF(TRANS(" Clock: %.0fMHz\n"), _pTimer->tm_llCPUSpeedHZ/1E6);
  130. sys_strCPUVendor = strVendor;
  131. sys_iCPUType = iType;
  132. sys_iCPUFamily = iFamily;
  133. sys_iCPUModel = iModel;
  134. sys_iCPUStepping = iStepping;
  135. sys_bCPUHasMMX = bMMX!=0;
  136. sys_bCPUHasCMOV = bCMOV!=0;
  137. sys_iCPUMHz = INDEX(_pTimer->tm_llCPUSpeedHZ/1E6);
  138. if( !bMMX) FatalError( TRANS("MMX support required but not present!"));
  139. }
  140. static void DetectCPUWrapper(void)
  141. {
  142. __try {
  143. DetectCPU();
  144. } __except(EXCEPTION_EXECUTE_HANDLER) {
  145. CPrintF( TRANS("Cannot detect CPU: exception raised.\n"));
  146. }
  147. }
  148. // reverses string
  149. void StrRev( char *str) {
  150. char ctmp;
  151. char *pch0 = str;
  152. char *pch1 = str+strlen(str)-1;
  153. while( pch1>pch0) {
  154. ctmp = *pch0;
  155. *pch0 = *pch1;
  156. *pch1 = ctmp;
  157. pch0++;
  158. pch1--;
  159. }
  160. }
  161. static char strExePath[MAX_PATH] = "";
  162. static char strDirPath[MAX_PATH] = "";
  163. static void AnalyzeApplicationPath(void)
  164. {
  165. strcpy(strDirPath, "D:\\");
  166. strcpy(strExePath, "D:\\TestExe.xbe");
  167. char strTmpPath[MAX_PATH] = "";
  168. // get full path to the exe
  169. GetModuleFileNameA( NULL, strExePath, sizeof(strExePath)-1);
  170. // copy that to the path
  171. strncpy(strTmpPath, strExePath, sizeof(strTmpPath)-1);
  172. strDirPath[sizeof(strTmpPath)-1] = 0;
  173. // remove name from application path
  174. StrRev(strTmpPath);
  175. // find last backslash
  176. char *pstr = strchr( strTmpPath, '\\');
  177. if( pstr==NULL) {
  178. // not found - path is just "\"
  179. strcpy( strTmpPath, "\\");
  180. pstr = strTmpPath;
  181. }
  182. // remove 'debug' from app path if needed
  183. if( strnicmp( pstr, "\\gubed", 6)==0) pstr += 6;
  184. if( pstr[0] = '\\') pstr++;
  185. char *pstrFin = strchr( pstr, '\\');
  186. if( pstrFin==NULL) {
  187. strcpy( pstr, "\\");
  188. pstrFin = pstr;
  189. }
  190. // copy that to the path
  191. StrRev(pstrFin);
  192. strncpy( strDirPath, pstrFin, sizeof(strDirPath)-1);
  193. strDirPath[sizeof(strDirPath)-1] = 0;
  194. }
  195. // startup engine
  196. ENGINE_API void SE_InitEngine(CTString strGameID)
  197. {
  198. #pragma message(">> Remove this from SE_InitEngine : _bWorldEditorApp")
  199. if(strGameID=="SeriousEditor") {
  200. _bWorldEditorApp = TRUE;
  201. }
  202. AnalyzeApplicationPath();
  203. _fnmApplicationPath = CTString(strDirPath);
  204. _fnmApplicationExe = CTString(strExePath);
  205. try {
  206. _fnmApplicationExe.RemoveApplicationPath_t();
  207. } catch (char *strError) {
  208. (void) strError;
  209. ASSERT(FALSE);
  210. }
  211. _pConsole = new CConsole;
  212. if (_strLogFile=="") {
  213. _strLogFile = CTFileName(CTString(strExePath)).FileName();
  214. }
  215. _pConsole->Initialize(_fnmApplicationPath+_strLogFile+".log", 90, 512);
  216. _pAnimStock = new CStock_CAnimData;
  217. _pTextureStock = new CStock_CTextureData;
  218. _pSoundStock = new CStock_CSoundData;
  219. _pModelStock = new CStock_CModelData;
  220. _pEntityClassStock = new CStock_CEntityClass;
  221. _pMeshStock = new CStock_CMesh;
  222. _pSkeletonStock = new CStock_CSkeleton;
  223. _pAnimSetStock = new CStock_CAnimSet;
  224. _pShaderStock = new CStock_CShader;
  225. _pTimer = new CTimer;
  226. _pGfx = new CGfxLibrary;
  227. _pSound = new CSoundLibrary;
  228. _pInput = new CInput;
  229. _pNetwork = new CNetworkLibrary;
  230. CRCT_Init();
  231. _strEngineBuild.PrintF( TRANS("SeriousEngine Build: %d.%d"), _SE_BUILD_MAJOR, _SE_BUILD_MINOR);
  232. // print basic engine info
  233. CPrintF(TRANS("--- Serious Engine Startup ---\n"));
  234. CPrintF(" %s\n\n", _strEngineBuild);
  235. // print info on the started application
  236. CPrintF(TRANS("Executable: %s\n"), strExePath);
  237. CPrintF(TRANS("Assumed engine directory: %s\n"), _fnmApplicationPath);
  238. CPrintF("\n");
  239. // report os info
  240. CPrintF(TRANS("Examining underlying OS...\n"));
  241. OSVERSIONINFOA osv;
  242. memset(&osv, 0, sizeof(osv));
  243. osv.dwOSVersionInfoSize = sizeof(osv);
  244. if (GetVersionExA(&osv)) {
  245. switch (osv.dwPlatformId) {
  246. case VER_PLATFORM_WIN32s: sys_strOS = "Win32s"; break;
  247. case VER_PLATFORM_WIN32_WINDOWS: sys_strOS = "Win9x"; break;
  248. case VER_PLATFORM_WIN32_NT: sys_strOS = "WinNT"; break;
  249. default: sys_strOS = "Unknown\n"; break;
  250. }
  251. sys_iOSMajor = osv.dwMajorVersion;
  252. sys_iOSMinor = osv.dwMinorVersion;
  253. sys_iOSBuild = osv.dwBuildNumber & 0xFFFF;
  254. sys_strOSMisc = osv.szCSDVersion;
  255. CPrintF(TRANS(" Type: %s\n"), (const char*)sys_strOS);
  256. CPrintF(TRANS(" Version: %d.%d, build %d\n"),
  257. osv.dwMajorVersion, osv.dwMinorVersion, osv.dwBuildNumber & 0xFFFF);
  258. CPrintF(TRANS(" Misc: %s\n"), osv.szCSDVersion);
  259. } else {
  260. CPrintF(TRANS("Error getting OS info: %s\n"), GetWindowsError(GetLastError()) );
  261. }
  262. CPrintF("\n");
  263. // init main shell
  264. _pShell = new CShell;
  265. _pShell->Initialize();
  266. // report CPU
  267. CPrintF(TRANS("Detecting CPU...\n"));
  268. DetectCPUWrapper();
  269. CPrintF("\n");
  270. // report memory info
  271. extern void ReportGlobalMemoryStatus(void);
  272. ReportGlobalMemoryStatus();
  273. MEMORYSTATUS ms;
  274. GlobalMemoryStatus(&ms);
  275. #define MB (1024*1024)
  276. sys_iRAMPhys = ms.dwTotalPhys /MB;
  277. sys_iRAMSwap = ms.dwTotalPageFile/MB;
  278. // initialize zip semaphore
  279. zip_csLock.cs_iIndex = -1; // not checked for locking order
  280. // get info on the first disk in system
  281. DWORD dwSerial;
  282. DWORD dwFreeClusters;
  283. DWORD dwClusters;
  284. DWORD dwSectors;
  285. DWORD dwBytes;
  286. char strDrive[] = "C:\\";
  287. strDrive[0] = strExePath[0];
  288. GetVolumeInformationA(strDrive, NULL, 0, &dwSerial, NULL, NULL, NULL, 0);
  289. GetDiskFreeSpaceA(strDrive, &dwSectors, &dwBytes, &dwFreeClusters, &dwClusters);
  290. sys_iHDDSize = __int64(dwSectors)*dwBytes*dwClusters/MB;
  291. sys_iHDDFree = __int64(dwSectors)*dwBytes*dwFreeClusters/MB;
  292. sys_iHDDMisc = dwSerial;
  293. // add console variables
  294. extern INDEX con_bNoWarnings;
  295. extern INDEX wld_bFastObjectOptimization;
  296. extern INDEX fil_bPreferZips;
  297. extern FLOAT mth_fCSGEpsilon;
  298. _pShell->DeclareSymbol("user INDEX con_bNoWarnings;", &con_bNoWarnings);
  299. _pShell->DeclareSymbol("user INDEX wld_bFastObjectOptimization;", &wld_bFastObjectOptimization);
  300. _pShell->DeclareSymbol("user FLOAT mth_fCSGEpsilon;", &mth_fCSGEpsilon);
  301. _pShell->DeclareSymbol("persistent user INDEX fil_bPreferZips;", &fil_bPreferZips);
  302. // OS info
  303. _pShell->DeclareSymbol("user const CTString sys_strOS ;", &sys_strOS);
  304. _pShell->DeclareSymbol("user const INDEX sys_iOSMajor ;", &sys_iOSMajor);
  305. _pShell->DeclareSymbol("user const INDEX sys_iOSMinor ;", &sys_iOSMinor);
  306. _pShell->DeclareSymbol("user const INDEX sys_iOSBuild ;", &sys_iOSBuild);
  307. _pShell->DeclareSymbol("user const CTString sys_strOSMisc;", &sys_strOSMisc);
  308. // CPU info
  309. _pShell->DeclareSymbol("user const CTString sys_strCPUVendor;", &sys_strCPUVendor);
  310. _pShell->DeclareSymbol("user const INDEX sys_iCPUType ;", &sys_iCPUType );
  311. _pShell->DeclareSymbol("user const INDEX sys_iCPUFamily ;", &sys_iCPUFamily );
  312. _pShell->DeclareSymbol("user const INDEX sys_iCPUModel ;", &sys_iCPUModel );
  313. _pShell->DeclareSymbol("user const INDEX sys_iCPUStepping ;", &sys_iCPUStepping);
  314. _pShell->DeclareSymbol("user const INDEX sys_bCPUHasMMX ;", &sys_bCPUHasMMX );
  315. _pShell->DeclareSymbol("user const INDEX sys_bCPUHasCMOV ;", &sys_bCPUHasCMOV );
  316. _pShell->DeclareSymbol("user const INDEX sys_iCPUMHz ;", &sys_iCPUMHz );
  317. _pShell->DeclareSymbol(" const INDEX sys_iCPUMisc ;", &sys_iCPUMisc );
  318. // RAM info
  319. _pShell->DeclareSymbol("user const INDEX sys_iRAMPhys;", &sys_iRAMPhys);
  320. _pShell->DeclareSymbol("user const INDEX sys_iRAMSwap;", &sys_iRAMSwap);
  321. _pShell->DeclareSymbol("user const INDEX sys_iHDDSize;", &sys_iHDDSize);
  322. _pShell->DeclareSymbol("user const INDEX sys_iHDDFree;", &sys_iHDDFree);
  323. _pShell->DeclareSymbol(" const INDEX sys_iHDDMisc;", &sys_iHDDMisc);
  324. // MOD info
  325. _pShell->DeclareSymbol("user const CTString sys_strModName;", &sys_strModName);
  326. _pShell->DeclareSymbol("user const CTString sys_strModExt;", &sys_strModExt);
  327. // Stock clearing
  328. extern void FreeUnusedStock(void);
  329. _pShell->DeclareSymbol("user void FreeUnusedStock(void);", &FreeUnusedStock);
  330. // Timer tick quantum
  331. _pShell->DeclareSymbol("user const FLOAT fTickQuantum;", (FLOAT*)&_pTimer->TickQuantum);
  332. // init MODs and stuff ...
  333. extern void InitStreams(void);
  334. InitStreams();
  335. // keep mod name in sys cvar
  336. sys_strModName = _strModName;
  337. sys_strModExt = _strModExt;
  338. // checking of crc
  339. #if 0
  340. ULONG ulCRCActual = -2;
  341. SLONG ulCRCExpected = -1;
  342. try {
  343. // get the checksum of engine
  344. #ifndef NDEBUG
  345. #define SELFFILE "Bin\\Debug\\EngineD.dll"
  346. #define SELFCRCFILE "Bin\\Debug\\EngineD.crc"
  347. #else
  348. #define SELFFILE "Bin\\Engine.dll"
  349. #define SELFCRCFILE "Bin\\Engine.crc"
  350. #endif
  351. ulCRCActual = GetFileCRC32_t(CTString(SELFFILE));
  352. // load expected checksum from the file on disk
  353. ulCRCExpected = 0;
  354. LoadIntVar(CTString(SELFCRCFILE), ulCRCExpected);
  355. } catch (char *strError) {
  356. CPrintF("%s\n", strError);
  357. }
  358. // if not same
  359. if (ulCRCActual!=ulCRCExpected) {
  360. // don't run
  361. //FatalError(TRANS("Engine CRC is invalid.\nExpected %08x, but found %08x.\n"), ulCRCExpected, ulCRCActual);
  362. }
  363. #endif
  364. _pInput->Initialize();
  365. _pGfx->Init();
  366. _pSound->Init();
  367. if (strGameID!="") {
  368. _pNetwork->Init(strGameID);
  369. // just make classes declare their shell variables
  370. try {
  371. CEntityClass* pec = _pEntityClassStock->Obtain_t(CTString("Classes\\Player.ecl"));
  372. ASSERT(pec != NULL);
  373. _pEntityClassStock->Release(pec); // this must not be a dependency!
  374. // if couldn't find player class
  375. } catch (char *strError) {
  376. FatalError(TRANS("Cannot initialize classes:\n%s"), strError);
  377. }
  378. } else {
  379. _pNetwork = NULL;
  380. }
  381. // mark that default fonts aren't loaded (yet)
  382. _pfdDisplayFont = NULL;
  383. _pfdConsoleFont = NULL;
  384. // readout system gamma table
  385. HDC hdc = GetDC(NULL);
  386. BOOL bOK = GetDeviceGammaRamp( hdc, &auwSystemGamma[0]);
  387. _pGfx->gl_ulFlags |= GLF_ADJUSTABLEGAMMA;
  388. if( !bOK) {
  389. _pGfx->gl_ulFlags &= ~GLF_ADJUSTABLEGAMMA;
  390. CPrintF( TRANS("\nWARNING: Gamma, brightness and contrast are not adjustable!\n\n"));
  391. } // done
  392. ReleaseDC( NULL, hdc);
  393. // init IFeel
  394. HWND hwnd = NULL;//GetDesktopWindow();
  395. HINSTANCE hInstance = GetModuleHandle(NULL);
  396. if(IFeel_InitDevice(hInstance,hwnd))
  397. {
  398. CTString strDefaultProject = "Data\\Default.ifr";
  399. // get project file name for this device
  400. CTString strIFeel = IFeel_GetProjectFileName();
  401. // if no file name is returned use default file
  402. if(strIFeel.Length()==0) strIFeel = strDefaultProject;
  403. if(!IFeel_LoadFile(strIFeel))
  404. {
  405. if(strIFeel!=strDefaultProject)
  406. {
  407. IFeel_LoadFile(strDefaultProject);
  408. }
  409. }
  410. CPrintF("\n");
  411. }
  412. }
  413. // shutdown entire engine
  414. ENGINE_API void SE_EndEngine(void)
  415. {
  416. // restore system gamma table (if needed)
  417. if( _pGfx->gl_ulFlags&GLF_ADJUSTABLEGAMMA) {
  418. HDC hdc = GetDC(NULL);
  419. BOOL bOK = SetDeviceGammaRamp( hdc, &auwSystemGamma[0]);
  420. //ASSERT(bOK);
  421. ReleaseDC( NULL, hdc);
  422. }
  423. // free stocks
  424. delete _pEntityClassStock; _pEntityClassStock = NULL;
  425. delete _pModelStock; _pModelStock = NULL;
  426. delete _pSoundStock; _pSoundStock = NULL;
  427. delete _pTextureStock; _pTextureStock = NULL;
  428. delete _pAnimStock; _pAnimStock = NULL;
  429. delete _pMeshStock; _pMeshStock = NULL;
  430. delete _pSkeletonStock; _pSkeletonStock = NULL;
  431. delete _pAnimSetStock; _pAnimSetStock = NULL;
  432. delete _pShaderStock; _pShaderStock = NULL;
  433. // free all memory used by the crc cache
  434. CRCT_Clear();
  435. // shutdown
  436. if( _pNetwork != NULL) { delete _pNetwork; _pNetwork=NULL; }
  437. delete _pInput; _pInput = NULL;
  438. delete _pSound; _pSound = NULL;
  439. delete _pGfx; _pGfx = NULL;
  440. delete _pTimer; _pTimer = NULL;
  441. delete _pShell; _pShell = NULL;
  442. delete _pConsole; _pConsole = NULL;
  443. extern void EndStreams(void);
  444. EndStreams();
  445. // shutdown profilers
  446. _sfStats.Clear();
  447. _pfGfxProfile .pf_apcCounters.Clear();
  448. _pfGfxProfile .pf_aptTimers .Clear();
  449. _pfModelProfile .pf_apcCounters.Clear();
  450. _pfModelProfile .pf_aptTimers .Clear();
  451. _pfSoundProfile .pf_apcCounters.Clear();
  452. _pfSoundProfile .pf_aptTimers .Clear();
  453. _pfNetworkProfile .pf_apcCounters.Clear();
  454. _pfNetworkProfile .pf_aptTimers .Clear();
  455. _pfRenderProfile .pf_apcCounters.Clear();
  456. _pfRenderProfile .pf_aptTimers .Clear();
  457. _pfWorldEditingProfile .pf_apcCounters.Clear();
  458. _pfWorldEditingProfile .pf_aptTimers .Clear();
  459. _pfPhysicsProfile .pf_apcCounters.Clear();
  460. _pfPhysicsProfile .pf_aptTimers .Clear();
  461. // remove default fonts if needed
  462. if( _pfdDisplayFont != NULL) { delete _pfdDisplayFont; _pfdDisplayFont=NULL; }
  463. if( _pfdConsoleFont != NULL) { delete _pfdConsoleFont; _pfdConsoleFont=NULL; }
  464. // deinit IFeel
  465. IFeel_DeleteDevice();
  466. }
  467. // prepare and load some default fonts
  468. ENGINE_API void SE_LoadDefaultFonts(void)
  469. {
  470. _pfdDisplayFont = new CFontData;
  471. _pfdConsoleFont = new CFontData;
  472. // try to load default fonts
  473. try {
  474. _pfdDisplayFont->Load_t( CTFILENAME( "Fonts\\Display3-narrow.fnt"));
  475. _pfdConsoleFont->Load_t( CTFILENAME( "Fonts\\Console1.fnt"));
  476. }
  477. catch (char *strError) {
  478. FatalError( TRANS("Error loading font: %s."), strError);
  479. }
  480. // change fonts' default spacing a bit
  481. _pfdDisplayFont->SetCharSpacing( 0);
  482. _pfdDisplayFont->SetLineSpacing(+1);
  483. _pfdConsoleFont->SetCharSpacing(-1);
  484. _pfdConsoleFont->SetLineSpacing(+1);
  485. _pfdConsoleFont->SetFixedWidth();
  486. }
  487. // updates main windows' handles for windowed mode and fullscreen
  488. ENGINE_API void SE_UpdateWindowHandle( HWND hwndMain)
  489. {
  490. ASSERT( hwndMain!=NULL);
  491. _hwndMain = hwndMain;
  492. _bFullScreen = _pGfx!=NULL && (_pGfx->gl_ulFlags&GLF_FULLSCREEN);
  493. }
  494. static BOOL TouchBlock(UBYTE *pubMemoryBlock, INDEX ctBlockSize)
  495. {
  496. // cannot pretouch block that are smaller than 64KB :(
  497. ctBlockSize -= 16*0x1000;
  498. if( ctBlockSize<4) return FALSE;
  499. __try {
  500. // 4 times should be just enough
  501. for( INDEX i=0; i<4; i++) {
  502. // must do it in asm - don't know what VC will try to optimize
  503. __asm {
  504. // The 16-page skip is to keep Win 95 from thinking we're trying to page ourselves in
  505. // (we are doing that, of course, but there's no reason we shouldn't) - THANX JOHN! :)
  506. mov esi,dword ptr [pubMemoryBlock]
  507. mov ecx,dword ptr [ctBlockSize]
  508. shr ecx,2
  509. touchLoop:
  510. mov eax,dword ptr [esi]
  511. mov ebx,dword ptr [esi+16*0x1000]
  512. add eax,ebx // BLA, BLA, TROOCH, TRUCH
  513. add esi,4
  514. dec ecx
  515. jnz touchLoop
  516. }
  517. }
  518. }
  519. __except(EXCEPTION_EXECUTE_HANDLER) {
  520. return FALSE;
  521. }
  522. return TRUE;
  523. }
  524. // pretouch all memory commited by process
  525. extern BOOL _bNeedPretouch = FALSE;
  526. ENGINE_API extern void SE_PretouchIfNeeded(void)
  527. {
  528. // only if pretouching is needed?
  529. extern INDEX gam_bPretouch;
  530. if( !_bNeedPretouch || !gam_bPretouch) return;
  531. _bNeedPretouch = FALSE;
  532. // set progress bar
  533. SetProgressDescription( TRANS("pretouching"));
  534. CallProgressHook_t(0.0f);
  535. // need to do this two times - 1st for numerations, and 2nd for real (progress bar and that shit)
  536. BOOL bPretouched = TRUE;
  537. INDEX ctFails, ctBytes, ctBlocks;
  538. INDEX ctPassBytes, ctTotalBlocks;
  539. for( INDEX iPass=1; iPass<=2; iPass++)
  540. {
  541. // flush variables
  542. ctFails=0; ctBytes=0; ctBlocks=0; ctTotalBlocks=0;
  543. void *pvNextBlock = NULL;
  544. MEMORY_BASIC_INFORMATION mbi;
  545. // lets walk thru memory blocks
  546. while( VirtualQuery( pvNextBlock, &mbi, sizeof(mbi)))
  547. {
  548. // don't mess with kernel's memory and zero-sized blocks
  549. if( ((ULONG)pvNextBlock)>0x7FFF0000UL || mbi.RegionSize<1) break;
  550. // if this region of memory belongs to our process
  551. BOOL bCanAccess = (mbi.Protect==PAGE_READWRITE); // || (mbi.Protect==PAGE_EXECUTE_READWRITE);
  552. if( mbi.State==MEM_COMMIT && bCanAccess && mbi.Type==MEM_PRIVATE) // && !IsBadReadPtr( mbi.BaseAddress, 1)
  553. {
  554. // increase counters
  555. ctBlocks++;
  556. ctBytes += mbi.RegionSize;
  557. // in first pass we only count
  558. if( iPass==1) goto nextRegion;
  559. // update progress bar
  560. CallProgressHook_t( (FLOAT)ctBytes/ctPassBytes);
  561. // pretouch
  562. ASSERT( mbi.RegionSize>0);
  563. BOOL bOK = TouchBlock((UBYTE *)mbi.BaseAddress, mbi.RegionSize);
  564. if( !bOK) {
  565. // whoops!
  566. ctFails++;
  567. }
  568. // for easier debugging (didn't help much, though)
  569. //Sleep(5);
  570. }
  571. nextRegion:
  572. // advance to next region
  573. pvNextBlock = ((UBYTE*)mbi.BaseAddress) + mbi.RegionSize;
  574. ctTotalBlocks++;
  575. }
  576. // done with one pass
  577. ctPassBytes = ctBytes;
  578. if( (ctPassBytes/1024/1024)>sys_iRAMPhys) {
  579. // not enough RAM, sorry :(
  580. bPretouched = FALSE;
  581. break;
  582. }
  583. }
  584. // report
  585. if( bPretouched) {
  586. // success
  587. CPrintF( TRANS("Pretouched %d KB of memory in %d blocks.\n"), ctBytes/1024, ctBlocks); //, ctTotalBlocks);
  588. } else {
  589. // fail
  590. CPrintF( TRANS("Cannot pretouch due to lack of physical memory (%d KB of overflow).\n"), ctPassBytes/1024-sys_iRAMPhys*1024);
  591. }
  592. // some blocks failed?
  593. if( ctFails>1) CPrintF( TRANS("(%d blocks were skipped)\n"), ctFails);
  594. //_pShell->Execute("StockDump();");
  595. }
  596. #if 0
  597. // printout block info
  598. CPrintF("--------\n");
  599. CTString strTmp1, strTmp2;
  600. CPrintF("Base/Alloc Address: 0x%8X / 0x%8X - Size: %d KB\n", mbi.BaseAddress, mbi.AllocationBase, mbi.RegionSize/1024);
  601. switch( mbi.Protect) {
  602. case PAGE_READONLY: strTmp1 = "PAGE_READONLY"; break;
  603. case PAGE_READWRITE: strTmp1 = "PAGE_READWRITE"; break;
  604. case PAGE_WRITECOPY: strTmp1 = "PAGE_WRITECOPY"; break;
  605. case PAGE_EXECUTE: strTmp1 = "PAGE_EXECUTE"; break;
  606. case PAGE_EXECUTE_READ: strTmp1 = "PAGE_EXECUTE_READ"; break;
  607. case PAGE_EXECUTE_READWRITE: strTmp1 = "PAGE_EXECUTE_READWRITE"; break;
  608. case PAGE_GUARD: strTmp1 = "PAGE_GUARD"; break;
  609. case PAGE_NOACCESS: strTmp1 = "PAGE_NOACCESS"; break;
  610. case PAGE_NOCACHE: strTmp1 = "PAGE_NOCACHE"; break;
  611. }
  612. switch( mbi.AllocationProtect) {
  613. case PAGE_READONLY: strTmp2 = "PAGE_READONLY"; break;
  614. case PAGE_READWRITE: strTmp2 = "PAGE_READWRITE"; break;
  615. case PAGE_WRITECOPY: strTmp2 = "PAGE_WRITECOPY"; break;
  616. case PAGE_EXECUTE: strTmp2 = "PAGE_EXECUTE"; break;
  617. case PAGE_EXECUTE_READ: strTmp2 = "PAGE_EXECUTE_READ"; break;
  618. case PAGE_EXECUTE_READWRITE: strTmp2 = "PAGE_EXECUTE_READWRITE"; break;
  619. case PAGE_GUARD: strTmp2 = "PAGE_GUARD"; break;
  620. case PAGE_NOACCESS: strTmp2 = "PAGE_NOACCESS"; break;
  621. case PAGE_NOCACHE: strTmp2 = "PAGE_NOCACHE"; break;
  622. }
  623. CPrintF("Current/Alloc protect: %s / %s\n", strTmp1, strTmp2);
  624. switch( mbi.State) {
  625. case MEM_COMMIT: strTmp1 = "MEM_COMMIT"; break;
  626. case MEM_FREE: strTmp1 = "MEM_FREE"; break;
  627. case MEM_RESERVE: strTmp1 = "MEM_RESERVE"; break;
  628. }
  629. switch( mbi.Type) {
  630. case MEM_IMAGE: strTmp2 = "MEM_IMAGE"; break;
  631. case MEM_MAPPED: strTmp2 = "MEM_MAPPED"; break;
  632. case MEM_PRIVATE: strTmp2 = "MEM_PRIVATE"; break;
  633. }
  634. CPrintF("State/Type: %s / %s\n", strTmp1, strTmp2);
  635. #endif