Open.cpp
上传用户:cjwanglu
上传日期:2013-07-10
资源大小:4744k
文件大小:11k
源码类别:

游戏

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "Open.h"
  3. COpen::COpen(HINSTANCE hinst,HWND hwnd)
  4. {
  5. hWnd=hwnd;
  6. hInst=hinst;
  7.     m_pD3D=Direct3DCreate9(D3D_SDK_VERSION);
  8. if(m_pD3D==NULL)
  9. {
  10. E_FAIL;
  11. }
  12. D3DDISPLAYMODE d3ddm;
  13. m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm);
  14. if(d3ddm.Format!=D3DFMT_UNKNOWN)
  15. {
  16. d3ddm.Width=800;
  17. d3ddm.Height=600;
  18. }
  19. m_nScreenWidth=d3ddm.Width;
  20. m_nScreenHeight=d3ddm.Height;
  21. D3DPRESENT_PARAMETERS d3dpp;
  22. ZeroMemory(&d3dpp,sizeof(d3dpp));
  23. d3dpp.Windowed=true;
  24. window=d3dpp.Windowed;
  25. d3dpp.BackBufferCount=1;
  26. d3dpp.BackBufferFormat=d3ddm.Format;
  27. d3dpp.BackBufferHeight=d3ddm.Height;
  28. d3dpp.BackBufferWidth=d3ddm.Width;
  29. d3dpp.hDeviceWindow=hWnd;
  30. d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
  31. d3dpp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT;
  32. d3dpp.PresentationInterval=D3DPRESENT_INTERVAL_ONE;
  33.     m_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&m_pD3DDevice);
  34.     m_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);
  35.    
  36.    
  37.     DirectInput8Create(hInst,DIRECTINPUT_VERSION,IID_IDirectInput8,(LPVOID*)&m_pInput,NULL);
  38.     m_pInput->CreateDevice(GUID_SysMouse,&m_pMouseInput,NULL);
  39. m_pMouseInput->SetDataFormat(&c_dfDIMouse);
  40. m_pMouseInput->SetCooperativeLevel(hWnd,DISCL_NONEXCLUSIVE|DISCL_FOREGROUND);
  41. m_pMouseInput->Acquire();
  42. memset(&m_MouseState,0,sizeof(m_MouseState));
  43. memset(&m_MouseStateOld,0,sizeof(m_MouseStateOld));
  44. NIcon=2;
  45. IconVer=NIcon*4;
  46.     TxOfIcon=NIcon*2;
  47. NumOfPos=new POS[NIcon];
  48. RECT rect1={165,100,214,42,};
  49. RECT rect2={435,100,214,42,};
  50. NumOfPos[0]=Calculate(rect1);
  51. NumOfPos[1]=Calculate(rect2);
  52. m_ArrayOfTx=new LPDIRECT3DTEXTURE9[TxOfIcon];
  53.     mygod=new bool[NIcon];
  54. motherfuck=new bool[NIcon];
  55. ButtonPress=new bool[NIcon];//false表示没被按下,true表示已按下
  56.     present=new bool[NIcon];
  57. m_pTx=new LPDIRECT3DTEXTURE9[NIcon];
  58. for(int i=0;i<NIcon;i++)
  59. {
  60. ButtonPress[i]=false;
  61. mygod[i]=false;
  62. motherfuck[i]=false;
  63. present[i]=false;
  64. }
  65.   
  66.     SetTexture();
  67. SetVertexBuffer();
  68. UpdateVertex();
  69.     
  70.     bOpen=true;
  71.     //LPSTR pstr="NIMA.WAV";
  72. //m_pSound=new CSound(hWnd,pstr);
  73. //m_pSound->Play();
  74.     m_pMusic=new CSound(hWnd);
  75. m_pMusic->Play(0,"./res/Midi_1.mid");
  76.     m_pSound=new CSound(hWnd);
  77. }
  78. COpen::~COpen()
  79. {
  80.     m_pMouseInput->Unacquire();
  81.     m_pMouseInput->Release();
  82. m_pInput->Release();
  83. m_pTexture->Release();
  84. m_pBuffer->Release();
  85. m_pIconBuffer->Release();
  86. for(int i=0;i<TxOfIcon;i++)
  87. {
  88. m_ArrayOfTx[i]->Release();
  89. }
  90. DeleteObject(m_ArrayOfTx);
  91. m_pD3DDevice->Release();
  92. m_pD3D->Release();
  93. // delete m_pSound;
  94. delete m_pMusic;
  95. //delete m_pSound;
  96. }
  97. void COpen::OpenLoop()
  98. {
  99.     MSG msg;
  100. BOOL bmessage;
  101. PeekMessage(&msg,NULL,0U,0U,PM_NOREMOVE);
  102. while((msg.message!=WM_QUIT)&(bOpen==true))//|(bOpen==true))
  103. {
  104. bmessage=PeekMessage(&msg,NULL,0U,0U,PM_REMOVE);
  105. if(bmessage)
  106. {
  107. TranslateMessage(&msg);
  108. DispatchMessage(&msg);
  109. }
  110. else
  111. {
  112. RenderOpen();
  113. }
  114. }
  115. }
  116. void COpen::RenderOpen()
  117. {
  118.     m_pD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0F,0);
  119. m_pD3DDevice->BeginScene();
  120. //SetCamera3D();
  121. //Render3D();
  122.   //  GetMouse();
  123.     SetCamera2D();
  124.     
  125.      Render2D();
  126.    
  127. m_pD3DDevice->EndScene();
  128. m_pD3DDevice->Present(NULL,NULL,NULL,NULL);
  129. }
  130. void COpen::SetVertexBuffer()
  131. {
  132. m_pD3DDevice->CreateVertexBuffer(4*sizeof(VERTEX),0,OPEN_D3DFVF_VERTEXBUFFER,D3DPOOL_DEFAULT,&m_pBuffer,NULL);
  133. m_pD3DDevice->CreateVertexBuffer(IconVer*sizeof(VERTEX),0,OPEN_D3DFVF_VERTEXBUFFER,D3DPOOL_DEFAULT,&m_pIconBuffer,NULL);
  134. }
  135. void COpen::UpdateVertex()
  136. {
  137. VERTEX * pVertex;
  138. m_pBuffer->Lock(0,4*sizeof(VERTEX),(void **)&pVertex,0);
  139. pVertex[0].x=0.0f;//-m_nWidth/2;
  140. pVertex[0].y=600.0f;
  141. pVertex[0].z=0.0f;
  142. pVertex[1].x=800.0f;
  143. pVertex[1].y=600.0f;
  144. pVertex[1].z=0.0f;
  145. pVertex[2].x=0.0f;
  146. pVertex[2].y=0.0f;
  147. pVertex[2].z=0.0f;
  148.     pVertex[3].x=800.0f;
  149. pVertex[3].y=0.0f;
  150. pVertex[3].z=0.0f;
  151. pVertex[0].tu=0.0f;
  152. pVertex[0].tv=0.0f;
  153. pVertex[1].tu=1.0f;
  154. pVertex[1].tv=0.0f;
  155. pVertex[2].tu=0.0f;
  156. pVertex[2].tv=1.0f;
  157. pVertex[3].tu=1.0f;
  158. pVertex[3].tv=1.0f;
  159. m_pBuffer->Unlock();
  160.  
  161.    pVertex=NULL;
  162. m_pIconBuffer->Lock(0,IconVer*sizeof(VERTEX),(void **)&pVertex,0);
  163. int k=0; 
  164. for(int i=0;i<NIcon;i++)
  165. {
  166. pVertex[k].x=NumOfPos[i].ltx;
  167. pVertex[k].y=NumOfPos[i].lty;
  168. pVertex[k].z=0;
  169.             
  170. pVertex[k+1].x=NumOfPos[i].rtx;
  171. pVertex[k+1].y=NumOfPos[i].rty;
  172.         pVertex[k+1].z=0.0f;
  173. pVertex[k+2].x=NumOfPos[i].lbx;
  174. pVertex[k+2].y=NumOfPos[i].lby;
  175.         pVertex[k+2].z=0.0f;
  176.             pVertex[k+3].x=NumOfPos[i].rbx;
  177. pVertex[k+3].y=NumOfPos[i].rby;
  178.         pVertex[k+3].z=0.0f;
  179.      
  180.    
  181.         pVertex[k+0].tu=0.0f;
  182.         pVertex[k+0].tv=0.0f;
  183.             pVertex[k+0].tu1=0.0f;
  184.         pVertex[k+0].tv1=0.0f;
  185.         
  186. pVertex[k+1].tu=1.0f;
  187.         pVertex[k+1].tv=0.0f;
  188.             pVertex[k+1].tu1=1.0f;
  189.         pVertex[k+1].tv1=0.0f;
  190.         
  191. pVertex[k+2].tu=0.0f;
  192.         pVertex[k+2].tv=1.0f;
  193.             pVertex[k+2].tu1=0.0f;
  194.         pVertex[k+2].tv1=1.0f;
  195.        
  196. pVertex[k+3].tu=1.0f;
  197.         pVertex[k+3].tv=1.0f;
  198.             pVertex[k+3].tu1=1.0f;
  199.         pVertex[k+3].tv1=1.0f;
  200.               k+=4;
  201. }
  202. m_pIconBuffer->Unlock();
  203.     
  204. }
  205. void COpen::SetTexture()
  206. {
  207. D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/open.jpg", 0, 0, 0, 0,
  208.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  209.   D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_pTexture);
  210.     D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/enter_1.bmp", 0, 0, 0, 0,
  211.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  212.   D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_ArrayOfTx[0]);
  213. D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/enter_3.bmp", 0, 0, 0, 0,
  214.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  215.   D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_ArrayOfTx[1]);//m_pIconTx
  216.     D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/quit_1.bmp", 0, 0, 0, 0,
  217.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  218.   D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_ArrayOfTx[2]);
  219.     D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/quit_3.bmp", 0, 0, 0, 0,
  220.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  221.   D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_ArrayOfTx[3]);
  222.     D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/quit_2.bmp", 0, 0, 0, 0,
  223.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  224.   D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_pTx[1]);
  225.     D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/enter_2.bmp", 0, 0, 0, 0,
  226.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  227.   D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_pTx[0]);
  228. }
  229. void COpen::SetCamera2D()
  230. {
  231.     D3DXMATRIX matPers,matView;
  232. D3DXMatrixOrthoLH(&matPers,(float)m_nScreenWidth,(float)m_nScreenHeight,0.0f,1.0f);
  233. D3DXMatrixIdentity(&matView);
  234. m_pD3DDevice->SetTransform(D3DTS_PROJECTION,&matPers);
  235. m_pD3DDevice->SetTransform(D3DTS_WORLD,&matView);
  236. m_pD3DDevice->SetTransform(D3DTS_VIEW,&matView);
  237. m_pD3DDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_FALSE);
  238. m_pD3DDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
  239. }
  240. void COpen::Render2D()
  241. {
  242.     Translate(-400,+300);
  243.     
  244. m_pD3DDevice->SetStreamSource(0,m_pBuffer,0,sizeof(VERTEX));
  245. m_pD3DDevice->SetFVF(OPEN_D3DFVF_VERTEXBUFFER);
  246. m_pD3DDevice->SetTexture(0,m_pTexture);
  247. m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);
  248.     
  249.     m_pD3DDevice->SetStreamSource(0,m_pIconBuffer,0,sizeof(VERTEX));
  250. m_pD3DDevice->SetFVF(OPEN_D3DFVF_VERTEXBUFFER);
  251. int k=0;int j=0;
  252. for(int i=0; i<NIcon;i++)
  253. {
  254. if(present[i]==true)
  255. {
  256.            m_pD3DDevice->SetTexture(0,m_pTx[i]);//m_ArrayOfTx[k+1]
  257.    m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1,D3DTA_TEXTURE);
  258.           
  259.    m_pD3DDevice->SetTexture(1,m_ArrayOfTx[k+1]);//m_ArrayOfTx[k+1]
  260.    m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1,D3DTA_CURRENT);
  261.  
  262.            m_pD3DDevice->SetTexture(2,m_ArrayOfTx[k]);
  263.    m_pD3DDevice->SetTextureStageState(2, D3DTSS_COLORARG1,D3DTA_CURRENT);
  264. }
  265. else
  266. {
  267. if(ButtonPress[i]==false)
  268. {
  269.      m_pD3DDevice->SetTexture(0,m_ArrayOfTx[k]);
  270. //m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  271. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1,D3DTA_TEXTURE);
  272. //m_pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_DISABLE); 
  273. m_pD3DDevice->SetTexture(1,m_ArrayOfTx[k+1]);//m_ArrayOfTx[k+1]
  274. //m_pD3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_SELECTARG1);
  275. m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1,D3DTA_CURRENT);
  276. //m_pD3DDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,D3DTOP_DISABLE); 
  277.         
  278. }
  279. else
  280. {
  281. m_pD3DDevice->SetTexture(0,m_ArrayOfTx[k+1]);//m_ArrayOfTx[k+1]
  282. //m_pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);
  283. m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1,D3DTA_TEXTURE);
  284. //m_pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_DISABLE); 
  285.         m_pD3DDevice->SetTexture(1,m_ArrayOfTx[k]);
  286. //m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  287. m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1,D3DTA_CURRENT);
  288. //m_pD3DDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,D3DTOP_DISABLE); 
  289. }
  290. }
  291. m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,j,2);
  292. k+=2;
  293. j+=4;
  294. }
  295. }
  296. void COpen::Translate(int x,int y)
  297. {
  298. D3DXMATRIX matMove;
  299. D3DXMatrixTranslation(&matMove, (float)x, -(float)y, 0.0f);
  300. m_pD3DDevice->SetTransform(D3DTS_WORLD, &matMove);
  301. }
  302. void COpen::Update()
  303. {
  304.     memcpy(&m_MouseStateOld,&m_MouseState,sizeof(m_MouseState));
  305. if(DI_OK!=m_pMouseInput->GetDeviceState(sizeof(m_MouseState),&m_MouseState))
  306. {
  307. memset(&m_MouseState,0,sizeof(m_MouseState));
  308. m_pMouseInput->Acquire();
  309. }
  310.     
  311. }
  312. void COpen::Control(bool * fuck)
  313. {    
  314.     
  315. GetCursorPos(&m_Point);
  316.     float x;float y;
  317.      //ScreenToClient(hWnd,&m_Point);
  318.  x=(float)m_Point.x-5;
  319.      y=(600)-(float)m_Point.y;
  320.    for(int i=0;i<NIcon;i++)
  321.    {
  322. if((x>=NumOfPos[i].ltx)&(x<=NumOfPos[i].rbx))
  323. {
  324. if((y>=NumOfPos[i].rby)&(y<=NumOfPos[i].lty))
  325. {
  326.               
  327.   if(0x80&m_MouseState.rgbButtons[0])
  328.       
  329.   { ButtonPress[i]=true;
  330.    mygod[i]=true;
  331.    present[i]=false;
  332.                m_pSound->PlaySound("./res/sound_2.wav");
  333.   //*fuck=false;
  334.   }
  335.   else
  336.   {
  337.                 present[i]=true;
  338.                 ButtonPress[i]=false;
  339. if(motherfuck[i]==true)
  340. {fuck[i]=false;mygod[i]=false;present[i]=false;}
  341.      if(mygod[i]==true)
  342. {
  343.  motherfuck[i]=true;
  344.  present[i]=false;
  345. }
  346.   
  347.   }
  348. }
  349. else
  350. {
  351.               ButtonPress[i]=false;
  352.   mygod[i]=false;
  353.   motherfuck[i]=false;
  354.               present[i]=false;
  355. }
  356. }
  357.         else
  358. {
  359.               ButtonPress[i]=false;
  360.               mygod[i]=false;
  361.   motherfuck[i]=false;
  362.               present[i]=false;
  363. }
  364.    }
  365. }
  366. POS COpen::Calculate(RECT rect)
  367. {
  368. POS a;
  369. a.ltx=rect.left; a.lty=rect.top; a.rtx=rect.left+rect.right; a.rty=rect.top;
  370. a.lbx=rect.left; a.lby=rect.top-rect.bottom; a.rbx=rect.left+rect.right; a.rby=rect.top-rect.bottom;
  371. return a;
  372. }