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

📄 cuboid.cpp

📁 3D俄罗斯方块源码.rar
💻 CPP
字号:
/********************************************************************
文件名: CUBOID.cpp
Copyright (c)2004  红孩儿工作室
创建人  : 卞安
创建时间: 2004-5-18 12:33:30
版本: 1.0
类名: CCuboid
说明:
	正方体类
********************************************************************/
#include "Cuboid.h"
/********************************************************************
函数名  : 构造函数
输入参数: 
	pD3DDevice:D3D设备
输出参数: 
功能描述:
	初始化变量,创建顶点缓冲,填充顶点缓冲
全局变量: 无
调用模块:
********************************************************************/
CCuboid::CCuboid(LPDIRECT3DDEVICE8 pD3DDevice)
{
	m_pD3DDevice = pD3DDevice;
	m_pVertexBuffer = NULL;
	m_pTexture = NULL;
	m_rWidth = 10.0;
	m_rHeight = 10.0;
	m_rDepth = 10.0;
	m_rX = 0.0;
	m_rY = 0.0;
	m_rZ = 0.0;

    if(SUCCEEDED(CreateVertexBuffer()))
	{
		UpdateVertices();
	}
}
/********************************************************************
函数名  : 析构函数
输入参数: 
输出参数: 
功能描述:
	删除指针并释放变量
全局变量: 无
调用模块:
    CreateVertexBuffer()
    UpdateVertices();
********************************************************************/
CCuboid::~CCuboid()
{
	SafeRelease(m_pTexture);
	SafeRelease(m_pVertexBuffer);
}
/********************************************************************
函数名  : Render
输入参数: 
输出参数: 
	TRUE	成功
	FALSE	失败
功能描述:
	渲染正方体
全局变量: 无
调用模块:
********************************************************************/
bool CCuboid::Render()
{
	m_pD3DDevice->SetStreamSource(0, m_pVertexBuffer, sizeof(CUBIOD_CUSTOMVERTEX));
    m_pD3DDevice->SetVertexShader(CUBIOD_D3DFVF_CUSTOMVERTEX);
	if(m_pTexture != NULL)
	{
        m_pD3DDevice->SetTexture(0, m_pTexture);
		m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
    }
	else
	{
		m_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
	}

    m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);		
	m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 8);		
	m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 14, 2);	

	return true;
}
/********************************************************************
函数名  : CreateVertexBuffer()
输入参数: 
输出参数: 
	S_OK	成功
	E_FAIL	失败
功能描述:
	创建顶点缓冲
全局变量: 无
调用模块:
********************************************************************/
HRESULT CCuboid::CreateVertexBuffer()
{
    if(FAILED(m_pD3DDevice->CreateVertexBuffer(18 * sizeof(CUBIOD_CUSTOMVERTEX),
                                               0, CUBIOD_D3DFVF_CUSTOMVERTEX,
                                               D3DPOOL_DEFAULT, &m_pVertexBuffer)))
    {
        return E_FAIL;
    }

    return S_OK;
}
/********************************************************************
函数名  : UpdateVertices()
输入参数: 
输出参数: 
	TRUE	成功
	FALSE	失败
功能描述:
	填充顶点缓冲
全局变量: 无
调用模块:
********************************************************************/
bool CCuboid::UpdateVertices()
{
	VOID* pVertices;
    CUBIOD_CUSTOMVERTEX cvVertices[] =
    {	
	
		{m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255), 0.0f, 1.0f,},		//Vertex 0 - Blue 
        {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0), 0.0f, 0.0f,},		//Vertex 1 - Red 
        {m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0), 1.0f, 1.0f,},		//Vertex 2 - Red 
		{m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0), 1.0f, 0.0f,},		//Vertex 3 - Green 

	
		{m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0), 0.0f, 1.0f,},		//Vertex 4 - Red 
        {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255), 0.0f, 0.0f,},		//Vertex 5 - Blue 
        {m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0), 1.0f, 1.0f,},		//Vertex 6 - Green 
		{m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0), 1.0f, 0.0f,},		//Vertex 7 - Red 

	
		{m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255), 0.0f, 1.0f,},		//Vertex 8 - Blue 
        {m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0), 0.0f, 0.0f,},		//Vertex 9 - Green
		
	
        {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0), 1.0f, 1.0f,},		//Vertex 10 - Green 
		{m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0), 1.0f, 0.0f,},		//Vertex 11 - Red 

	
        {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0), 0.0f, 1.0f,},		//Vertex 12 - Red 
        {m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255), 0.0f, 0.0f,},		//Vertex 13 - Blue

	
		{m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0), 0.0f, 1.0f,},		//Vertex 14 - Green 
        {m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 0, 255), 0.0f, 0.0f,},		//Vertex 15 - Blue 
        {m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), D3DCOLOR_XRGB(255, 0, 0), 1.0f, 1.0f,},		//Vertex 16 - Red 
		{m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), D3DCOLOR_XRGB(0, 255, 0), 1.0f, 0.0f,},		//Vertex 17 - Green
    };


    if(FAILED(m_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE**)&pVertices, 0)))
    {
        return false;
    }


    memcpy(pVertices, cvVertices, sizeof(cvVertices));


    m_pVertexBuffer->Unlock();

	return true;
}
/********************************************************************
函数名  : SetSize
输入参数: 
	rWidth:宽
    rHeight:高
    rDepth:深
输出参数: 
	TRUE	成功
	FALSE	失败
功能描述:
	设置正方体大小
全局变量: 无
调用模块:UpdateVertices()
********************************************************************/
bool CCuboid::SetSize(float rWidth, float rHeight, float rDepth)
{
	m_rWidth = rWidth;
	m_rHeight = rHeight;
	m_rDepth = rDepth;

	UpdateVertices();

	return true;
}
/********************************************************************
函数名  : SetPosition
输入参数: 
	x:x轴位置
	y:Y轴位置
	z:Z轴位置
输出参数: 
	TRUE	成功
	FALSE	失败
功能描述:
	设置正方体位置
全局变量: 无
调用模块:UpdateVertices()
********************************************************************/
bool CCuboid::SetPosition(float x, float y, float z)
{
	m_rX = x;
	m_rY = y;
	m_rZ = z;

	UpdateVertices();

	return true;
}
/********************************************************************
函数名  : SetTexture
输入参数: 
	szTextureFilePath:纹理图
输出参数: 
	TRUE	成功
	FALSE	失败
功能描述:
	设置正方体纹理图
全局变量: 无
调用模块:
********************************************************************/
bool CCuboid::SetTexture(const char *szTextureFilePath)
{
	if(FAILED(D3DXCreateTextureFromFile(m_pD3DDevice, szTextureFilePath, &m_pTexture)))
	{
		return false;
	}

	return true;
}

⌨️ 快捷键说明

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