⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 capp.cpp

📁 HeightMap_D3D
💻 CPP
字号:
//-----------------------------------------------------------------------------
// File: cApp.cpp
// Desc: This is our main file' all the program procces happens here.
//-----------------------------------------------------------------------------
#include "Main.h"




//-----------------------------------------------------------------------------
// Func: cApp()
// Desc: The Constructor of cApp class.
//-----------------------------------------------------------------------------
cApp::cApp()
{
	m_pD3D = NULL;
	m_pD3DDevice = NULL;
	m_lpD3DFont = NULL;
	cInput* m_pInput = NULL;
	cCamera* m_pCamera = NULL;
	cTerrain* m_pHeightMap = NULL;
	

	FontRect.top = 0;
	FontRect.left = 0;
	FontRect.bottom = WIN_HEIGHT;
	FontRect.right = WIN_WIDTH;
}




//-----------------------------------------------------------------------------
// Func: Init()
// Desc: Public func that tells what initialed first and what second.
//-----------------------------------------------------------------------------
HRESULT cApp::Init()
{
	if(FAILED(InitD3D()))
	{
		MessageBox( hWnd, "InitD3D() failed!", "cApp::Init()", MB_OK );
        return E_FAIL;
	}

	if(FAILED(InitFont()))
	{
		MessageBox( hWnd, "InitFont() failed!", "cApp::Init()", MB_OK );
        return E_FAIL;
	}

	if(FAILED(InitApp()))
	{
		MessageBox( hWnd, "IniteApp() failed!", "cApp::Init()", MB_OK );
        return E_FAIL;
	}

	return S_OK;
}




//-----------------------------------------------------------------------------
// Func: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT cApp::InitD3D()
{
    if( NULL == ( m_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
	{
		MessageBox( hWnd, "Direct3DCreate9() failed!", "cApp::InitD3D()", MB_OK );
        return E_FAIL;
	}

    D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = false;
	d3dpp.BackBufferCount = 1;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferWidth = WIN_WIDTH;
    d3dpp.BackBufferHeight = WIN_HEIGHT;
	d3dpp.hDeviceWindow = hWnd;
    d3dpp.EnableAutoDepthStencil = true;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
	d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
    d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;


	if( FAILED( m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &m_pD3DDevice ) ) )
    {
		MessageBox( hWnd, "CreateDevice() failed!", "cApp::InitD3D()", MB_OK );
        return E_FAIL;
    }

    m_pD3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
    m_pD3DDevice->SetRenderState( D3DRS_LIGHTING, false );
    m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
	m_pD3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);

    return S_OK;
}




//-----------------------------------------------------------------------------
// Func: InitFont()
// Desc: Initializes the font here.
//-----------------------------------------------------------------------------
HRESULT cApp::InitFont()
{
	LOGFONT logFont = {
		12, //height
		0,  //width; 
		0,  // lfEscapement; 
		0,  //lfOrientation; 
		FW_BOLD, // lfWeight; 
		FALSE, // lfItalic; 
		FALSE, // lfUnderline; 
		FALSE, // lfStrikeOut; 
		DEFAULT_CHARSET, // lfCharSet; 
		OUT_DEFAULT_PRECIS, //lfOutPrecision; 
		CLIP_DEFAULT_PRECIS, // lfClipPrecision; 
		ANTIALIASED_QUALITY,// lfQuality; 
		DEFAULT_PITCH,// lfPitchAndFamily; 
		"Arial"// lfFaceName[LF_FACESIZE]; 
    };

	if(FAILED(D3DXCreateFontIndirect( m_pD3DDevice, &logFont, &m_lpD3DFont )))
	{
		MessageBox(hWnd, "D3DXCreateFontIndirect() Failed!", "cApp::InitFont()", MB_OK);
		return E_FAIL;
	}
	return S_OK;
}




//-----------------------------------------------------------------------------
// Func: InitApp()
// Desc: Initializes App
//-----------------------------------------------------------------------------
HRESULT cApp::InitApp()
{
	m_pInput = new cInput();
	m_pCamera = new cCamera();
	m_pHeightMap = new cTerrain();
	return S_OK;
}




//-----------------------------------------------------------------------------
// Name: Render()
// Desc: This is the place that sends render commands to all the created
//		 object there are that need to be rendered.
//-----------------------------------------------------------------------------
HRESULT cApp::Render()
{
	if( m_pD3DDevice == NULL )
	{
		MessageBox( hWnd, "m_pD3DDevice == NULL", "cApp::Render()", MB_OK );
        return E_FAIL;
	}

	m_pInput->GetDeviceState();
	if(SUCCEEDED(m_pInput->BKeyDown(DIK_ESCAPE)))
		DestroyWindow(hWnd);

	m_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );

	if( SUCCEEDED( m_pD3DDevice->BeginScene() ) )
	{
		if(FAILED(m_pHeightMap->Render()))
		{
			MessageBox( hWnd, "m_pHeightMap->Render() Failed!", "cApp::Render()", MB_OK );
			return E_FAIL;
		}

		if(FAILED(m_pCamera->Render()))
		{
			MessageBox( hWnd, "m_pCamera->Render() Failed!", "cApp::Render()", MB_OK );
			return E_FAIL;
		}

		if(FAILED(m_pInput->Render()))
		{
			MessageBox( hWnd, "m_pInput->Render() Failed!", "cApp::Render()", MB_OK );
			return E_FAIL;
		}
		m_pD3DDevice->EndScene();
	}

	m_pD3DDevice->Present( NULL, NULL, NULL, NULL );
	return S_OK;
}




//-----------------------------------------------------------------------------
// Func: RenderFont()
// Desc: Rendering the font here. The function does not recieve any parameters
//       i'll add that in time.
//-----------------------------------------------------------------------------
HRESULT cApp::RenderFont(char FontBuffer[255], int Top )
{
	m_lpD3DFont->Begin();

	//sprintf(FontBuffer, "X = %d, Y = %d", m_pInput->getM_BuffX(), m_pInput->getM_BuffY() );
	FontRect.top = Top;
	m_lpD3DFont->DrawText( FontBuffer, -1, &FontRect, DT_LEFT, D3DCOLOR_XRGB(255, 255, 255));

	/*sprintf(FontBuffer, "vLookAtPt(%f, %f, %f)", m_pCamera->vLookAtPt.x, m_pCamera->vLookAtPt.y, m_pCamera->vLookAtPt.z );
	FontRect.top = 15;
	m_lpD3DFont->DrawText( FontBuffer, -1, &FontRect, DT_LEFT, D3DCOLOR_XRGB(255, 255, 255));

	sprintf(FontBuffer, "vLookAtPt(%f, %f, %f)", m_pCamera->vUpVec.x, m_pCamera->vUpVec.y, m_pCamera->vUpVec.z );
	FontRect.top = 30;
	m_lpD3DFont->DrawText( FontBuffer, -1, &FontRect, DT_LEFT, D3DCOLOR_XRGB(255, 255, 255));*/

	m_lpD3DFont->End();
	return S_OK;
}




//-----------------------------------------------------------------------------
// Func: ~cApp()
// Desc: The deconstructor of cApp class.
//-----------------------------------------------------------------------------
cApp::~cApp()
{
	SafeRelease(m_pD3D);
	SafeRelease(m_pD3DDevice);
	SafeRelease(m_lpD3DFont);
	SafeDelete(m_pInput);
	SafeDelete(m_pCamera);
	SafeDelete(m_pHeightMap);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -