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

📄 graphics.cpp

📁 一个自己写的游戏引擎,用DirectX 写成
💻 CPP
📖 第 1 页 / 共 3 页
字号:
			{ (float)x, (float)y+Sheight, 1.2f, color, (float)SrcRect.left/width, (float)SrcRect.bottom/height },
			{ (float)x+Swidth, (float)y+Sheight, 1.2f, color, (float)SrcRect.right/width, (float)SrcRect.bottom/height }
		};
		
		m_pD3DDevice->SetTexture(0, pTexture->GetTexture());
		m_pD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, vPoint, sizeof(CustomVertex));
	}
	*/
	RenderSprite(hTex, x, y, rcSrc, color);
}

void CGraphics::RenderSprite(TextureHandle hTex, int x, int y, const RECT *rcSrc, DWORD color)
{
	
	CTexture* pTexture = m_pTextureManager->GetTexture(hTex);
	if(!pTexture)
		return;
	if(pTexture->AutoLoad())
	{
		RECT SrcRect;
		if(rcSrc)
		{
			SrcRect = *rcSrc;
		}
		else
		{
			SrcRect.left = SrcRect.top = 0;
			SrcRect.right = pTexture->GetWidth();
			SrcRect.bottom = pTexture->GetHeight();
		}
		D3DXVECTOR2 vPos((float)x, (float)y);
		m_pSprite->Draw(pTexture->GetTexture(), &SrcRect, NULL, NULL, 0.0f, &vPos, color);
	}
}

void CGraphics::Render(TextureHandle hTex, const RECT *rcDst, const RECT *rcSrc, DWORD color)
{
	CTexture* pTexture = m_pTextureManager->GetTexture(hTex);
	if(!pTexture)
		return;
	if(pTexture->AutoLoad())
	{
		RECT SrcRect;
		if(rcSrc)
		{
			SrcRect = *rcSrc;
		}
		else
		{
			SrcRect.left = SrcRect.top = 0;
			SrcRect.right = pTexture->GetWidth();
			SrcRect.bottom = pTexture->GetHeight();
		}
		float width = (float)pTexture->GetWidth();
		float height = (float)pTexture->GetHeight();
		CustomVertex vPoint[4] = 
		{
			{ (float)rcDst->left, (float)rcDst->top, 0.0f, color, (float)SrcRect.left/width, (float)SrcRect.top/height },
			{ (float)rcDst->right, (float)rcDst->top, 0.0f, color, (float)SrcRect.right/width, (float)SrcRect.top/height },
			{ (float)rcDst->left, (float)rcDst->bottom, 0.0f, color, (float)SrcRect.left/width, (float)SrcRect.bottom/height },
			{ (float)rcDst->right, (float)rcDst->bottom, 0.0f, color, (float)SrcRect.right/width, (float)SrcRect.bottom/height }
		};
		m_pD3DDevice->SetTexture(0, pTexture->GetTexture());
		m_pD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, vPoint, sizeof(CustomVertex));
	}
}

void CGraphics::Render(TextureHandle hTex, int x, int y, float xScale, float yScale, const RECT *rcSrc, DWORD color)
{
	CTexture* pTexture = m_pTextureManager->GetTexture(hTex);
	if(!pTexture)
		return;
	if(pTexture->AutoLoad())
	{
		RECT SrcRect;
		if(rcSrc)
		{
			SrcRect = *rcSrc;
		}
		else
		{
			SrcRect.left = SrcRect.top = 0;
			SrcRect.right = pTexture->GetWidth();
			SrcRect.bottom = pTexture->GetHeight();
		}
		float width = (float)pTexture->GetWidth();
		float height = (float)pTexture->GetHeight();
		if(xScale<0.0f)
			x += (int)((-xScale)*width);
		if(yScale<0.0f)
			y += (int)((-yScale)*height);
		CustomVertex vPoint[4] = 
		{
			{ (float)x, (float)y, 0.0f, color, (float)SrcRect.left/width, (float)SrcRect.top/height },
			
			{ (float)x+width*xScale, (float)y, 0.0f, color, (float)SrcRect.right/width, (float)SrcRect.top/height },
			{ (float)x, (float)y+height*yScale, 0.0f, color, (float)SrcRect.left/width, (float)SrcRect.bottom/height },
			{ (float)x+width*xScale, (float)y+height*yScale, 0.0f, color, (float)SrcRect.right/width, (float)SrcRect.bottom/height }
		};
		m_pD3DDevice->SetTexture(0, pTexture->GetTexture());
		m_pD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, vPoint, sizeof(CustomVertex));
	}
}

void CGraphics::RenderSprite(DWORD hTex, const RECT *rcDst, const RECT *rcSrc /* = NULL */, 
							 DWORD color /* = 0xffffffff */)
{
	CTexture* pTexture = m_pTextureManager->GetTexture(hTex);
	if(!pTexture)
		return;
	if(pTexture->AutoLoad())
	{
		RECT SrcRect;
		if(rcSrc)
		{
			SrcRect = *rcSrc;
		}
		else
		{
			SrcRect.left = SrcRect.top = 0;
			SrcRect.right = pTexture->GetWidth();
			SrcRect.bottom = pTexture->GetHeight();
		}
		D3DXVECTOR2 vPos((float)rcDst->left, (float)rcDst->top);
		D3DXVECTOR2 vScale((float)(rcDst->right-rcDst->left)/(SrcRect.right-SrcRect.left), 
			(float)(rcDst->bottom-rcDst->top)/(SrcRect.bottom-SrcRect.top));
		m_pSprite->Draw(pTexture->GetTexture(), &SrcRect, &vScale, NULL, 0.0f, &vPos, color);
	}
}

void CGraphics::RenderAnimation(FrameHandle hFrm, DWORD index, const RECT *rcDst, 
								const RECT *rcSrc /* = NULL */, DWORD color /* = 0xffffffff */)
{
	CTexture* pTexture = m_pFrameManager->GetTexture(hFrm, index);
	if(!pTexture)
		return;

	RECT SrcRect;
	if(rcSrc)
	{
		SrcRect = *rcSrc;
	}
	else
	{
		SrcRect.left = SrcRect.top = 0;
		SrcRect.right = pTexture->GetWidth();
		SrcRect.bottom = pTexture->GetHeight();
	}
	D3DXVECTOR2 vPos((float)rcDst->left, (float)rcDst->top);
	D3DXVECTOR2 vScale((float)(rcDst->right-rcDst->left)/(SrcRect.right-SrcRect.left), 
		(float)(rcDst->bottom-rcDst->top)/(SrcRect.bottom-SrcRect.top));
	m_pSprite->Draw(pTexture->GetTexture(), &SrcRect, &vScale, NULL, 0.0f, &vPos, color);
}

void CGraphics::RenderAnimation(FrameHandle hFrm, DWORD index, int x, int y, 
								float xScale, float yScale, const RECT *rcSrc /* = NULL */, 
								DWORD color /* = 0xffffffff */)
{
	CTexture* pTexture = m_pFrameManager->GetTexture(hFrm, index);
	if(!pTexture)
		return;

	RECT SrcRect;
	if(rcSrc)
	{
		SrcRect = *rcSrc;
	}
	else
	{
		SrcRect.left = SrcRect.top = 0;
		SrcRect.right = pTexture->GetWidth();
		SrcRect.bottom = pTexture->GetHeight();
	}
	float width = (float)pTexture->GetWidth();
	float height = (float)pTexture->GetHeight();
	if(xScale<0.0f)
		x += (int)((-xScale)*width);
	if(yScale<0.0f)
		y += (int)((-yScale)*height);

	D3DXVECTOR2 vPos((float)x, (float)y);
	D3DXVECTOR2 vScale(xScale, yScale);
	m_pSprite->Draw(pTexture->GetTexture(), &SrcRect, &vScale, NULL, 0.0f, &vPos, color);
	/*
	CustomVertex vPoint[4] = 
	{
		{ (float)x, (float)y, 0.0f, color, (float)SrcRect.left/width, (float)SrcRect.top/height },
		{ (float)x+width*xScale, (float)y, 0.0f, color, (float)SrcRect.right/width, (float)SrcRect.top/height },
		{ (float)x, (float)y+height*yScale, 0.0f, color, (float)SrcRect.left/width, (float)SrcRect.bottom/height },
		{ (float)x+width*xScale, (float)y+height*yScale, 0.0f, color, (float)SrcRect.right/width, (float)SrcRect.bottom/height }
	};
	m_pD3DDevice->SetTexture(0, pTexture->GetTexture());
	m_pD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, vPoint, sizeof(CustomVertex));
	*/
}

void CGraphics::Point(int x, int y, DWORD color)
{
	CustomVertex vPoint = { (float)x, (float)y, 0.0f, color, 0.0f, 0.0f };
	m_pD3DDevice->SetTexture(0, NULL);
	m_pD3DDevice->DrawPrimitiveUP(D3DPT_POINTLIST, 1, &vPoint, sizeof(CustomVertex));
}

void CGraphics::Line(int x1, int y1, int x2, int y2, DWORD color)
{
	CustomVertex vPoint[2] = 
	{
		{ (float)x1, (float)y1, 0.0f, color, 0.0f, 0.0f },
		{ (float)x2, (float)y2, 0.0f, color, 0.0f, 0.0f }
	};

	m_pD3DDevice->SetTexture(0, NULL);
	m_pD3DDevice->DrawPrimitiveUP(D3DPT_LINELIST, 1, vPoint, sizeof(CustomVertex));
}

void CGraphics::Rectangle(RECT rc, DWORD color, bool bFill)
{
	if(bFill)
	{
		m_pD3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
		CustomVertex vPoint[4] = 
		{
			{ (float)rc.left, (float)rc.top, 0.0f, color, 0.0f, 0.0f },
			{ (float)rc.right, (float)rc.top, 0.0f, color, 0.0f, 0.0f },
			{ (float)rc.left, (float)rc.bottom, 0.0f, color, 0.0f, 0.0f },
			{ (float)rc.right, (float)rc.bottom, 0.0f, color, 0.0f, 0.0f }
		};
		m_pD3DDevice->SetTexture(0, NULL);
		m_pD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, vPoint, sizeof(CustomVertex));
	}
	else
	{
		//m_pD3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
		CustomVertex vPoint[5] = 
		{
			{ (float)rc.left, (float)rc.top, 0.0f, color, 0.0f, 0.0f },
			{ (float)rc.right, (float)rc.top, 0.0f, color, 0.0f, 0.0f },
			{ (float)rc.right, (float)rc.bottom, 0.0f, color, 0.0f, 0.0f },
			{ (float)rc.left, (float)rc.bottom, 0.0f, color, 0.0f, 0.0f },
			{ (float)rc.left, (float)rc.top, 0.0f, color, 0.0f, 0.0f }
		};

		m_pD3DDevice->SetTexture(0, NULL);
		m_pD3DDevice->DrawPrimitiveUP(D3DPT_LINESTRIP, 4, vPoint, sizeof(CustomVertex));
	}
}

void CGraphics::Circle(int x0, int y0, int r, DWORD color)
{
	int x = 0;
	int y = r;
	long p = 3 - 2*r;
	CustomVertex vPoint[5000];
	ZeroMemory(vPoint, sizeof(vPoint));
	int pos = 0;
	while(x<=y)
	{
		if(p>=0)
		{
			p += (((x-y)<<2) + 10);
			y--;
		}
		else
		{
			p += ((x<<2) + 6);
		}

		vPoint[pos+0].x = (float)x0+x;
		vPoint[pos+0].y = (float)y0+y;
		vPoint[pos+0].color = color;
		//vPoint[pos+0].rhw = 1.0f;
		vPoint[pos+1].x = (float)x0-x;
		vPoint[pos+1].y = (float)y0-y;
		vPoint[pos+1].color = color;
		//vPoint[pos+1].rhw = 1.0f;
		vPoint[pos+2].x = (float)x0+y;
		vPoint[pos+2].y = (float)y0+x;
		vPoint[pos+2].color = color;
		//vPoint[pos+2].rhw = 1.0f;
		vPoint[pos+3].x = (float)x0-y;
		vPoint[pos+3].y = (float)y0-x;
		vPoint[pos+3].color = color;
		//vPoint[pos+3].rhw = 1.0f;
		vPoint[pos+4].x = (float)x0-x;
		vPoint[pos+4].y = (float)y0+y;
		vPoint[pos+4].color = color;
		//vPoint[pos+4].rhw = 1.0f;
		vPoint[pos+5].x = (float)x0+x;
		vPoint[pos+5].y = (float)y0-y;
		vPoint[pos+5].color = color;
		//vPoint[pos+5].rhw = 1.0f;
		vPoint[pos+6].x = (float)x0-y;
		vPoint[pos+6].y = (float)y0+x;
		vPoint[pos+6].color = color;
		//vPoint[pos+6].rhw = 1.0f;
		vPoint[pos+7].x = (float)x0+y;
		vPoint[pos+7].y = (float)y0-x;
		vPoint[pos+7].color = color;
		//vPoint[pos+7].rhw = 1.0f;

		pos += 8; 
		x++;
	}
	m_pD3DDevice->SetTexture(0, NULL);
	m_pD3DDevice->DrawPrimitiveUP(D3DPT_POINTLIST, pos, vPoint, sizeof(CustomVertex));
	// Solid Fill
	//m_pD3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
	//m_pD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, pos/3, vPoint, sizeof(CustomVertex));
}

void CGraphics::DrawText(int x, int y, DWORD color, const char *szInfo, ...)
{
	/*
	char szText[1024];

	va_list va;
	va_start(va, szInfo);
	vsprintf(szText, szInfo, va);
	va_end(va);
	m_pFont->Begin();
	RECT rc = { x, y, x + strlen(szText)*20, y + 20 };
	m_pFont->DrawText(szText, -1, &rc, 0, color);
	m_pFont->End();
	*/
}

void CGraphics::DrawCaption(int xCenter, int yCenter, DWORD color, const char *szInfo, ...)
{
	char szText[256];

	va_list va;
	va_start(va, szInfo);
	vsprintf(szText, szInfo, va);
	va_end(va);
//	m_SystemFont.DrawSingleLine(xCenter, yCenter, color, true, szText);
}

// end here

⌨️ 快捷键说明

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