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

📄 core_graphics.h

📁 [游戏开发参考书-用DirectX编写RPG游戏]这是一个系列的丛书如果你都看并且懂的话你就可以你工作啦!
💻 H
📖 第 1 页 / 共 2 页
字号:
    // Search child lists
    if(m_Child != NULL) {
      if((Frame = m_Child->FindFrame(Name)) != NULL)
        return Frame;
    }

    // Search sibling lists
    if(m_Sibling != NULL) {
      if((Frame = m_Sibling->FindFrame(Name)) != NULL)
        return Frame;
    }

    return NULL;
  }

  void ResetMatrices()
  {
    m_matTransformed = m_matOriginal;

    if(m_Child != NULL)
      m_Child->ResetMatrices();
    if(m_Sibling != NULL)
      m_Sibling->ResetMatrices();      
  }

  void AddMesh(sMesh *Mesh)
  {
    sFrameMeshList *List;

    List = new sFrameMeshList();
    List->m_Mesh = Mesh;
    List->m_Next = m_MeshList;
    m_MeshList = List;
  }
} sFrame;

class cMesh
{
  private:
    cGraphics *m_Graphics;

    long       m_NumMeshes;
    sMesh     *m_Meshes;

    long       m_NumFrames;
    sFrame    *m_Frames;

    D3DXVECTOR3    m_Min, m_Max;
    float          m_Radius;

    void ParseXFileData(IDirectXFileData *pData, sFrame *ParentFrame, char *TexturePath);
    void MapFramesToBones(sFrame *Frame);

  public:
    cMesh();
    ~cMesh();

    BOOL    IsLoaded();

    long    GetNumFrames();
    sFrame *GetParentFrame();
    sFrame *GetFrame(char *Name);

    long    GetNumMeshes();
    sMesh  *GetParentMesh();
    sMesh  *GetMesh(char *Name);

    BOOL    GetBounds(float *MinX, float *MinY, float *MinZ, float *MaxX, float *MaxY, float *MaxZ, float *Radius);

    BOOL    Load(cGraphics *Graphics, char *Filename, char *TexturePath = ".\\");
    BOOL    Free();
};

class cObject
{
  protected:
    cGraphics     *m_Graphics;
    cMesh         *m_Mesh;
    sAnimationSet *m_AnimationSet;
    cWorldPosition m_Pos;

    BOOL           m_Billboard;

    unsigned long  m_StartTime;

    void UpdateFrame(sFrame *Frame, D3DXMATRIX *Matrix);
    void DrawFrame(sFrame *Frame);
    void DrawMesh(sMesh *Mesh);

  public:
    cObject();
    ~cObject();

    BOOL Create(cGraphics *Graphics, cMesh *Mesh = NULL);
    BOOL Free();

    BOOL EnableBillboard(BOOL Enable = TRUE);
    BOOL AttachToObject(cObject *Object, char *FrameName = NULL);

    BOOL Move(float XPos, float YPos, float ZPos);
    BOOL MoveRel(float XAdd, float YAdd, float ZAdd);

    BOOL Rotate(float XRot, float YRot, float ZRot);
    BOOL RotateRel(float XAdd, float YAdd, float ZAdd);

    BOOL Scale(float XScale, float YScale, float ZScale);
    BOOL ScaleRel(float XAdd, float YAdd, float ZAdd);

    D3DXMATRIX *GetMatrix();

    float GetXPos();
    float GetYPos();
    float GetZPos();
    float GetXRotation();
    float GetYRotation();
    float GetZRotation();
    float GetXScale();
    float GetYScale();
    float GetZScale();

    BOOL  GetBounds(float *MinX, float *MinY, float *MinZ, float *MaxX, float *MaxY, float *MaxZ, float *Radius);

    BOOL SetMesh(cMesh *Mesh);
    cMesh *GetMesh();
    BOOL SetAnimation(cAnimation *Animation, char *Name = NULL, unsigned long StartTime = 0);
    char *GetAnimation();
    BOOL ResetAnimation(unsigned long StartTime = 0);

    BOOL UpdateAnimation(unsigned long Time, BOOL Smooth = TRUE);
    BOOL AnimationComplete(unsigned long Time);
 
    BOOL Update();
    BOOL Render();
};

typedef struct 
{
	DWORD Time;
  DWORD Floats;
	float	w;
  float x;
  float y;
  float z;
} sXFileRotateKey;

typedef struct 
{
	DWORD	      Time;
  DWORD       Floats;	
	D3DXVECTOR3	Scale;	
} sXFileScaleKey;

typedef struct
{
	DWORD       Time;
  DWORD       Floats;	
	D3DXVECTOR3	Pos;	
} sXFilePositionKey;

typedef struct
{
	DWORD	      Time;
  DWORD       Floats;	
	D3DXMATRIX	Matrix;
} sXFileMatrixKey;

typedef struct
{
	DWORD          Time;
	D3DXQUATERNION Quaternion;
} sRotateKey;

typedef struct
{
	DWORD     	Time;
	D3DXVECTOR3	Pos;
	D3DXVECTOR3	PosInterpolation;
} sPositionKey;

typedef struct
{
	DWORD	      Time;
	D3DXVECTOR3	Scale;
	D3DXVECTOR3	ScaleInterpolation;
} sScaleKey;

typedef struct
{
	DWORD	     Time;
	D3DXMATRIX Matrix;
  D3DXMATRIX MatInterpolation;
} sMatrixKey;

typedef struct sAnimation
{
  char         *m_Name;
  char         *m_FrameName;
  sFrame       *m_Frame;

  BOOL          m_Loop;
  BOOL          m_Linear;

  DWORD           m_NumPositionKeys;
  sPositionKey *m_PositionKeys;

  DWORD           m_NumRotateKeys;
  sRotateKey   *m_RotateKeys;

  DWORD           m_NumScaleKeys;
  sScaleKey    *m_ScaleKeys;

  DWORD           m_NumMatrixKeys;
  sMatrixKey     *m_MatrixKeys;

  sAnimation   *m_Next;

  sAnimation()
  {
    m_Name            = NULL;
    m_FrameName       = NULL;
    m_Frame           = NULL;
    m_Loop            = FALSE;
    m_Linear          = TRUE;
    m_NumPositionKeys = 0;
    m_PositionKeys    = NULL;
    m_NumRotateKeys   = 0;
    m_RotateKeys      = NULL;
    m_NumScaleKeys    =0;
    m_ScaleKeys       = NULL;
    m_NumMatrixKeys   = 0;
    m_MatrixKeys      = NULL;
    m_Next            = NULL;
  }

  ~sAnimation()
  {
    delete [] m_Name;
    delete [] m_FrameName;
    delete [] m_PositionKeys;
    delete [] m_RotateKeys;
    delete [] m_ScaleKeys;
    delete [] m_MatrixKeys;
    delete m_Next;
  }

  void Update(DWORD Time, BOOL Smooth)
  {
    unsigned long i, Key;
    DWORD TimeDiff, IntTime;
    D3DXMATRIX     Matrix, MatTemp;
    D3DXVECTOR3    Vector;
    D3DXQUATERNION Quat;
    
    if(m_Frame == NULL)
      return;

    // update rotation, scale, and position keys
    if(m_NumRotateKeys || m_NumScaleKeys || m_NumPositionKeys) {
      D3DXMatrixIdentity(&Matrix);

      if(m_NumRotateKeys && m_RotateKeys != NULL) {
        // Find the key that fits this time
        Key = 0;
        for(i=0;i<m_NumRotateKeys;i++) {
          if(m_RotateKeys[i].Time <= Time)
            Key = i;
          else
            break;
        }

        // If it's the last key or non-smooth animation,
        // then just set the key value.
        if(Key == (m_NumRotateKeys-1) || Smooth == FALSE) {
          Quat = m_RotateKeys[Key].Quaternion;
        } else {
          // Calculate the time difference and 
          // interpolate time.
          TimeDiff = m_RotateKeys[Key+1].Time - m_RotateKeys[Key].Time;
          IntTime  = Time - m_RotateKeys[Key].Time;

          // Get the quaternion value
          D3DXQuaternionSlerp(&Quat, &m_RotateKeys[Key].Quaternion, &m_RotateKeys[Key+1].Quaternion, ((float)IntTime / (float)TimeDiff));
        }

        // Combine with the new matrix
        D3DXMatrixRotationQuaternion(&MatTemp, &Quat);
        D3DXMatrixMultiply(&Matrix, &Matrix, &MatTemp);
      }

      if(m_NumScaleKeys && m_ScaleKeys != NULL) {
        // Find the key that fits this time
        Key = 0;
        for(i=0;i<m_NumScaleKeys;i++) {
          if(m_ScaleKeys[i].Time <= Time)
            Key = i;
          else
            break;
        }

        // If it's the last key or non-smooth animation,
        // then just set the key value.
        if(Key == (m_NumScaleKeys-1) || Smooth == FALSE) {
          Vector = m_ScaleKeys[Key].Scale;
        } else {
          // Calculate the time difference and 
          // interpolate time.
          IntTime  = Time - m_ScaleKeys[Key].Time;

          // Get the interpolated vector value
          Vector = m_ScaleKeys[Key].Scale + m_ScaleKeys[Key].ScaleInterpolation * (float)IntTime;
        }

        // Combine with the new matrix
        D3DXMatrixScaling(&MatTemp, Vector.x, Vector.y, Vector.z);
        D3DXMatrixMultiply(&Matrix, &Matrix, &MatTemp);
      }

      if(m_NumPositionKeys && m_PositionKeys != NULL) {
        // Find the key that fits this time
        Key = 0;
        for(i=0;i<m_NumPositionKeys;i++) {
          if(m_PositionKeys[i].Time <= Time)
            Key = i;
          else
            break;
        }

        // If it's the last key or non-smooth animation,
        // then just set the key value.
        if(Key == (m_NumPositionKeys-1) || Smooth == FALSE) {
          Vector = m_PositionKeys[Key].Pos;
        } else {
          // Calculate the time difference and 
          // interpolate time.
          IntTime  = Time - m_PositionKeys[Key].Time;

          // Get the interpolated vector value
          Vector = m_PositionKeys[Key].Pos + m_PositionKeys[Key].PosInterpolation * (float)IntTime;
        }

        // Combine with the new matrix
        D3DXMatrixTranslation(&MatTemp, Vector.x, Vector.y, Vector.z);
        D3DXMatrixMultiply(&Matrix, &Matrix, &MatTemp);
      }

      // Set the new matrix
      m_Frame->m_matTransformed = Matrix;
    }

    // update matrix keys
    if(m_NumMatrixKeys && m_MatrixKeys != NULL) {
      // Find the key that fits this time
      Key = 0;
      for(i=0;i<m_NumMatrixKeys;i++) {
        if(m_MatrixKeys[i].Time <= Time)
          Key = i;
        else
          break;
      }

      // If it's the last key or non-smooth animation,
      // then just set the matrix.
      if(Key == (m_NumMatrixKeys-1) || Smooth == FALSE) {
        m_Frame->m_matTransformed = m_MatrixKeys[Key].Matrix;
      } else {
        // Calculate the time difference and 
        // interpolate time.
        IntTime  = Time - m_MatrixKeys[Key].Time;

        // Set the new interpolation matrix
        Matrix = m_MatrixKeys[Key].MatInterpolation * (float)IntTime;
        m_Frame->m_matTransformed = Matrix + m_MatrixKeys[Key].Matrix;
      }
    }
  }
} sAnimation;

typedef struct sAnimationSet
{
  char          *m_Name;
  sAnimation    *m_Animation;

  unsigned long  m_Length;

  sAnimationSet *m_Next;

  sAnimationSet()
  {
    m_Name      = NULL;
    m_Animation = NULL;
    m_Length    = 0;
    m_Next      = NULL;
  }

  ~sAnimationSet()
  {
    delete [] m_Name;
    delete m_Animation;
    delete m_Next;
  }

  sAnimationSet *FindSet(char *Name)
  {
    sAnimationSet *AnimSet;

    // return first instance if name=NULL
    if(Name == NULL)
      return this;

    // Compare names and return if exact match
    if(m_Name != NULL && !strcmp(Name, m_Name))
      return this;

    // Search next in list
    if(m_Next != NULL) {
      if((AnimSet = m_Next->FindSet(Name)) != NULL)
        return AnimSet;
    }

    return NULL;
  }

  void Update(DWORD Time, BOOL Smooth)
  {
    sAnimation *Anim;

    Anim = m_Animation;
    while(Anim != NULL) {
      if(!m_Length)
        Anim->Update(0, FALSE);
      else
      if(Time >= m_Length && Anim->m_Loop == FALSE)
        Anim->Update(Time, FALSE);
      else
        Anim->Update((Time % m_Length), Smooth);

      Anim = Anim->m_Next;
    }
  }

} sAnimationSet;

class cAnimation
{
  protected:
    long           m_NumAnimations;
    sAnimationSet *m_AnimationSet;

    void ParseXFileData(IDirectXFileData *DataObj, sAnimationSet *ParentAnim, sAnimation *CurrentAnim);

  public:
    cAnimation();
    ~cAnimation();

    BOOL           IsLoaded();

    long           GetNumAnimations();
    sAnimationSet *GetAnimationSet(char *Name = NULL);
    unsigned long  GetLength(char *Name = NULL);

    BOOL Load(char *Filename, cMesh *MapMesh = NULL);
    BOOL Free();

    BOOL MapToMesh(cMesh *Mesh);

    BOOL SetLoop(BOOL ToLoop, char *Name = NULL);
};

class cVertexBuffer
{
  private:
    cGraphics              *m_Graphics;
    IDirect3DVertexBuffer8 *m_pVB;

    DWORD                   m_NumVertices;
    DWORD                   m_VertexSize;
    DWORD                   m_FVF;

    BOOL                    m_Locked;
    char                   *m_Ptr;

  public:
    cVertexBuffer();
    ~cVertexBuffer();

    IDirect3DVertexBuffer8 *GetVertexBufferCOM();
    unsigned long           GetVertexSize();
    unsigned long           GetVertexFVF();
    unsigned long           GetNumVertices();

    BOOL Create(cGraphics *Graphics, unsigned long NumVertices, DWORD Descriptor, long VertexSize);
    BOOL Free();

    BOOL IsLoaded();

    BOOL Set(unsigned long FirstVertex, unsigned long NumVertices, void *VertexList);
    BOOL Render(unsigned long FirstVertex, unsigned long NumPrimitives, DWORD Type);

    BOOL  Lock(unsigned long FirstVertex = 0, unsigned long NumVertices = 0);
    BOOL  Unlock();
    void *GetPtr();
};

#endif

⌨️ 快捷键说明

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