www.pudn.com > 3DRPG.rar > Open.cpp


#include "stdafx.h" 
 
#include "Open.h" 
 
COpen::COpen(HINSTANCE hinst,HWND hwnd) 
{ 
	hWnd=hwnd; 
	hInst=hinst; 
    m_pD3D=Direct3DCreate9(D3D_SDK_VERSION); 
	if(m_pD3D==NULL) 
	{ 
		E_FAIL; 
	} 
	D3DDISPLAYMODE d3ddm; 
	m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm); 
	if(d3ddm.Format!=D3DFMT_UNKNOWN) 
	{ 
		d3ddm.Width=800; 
		d3ddm.Height=600; 
	} 
	m_nScreenWidth=d3ddm.Width; 
	m_nScreenHeight=d3ddm.Height; 
	D3DPRESENT_PARAMETERS d3dpp; 
	ZeroMemory(&d3dpp,sizeof(d3dpp)); 
	d3dpp.Windowed=true; 
	window=d3dpp.Windowed; 
	d3dpp.BackBufferCount=1; 
	d3dpp.BackBufferFormat=d3ddm.Format; 
	d3dpp.BackBufferHeight=d3ddm.Height; 
	d3dpp.BackBufferWidth=d3ddm.Width; 
	d3dpp.hDeviceWindow=hWnd; 
	d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD; 
	d3dpp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT; 
	d3dpp.PresentationInterval=D3DPRESENT_INTERVAL_ONE; 
    m_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&m_pD3DDevice); 
    m_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW); 
    
	 
	 
    
    DirectInput8Create(hInst,DIRECTINPUT_VERSION,IID_IDirectInput8,(LPVOID*)&m_pInput,NULL); 
    m_pInput->CreateDevice(GUID_SysMouse,&m_pMouseInput,NULL); 
	m_pMouseInput->SetDataFormat(&c_dfDIMouse); 
	m_pMouseInput->SetCooperativeLevel(hWnd,DISCL_NONEXCLUSIVE|DISCL_FOREGROUND); 
	m_pMouseInput->Acquire(); 
	memset(&m_MouseState,0,sizeof(m_MouseState)); 
	memset(&m_MouseStateOld,0,sizeof(m_MouseStateOld)); 
	 
	NIcon=2; 
	IconVer=NIcon*4; 
    TxOfIcon=NIcon*2; 
 
	NumOfPos=new POS[NIcon]; 
	RECT rect1={165,100,214,42,}; 
	RECT rect2={435,100,214,42,}; 
	NumOfPos[0]=Calculate(rect1); 
	NumOfPos[1]=Calculate(rect2); 
	 
	m_ArrayOfTx=new LPDIRECT3DTEXTURE9[TxOfIcon]; 
    mygod=new bool[NIcon]; 
	motherfuck=new bool[NIcon]; 
	ButtonPress=new bool[NIcon];//false表示没被按下,true表示已按下 
    present=new bool[NIcon]; 
	m_pTx=new LPDIRECT3DTEXTURE9[NIcon]; 
	for(int i=0;iPlay(); 
 
    m_pMusic=new CSound(hWnd); 
	m_pMusic->Play(0,"./res/Midi_1.mid"); 
    m_pSound=new CSound(hWnd); 
	 
 
} 
 
COpen::~COpen() 
{ 
    m_pMouseInput->Unacquire(); 
    m_pMouseInput->Release(); 
	m_pInput->Release(); 
	m_pTexture->Release(); 
	m_pBuffer->Release(); 
	m_pIconBuffer->Release(); 
	for(int i=0;iRelease(); 
	} 
	DeleteObject(m_ArrayOfTx); 
	m_pD3DDevice->Release(); 
	m_pD3D->Release(); 
//	delete m_pSound; 
	delete m_pMusic; 
	//delete m_pSound; 
} 
void COpen::OpenLoop() 
{ 
    MSG msg; 
	BOOL bmessage; 
	PeekMessage(&msg,NULL,0U,0U,PM_NOREMOVE); 
	while((msg.message!=WM_QUIT)&(bOpen==true))//|(bOpen==true)) 
	{ 
		bmessage=PeekMessage(&msg,NULL,0U,0U,PM_REMOVE); 
		if(bmessage) 
		{ 
			TranslateMessage(&msg); 
			DispatchMessage(&msg); 
		} 
		else 
		{ 
			 
			RenderOpen(); 
		} 
	} 
} 
 
void COpen::RenderOpen() 
{ 
    m_pD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0F,0); 
	m_pD3DDevice->BeginScene(); 
	//SetCamera3D(); 
	 
	//Render3D(); 
  //  GetMouse(); 
    SetCamera2D(); 
     
	 
     Render2D(); 
	 
    
	 
	m_pD3DDevice->EndScene(); 
	m_pD3DDevice->Present(NULL,NULL,NULL,NULL); 
} 
 
void COpen::SetVertexBuffer() 
{ 
	m_pD3DDevice->CreateVertexBuffer(4*sizeof(VERTEX),0,OPEN_D3DFVF_VERTEXBUFFER,D3DPOOL_DEFAULT,&m_pBuffer,NULL); 
	m_pD3DDevice->CreateVertexBuffer(IconVer*sizeof(VERTEX),0,OPEN_D3DFVF_VERTEXBUFFER,D3DPOOL_DEFAULT,&m_pIconBuffer,NULL); 
 
} 
 
void COpen::UpdateVertex() 
{ 
	VERTEX * pVertex; 
	m_pBuffer->Lock(0,4*sizeof(VERTEX),(void **)&pVertex,0); 
	 
	pVertex[0].x=0.0f;//-m_nWidth/2; 
	pVertex[0].y=600.0f; 
	pVertex[0].z=0.0f; 
	 
	pVertex[1].x=800.0f; 
	pVertex[1].y=600.0f; 
	pVertex[1].z=0.0f; 
 
	pVertex[2].x=0.0f; 
	pVertex[2].y=0.0f; 
	pVertex[2].z=0.0f; 
 
    pVertex[3].x=800.0f; 
	pVertex[3].y=0.0f; 
	pVertex[3].z=0.0f; 
 
	pVertex[0].tu=0.0f; 
	pVertex[0].tv=0.0f; 
 
	pVertex[1].tu=1.0f; 
	pVertex[1].tv=0.0f; 
 
	pVertex[2].tu=0.0f; 
	pVertex[2].tv=1.0f; 
 
	pVertex[3].tu=1.0f; 
	pVertex[3].tv=1.0f; 
 
	m_pBuffer->Unlock(); 
 
  
 
   pVertex=NULL; 
	m_pIconBuffer->Lock(0,IconVer*sizeof(VERTEX),(void **)&pVertex,0); 
		int k=0;  
		for(int i=0;iUnlock(); 
     
} 
 
void COpen::SetTexture() 
{ 
	D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/open.jpg", 0, 0, 0, 0, 
										  D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, 
										  D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_pTexture); 
    D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/enter_1.bmp", 0, 0, 0, 0, 
										  D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, 
										  D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_ArrayOfTx[0]); 
	D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/enter_3.bmp", 0, 0, 0, 0, 
										  D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, 
										  D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_ArrayOfTx[1]);//m_pIconTx 
    D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/quit_1.bmp", 0, 0, 0, 0, 
										  D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, 
										  D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_ArrayOfTx[2]); 
 
    D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/quit_3.bmp", 0, 0, 0, 0, 
										  D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, 
										  D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_ArrayOfTx[3]); 
 
    D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/quit_2.bmp", 0, 0, 0, 0, 
										  D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, 
										  D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_pTx[1]); 
    D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/enter_2.bmp", 0, 0, 0, 0, 
										  D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, 
										  D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_pTx[0]); 
} 
void COpen::SetCamera2D() 
{ 
    D3DXMATRIX matPers,matView; 
 
	D3DXMatrixOrthoLH(&matPers,(float)m_nScreenWidth,(float)m_nScreenHeight,0.0f,1.0f); 
	D3DXMatrixIdentity(&matView); 
	m_pD3DDevice->SetTransform(D3DTS_PROJECTION,&matPers); 
	m_pD3DDevice->SetTransform(D3DTS_WORLD,&matView); 
	m_pD3DDevice->SetTransform(D3DTS_VIEW,&matView); 
	m_pD3DDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_FALSE); 
	m_pD3DDevice->SetRenderState(D3DRS_LIGHTING,FALSE); 
} 
 
void COpen::Render2D() 
{ 
    Translate(-400,+300); 
 
 
     
 
 
	m_pD3DDevice->SetStreamSource(0,m_pBuffer,0,sizeof(VERTEX)); 
	m_pD3DDevice->SetFVF(OPEN_D3DFVF_VERTEXBUFFER); 
	m_pD3DDevice->SetTexture(0,m_pTexture); 
	m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2); 
     
    m_pD3DDevice->SetStreamSource(0,m_pIconBuffer,0,sizeof(VERTEX)); 
	m_pD3DDevice->SetFVF(OPEN_D3DFVF_VERTEXBUFFER); 
	int k=0;int j=0; 
	for(int i=0; iSetTexture(0,m_pTx[i]);//m_ArrayOfTx[k+1] 
		   m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1,D3DTA_TEXTURE); 
           
		   m_pD3DDevice->SetTexture(1,m_ArrayOfTx[k+1]);//m_ArrayOfTx[k+1] 
		   m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1,D3DTA_CURRENT); 
		  
 
           m_pD3DDevice->SetTexture(2,m_ArrayOfTx[k]); 
		   m_pD3DDevice->SetTextureStageState(2, D3DTSS_COLORARG1,D3DTA_CURRENT); 
		} 
		else 
		{ 
		if(ButtonPress[i]==false) 
		{ 
	     m_pD3DDevice->SetTexture(0,m_ArrayOfTx[k]); 
		//m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); 
		m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1,D3DTA_TEXTURE); 
		//m_pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_DISABLE);  
 
		m_pD3DDevice->SetTexture(1,m_ArrayOfTx[k+1]);//m_ArrayOfTx[k+1] 
		//m_pD3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_SELECTARG1); 
		m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1,D3DTA_CURRENT); 
		//m_pD3DDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,D3DTOP_DISABLE);  
         
		} 
		else 
		{ 
 
		m_pD3DDevice->SetTexture(0,m_ArrayOfTx[k+1]);//m_ArrayOfTx[k+1] 
		//m_pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1); 
		m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1,D3DTA_TEXTURE); 
		//m_pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_DISABLE);  
 
        m_pD3DDevice->SetTexture(1,m_ArrayOfTx[k]); 
		//m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1); 
		m_pD3DDevice->SetTextureStageState(1, D3DTSS_COLORARG1,D3DTA_CURRENT); 
		//m_pD3DDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,D3DTOP_DISABLE);  
 
 
		} 
		} 
	m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,j,2); 
	k+=2; 
	j+=4; 
	} 
	 
	 
} 
 
void COpen::Translate(int x,int y) 
{ 
D3DXMATRIX matMove; 
 
	 
 
	D3DXMatrixTranslation(&matMove, (float)x, -(float)y, 0.0f); 
	 
	m_pD3DDevice->SetTransform(D3DTS_WORLD, &matMove); 
} 
 
void COpen::Update() 
{ 
 
    memcpy(&m_MouseStateOld,&m_MouseState,sizeof(m_MouseState)); 
	if(DI_OK!=m_pMouseInput->GetDeviceState(sizeof(m_MouseState),&m_MouseState)) 
	{ 
		memset(&m_MouseState,0,sizeof(m_MouseState)); 
		m_pMouseInput->Acquire(); 
	} 
     
} 
void COpen::Control(bool * fuck) 
{     
     
	GetCursorPos(&m_Point); 
    float x;float y; 
     //ScreenToClient(hWnd,&m_Point); 
	 x=(float)m_Point.x-5; 
     y=(600)-(float)m_Point.y; 
	   for(int i=0;i=NumOfPos[i].ltx)&(x<=NumOfPos[i].rbx)) 
		{ 
			if((y>=NumOfPos[i].rby)&(y<=NumOfPos[i].lty)) 
			{ 
               
			  if(0x80&m_MouseState.rgbButtons[0]) 
		       
			  { ButtonPress[i]=true; 
			   mygod[i]=true; 
			   present[i]=false; 
               m_pSound->PlaySound("./res/sound_2.wav"); 
			  //*fuck=false; 
			  } 
			  else 
			  { 
                present[i]=true; 
                ButtonPress[i]=false; 
				 
				if(motherfuck[i]==true) 
				{fuck[i]=false;mygod[i]=false;present[i]=false;} 
			     if(mygod[i]==true) 
					{ 
					 motherfuck[i]=true; 
					 present[i]=false; 
					} 
				   
			  } 
 
			} 
			else 
			{ 
              ButtonPress[i]=false; 
			  mygod[i]=false; 
			  motherfuck[i]=false; 
              present[i]=false; 
			} 
		 
		} 
        else 
		{ 
              ButtonPress[i]=false; 
              mygod[i]=false; 
			  motherfuck[i]=false; 
              present[i]=false; 
		} 
	   } 
	 
} 
POS COpen::Calculate(RECT rect) 
{ 
	POS a; 
	a.ltx=rect.left; a.lty=rect.top; a.rtx=rect.left+rect.right; a.rty=rect.top; 
	a.lbx=rect.left; a.lby=rect.top-rect.bottom; a.rbx=rect.left+rect.right; a.rby=rect.top-rect.bottom; 
	return a; 
}