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

📄 directx8visualsystem.cpp

📁 game code对于游戏地图的编辑代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	else
	{
		tss = D3DTSS_ALPHAARG1;
		color = D3DXCOLOR(0.0,0.0,0.0,mode.alphaArg1);
	}
	if(mode.source1 = LBS_MANUAL)
	{

		if( FAILED( m_pd3dDevice->SetRenderState(D3DRS_TEXTUREFACTOR,color)))
		{
			LOGLOW("DirectX8VisualSystem::_setTextureBlendMode : Cannot set Arg1 factor");
			return;
		}
	}
	if( FAILED( m_pd3dDevice->SetTextureStageState(stage,tss,D3DMapping::Get(mode.source1))))
	{
		LOGLOW("DirectX8VisualSystem::_setTextureBlendMode : Cannot set Arg1");
		return;
	}
	//设置Arg2
	if(mode.blendType == LBT_COLOUR)
	{
		tss= D3DTSS_COLORARG2;
		color = ColorToD3DXCOLOR(mode.colourArg2);
	}
	else
	{
		tss = D3DTSS_ALPHAARG2;
		color = D3DXCOLOR(0,0,0,mode.alphaArg2);
	}
	if( mode.source2 == LBS_MANUAL)
	{
		if( FAILED( m_pd3dDevice->SetRenderState(D3DRS_TEXTUREFACTOR,color)))
		{
			LOGLOW("DirectX8VisualSystem::_setTextureBlendMode : Cannot set Arg2 factor");
			return;
		}
	}
	if( FAILED( m_pd3dDevice->SetTextureStageState(stage,tss,D3DMapping::Get(mode.source2))))
	{
		LOGLOW("DirectX8VisualSystem::_setTextureBlendMode : Cannot set Arg2");
		return;
	}
}
void DirectX8VisualSystem::_setTextureFilter(Int32 stage,TexFilterOptions tfo)
{
	LOGLOW("DirectX8VisualSystem::_setTextureFilter : Cannot set TextureFilter");
}
void DirectX8VisualSystem::SetShadingType(ShadingOptions os)
{
	if( FAILED( m_pd3dDevice->SetRenderState(D3DRS_SHADEMODE,D3DMapping::Get(os) )))
	{
		LOGLOW("DirectX8VisualSystem::_setShadingType : Cannot set ShadeMode");
	}
}
void DirectX8VisualSystem::SetCullingMode(CullingMode cm)
{
	m_CullingMode = cm;
	if( FAILED( m_pd3dDevice->SetRenderState(D3DRS_CULLMODE,D3DMapping::Get(cm) )))
	{
		LOGLOW("DirectX8VisualSystem::_setCullingMode : Cannot set CullMode");
	}
}

void DirectX8VisualSystem::_setMaterial(Material* material)
{
}
/************************************************************************/
/* 模版设置															    */
/************************************************************************/
void DirectX8VisualSystem::_setStencilCheckEnabled(Bool enable)
{
	if( FAILED( m_pd3dDevice->SetRenderState(D3DRS_STENCILENABLE , enable) ))
	{
		LOGLOW("DirectX8VisualSystem::_setStencilCheckEnabled : Cannot enable/disable stencil");
	}
}
void DirectX8VisualSystem::_setStencilMask(Int32 mask)
{
	if( FAILED( m_pd3dDevice->SetRenderState(D3DRS_STENCILMASK,mask) ))
	{
		LOGLOW("DirectX8VisualSystem::_setStencilMask : Cannot set the Stencil Mask");
	}
}
void DirectX8VisualSystem::_setStencilBufferFunction(CompareFunction func)
{
	HRESULT hr = m_pd3dDevice->SetRenderState(D3DRS_STENCILFUNC,D3DMapping::Get(func));
	if( FAILED(hr))
		LOGLOW("DirectX8VisualSystem::_setStencilBufferFunction : Cannot set Stencil test func");
}
void DirectX8VisualSystem::_setStencilBufferReferenceValue(Int32 refvalue )
{
	HRESULT hr = m_pd3dDevice->SetRenderState(D3DRS_STENCILREF,refvalue);
	if( FAILED(hr) )
		LOGLOW("DirectX8VisualSystem::_setStencilBufferReferenceValue : Cannot set Stencil ref Value");
}
void DirectX8VisualSystem::_setStencilBufferDepthFailOperation(StencilOperation op)
{
	if( FAILED( m_pd3dDevice->SetRenderState(D3DRS_STENCILZFAIL,D3DMapping::Get(op) )))
	{
		LOGLOW("DirectX8VisualSystem::_setStencilBufferDepthFailOperation : Cannot set Stencil pass And Z fail Opertaion");
	}
}
void DirectX8VisualSystem::_setStencilBufferFailOperation(StencilOperation op)
{
	HRESULT hr = m_pd3dDevice->SetRenderState(D3DRS_STENCILFAIL,D3DMapping::Get(op) );
	if( FAILED(hr))
		LOGLOW("DirectX8VisualSystem::_setStencilBufferFailOperation : Cannot set StencilFail Operation");
}
void DirectX8VisualSystem::_setStencilBufferPassOperation(StencilOperation op)
{
	HRESULT hr = m_pd3dDevice->SetRenderState(D3DRS_STENCILPASS,D3DMapping::Get(op) );
	if( FAILED(hr) )
		LOGLOW("DirectX8VisualSystem::_setStencilBufferPassOperation : Cannot set StencilPass Operation ");
}
/************************************************************************/
/*  深度缓冲															*/
/************************************************************************/
void DirectX8VisualSystem::_setDepthBufferCheckEnabled(Bool enable)
{
	HRESULT hr;
	if( enable )
	{
		if( D3DPRASTERCAPS_WBUFFER & m_Caps.RasterCaps )
			hr = m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,D3DZB_USEW );
		else
			hr = m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,D3DZB_TRUE );
	}
	else 
		hr = m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,D3DZB_FALSE );
	if( FAILED(hr) )
		LOGLOW("DirectX8VisualSystem::_setDepthBufferCheckEnabled : Cannot setDepthBufferCheckEnable");
}
void DirectX8VisualSystem::_setDepthBufferWriteEnabled(Bool enable)
{
	HRESULT hr = m_pd3dDevice->SetRenderState(D3DRS_ZWRITEENABLE,enable);
	if( FAILED(hr) )
		LOGLOW("DirectX8VisualSystem::_setDepthBufferWriteEnabled : Cannot set DepthBufferWriteEnable State");
}
void DirectX8VisualSystem::_setDepthTestFunc(CompareFunction depthFunc /* = CMPF_LESS_EQUAL */)
{
	HRESULT hr = m_pd3dDevice->SetRenderState(D3DRS_ZFUNC,D3DMapping::Get(depthFunc));
	if( FAILED(hr) )
		LOGLOW("DirectX8VisualSystem::_setDepthTestFunc : Cannot setDepthTestFunc");
}
void DirectX8VisualSystem::_setDepthBias(Int16 bias)
{
	HRESULT hr = m_pd3dDevice->SetRenderState(D3DRS_ZBIAS,bias);
	if( FAILED(hr) )
		LOGLOW("DirectX8VisualSystem::_setDepthBias : Cannot setDepthBias");
}

Bool DirectX8VisualSystem::SetViewPort(ViewPort *vp)
{
	if( vp != m_ActiveViewPort || vp->_isUpdated() )
	{
		m_ActiveViewPort = vp;
		D3DVIEWPORT8 d3dvp;

		d3dvp.X = vp->getLeft();
		d3dvp.Y = vp->getTop();
		d3dvp.Width = vp->getWidth();
		d3dvp.Height= vp->getHeight();
		d3dvp.MinZ = 0.0f;
		d3dvp.MaxZ = 1.0f;
		HRESULT hr;
		if( FAILED(hr=m_pd3dDevice->SetViewport(&d3dvp)) )
		{
			String str = DXGetErrorDescription8(hr);
			LOGLOW("DirectX8VisualSystem::_setViewPort : Cannot setViewPort");
			return false;
		}
		vp->_clearUpdatedFlag();
	}
	return true;
}

void DirectX8VisualSystem::BeginFrame()
{
	AUTOGUARD("DirectX8VisualSystem::_beginFrame");
	if( !m_ActiveViewPort )
		LOGLOW("DirectX8VisualSystem::_beginFrame : no active viewport found");
	HRESULT hr;
	if( FAILED( hr=m_pd3dDevice->BeginScene()))
	{
		String msg = DXGetErrorDescription8(hr);
		LOGLOW(String("DirectX8VisualSystem::_beginFrame : Cannot BeginScene : "+msg));
		ThrowException(ERR_VISUALSYS_ERROR,"Cannot BegineScene : "+msg);
	}
}
void DirectX8VisualSystem::EndFrame()
{
	AUTOGUARD("DirectX8VisualSystem::_endFrame");
	if( FAILED(m_pd3dDevice->EndScene()))
	{
		LOGLOW("DirectX8VisualSystem::_beginFrame : Cannot EndScene");
		ThrowException(ERR_VISUALSYS_ERROR,"Cannot BegineScene");
	}
}void DirectX8VisualSystem::Clear(ClearMode clearmode/* =CLEAR_TARGET */,
								  Rect* rect/* =0 */,
								  Color color/* =0 */,
								  Real32 Z /* = 1.0f */,
								  Int32 stencil/* =0 */)
{
	D3DRECT tmp;
	D3DRECT* r=0;
	if( rect )
	{
		tmp = RectToD3DRECT(*rect);
		r = &tmp;
	}
	HRESULT hr = m_pd3dDevice->Clear(0,r,D3DMapping::Get(clearmode),color,Z,stencil);
	if( FAILED(hr) )
		LOGLOW("DirectX8VisualSystem::_clear : Cannot clear");
}
void DirectX8VisualSystem::_setRasteriseMode(SceneDetailLevel sl)
{
	HRESULT hr = m_pd3dDevice->SetRenderState(D3DRS_FILLMODE,D3DMapping::Get(sl));
	if( FAILED(hr) )
		LOGLOW("DirectX8VisualSystem::_setRasteriseMode : Cannot setRasteriseMode");
}
Bool DirectX8VisualSystem::_softwareVertexBlend(RenderOperation* ro, Matrix4* pMatrices)
{
	return true;
}

void DirectX8VisualSystem::SetProjectionMatrix(const Matrix4& mx)
{
	D3DXMATRIX mat;
	Matrix4ToD3DXMATRIX(mx,mat);
	HRESULT hr = m_pd3dDevice->SetTransform(D3DTS_PROJECTION,&mat);
	if( FAILED(hr) )
		LOGLOW("DirectX8VisualSystem::_setProjectionMatrix : Cannot setProjectionMatrix");
}
void DirectX8VisualSystem::SetProjectionMatrix(Real32 FOV,Real32 aspect,Real32 fz,Real32 bz)
{
	D3DXMATRIX mat;
	D3DXMatrixPerspectiveFovLH(&mat,FOV,aspect,fz,bz);
	HRESULT hr = m_pd3dDevice->SetTransform(D3DTS_PROJECTION,&mat);
	if( FAILED(hr) )
		LOGLOW("DirectX8VisualSystem::_setProjectionMatrix : Cannot setProjectionMatrix");
}
void DirectX8VisualSystem::SetViewMatrix(const Matrix4& mx)
{
	D3DXMATRIX mat;
	Matrix4ToD3DXMATRIX(mx,mat);
	HRESULT hr = m_pd3dDevice->SetTransform(D3DTS_VIEW,&mat);
	if( FAILED(hr) )
		LOGLOW("DirectX8VisualSystem::_setViewMatrix : Cannot setViewMatrix");
}
void DirectX8VisualSystem::SetViewMatrix(Vector3 from,Vector3 lookat,Vector3 up)
{
	D3DXMATRIX matView;
	D3DXVECTOR3 vFromPt   = D3DXVECTOR3( from.x, from.y, from.z );
	D3DXVECTOR3 vLookatPt = D3DXVECTOR3( lookat.x, lookat.y, lookat.z );
	D3DXVECTOR3 vUpVec    = D3DXVECTOR3( up.x, up.y, up.z );
    D3DXMatrixLookAtLH( &matView, &vFromPt, &vLookatPt, &vUpVec );
	HRESULT hr = m_pd3dDevice->SetTransform(D3DTS_VIEW,&matView);
	if( FAILED(hr) )
		LOGLOW("DirectX8VisualSystem::_setViewMatrix : Cannot setViewMatrix");
}
void DirectX8VisualSystem::SetWorldMatrix(const Matrix4& mx)
{
	D3DXMATRIX mat;
	Matrix4ToD3DXMATRIX(mx,mat);
	HRESULT hr=m_pd3dDevice->SetTransform(D3DTS_WORLD,&mat);
	if( FAILED(hr) )
		LOGLOW("DirectX8VisualSystem::_setWorldMatrix : Cannot setWorldMatrix");
}

/*
		测试

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
struct CUSTOMVERTEX
{
	float x,y,z;
	D3DCOLOR color;
};
CUSTOMVERTEX g_Verteices[]=
{
	{ -0.5,-0.5,0,0x00ffffff},
	{ 0.5 ,-0.5,0,0x00ffffff},
	{ 0,   0.5, 0,0x00ffffff},
};
*/

Bool DirectX8VisualSystem::Render(RenderOperation* ro)
{
/*		测试
	m_pd3dDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);
	m_pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST,1,g_Verteices,sizeof(CUSTOMVERTEX));
	return true;
*/
	if( !IVisualSys::Render(ro) )
		return false;

	// 设置数据流
	vector<VertexData*>::iterator it = ro->vertexData.begin();
	for(Int32 i=0 ;it!=ro->vertexData.end();++it,++i )
	{
		D3D8VertexBuffer* vb = (D3D8VertexBuffer*)HardwareBufferManager::Instance().GetVertexBuffer(*it);
		HRESULT hr = m_pd3dDevice->SetStreamSource(i,vb->GetBuffer(),GetVDTSize((*it)->vertex_type));
		if( FAILED(hr) )
		{
			LOGLOW("DirectX8VisualSystem::Render : Cannot set StreamSource");
			return false;
		}
	}

	// 设置 VertexDeclaration
	Int32 vd = HardwareBufferManager::Instance().GetVertexDeclaration(ro);
	if( vd ==0xFFFFFFFF || FAILED( m_pd3dDevice->SetVertexShader(vd) ))
	{
		LOGLOW("DirectX8VisualSystem::Render : Cannot set VertexDeclaration");
		return false;
	}

	// Count the primitive
	D3DPRIMITIVETYPE primType;
	DWORD primCount = 0;
	switch( ro->type )
	{
	case RenderOperation::OT_POINT_LIST:
		primType = D3DPT_POINTLIST;
		primCount = (DWORD)(ro->bUseIndex ? ro->indices->index_count : ro->vertex_count);
		break;

	case RenderOperation::OT_LINE_LIST:
		primType = D3DPT_LINELIST;
		primCount = (DWORD)(ro->bUseIndex ? ro->indices->index_count : ro->vertex_count ) / 2;
		break;

	case RenderOperation::OT_LINE_STRIP:
		primType = D3DPT_LINESTRIP;
		primCount = (DWORD)(ro->bUseIndex ? ro->indices->index_count : ro->vertex_count) - 1;
		break;

	case RenderOperation::OT_TRIANGLE_LIST:
		primType = D3DPT_TRIANGLELIST;
		primCount = (DWORD)(ro->bUseIndex ? ro->indices->index_count : ro->vertex_count) / 3;
		break;

	case RenderOperation::OT_TRIANGLE_STRIP:
		primType = D3DPT_TRIANGLESTRIP;
		primCount = (DWORD)(ro->bUseIndex ? ro->indices->index_count : ro->vertex_count) - 2;
		break;

	case RenderOperation::OT_TRIANGLE_FAN:
		primType = D3DPT_TRIANGLEFAN;
		primCount = (DWORD)(ro->bUseIndex ? ro->indices->index_count : ro->vertex_count) - 2;
		break;
	}
    if (!primCount)
		return false;

	// 渲染
	if( ro->bUseIndex )
	{
		D3D8IndexBuffer* buf = (D3D8IndexBuffer*)HardwareBufferManager::Instance().GetIndexBuffer(ro->indices);
		if( FAILED(m_pd3dDevice->SetIndices( buf->GetBuffer(),0 )))
		{
			LOGLOW("DirectX8VisualSystem::Render : Cannot set indexBuffer");
			return false;
		}
		if( FAILED(m_pd3dDevice->DrawIndexedPrimitive(primType,
											0,
											ro->vertex_count,
											ro->indices->index_start,
											primCount)))
		{
			LOGLOW("DirectX8VisualSystem::Render : Cannot draw Indexed Primitive");
		}
	}
	else
	{
		if( FAILED( m_pd3dDevice->DrawPrimitive(primType,ro->vertex_start,primCount)))
		{
			LOGLOW("DirectX8VisualSystem::Render : Cannot draw Primitive");
			return false;
		}
	}
	return true;
}

Bool DirectX8VisualSystem::Present(Rect* src,Rect* dest)
{
	RECT t1,t2;
	RECT *s=0,*d=0;
	if( src )
	{
		t1.left = src->x;
		t1.top  = src->y;
		t1.right= src->x+src->width;
		t1.bottom=src->y+src->height;
		s = &t1;
	}
	if( dest )
	{
		t2.left = dest->x;
		t2.top  = dest->y;
		t2.right= dest->x+src->width;
		t2.bottom=dest->y+src->height;
		d = &t2;
	}
	if( FAILED(m_pd3dDevice->Present(s,d,0,0)))
		return false;
	return true;
}

⌨️ 快捷键说明

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