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

游戏引擎

开发平台:

Visual C++

  1. // Menu.cpp: implementation of the CMenu class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Menu.h"
  6. #include "texmanager.h"
  7. #include "audio.h"
  8. #include "input.h"
  9. #include "gamesetting.h"
  10. #include "AudioManager.h"
  11. //////////////////////////////////////////////////////////////////////
  12. // Construction/Destruction
  13. //////////////////////////////////////////////////////////////////////
  14. CMenu::CMenu()
  15. {
  16.     m_cMainMenuSheet=NULL;
  17. m_cSinglePlayerSheet=NULL;
  18. m_cOptionsSheet=NULL;
  19. m_cCreditSheet=NULL;
  20. m_cNewGameSheet=NULL;
  21. m_bActive=false;
  22. m_bResource=false;
  23. }
  24. CMenu::~CMenu()
  25. {
  26. DeleteMenu();
  27. }
  28. bool  CMenu::LoadMenu()
  29. {
  30. if(m_bResource)return true;
  31. //////// Texture
  32. CTexManager cTexManager;
  33.     //////
  34. if(!cTexManager.CreateMenuResource())
  35.         MessageBox(0, "Create textures error", "Error", MB_OK | MB_ICONERROR);
  36.     //////////////Cursor and background
  37. m_texCursor = cTexManager.GetTextureID(TEX_MENU_CURSOR);
  38. m_texBkg    = cTexManager.GetTextureID(TEX_MENU_BKG);
  39. ///////////////////Sheets
  40.     m_cMainMenuSheet     = new CMainMenuSheet;
  41. m_cSinglePlayerSheet = new CSinglePlayerSheet;
  42. m_cOptionsSheet      = new COptionsSheet;
  43. m_cCreditSheet       = new CCreditSheet;
  44. m_cNewGameSheet      = new CNewGameSheet;
  45.     m_cMainMenuSheet->InitSheet();
  46. m_cSinglePlayerSheet->InitSheet();
  47. m_cOptionsSheet->InitSheet();
  48. m_cCreditSheet->InitSheet();
  49. m_cNewGameSheet->InitSheet();
  50.     m_cMainMenuSheet->m_bActive=true;
  51. ///////// sound 
  52.     CAudioManager  cAudioManager;
  53. cAudioManager.CreateMenuResource();
  54.     ///////////////////////////////
  55. m_bActive=true;
  56. m_bResource=true;
  57. return true;
  58. }
  59. void  CMenu::DeleteMenu()
  60. {
  61. if(!m_bResource)return;
  62. //////// Texture
  63. CTexManager cTexManager;
  64. cTexManager.DeleteMenuResource();
  65.     ////////// sheets
  66.     if(m_cMainMenuSheet !=NULL)
  67. {
  68. delete m_cMainMenuSheet;
  69. m_cMainMenuSheet=NULL;
  70. }
  71. if(m_cSinglePlayerSheet!=NULL)
  72. {
  73. delete m_cSinglePlayerSheet;
  74. m_cSinglePlayerSheet=NULL;
  75. }
  76. if(m_cOptionsSheet  !=NULL) 
  77. {
  78. delete m_cOptionsSheet;
  79. m_cOptionsSheet=NULL;
  80. }
  81. if(m_cCreditSheet   !=NULL) 
  82. {
  83. delete m_cCreditSheet;
  84. m_cCreditSheet=NULL;
  85. }
  86. if(m_cNewGameSheet  !=NULL)  
  87. {
  88. delete m_cNewGameSheet;
  89. m_cNewGameSheet=NULL;
  90. }
  91. //////////////// delete audio
  92.     CAudioManager  cAudioManager;
  93. cAudioManager.DeleteMenuResource();
  94. /////////////////////////////
  95. m_bActive=false;
  96. m_bResource=false;
  97. }
  98. void CMenu::RenderMenu()
  99. {
  100. if(!m_bActive)return;
  101.  
  102. UpdateMenu();
  103. DrawBackGround();
  104. /////////////////////////////////////
  105. m_cMainMenuSheet->RenderSheet();
  106. m_cSinglePlayerSheet->RenderSheet();
  107. m_cOptionsSheet->RenderSheet();
  108. m_cCreditSheet->RenderSheet();
  109. m_cNewGameSheet->RenderSheet();
  110. //////////////////////////////
  111. if(CInput::m_bShowMouse)DrawCursor();
  112. }
  113. void CMenu::UpdateMenu()
  114. {
  115. if(m_cMainMenuSheet->m_bActive)
  116. {
  117. if(m_cMainMenuSheet->m_iSelect==0)
  118. {
  119.             m_cMainMenuSheet->m_iSelect=-1;
  120. m_cMainMenuSheet->m_bActive=false;
  121. m_cSinglePlayerSheet->m_bActive=true;
  122. }
  123. if(m_cMainMenuSheet->m_iSelect==2)
  124. {
  125.             m_cMainMenuSheet->m_iSelect=-1;
  126. m_cMainMenuSheet->m_bActive=false;
  127. m_cOptionsSheet->m_bActive=true;
  128. m_cOptionsSheet->m_iFather=FATHER_MAIN_MENU;
  129. m_cOptionsSheet->LoadSetting();
  130. }
  131. if(m_cMainMenuSheet->m_iSelect==3)
  132. {
  133.             m_cMainMenuSheet->m_iSelect=-1;
  134. m_cMainMenuSheet->m_bActive=false;
  135. m_cCreditSheet->m_bActive=true;
  136. }
  137. if(m_cMainMenuSheet->m_iSelect==4)
  138. {
  139. CGameSetting::m_iGameState=GAME_DEAD;
  140. }
  141. }
  142. if(m_cSinglePlayerSheet->m_bActive)
  143. {
  144. if(m_cSinglePlayerSheet->m_iSelect==0)
  145. {
  146. m_cSinglePlayerSheet->m_iSelect=-1;
  147. m_cSinglePlayerSheet->m_bActive=false;
  148. m_cMainMenuSheet->m_bActive=true;
  149. }
  150. if(m_cSinglePlayerSheet->m_iSelect==1)
  151. {
  152. m_cSinglePlayerSheet->m_iSelect=-1;
  153. m_cSinglePlayerSheet->SavePlayerInfo();
  154. }
  155. if(m_cSinglePlayerSheet->m_iSelect==2)
  156. {
  157. m_cSinglePlayerSheet->m_iSelect=-1;
  158. m_cSinglePlayerSheet->SavePlayerInfo();
  159. m_cSinglePlayerSheet->m_bActive=false;
  160. m_cNewGameSheet->m_bActive=true;
  161. }
  162. }
  163. if(m_cOptionsSheet->m_bActive)
  164. {
  165. if(m_cOptionsSheet->m_iSelect==0  || m_cOptionsSheet->m_iSelect==2)
  166. {
  167. m_cOptionsSheet->m_iSelect=-1;
  168. m_cOptionsSheet->m_bActive=false;
  169. if(m_cOptionsSheet->m_iFather==FATHER_MAIN_MENU)
  170. m_cMainMenuSheet->m_bActive=true;
  171. if(m_cOptionsSheet->m_iFather==FATHER_NEW_GAME)
  172. m_cNewGameSheet->m_bActive=true;
  173. }
  174. }
  175. if(m_cCreditSheet->m_bActive)
  176. {
  177. if(m_cCreditSheet->m_iSelect==0 )
  178. {
  179. m_cCreditSheet->m_iSelect=-1;
  180. m_cCreditSheet->m_bActive=false;
  181. m_cMainMenuSheet->m_bActive=true;
  182. }
  183. }
  184. if(m_cNewGameSheet->m_bActive)
  185. {
  186. if(m_cNewGameSheet->m_iSelect==0)
  187. {
  188. m_cNewGameSheet->m_iSelect=-1;
  189. m_cNewGameSheet->m_bActive=false;
  190. m_cSinglePlayerSheet->m_bActive=true;
  191. }
  192. if(m_cNewGameSheet->m_iSelect==1)
  193. {
  194. m_cNewGameSheet->m_iSelect=-1;
  195. m_cNewGameSheet->m_bActive=false;
  196. m_cOptionsSheet->m_bActive=true;
  197. m_cOptionsSheet->m_iFather=FATHER_NEW_GAME;
  198. m_cOptionsSheet->LoadSetting();
  199. }
  200. if(m_cNewGameSheet->m_iSelect==2)
  201. {
  202. m_cNewGameSheet->m_iSelect=-1;
  203. // m_cNewGameSheet->m_bActive=false;
  204. CGameSetting::m_iMissionSelect=m_cNewGameSheet->GetMissionSelect();
  205. CGameSetting::m_iGameState=GAME_MISSION;
  206. }
  207. }
  208. }
  209. void  CMenu::DrawBackGround()
  210. {
  211.       glBindTexture(GL_TEXTURE_2D, m_texBkg);
  212.     glEnable(GL_TEXTURE_2D);
  213. glColor3f(1,1,1);
  214.       glBegin(GL_QUADS);
  215.           glTexCoord2f(0,1);    
  216.         glVertex3i(-400 , 300 , -520);
  217.         glTexCoord2f(1+0,1);   
  218.         glVertex3i(400 , 300 , -520);
  219.     glTexCoord2f(1+0,0);   
  220.         glVertex3i(400 , -300 , -520);
  221.     glTexCoord2f(0,0);    
  222.         glVertex3i(-400 , -300 , -520);
  223.     glEnd();
  224.     glDisable(GL_TEXTURE_2D);
  225. }
  226. void  CMenu::DrawCursor()
  227. {
  228.      glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  229.     glEnable(GL_BLEND);
  230.       glBindTexture(GL_TEXTURE_2D, m_texCursor);
  231.     glEnable(GL_TEXTURE_2D);
  232. glColor3f(1,1,1);
  233.       glBegin(GL_QUADS);
  234.           glTexCoord2f(0,1);    
  235.         glVertex3i(m_cInput.m_mousePosX-400 , 300-m_cInput.m_mousePosY , -520);
  236.         glTexCoord2f(1,1);   
  237.         glVertex3i(m_cInput.m_mousePosX+16-400 , 300-m_cInput.m_mousePosY , -520);
  238.     glTexCoord2f(1,0);   
  239.         glVertex3i(m_cInput.m_mousePosX+16-400 , 300-m_cInput.m_mousePosY-16 , -520);
  240.     glTexCoord2f(0,0);    
  241.         glVertex3i(m_cInput.m_mousePosX-400 , 300-m_cInput.m_mousePosY-16 , -520);
  242.     glEnd();
  243.     glDisable(GL_TEXTURE_2D);
  244.     glDisable(GL_BLEND);
  245. }