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

📄 core_graphics.cpp

📁 [游戏开发参考书-用DirectX编写RPG游戏]这是一个系列的丛书如果你都看并且懂的话你就可以你工作啦!
💻 CPP
📖 第 1 页 / 共 5 页
字号:
{
  if(m_pD3DDevice == NULL)
    return FALSE;

  if(FAILED(m_pD3DDevice->LightEnable(Num, Enable)))
    return FALSE;

  return TRUE;
}

BOOL cGraphics::EnableLighting(BOOL Enable)
{
  if(m_pD3DDevice == NULL)
    return FALSE;

  if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_LIGHTING, Enable)))
    return FALSE;

  return TRUE;
}

BOOL cGraphics::EnableZBuffer(BOOL Enable)
{
  if(m_pD3DDevice == NULL || m_ZBuffer == FALSE)
    return FALSE;

  if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, (Enable == TRUE) ? D3DZB_TRUE : D3DZB_FALSE)))
    return FALSE;

  return TRUE;
}

BOOL cGraphics::EnableAlphaBlending(BOOL Enable, DWORD Src, DWORD Dest)
{
  if(m_pD3DDevice == NULL)
    return FALSE;

  // Enable or disable
  if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, Enable)))
    return FALSE;

  // Set blend type
  if(Enable == TRUE) {
    m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND,  Src);
    m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, Dest);
  }

  return TRUE;
}

BOOL cGraphics::EnableAlphaTesting(BOOL Enable)
{
  if(m_pD3DDevice == NULL)
    return FALSE;

  if(FAILED(m_pD3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, Enable)))
    return FALSE;

  // Set test type
  if(Enable == TRUE) {
    m_pD3DDevice->SetRenderState(D3DRS_ALPHAREF, 0x08);
    m_pD3DDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
  }

  return TRUE;
}

cWorldPosition::cWorldPosition()
{
  m_Billboard = FALSE;
  m_matCombine1 = m_matCombine2 = NULL;

  Move(0.0f,0.0f,0.0f);
  Rotate(0.0f,0.0f,0.0f);
  Scale(1.0f, 1.0f, 1.0f);

  Update();
}

BOOL cWorldPosition::Copy(cWorldPosition *DestPos)
{
  DestPos->Move(m_XPos, m_YPos, m_ZPos);
  DestPos->Rotate(m_XRotation, m_YRotation, m_ZRotation);
  DestPos->Scale(m_XScale, m_YScale, m_ZScale);
  DestPos->EnableBillboard(m_Billboard);
 
  return TRUE;
}

BOOL cWorldPosition::Move(float XPos, float YPos, float ZPos)
{
  m_XPos = XPos;
  m_YPos = YPos;
  m_ZPos = ZPos;

  D3DXMatrixTranslation(&m_matTranslation, m_XPos, m_YPos, m_ZPos);

  return TRUE;
}

BOOL cWorldPosition::MoveRel(float XAdd, float YAdd, float ZAdd)
{
  return Move(m_XPos + XAdd, m_YPos + YAdd, m_ZPos + ZAdd);
}

BOOL cWorldPosition::Rotate(float XRot, float YRot, float ZRot)
{
  m_XRotation = XRot;
  m_YRotation = YRot;
  m_ZRotation = ZRot;

  D3DXMatrixRotationYawPitchRoll(&m_matRotation, m_YRotation, m_XRotation, m_ZRotation);

  return TRUE;
}

BOOL cWorldPosition::RotateRel(float XAdd, float YAdd, float ZAdd)
{
  return Rotate(m_XRotation + XAdd, m_YRotation + YAdd, m_ZRotation + ZAdd);
}

BOOL cWorldPosition::Scale(float XScale, float YScale, float ZScale)
{
  m_XScale = XScale;
  m_YScale = YScale;
  m_ZScale = ZScale;

  D3DXMatrixScaling(&m_matScale, XScale, YScale, ZScale);

  return TRUE;
}

BOOL cWorldPosition::ScaleRel(float XAdd, float YAdd, float ZAdd)
{
  return Scale(m_XScale + XAdd, m_YScale + YAdd, m_ZScale + ZAdd);
}

BOOL cWorldPosition::Update(cGraphics *Graphics)
{
  D3DXMATRIX matView, matTransposed;

  // Setup billboarding matrix
  if(m_Billboard == TRUE) {
    if(Graphics != NULL && Graphics->GetDeviceCOM() != NULL) {
      Graphics->GetDeviceCOM()->GetTransform(D3DTS_VIEW, &matView);
      D3DXMatrixTranspose(&matTransposed, &matView);
      matTransposed._41 = matTransposed._42 = matTransposed._43 = matTransposed._14 = matTransposed._24 = matTransposed._34 = 0.0f;
    } else {
      D3DXMatrixIdentity(&matTransposed);
    }
  }

  // Combine scaling and rotation matrices first
  D3DXMatrixMultiply(&m_matWorld, &m_matScale, &m_matRotation);

  // Apply billboard matrix
  if(m_Billboard == TRUE)
    D3DXMatrixMultiply(&m_matWorld, &m_matWorld, &matTransposed);  

  // Combine with translation matrix
  D3DXMatrixMultiply(&m_matWorld, &m_matWorld, &m_matTranslation);

  // Combine with combined matrices (if any)
  if(m_matCombine1 != NULL) 
    D3DXMatrixMultiply(&m_matWorld, &m_matWorld, m_matCombine1);
  if(m_matCombine2 != NULL) 
    D3DXMatrixMultiply(&m_matWorld, &m_matWorld, m_matCombine2);

  return TRUE;
}

BOOL cWorldPosition::EnableBillboard(BOOL Enable)
{
  m_Billboard = Enable;
  return TRUE;
}

D3DXMATRIX *cWorldPosition::GetMatrix(cGraphics *Graphics)
{
  Update(Graphics);
  return &m_matWorld;
}

BOOL cWorldPosition::SetCombineMatrix1(D3DXMATRIX *Matrix)
{
  m_matCombine1 = Matrix;
  return TRUE;
}

BOOL cWorldPosition::SetCombineMatrix2(D3DXMATRIX *Matrix)
{
  m_matCombine2 = Matrix;
  return TRUE;
}

float cWorldPosition::GetXPos()
{
  return m_XPos;
}

float cWorldPosition::GetYPos()
{
  return m_YPos;
}

float cWorldPosition::GetZPos()
{
  return m_ZPos;
}

float cWorldPosition::GetXRotation()
{
  return m_XRotation;
}

float cWorldPosition::GetYRotation()
{
  return m_YRotation;
}

float cWorldPosition::GetZRotation()
{
  return m_ZRotation;
}

float cWorldPosition::GetXScale()
{
  return m_XScale;
}

float cWorldPosition::GetYScale()
{
  return m_YScale;
}

float cWorldPosition::GetZScale()
{
  return m_ZScale;
}

cCamera::cCamera()
{
  Move(0.0f,0.0f,0.0f);
  Rotate(0.0f,0.0f,0.0f);
  Update();
}

BOOL cCamera::Move(float XPos, float YPos, float ZPos)
{
  m_XPos = XPos;
  m_YPos = YPos;
  m_ZPos = ZPos;

  D3DXMatrixTranslation(&m_matTranslation, -m_XPos, -m_YPos, -m_ZPos);

  return TRUE;
}

BOOL cCamera::MoveRel(float XAdd, float YAdd, float ZAdd)
{
  return Move(m_XPos + XAdd, m_YPos + YAdd, m_ZPos + ZAdd);
}

BOOL cCamera::Rotate(float XRot, float YRot, float ZRot)
{
  D3DXMATRIX matXRotation, matYRotation, matZRotation;

  m_XRot = XRot;
  m_YRot = YRot;
  m_ZRot = ZRot;

  D3DXMatrixRotationX(&matXRotation, -m_XRot);
  D3DXMatrixRotationY(&matYRotation, -m_YRot);
  D3DXMatrixRotationZ(&matZRotation, -m_ZRot);

  m_matRotation = matZRotation;
  D3DXMatrixMultiply(&m_matRotation, &m_matRotation, &matYRotation);
  D3DXMatrixMultiply(&m_matRotation, &m_matRotation, &matXRotation);

  return TRUE;
}

BOOL cCamera::RotateRel(float XAdd, float YAdd, float ZAdd)
{
  return Rotate(m_XRot + XAdd, m_YRot + YAdd, m_ZRot + ZAdd);
}

BOOL cCamera::Point(float XEye, float YEye, float ZEye, float XAt, float YAt, float ZAt)
{
  float XRot, YRot, XDiff, YDiff, ZDiff;

  // Calculate angles between points
  XDiff = XAt - XEye;
  YDiff = YAt - YEye;
  ZDiff = ZAt - ZEye;
  XRot = (float)atan2(-YDiff, sqrt(XDiff*XDiff+ZDiff*ZDiff));
  YRot = (float)atan2(XDiff, ZDiff);

  Move(XEye, YEye, ZEye);
  Rotate(XRot, YRot, 0.0f);

  return TRUE;
}

BOOL cCamera::SetStartTrack()
{
  m_StartXPos = m_XPos;
  m_StartYPos = m_YPos;
  m_StartZPos = m_ZPos;
  m_StartXRot = m_XRot;
  m_StartYRot = m_YRot;
  m_StartZRot = m_ZRot;
  return TRUE;
}

BOOL cCamera::SetEndTrack()
{
  m_EndXPos = m_XPos;
  m_EndYPos = m_YPos;
  m_EndZPos = m_ZPos;
  m_EndXRot = m_XRot;
  m_EndYRot = m_YRot;
  m_EndZRot = m_ZRot;
  return TRUE;
}

BOOL cCamera::Track(float Time, float Length)
{
  float x, y, z;
  float TimeOffset;

  TimeOffset = Length * Time;

  x = (m_EndXPos - m_StartXPos) / Length * TimeOffset;
  y = (m_EndYPos - m_StartYPos) / Length * TimeOffset;
  z = (m_EndZPos - m_StartZPos) / Length * TimeOffset;
  Move(m_StartXPos + x, m_StartYPos + y, m_StartZPos + z);

  x = (m_EndXRot - m_StartXRot) / Length * TimeOffset;
  y = (m_EndYRot - m_StartYRot) / Length * TimeOffset;
  z = (m_EndZRot - m_StartZRot) / Length * TimeOffset;
  Rotate(m_StartXRot + x, m_StartYRot + y, m_StartZRot + z);

  return TRUE;
}

BOOL cCamera::Update()
{
  D3DXMatrixMultiply(&m_matWorld, &m_matTranslation, &m_matRotation);
  return TRUE;
}

D3DXMATRIX *cCamera::GetMatrix()
{
  Update();
  return &m_matWorld;
}

float cCamera::GetXPos()
{
  return m_XPos;
}

float cCamera::GetYPos()
{
  return m_YPos;
}

float cCamera::GetZPos()
{
  return m_ZPos;
}

float cCamera::GetXRotation()
{
  return m_XRot;
}

float cCamera::GetYRotation()
{
  return m_YRot;
}

float cCamera::GetZRotation()
{
  return m_ZRot;
}

cFont::cFont()
{
  m_Font = NULL;
}

cFont::~cFont()
{
  Free();
}

ID3DXFont *cFont::GetFontCOM()
{
  return m_Font;
}

BOOL cFont::Create(cGraphics *Graphics, char *Name, long Size, BOOL Bold, BOOL Italic, BOOL Underline, BOOL Strikeout)
{
  LOGFONT lf;

  if(Graphics == NULL || Name == NULL)
    return FALSE;
  if(Graphics->GetDeviceCOM() == NULL)
    return FALSE;

  // Clear out the font structure
  ZeroMemory(&lf, sizeof(LOGFONT));

  // Set the font name and height
  strcpy(lf.lfFaceName, Name);
  lf.lfHeight = -Size;
  lf.lfWeight = (Bold == TRUE) ? 700 : 0;
  lf.lfItalic = Italic;
  lf.lfUnderline = Underline;
  lf.lfStrikeOut = Strikeout;

  // Create the font object
  if(FAILED(D3DXCreateFontIndirect(Graphics->GetDeviceCOM(), &lf, &m_Font)))
    return FALSE;
  return TRUE;
}

BOOL cFont::Free()
{
  ReleaseCOM(m_Font);
  return TRUE;
}

BOOL cFont::Begin()
{
  if(m_Font == NULL)
    return FALSE;
  if(FAILED(m_Font->Begin()))
    return FALSE;
  return TRUE;
}

BOOL cFont::End()
{
  if(m_Font == NULL)
    return FALSE;
  if(FAILED(m_Font->End()))
    return FALSE;
  return TRUE;
}

BOOL cFont::Print(char *Text, long XPos, long YPos, long Width, long Height, D3DCOLOR Color, DWORD Format)
{
  RECT Rect;

  if(m_Font == NULL)
    return FALSE;

  if(!Width)
    Width = 65535;
  if(!Height)
    Height = 65536;

  Rect.left   = XPos;
  Rect.top    = YPos;
  Rect.right  = Rect.left + Width;
  Rect.bottom = Rect.top + Height;
  if(FAILED(m_Font->DrawText(Text, -1, &Rect, Format, Color)))
    return FALSE;
  return TRUE;
}

cMesh::cMesh()
{
  m_Graphics = NULL;
  m_NumMeshes = 0;
  m_Meshes = NULL;
  m_NumFrames = 0;
  m_Frames = NULL;

  m_Min.x = m_Min.y = m_Min.z = m_Max.x = m_Max.y = m_Max.z = 0.0f;
  m_Radius = 0.0f;
}

cMesh::~cMesh()
{
  Free();
}

BOOL cMesh::Load(cGraphics *Graphics, char *Filename, char *TexturePath)
{
  IDirectXFile           *pDXFile = NULL;
  IDirectXFileEnumObject *pDXEnum = NULL;
  IDirectXFileData       *pDXData = NULL;
  sFrame                 *TempFrame, *FramePtr;
  sMesh                  *Mesh;
  D3DXMATRIX             *Matrix;
  DWORD                   i;

  // Free prior mesh object data
  Free();

  // Error checking
  if((m_Graphics = Graphics) == NULL || Filename == NULL)
    return FALSE;

  // Create the file object
  if(FAILED(DirectXFileCreate(&pDXFile)))
    return FALSE;

  // Register the templates
  if(FAILED(pDXFile->RegisterTemplates((LPVOID)D3DRM_XTEMPLATES, D3DRM_XTEMPLATE_BYTES))) {
    pDXFile->Release();
    return FALSE;
  }

  // Create an enumeration object
  if(FAILED(pDXFile->CreateEnumObject((LPVOID)Filename, DXFILELOAD_FROMFILE, &pDXEnum))) {
    pDXFile->Release();
    return FALSE;
  }

  // Create a temporary frame
  TempFrame = new sFrame();

  // Loop through all objects looking for the frames and meshes
  while(SUCCEEDED(pDXEnum->GetNextDataObject(&pDXData))) {
    ParseXFileData(pDXData, TempFrame, TexturePath);
    ReleaseCOM(pDXData);
  }

  // Release used COM objects
  ReleaseCOM(pDXEnum);
  ReleaseCOM(pDXFile);

  // See if we should keep the tempframe as root
  if(TempFrame->m_MeshList != NULL) {
    m_Frames = TempFrame;
    m_Frames->m_Name = new char[7];
    strcpy(m_Frames->m_Name, "%ROOT%");
  } else {
    // Assign the root frame and release temporary frame
    m_Frames = TempFrame->m_Child;
    FramePtr = m_Frames;
    while(FramePtr != NULL) {
      FramePtr->m_Parent = NULL;
      FramePtr = FramePtr->m_Sibling;
    }
    TempFrame->m_Child = NULL;
    delete TempFrame;
  }

  // Match frames to bones (for matrices)
  MapFramesToBones(m_Frames);
  
  // Generate default mesh (if contains a skin mesh)
  if((Mesh = m_Meshes) != NULL) {

⌨️ 快捷键说明

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