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

📄 graphics.cpp

📁 一个类似坦克大战的小小游戏
💻 CPP
字号:
// Graphics.cpp: implementation of the CGraphics class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include "MapEdit.h"
#include "Graphics.h"
#include "GlobalDefs.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CGraphics::CGraphics()
{
	m_pD3D = NULL;
	m_pd3dDevice = NULL;
	m_pSprite = NULL;
}

CGraphics::~CGraphics()
{
	Release();
}

// Init //////////////////////////////////////////////////////////////

BOOL CGraphics::InitD3D(HWND inhWnd)
{
	Release();

	// Create an IDirect3D9 object and return an interace pointer
	m_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
	if (NULL == m_pD3D)
	{
		return FALSE;
	}

	// Filling in the fields of D3DPRESENT_PARAMETERS
	D3DPRESENT_PARAMETERS d3dpp; 
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

	// Create the Direct3D device
	if(FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, inhWnd,
                                  D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                  &d3dpp, &m_pd3dDevice)))
	{
		return FALSE;
	}

	// Set Render State
	m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
	m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	m_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
	m_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
	m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
	
	return TRUE;
}

// Render ////////////////////////////////////////////////////////////

void CGraphics::Render()
{
	if (NULL != m_pd3dDevice)
	{
		m_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0);

		if (SUCCEEDED(m_pd3dDevice->BeginScene()))
		{
			m_pd3dDevice->EndScene();
		}

		m_pd3dDevice->Present( NULL, NULL, NULL, NULL );
	}
}

// Release ///////////////////////////////////////////////////////////

void CGraphics::Release()
{
	if (NULL != m_pD3D)
	{
		SAFE_RELEASE(m_pD3D);
	}

	if (NULL != m_pd3dDevice)
	{
		SAFE_RELEASE(m_pd3dDevice);
	}

	if (NULL != m_pSprite)
	{
		SAFE_RELEASE(m_pSprite);
	}
}

// Get device ////////////////////////////////////////////////////////

LPDIRECT3DDEVICE9 &CGraphics::Getd3dDevice()
{
	return m_pd3dDevice;
}

// Scene /////////////////////////////////////////////////////////////

BOOL CGraphics::BeginScene()
{
	if (NULL != m_pd3dDevice)
	{
		m_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
		
		if (SUCCEEDED(m_pd3dDevice->BeginScene()))
		{
			return TRUE;
		}
	}
	return FALSE;
}

void CGraphics::EndScene()
{
	m_pd3dDevice->EndScene();

	// Presents the contents of the next buffer in the sequence of 
	// back buffers owned by the device.
	m_pd3dDevice->Present(NULL, NULL, NULL, NULL);
}

// Draw object ////////////////////////////////////////////////////////

void CGraphics::DrawObject(LPDIRECT3DTEXTURE9 &pTexture, 
						   int x, int y, int width, int height, DWORD diffuse)
{
	CUSTOMVERTEX vertex[4];
	memset(vertex, 0, sizeof(vertex));

	// Set picture position
	vertex[0].x = (float)x;
	vertex[0].y = (float)y;
	vertex[0].z = 0.5f;
	vertex[1].x = (float)(x + width);
	vertex[1].y = (float)y;
	vertex[1].z = 0.5f;
	vertex[2].x = (float)(x + width);
	vertex[2].y = (float)(y + height);
	vertex[2].z = 0.5f;
	vertex[3].x = (float)x;
	vertex[3].y = (float)(y + height);
	vertex[3].z = 0.5f;

	// Set rhw
	vertex[0].rhw = 0.5f;
	vertex[1].rhw = 0.5f;
	vertex[2].rhw = 0.5f;
	vertex[3].rhw = 0.5f;

	// Set diffuse;
	vertex[0].diffuse = diffuse;
	vertex[1].diffuse = diffuse;
	vertex[2].diffuse = diffuse;
	vertex[3].diffuse = diffuse;

	// Set UV position
	vertex[1].tu = 1.0f;
	vertex[2].tu = 1.0f;
	vertex[2].tv = 1.0f;
	vertex[3].tv = 1.0f;

	// Draw object
	m_pd3dDevice->SetTexture(0 , pTexture);
	m_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
	m_pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, (LPVOID)vertex , sizeof(CUSTOMVERTEX));
}

⌨️ 快捷键说明

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