EngineGUI.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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/Templates/Stock_CTextureData.h>
  14. // global engine gui handling object
  15. CEngineGUI _EngineGUI;
  16. /*
  17. We cannot use dllmain if using MFC.
  18. See MSDN article "Regular DLLs Dynamically Linked to MFC" if initialization is needed.
  19. BOOL APIENTRY DllMain( HANDLE hModule,
  20. DWORD ul_reason_for_call,
  21. LPVOID lpReserved
  22. )
  23. {
  24. switch (ul_reason_for_call)
  25. {
  26. case DLL_PROCESS_ATTACH:
  27. case DLL_THREAD_ATTACH:
  28. case DLL_THREAD_DETACH:
  29. case DLL_PROCESS_DETACH:
  30. break;
  31. }
  32. return TRUE;
  33. }
  34. */
  35. void CEngineGUI::SelectMode( CDisplayMode &dm, GfxAPIType &gat)
  36. {
  37. // stupid way to change resources, but it must be done
  38. HANDLE hOldResource = AfxGetResourceHandle();
  39. // call select mode dialog
  40. CDlgSelectMode dlgSelectMode( dm, gat);
  41. // activate CTGfx resources
  42. AfxSetResourceHandle( GetModuleHandleA(ENGINEGUI_DLL_NAME));
  43. INDEX iDialogResult = dlgSelectMode.DoModal();
  44. // restore resources
  45. AfxSetResourceHandle( (HINSTANCE)hOldResource);
  46. // if mode dialog ended successfully
  47. if( iDialogResult == IDOK) SetFullScreenModeToRegistry( "Display modes", dm, gat);
  48. }
  49. CTFileName CEngineGUI::CreateTexture(CTFileName fnTexFileToRecreate/*=CTString("")*/,
  50. CDynamicArray<CTFileName> *pafnCreatedTextures/*=NULL*/)
  51. {
  52. CTFileName fnResult;
  53. // stupid way to change resources, but it must be done
  54. HANDLE hOldResource = AfxGetResourceHandle();
  55. // activate CTGfx resources
  56. AfxSetResourceHandle( GetModuleHandleA(ENGINEGUI_DLL_NAME) );
  57. // if create texture is called with a wish to recreate texture
  58. if( fnTexFileToRecreate != "")
  59. {
  60. fnResult = fnTexFileToRecreate;
  61. CTextureData *ptdTextureToRecreate;
  62. CTFileName fnToRecreateNoExt =
  63. fnTexFileToRecreate.FileDir()+fnTexFileToRecreate.FileName();
  64. // try to
  65. try
  66. {
  67. // obtain texture to recreate
  68. ptdTextureToRecreate = _pTextureStock->Obtain_t( fnTexFileToRecreate);
  69. ptdTextureToRecreate->Reload();
  70. // if texture is of effect type, call create effect texture dialog
  71. if( ptdTextureToRecreate->td_ptegEffect != NULL)
  72. {
  73. // call create effect texture dialog with .tex name
  74. CDlgCreateEffectTexture dlgCreateEffectTexture( fnTexFileToRecreate);
  75. dlgCreateEffectTexture.DoModal();
  76. }
  77. // else this texture was created from script or from single picture
  78. else
  79. {
  80. // search for script with same name
  81. CTString strFullNameNoExt = _fnmApplicationPath +
  82. fnTexFileToRecreate.FileDir() + fnTexFileToRecreate.FileName();
  83. // if there is tga picture with same name
  84. if( GetFileAttributesA( strFullNameNoExt+".tga") != -1)
  85. {
  86. // call create normal texture dialog with tga picture name
  87. CDlgCreateNormalTexture dlgCreateNormalTexture( fnToRecreateNoExt+".tga");
  88. if( dlgCreateNormalTexture.m_bSourcePictureValid)
  89. {
  90. if( dlgCreateNormalTexture.DoModal() == IDOK)
  91. {
  92. fnResult = dlgCreateNormalTexture.m_fnCreatedFileName;
  93. }
  94. }
  95. }
  96. // else if there is pcx picture with same name
  97. else if( GetFileAttributesA( strFullNameNoExt+".pcx") != -1)
  98. {
  99. // call create normal texture dialog with tga picture name
  100. CDlgCreateNormalTexture dlgCreateNormalTexture( fnToRecreateNoExt+".pcx");
  101. if( dlgCreateNormalTexture.m_bSourcePictureValid)
  102. {
  103. if( dlgCreateNormalTexture.DoModal() == IDOK)
  104. {
  105. fnResult = dlgCreateNormalTexture.m_fnCreatedFileName;
  106. }
  107. }
  108. }
  109. // else if script exists
  110. else if( GetFileAttributesA( strFullNameNoExt+".scr") != -1)
  111. {
  112. CDynamicArray<CTFileName> afnScript;
  113. CTFileName *pfnScript = afnScript.New();
  114. *pfnScript = fnToRecreateNoExt+".scr";
  115. // call create animated texture dialog with script name
  116. CDlgCreateAnimatedTexture dlgCreateAnimatedTexture( afnScript);
  117. dlgCreateAnimatedTexture.DoModal();
  118. }
  119. else
  120. {
  121. WarningMessage( "Cannot find source for recreating texture: \"%s\"", (CTString&)fnTexFileToRecreate);
  122. }
  123. }
  124. // reload the texture
  125. ptdTextureToRecreate->Reload();
  126. // release obtained texture
  127. _pTextureStock->Release( ptdTextureToRecreate);
  128. }
  129. catch( char *err_str)
  130. {
  131. WarningMessage( err_str);
  132. }
  133. }
  134. else
  135. {
  136. // call choose texture type dialog
  137. CDlgChooseTextureType dlgChooseTextureType;
  138. int iDlgResult = dlgChooseTextureType.DoModal();
  139. // if user choosed a texture type
  140. if( (iDlgResult != -1) && (iDlgResult >= 0) && (iDlgResult <= 2) )
  141. {
  142. // if result is 2 we want to create effect texture
  143. if( iDlgResult == 2)
  144. {
  145. // no file requester, just call dialog
  146. CDlgCreateEffectTexture dlgCreateEffectTexture;
  147. if( dlgCreateEffectTexture.DoModal() == IDOK)
  148. {
  149. fnResult = dlgCreateEffectTexture.m_fnCreatedTextureName;
  150. if( pafnCreatedTextures != NULL)
  151. {
  152. CTFileName *pfnCreatedTexture = pafnCreatedTextures->New();
  153. *pfnCreatedTexture = fnResult;
  154. }
  155. }
  156. }
  157. // both normal and animated textures need to call open file requester
  158. else
  159. {
  160. // different filters for different requests
  161. char *pFilters = "All files (*.*)\0*.*\0\0";
  162. // if dialog result is 0 we want to create normal texture
  163. if( iDlgResult == 0)
  164. {
  165. pFilters = "Pictures (*.pcx;*.tga)\0*.pcx;*.tga\0"
  166. "PCX files (*.pcx)\0*.pcx\0"
  167. "TGA files (*.tga)\0*.tga\0\0";
  168. }
  169. // if dialog result is 1 we want to create animated texture
  170. else if( iDlgResult == 1)
  171. {
  172. pFilters = "Picture or script files (*.pcx;*.tga;*.scr)\0*.pcx;*.tga;*.scr\0"
  173. "PCX files (*.pcx)\0*.pcx\0"
  174. "TGA files (*.tga)\0*.tga\0"
  175. "Script files (*.scr)\0;*.scr\0\0";
  176. }
  177. // call file requester for opening textures
  178. CDynamicArray<CTFileName> afnCreateTexture;
  179. FileRequester( "Create texture", pFilters, KEY_NAME_CREATE_TEXTURE_DIR,
  180. "Textures\\", "", &afnCreateTexture);
  181. if( afnCreateTexture.Count() == 0)
  182. {
  183. // restore resources
  184. AfxSetResourceHandle( (HINSTANCE) hOldResource);
  185. return CTString("");
  186. }
  187. // if requested texture type is 0 we want to create normal texture
  188. if( iDlgResult == 0)
  189. {
  190. // create textures
  191. FOREACHINDYNAMICARRAY( afnCreateTexture, CTFileName, itPicture)
  192. {
  193. CTFileName fnSource = itPicture.Current();
  194. if( (fnSource.FileExt() == ".pcx") || (fnSource.FileExt() == ".tga") )
  195. {
  196. // call create normal texture dialog
  197. CDlgCreateNormalTexture dlgCreateNormalTexture( fnSource);
  198. if( dlgCreateNormalTexture.m_bSourcePictureValid)
  199. {
  200. if( dlgCreateNormalTexture.DoModal() == IDOK)
  201. {
  202. fnResult = dlgCreateNormalTexture.m_fnCreatedFileName;
  203. if( pafnCreatedTextures != NULL)
  204. {
  205. CTFileName *pfnCreatedTexture = pafnCreatedTextures->New();
  206. *pfnCreatedTexture = fnResult;
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. // if requested texture type is 1 we want to create animated texture
  214. else if( iDlgResult == 1)
  215. {
  216. // call create animated texture dialog
  217. CDlgCreateAnimatedTexture dlgCreateAnimatedTexture( afnCreateTexture);
  218. INDEX iDlgResult = dlgCreateAnimatedTexture.DoModal();
  219. if( iDlgResult == IDOK)
  220. {
  221. fnResult = dlgCreateAnimatedTexture.m_fnCreatedFileName;
  222. if( pafnCreatedTextures != NULL)
  223. {
  224. CTFileName *pfnCreatedTexture = pafnCreatedTextures->New();
  225. *pfnCreatedTexture = fnResult;
  226. }
  227. }
  228. }
  229. }
  230. }
  231. }
  232. // restore resources
  233. AfxSetResourceHandle( (HINSTANCE) hOldResource);
  234. return fnResult;
  235. }
  236. /* Functions used by application for getting and setting registry keys concerning modes */
  237. void CEngineGUI::GetFullScreenModeFromRegistry( CTString strSectionName, CDisplayMode &dm, GfxAPIType &gat)
  238. {
  239. // prepare full screen mode as default
  240. dm.dm_pixSizeI = 640;
  241. dm.dm_pixSizeJ = 480;
  242. dm.dm_ddDepth = DD_DEFAULT;
  243. // read FS parameters from registry
  244. CTString strResult = CStringA(AfxGetApp()->GetProfileString( CString(strSectionName), L"Full screen mode", L"640 x 480 x 0"));
  245. strResult.ScanF( "%d x %d x %d", &dm.dm_pixSizeI, &dm.dm_pixSizeJ, &dm.dm_ddDepth);
  246. if( dm.dm_ddDepth<DD_DEFAULT || dm.dm_ddDepth>DD_32BIT) dm.dm_ddDepth = DD_DEFAULT;
  247. strResult = CStringA(AfxGetApp()->GetProfileString( CString(strSectionName), L"Full screen API", L"OpenGL"));
  248. #ifdef SE1_D3D
  249. gat = (strResult=="Direct3D") ? GAT_D3D : GAT_OGL;
  250. #else // SE1_D3D
  251. gat = GAT_OGL;
  252. #endif // SE1_D3D
  253. }
  254. void CEngineGUI::SetFullScreenModeToRegistry( CTString strSectionName, CDisplayMode dm, GfxAPIType gat)
  255. {
  256. CTString strDM( 0, "%d x %d x %d", dm.dm_pixSizeI, dm.dm_pixSizeJ, dm.dm_ddDepth);
  257. #ifdef SE1_D3D
  258. CTString strGAT = (gat==GAT_D3D) ? "Direct3D" : "OpenGL";
  259. #else // SE1_D3D
  260. CTString strGAT = "OpenGL";
  261. #endif // SE1_D3D
  262. AfxGetApp()->WriteProfileString(CString(strSectionName), L"Full screen mode", CString(strDM));
  263. AfxGetApp()->WriteProfileString(CString(strSectionName), L"Full screen API", CString(strGAT));
  264. }