Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. void AnimatedSprite::Update()
  2. {
  3.     if (m_maxFrames == 1.0f)return;
  4.    
  5.     if (m_currentFrame < m_maxFrames)
  6.     {
  7.         float dt = Timer::GetDeltaTime();
  8.  
  9.         m_currentSpeed += m_animationSpeed * dt;
  10.  
  11.         if (m_currentSpeed > m_framesPerSecond)
  12.         {
  13.             m_currentFrame++;
  14.             m_currentSpeed = 0.0f;
  15.  
  16.             if (m_currentFrame >= m_maxFrames)
  17.             {
  18.                 if (m_isLooping)
  19.                 {
  20.                     m_currentFrame = 0.0f;
  21.                 }
  22.                 else
  23.                 {
  24.                     m_currentFrame = m_maxFrames;
  25.                 }
  26.             }
  27.         }
  28.     }
  29.     //no reason to lock/update the buffer if its still the same frame
  30.     if (m_currentFrame == m_previousFrame)return;
  31.  
  32.     D3D11_MAPPED_SUBRESOURCE mappedResource;
  33.     VertexBuffer::VertexType* vertices = m_vertexBuffer->GetVertices();
  34.  
  35.     vertices[0].uv.x = m_currentFrame / m_maxFrames;
  36.     vertices[0].uv.y = 1.0f;
  37.  
  38.     vertices[1].uv.x = m_currentFrame / m_maxFrames;
  39.     vertices[1].uv.y = 0.0f;
  40.  
  41.     vertices[2].uv.x = (m_currentFrame + 1.0f) / m_maxFrames;
  42.     vertices[2].uv.y = 0.0f;
  43.  
  44.     vertices[3].uv.x = (m_currentFrame + 1.0f) / m_maxFrames;
  45.     vertices[3].uv.y = 1.0f;
  46.  
  47.     //lock the vertex buffer so it can be written to
  48.     HRESULT result = m_deviceContext->Map(m_vertexBuffer->GetVertexBuffer(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
  49.     if (FAILED(result))
  50.     {
  51.         return;
  52.     }
  53.     VertexBuffer::VertexType* verticesPtr = (VertexBuffer::VertexType*)mappedResource.pData;
  54.     memcpy(verticesPtr, (void*)vertices, sizeof(VertexBuffer::VertexType)* m_vertexBuffer->GetVertexCount());
  55.  
  56.     m_deviceContext->Unmap(m_vertexBuffer->GetVertexBuffer(), 0);
  57.  
  58.     //update the prev frame
  59.     m_previousFrame = m_currentFrame;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement