GameSetting.cpp
上传用户:hkb425
上传日期:2007-06-16
资源大小:34191k
文件大小:13k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. // GameSetting.cpp: implementation of the CGameSetting class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "GameSetting.h"
  6. ////////// Resolution
  7. int   CGameSetting::m_iScrWidth  = 800;
  8. int   CGameSetting::m_iScrHeight = 600;
  9. int   CGameSetting::m_iColorDepth= 16;
  10. ////////// Render
  11. int   CGameSetting::m_iGamma = 70;
  12. int   CGameSetting::m_iModelDetail=MEDIUM;
  13. int   CGameSetting::m_iTexQuality=MEDIUM;
  14. int   CGameSetting::m_iVisibleDist=50;
  15. int   CGameSetting::m_iForestDensity=LOW;
  16. int   CGameSetting::m_iFogDensity=45;
  17. ////////// Audio
  18. bool  CGameSetting::m_bAudioEnable = 1;
  19. bool  CGameSetting::m_bSound = 1;
  20. bool  CGameSetting::m_bMusic = 1;
  21. int   CGameSetting::m_iSoundVolume = 60;
  22. int   CGameSetting::m_iMusicVolume = 60;
  23. ////////// Control
  24. //mouse 
  25. int   CGameSetting::m_iKeyFire = 0;
  26. int   CGameSetting::m_iKeyZoom = 1;
  27. //keyboard
  28. int   CGameSetting::m_iKeyHelp     = VK_F1;
  29. int   CGameSetting::m_iKeyJump     = VK_SPACE;
  30. int   CGameSetting::m_iKeyForward  = VK_UP;
  31. int   CGameSetting::m_iKeyBackward = VK_DOWN;
  32. int   CGameSetting::m_iKeyLeft     = VK_LEFT;
  33. int   CGameSetting::m_iKeyRight    = VK_RIGHT;
  34. int   CGameSetting::m_iKeyDrawFPS  = VK_F2;
  35. int   CGameSetting::m_iKeyMute     = 'M';
  36. //////////// Mouse
  37. bool  CGameSetting::m_bMouseInvert=false;
  38. int   CGameSetting::m_iMouseSens=60;
  39. ////////// Game Activate
  40. int   CGameSetting::m_iGameState=GAME_MAIN_MENU;
  41. /////////////////////////////////////////////////////
  42. /////////////////////////////////////////////////////
  43. /////////// Mission 
  44. int   CGameSetting::m_iMissionSelect=0;
  45. ///sky
  46. char *CGameSetting::m_strSkyDir =NULL;
  47. bool  CGameSetting::m_bLensFlare =true;
  48. ///plants
  49. char *CGameSetting::m_strPlantsDir =NULL;
  50. char *CGameSetting::m_strPlantIndex=NULL;
  51. ///Viewer
  52. float CGameSetting::m_fViewerPosX=0;
  53. float CGameSetting::m_fViewerPosZ=0;
  54. float CGameSetting::m_fViewerRotY=0;
  55. char *CGameSetting::m_strViewerWeapon=NULL;;
  56. char *CGameSetting::m_strViewerWeaponSkin=NULL;
  57. ///Terrain
  58. int   CGameSetting::m_iTextureType=0;
  59. char *CGameSetting::m_strHeightmap=NULL;
  60. char *CGameSetting::m_strTerrainSkinDir=NULL;
  61. char *CGameSetting::m_strTextureIndex=NULL;
  62. char *CGameSetting::m_strLODmap=NULL;
  63. //////////////////////////////////////////////
  64. int   CGameSetting::m_numUser=0;
  65. //////////////////////////////////////////////////////////////////////
  66. // Construction/Destruction
  67. //////////////////////////////////////////////////////////////////////
  68. CGameSetting::CGameSetting()
  69. {
  70. if(m_numUser==0)
  71. {
  72. ////sky
  73.         m_strSkyDir = new char [STRING_LENGTH];
  74. ///plants
  75. m_strPlantsDir = new char [STRING_LENGTH];
  76.         m_strPlantIndex     = new char [STRING_LENGTH];
  77.         ///Viewer
  78.         m_strViewerWeapon     = new char [STRING_LENGTH];
  79.         m_strViewerWeaponSkin = new char [STRING_LENGTH];
  80.         ///Terrain
  81.         m_strHeightmap      = new char [STRING_LENGTH];
  82.         m_strTerrainSkinDir = new char [STRING_LENGTH];
  83.         m_strTextureIndex   = new char [STRING_LENGTH];
  84.         m_strLODmap         = new char [STRING_LENGTH];
  85. }
  86. m_numUser++;
  87. }
  88. CGameSetting::~CGameSetting()
  89. {
  90. m_numUser--;
  91. if(m_numUser==0)
  92. {
  93. ////sky
  94.         if(m_strSkyDir !=NULL)delete [] m_strSkyDir ;
  95. ////plants
  96.         if(m_strPlantsDir !=NULL)delete [] m_strPlantsDir ;
  97.         if(m_strPlantIndex     !=NULL)delete [] m_strPlantIndex ;
  98.         ///Viewer
  99.         if(m_strViewerWeapon     !=NULL)delete [] m_strViewerWeapon ;
  100.         if(m_strViewerWeaponSkin !=NULL)delete [] m_strViewerWeaponSkin ;
  101.         ///Terrain
  102.         if(m_strHeightmap      !=NULL)delete [] m_strHeightmap ;
  103.         if(m_strTerrainSkinDir !=NULL)delete [] m_strTerrainSkinDir ;
  104.         if(m_strTextureIndex   !=NULL)delete [] m_strTextureIndex ;
  105.         if(m_strLODmap         !=NULL)delete [] m_strLODmap ;
  106. }
  107. }
  108. ///////////////////////////////////////////////////////
  109. void CGameSetting::LoadDefaultSetting()
  110. {
  111.     ReadSettingFromFile("default.ini");
  112. }
  113. void CGameSetting::LoadSetting()
  114. {
  115.     ReadSettingFromFile("config.ini");
  116. }
  117. ///////////////////////////////////////////////////////
  118. void CGameSetting::ReadSettingFromFile(char filename[])
  119. {
  120. // Convert the INI filename into a full pathname
  121. char sINIPath[MAX_PATH];
  122. MakeModulePath(filename, sINIPath);
  123. ////////// Resolution
  124. m_iScrWidth      = GetPrivateProfileInt("Resolution", "ScrWidth", 800, sINIPath);
  125. m_iScrHeight     = GetPrivateProfileInt("Resolution", "ScrHeight",600, sINIPath);
  126.     m_iColorDepth    = GetPrivateProfileInt("Resolution", "ColorDepth",16, sINIPath);
  127. ////////// Render
  128.     m_iGamma         = GetPrivateProfileInt("Render", "Gamma",70,sINIPath);
  129.     m_iModelDetail   = GetPrivateProfileInt("Render", "ModelDetail",MEDIUM,sINIPath);
  130.     m_iTexQuality    = GetPrivateProfileInt("Render", "TexQuality",MEDIUM,sINIPath);
  131.     m_iVisibleDist   = GetPrivateProfileInt("Render", "VisibleDist",50,sINIPath);
  132.     m_iForestDensity = GetPrivateProfileInt("Render", "ForestDensity",LOW,sINIPath);
  133.     m_iFogDensity    = GetPrivateProfileInt("Render", "FogDensity",45,sINIPath);
  134. ////////// Audio
  135. int temp;
  136. temp             = GetPrivateProfileInt("Audio", "Sound",      1, sINIPath);
  137. if(temp==0)      m_bSound= false;
  138. else             m_bSound= true;
  139.     temp             = GetPrivateProfileInt("Audio", "Music",      1, sINIPath);
  140. if(temp==0)      m_bMusic= false;
  141. else             m_bMusic= true;
  142. m_iSoundVolume   = GetPrivateProfileInt("Audio", "SoundVolume",60,sINIPath);
  143. m_iMusicVolume   = GetPrivateProfileInt("Audio", "MusicVolume",60,sINIPath);
  144. ////////// Control
  145.     //mouse 
  146.     m_iKeyFire       = GetPrivateProfileInt("Control", "Fire",0,sINIPath);
  147.     m_iKeyZoom       = GetPrivateProfileInt("Control", "Zoom",1,sINIPath);
  148.     //keyboard
  149.     m_iKeyHelp       = GetPrivateProfileInt("Control", "Help",    VK_F1,   sINIPath);
  150.     m_iKeyJump       = GetPrivateProfileInt("Control", "Jump",    VK_SPACE,sINIPath);
  151.     m_iKeyForward    = GetPrivateProfileInt("Control", "Forward", VK_UP,   sINIPath);
  152.     m_iKeyBackward   = GetPrivateProfileInt("Control", "Backward",VK_DOWN, sINIPath);
  153.     m_iKeyLeft       = GetPrivateProfileInt("Control", "Left",    VK_LEFT, sINIPath);
  154.     m_iKeyRight      = GetPrivateProfileInt("Control", "Right",   VK_RIGHT,sINIPath);
  155.     m_iKeyDrawFPS    = GetPrivateProfileInt("Control", "DrawFPS", VK_F2,   sINIPath);
  156.     m_iKeyMute       = GetPrivateProfileInt("Control", "Mute",    'M',     sINIPath);
  157. ////////// Mouse
  158. temp             = GetPrivateProfileInt("Mouse", "MouseInvert",0,sINIPath);
  159. if(temp==0)      m_bMouseInvert= false;
  160. else             m_bMouseInvert= true;
  161.     m_iMouseSens     = GetPrivateProfileInt("Mouse", "MouseSens",60,sINIPath);
  162. //////////////// Safe Check Parameters/////////
  163.     SafeCheck();
  164. }
  165. ///////////////////////////////////////////////////////
  166. void CGameSetting::SaveSetting()
  167. {
  168. // Convert the INI filename into a full pathname
  169. char sINIPath[MAX_PATH];
  170. MakeModulePath("config.ini", sINIPath);
  171. //////////////// Safe Check Parameters/////////
  172.     SafeCheck();
  173. ////////// Resolution
  174. SaveSettingItem("Resolution", "ScrWidth", m_iScrWidth ,   sINIPath);
  175. SaveSettingItem("Resolution", "ScrHeight",m_iScrHeight,   sINIPath);
  176. SaveSettingItem("Resolution", "ColorDepth",m_iColorDepth, sINIPath);
  177. ////////// Render
  178.     SaveSettingItem("Render", "Gamma",         m_iGamma,        sINIPath);
  179.     SaveSettingItem("Render", "ModelDetail",   m_iModelDetail,  sINIPath);
  180.     SaveSettingItem("Render", "TexQuality",    m_iTexQuality,   sINIPath);
  181.     SaveSettingItem("Render", "VisibleDist",   m_iVisibleDist,  sINIPath);
  182.     SaveSettingItem("Render", "ForestDensity", m_iForestDensity,sINIPath);
  183.     SaveSettingItem("Render", "FogDensity",    m_iFogDensity,   sINIPath);
  184. ////////// Audio
  185. SaveSettingItem("Audio", "Sound",      (int)m_bSound    ,   sINIPath);
  186. SaveSettingItem("Audio", "Music",      (int)m_bMusic   ,    sINIPath);
  187. SaveSettingItem("Audio", "SoundVolume",m_iSoundVolume,      sINIPath);
  188. SaveSettingItem("Audio", "MusicVolume",m_iMusicVolume,      sINIPath);
  189. ////////// Control
  190.     //mouse 
  191.     SaveSettingItem("Control", "Fire",m_iKeyFire,sINIPath);
  192.     SaveSettingItem("Control", "Zoom",m_iKeyZoom,sINIPath);
  193.     //keyboard
  194.     SaveSettingItem("Control", "Help",    m_iKeyHelp ,   sINIPath);
  195.     SaveSettingItem("Control", "Jump",    m_iKeyJump  ,  sINIPath);
  196.     SaveSettingItem("Control", "Forward", m_iKeyForward ,sINIPath);
  197.     SaveSettingItem("Control", "Backward",m_iKeyBackward,sINIPath);
  198.     SaveSettingItem("Control", "Left",    m_iKeyLeft ,   sINIPath);
  199.     SaveSettingItem("Control", "Right",   m_iKeyRight ,  sINIPath);
  200.     SaveSettingItem("Control", "DrawFPS", m_iKeyDrawFPS ,sINIPath);
  201.     SaveSettingItem("Control", "Mute",    m_iKeyMute ,   sINIPath);
  202. ///////// Mouse 
  203.     SaveSettingItem("Mouse", "MouseInvert",(int)m_bMouseInvert ,sINIPath);
  204.     SaveSettingItem("Mouse", "MouseSens",  m_iMouseSens ,       sINIPath);
  205. ///////////////////////
  206. }
  207. void CGameSetting::SaveSettingItem(char SectionName[],char KeyName[],
  208.    int KeyValue,char sINIPath[])
  209. {
  210. char strValue[16];
  211. wsprintf(strValue,"%d",KeyValue);
  212.     WritePrivateProfileString(SectionName, KeyName, strValue, sINIPath);
  213. }
  214. void CGameSetting::SafeCheck()
  215. {
  216. ////////// Check Resolution 
  217. switch(m_iScrWidth)
  218. {
  219. case 640:
  220. m_iScrHeight=480;
  221. break;
  222. case 800:
  223. m_iScrHeight=600;
  224. break;
  225. case 1024:
  226. m_iScrHeight=768;
  227. break;
  228. case 1280:
  229. m_iScrHeight=960;
  230. break;
  231. default:
  232. m_iScrWidth=800;
  233.         m_iScrHeight=600;
  234. break;
  235. }
  236. //Check color depth
  237. if(m_iColorDepth!=16 && m_iColorDepth!=32 ) m_iColorDepth=16;
  238. }
  239. void CGameSetting::LoadMissionConfig()
  240. {
  241. // Convert the INI filename into a full pathname
  242. char missionName[64];
  243. char sINIPath[MAX_PATH];
  244. wsprintf(missionName,"mission/mission%d.ini",m_iMissionSelect);
  245. MakeModulePath(missionName, sINIPath);
  246.     ///sky
  247. GetPrivateProfileString("Sky","SkyDir","sky/",m_strSkyDir,STRING_LENGTH,sINIPath);
  248.   int temp;
  249. temp  = GetPrivateProfileInt("Sky", "LensFlare", 1, sINIPath);
  250. if(temp==1)m_bLensFlare=true;
  251. else m_bLensFlare=false;
  252. ///plants
  253. GetPrivateProfileString("Plants","PlantsDir","Plants/summer/",m_strPlantsDir,STRING_LENGTH,sINIPath);
  254. GetPrivateProfileString("Plants","PlantIndex",    "plants/plantmap.bmp",m_strPlantIndex,STRING_LENGTH,sINIPath);
  255.     ///Viewer
  256. m_fViewerPosX = (float)GetPrivateProfileInt("3dExplorer", "XPos", 800, sINIPath);
  257. m_fViewerPosZ = (float)GetPrivateProfileInt("3dExplorer", "ZPos", 800, sINIPath);
  258. m_fViewerRotY = (float)GetPrivateProfileInt("3dExplorer", "YRot", 0,   sINIPath);
  259. GetPrivateProfileString("3dExplorer","Weapon",    "model/mygun.md2",m_strViewerWeapon,STRING_LENGTH,sINIPath);
  260. GetPrivateProfileString("3dExplorer","WeaponSkin","model/mygun.bmp",m_strViewerWeaponSkin,STRING_LENGTH,sINIPath);
  261.     ///Terrain
  262.     m_iTextureType = GetPrivateProfileInt("Terrain",   "TextureType", 0, sINIPath);
  263. GetPrivateProfileString("Terrain","Heightmap",     "terrains/ter256.bmp",  m_strHeightmap,STRING_LENGTH,sINIPath);
  264. GetPrivateProfileString("Terrain","TerrainSkinDir","terrains/terrainSkin/",m_strTerrainSkinDir,STRING_LENGTH,sINIPath);
  265. GetPrivateProfileString("Terrain","TextureIndex",  "terrains/64_index.BMP",m_strTextureIndex,STRING_LENGTH,sINIPath);
  266.     if( m_iModelDetail== LOW)   wsprintf(m_strLODmap,"Terrains/lodmap0.lod");
  267.     if( m_iModelDetail== MEDIUM)wsprintf(m_strLODmap,"Terrains/lodmap0.lod");
  268.     if( m_iModelDetail== HEIGHT)wsprintf(m_strLODmap,"Terrains/lodmap0.lod");
  269. }
  270. void CGameSetting::SaveMissionConfig()
  271. {
  272. }
  273. ///////////////////////////////////////////////////////
  274. void CGameSetting::MakeModulePath(char filename[], char sINIPath[])
  275. {
  276. // Add the path of the executable to the passed INI filename
  277. // Buffer to hold the filename
  278. char sModuleFileName[MAX_PATH];
  279. // Pass NULL to get the handle of the current module
  280. HMODULE hModule = GetModuleHandle(NULL);
  281. // Fill the path into sModuleFileName and get the number of returned chars
  282. int iNumChars = GetModuleFileName(hModule, sModuleFileName, MAX_PATH);
  283. // If the path was received
  284. if (iNumChars)
  285. {
  286. // Loop from the last to the first char
  287. for (int iCurChar=iNumChars-1; iCurChar>=0; iCurChar--)
  288. {
  289. // If the current char is a backslash
  290. if (sModuleFileName[iCurChar] == char('\'))
  291. {
  292. // Terminate the the string behind the backslash and
  293. sModuleFileName[iCurChar+1] = char('');
  294. break;
  295. }
  296. }
  297. // Append INI filename at the end of the path
  298. strcat(sModuleFileName, filename);
  299. // Copy the full path into the destibation
  300. strcpy(sINIPath, sModuleFileName);
  301. }
  302. }