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

📄 ch17p2_staticdissolvetransition.cpp

📁 游戏开发特殊技巧-special.effects.game.programming
💻 CPP
字号:
#include "Ch17p2_StaticDissolveTransition.h"

CStaticDissolveTransition::CStaticDissolveTransition(LPDIRECT3DDEVICE8 pDev, LPDIRECT3DTEXTURE8 pOrigTexture, 
                                         LPDIRECT3DTEXTURE8 texImage, int iStaticSize, int iStaticTile)
{
  m_texImage = texImage;
  m_pd3dDevice = pDev;
  m_fFadeTime = 0;
  m_pOrigImage = pOrigTexture;
  m_iStaticSize = GetLowestPowerOf2(iStaticSize);
  m_iStaticTile = iStaticTile;

  // create vertex buffer for one quad
  if (FAILED(CreateQuad(&m_pVB, D3DPOOL_MANAGED, 2, 0, pDev))) {
    throw("Error creating vertex buffer!");
  }

  if (FAILED(CreateQuad(&m_pVBStatic, D3DPOOL_MANAGED, 2, 0, pDev, m_iStaticTile, m_iStaticTile))) {
    throw("Error creating static vertex buffer!");
  }

  if (FAILED(D3DXCreateTexture(m_pd3dDevice, m_iStaticSize, m_iStaticSize, 
    1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &m_texStatic))) throw("Can't create texture!");
}

CStaticDissolveTransition::~CStaticDissolveTransition()
{
  SAFE_RELEASE(m_texStatic);
  SAFE_RELEASE(m_pVB);
  SAFE_RELEASE(m_pVBStatic);
}

void CStaticDissolveTransition::DoTransition(float fTransTime)
{
  m_fFadeTime += fTransTime;

  // lock texture
  D3DLOCKED_RECT lockedrect;
  ::ZeroMemory(&lockedrect, sizeof(lockedrect));
  
  if (FAILED(m_texStatic->LockRect(0, &lockedrect, NULL, 0))) return;
  
  // our texture surface is now locked, and we can use the pitch to traverse it.
  unsigned char *pSurfBits = static_cast<unsigned char *>(lockedrect.pBits);
  
  for (int y=0; y < m_iStaticSize; y++) {
    for (int x=0; x < m_iStaticSize; x++) {
      // the fire value at this position determines the color of this texel
      unsigned char s = RandomNumber(0,255);
      *(pSurfBits++) = s; // blue
      *(pSurfBits++) = s; // green
      *(pSurfBits++) = s; // red
      *(pSurfBits++) = 255; // alpha
    }
    // next line
    pSurfBits += lockedrect.Pitch - (m_iStaticSize*4);
  }

  // unlock texture surface
  if (FAILED(m_texStatic->UnlockRect(0))) return;
}


void CStaticDissolveTransition::FadeQuadVerts()
{
  HRESULT hr;
  VERTEX_XYZ_DIFFUSE_TEX1* pVertices;
  D3DXCOLOR FadeToColor(0.0f, 0.0f, 0.0f, 1.0f);

  // calculate the new color value
  if (m_fFadeTime > m_fDuration/2.0f) {
    FadeToColor.a = FadeToColor.a * ((m_fDuration-m_fFadeTime) / (m_fDuration/2.0f));
  }
  else {
    FadeToColor.a = FadeToColor.a * (m_fFadeTime / (m_fDuration/2.0f));
  }
  if (FadeToColor.a > 1.0f) FadeToColor.a = 1.0f;
  if (FadeToColor.a < 0.0f) FadeToColor.a = 0.0f;

  if( FAILED( hr = m_pVB->Lock( 0, 6*sizeof(VERTEX_XYZ_DIFFUSE_TEX1), (BYTE**)&pVertices, 0 ) ) )
      return;

  pVertices[0].color    = (DWORD)FadeToColor; 
  pVertices[1].color    = (DWORD)FadeToColor;
  pVertices[2].color    = (DWORD)FadeToColor;
  
  pVertices[3].color    = (DWORD)FadeToColor;
  pVertices[4].color    = (DWORD)FadeToColor;
  pVertices[5].color    = (DWORD)FadeToColor;
  
  if( FAILED( hr = m_pVB->Unlock() ) ) return;
}

void CStaticDissolveTransition::DoRender(D3DXMATRIX mFinalScale)
{
  m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
  
  // set our texture active...
  m_pd3dDevice->SetTexture( 0, (m_fFadeTime < m_fDuration/2.0f) ? m_pOrigImage : m_texImage);

  // set up our texture stages for a simple texture copy...
  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );

  // texture wrapping ON (for tiled static)
  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU,   D3DTADDRESS_WRAP );
  m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV,   D3DTADDRESS_WRAP );

  // draw our quad
  m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(VERTEX_XYZ_DIFFUSE_TEX1) );
  m_pd3dDevice->SetVertexShader( D3DFVF_XYZ_DIFFUSE_TEX1 );
  m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );

  if (m_fFadeTime > 0.0f) {
    // set up texture stage states for blending the 2nd quad on top
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );

    // put the correct colors into the quad's verts
    FadeQuadVerts();

    m_pd3dDevice->SetTexture( 0, m_texStatic );
    m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
    m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
    m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );

    m_pd3dDevice->SetStreamSource(0, m_pVB, sizeof(VERTEX_XYZ_DIFFUSE_TEX1));
    m_pd3dDevice->SetVertexShader(D3DFVF_XYZ_DIFFUSE_TEX1);
    m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);

    m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
  }
}

⌨️ 快捷键说明

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