Advertisement
noodleBowl

vBatcher.cpp

Oct 6th, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.53 KB | None | 0 0
  1. #include "vBatcher.h"
  2.  
  3. vBatcher::vBatcher()
  4. {
  5.     //NULL the device
  6.     batDevice = NULL;
  7.  
  8.     //Set the FVF
  9.     CUSTOMFVF2 = D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1;
  10.  
  11.     //NULL init the buffers and related items
  12.     vBuffer = NULL;
  13.     iBuffer = NULL;
  14.     vertexBufferSize = 0;
  15.     indexBufferSize = 0;
  16.  
  17.     //Set the lock flag to NOOVERWRITE on deffault
  18.     bufferLockFlag = D3DLOCK_NOOVERWRITE;
  19.     bufferIsFull = false;
  20.  
  21.     //Render items defaults
  22.     currentTexture = NULL;
  23.     vertices = NULL;
  24.     indices = NULL;
  25.     currentVertexBufferPosition = 0;
  26.     currentIndexBufferPosition = 0;
  27.     vertexOffsetForBuffer = 0;
  28.     numberOfVertsToDraw = 0;
  29.     numberOfShapesToDraw = 0;
  30.     totalAmountOfData = 0;
  31.     bufferIsFull = false;
  32. }
  33.  
  34. vBatcher::~vBatcher()
  35. {
  36.     //Release everything
  37.     if(vBuffer != NULL)
  38.         vBuffer->Release();
  39.  
  40.     if(iBuffer != NULL)
  41.         iBuffer->Release();
  42.  
  43.     if(currentTexture != NULL)
  44.         currentTexture->Release();
  45. }
  46.  
  47. void vBatcher::initBatcher(LPDIRECT3DDEVICE9 &device)
  48. {
  49.     //Set the device and the FVF
  50.     batDevice = device;
  51.     batDevice->SetFVF(CUSTOMFVF2);
  52.  
  53.     //Set the MAX buffer size
  54.     vertexBufferSize = sizeof(vertex) * 16;
  55.     indexBufferSize = sizeof(short) * 24;
  56.  
  57.     //Make sure we can make the dynamic index buffer
  58.     HRESULT HR = batDevice->CreateVertexBuffer(vertexBufferSize, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, CUSTOMFVF2, D3DPOOL_DEFAULT, &vBuffer, NULL);
  59.     if(FAILED(HR))
  60.         std::cout<<"Failed to make the vertex buffer"<<std::endl;
  61.     else
  62.         std::cout<<"Created vertex buffer"<<std::endl;
  63.  
  64.     //Make sure we can make the index buffer
  65.     HR = batDevice->CreateIndexBuffer(indexBufferSize, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &iBuffer, NULL);
  66.     if(FAILED(HR))
  67.         std::cout<<"Failed to make the index buffer"<<std::endl;
  68.     else
  69.         std::cout<<"Created index buffer"<<std::endl;
  70.  
  71.     //Set the stream source for the vertex buffer and set the index buffer source
  72.     batDevice->SetStreamSource(0, vBuffer, 0, sizeof(vertex));
  73.     batDevice->SetIndices(iBuffer);
  74. }
  75.  
  76. void vBatcher::draw(float x, float y, float width, float height, LPDIRECT3DTEXTURE9 texture)
  77. {
  78.  
  79.     #pragma region Vertex Data
  80.  
  81.     //Vertex 0
  82.     quadData.verts[0].x = x;
  83.     quadData.verts[0].y = y;
  84.     quadData.verts[0].z = 1.0f;
  85.     quadData.verts[0].rhw = 1.0f;
  86.     quadData.verts[0].color = D3DCOLOR_XRGB(0, 0, 0);
  87.     quadData.verts[0].u = 0.0f;
  88.     quadData.verts[0].v = 0.0f;
  89.  
  90.     //Vertex 1
  91.     quadData.verts[1].x = x + width;
  92.     quadData.verts[1].y = y;
  93.     quadData.verts[1].z = 1.0f;
  94.     quadData.verts[1].rhw = 1.0f;
  95.     quadData.verts[1].color = D3DCOLOR_XRGB(0, 0, 0);
  96.     quadData.verts[1].u = 1.0f;
  97.     quadData.verts[1].v = 0.0f;
  98.  
  99.  
  100.     //Vertex 2
  101.     quadData.verts[2].x = x + width;
  102.     quadData.verts[2].y = y + height;
  103.     quadData.verts[2].z = 1.0f;
  104.     quadData.verts[2].rhw = 1.0f;
  105.     quadData.verts[2].color = D3DCOLOR_XRGB(0, 0, 0);
  106.     quadData.verts[2].u = 1.0f;
  107.     quadData.verts[2].v = 1.0f;
  108.  
  109.     //Vertex 3
  110.     quadData.verts[3].x = x;
  111.     quadData.verts[3].y = y + height;
  112.     quadData.verts[3].z = 1.0f;
  113.     quadData.verts[3].rhw = 1.0f;
  114.     quadData.verts[3].color = D3DCOLOR_XRGB(0, 0, 0);
  115.     quadData.verts[3].u = 0.0f;
  116.     quadData.verts[3].v = 1.0f;
  117.  
  118.     #pragma endregion
  119.  
  120.     #pragma region texture
  121.  
  122.     quadData.texture = texture;
  123.  
  124.     #pragma endregion
  125.  
  126.     drawData.push_back(quadData);
  127. }
  128.  
  129. void vBatcher::beginBatch()
  130. {
  131.     //Not really used or need. Something will be here.
  132. }
  133.  
  134. void vBatcher::endBatch()
  135. {
  136.     //Lock the buffers based on the current lock flag. By default on first go this is NOOVERWRITE
  137.     vBuffer->Lock(0, 0, (void**) &vertices, bufferLockFlag);
  138.     iBuffer->Lock(0, 0, (void**) &indices, bufferLockFlag);
  139.  
  140.     //Loop through the quad to draw
  141.     for(std::vector<quad>::iterator i = drawData.begin(); i != drawData.end(); i++)
  142.     {
  143.         //Check if we have hit the max size of the buffer
  144.         if(vertexBufferSize - totalAmountOfData == 0)
  145.         {
  146.             //Set the new lock flag and that we have hit the max size of the buffer
  147.             //std::cout<<"Buffer full!"<<std::endl;
  148.             bufferLockFlag = D3DLOCK_DISCARD;
  149.             bufferIsFull = true;
  150.  
  151.             //Reset the vertex and index position;
  152.             //Resset the total amount of data we are using to 0
  153.             currentVertexBufferPosition = 0;
  154.             currentIndexBufferPosition = 0;
  155.             totalAmountOfData = 0;
  156.         }
  157.  
  158.         //If the buffer is full or we have to texture swap
  159.         if(bufferIsFull || currentTexture != (*i).texture)
  160.         {
  161.             //Unlock the buffers
  162.             vBuffer->Unlock();
  163.             iBuffer->Unlock();
  164.  
  165.             //Set the texture to the curretn texture and draw all the quads needed to be drawn
  166.             //The Stream Source and Index buffer are set in an init method
  167.             batDevice->SetTexture(0, currentTexture);
  168.             //std::cout<<"Set tex: "<<currentTexture<<std::endl;
  169.             batDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, vertexOffsetForBuffer, 0, numberOfVertsToDraw, 0, numberOfShapesToDraw);
  170.            
  171.             //Increase the offset of the buffer based on the number of vertices we drew
  172.             vertexOffsetForBuffer += numberOfVertsToDraw;
  173.  
  174.             //Reset the number of items we need to drwa to 0
  175.             numberOfVertsToDraw = 0;
  176.             numberOfShapesToDraw = 0;
  177.  
  178.             //If the buffer was full; reset it
  179.             if(bufferIsFull == true)
  180.             {
  181.                 //Say the buffer is no longer full
  182.                 bufferIsFull = false;
  183.  
  184.                 //Reset our offset on the vertex buffer so we start drawing
  185.                 //back at 0 position
  186.                 vertexOffsetForBuffer = 0;
  187.             }
  188.  
  189.             //Set the current texture to the new texture we need
  190.             currentTexture = (*i).texture;
  191.             //std::cout<<"Curr tex: "<<currentTexture<<std::endl;
  192.  
  193.             //Relock the buffers based on the flag we need;
  194.             //NOOVERWRITE if the buffer still has room
  195.             //Otherwise it will lock DISCARD if the buffer is full
  196.             vBuffer->Lock(0, 0, (void**) &vertices, bufferLockFlag);
  197.             iBuffer->Lock(0, 0, (void**) &indices, bufferLockFlag);
  198.  
  199.             //Change the flag to NOOVERWRITE
  200.             bufferLockFlag = D3DLOCK_NOOVERWRITE;
  201.         }
  202.  
  203.         //Copy in the vertices for the quad to draw
  204.         memcpy(vertices + currentVertexBufferPosition, (*i).verts, sizeof((*i).verts));
  205.  
  206.         //Load the indices in the index buffer
  207.         indices[currentIndexBufferPosition] = currentVertexBufferPosition;
  208.         indices[currentIndexBufferPosition + 1] = currentVertexBufferPosition + 1;
  209.         indices[currentIndexBufferPosition + 2] = currentVertexBufferPosition + 2;
  210.         indices[currentIndexBufferPosition + 3] = currentVertexBufferPosition + 3;
  211.         indices[currentIndexBufferPosition + 4] = currentVertexBufferPosition;
  212.         indices[currentIndexBufferPosition + 5] = currentVertexBufferPosition + 2;
  213.    
  214.         //Set the current positions based on the loaded items
  215.         currentVertexBufferPosition += 4;
  216.         currentIndexBufferPosition += 6;
  217.  
  218.         //INcrease the number of items we will draw
  219.         numberOfVertsToDraw += 4;
  220.         numberOfShapesToDraw += 2;
  221.  
  222.         //Increse the amount of data we used to draw;
  223.         //This is based on the vertex buffer; This data for a single quad
  224.         //So calculations are based on 4 vertexes
  225.         totalAmountOfData += 4 * sizeof(vertex);
  226.         //std::cout<<"Tot Data: "<<totalAmountOfData<<std::endl;
  227.     }
  228.  
  229.  
  230.     //Unlock the buffers
  231.     vBuffer->Unlock();
  232.     iBuffer->Unlock();
  233.  
  234.     //Set the texture to the curretn texture and draw all the quads needed to be drawn
  235.     //The Stream Source and Index buffer are set in an init method
  236.     batDevice->SetTexture(0, currentTexture);
  237.     batDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, vertexOffsetForBuffer, 0, numberOfVertsToDraw, 0, numberOfShapesToDraw);
  238.  
  239.     //Increase where our buffer offset is at based on the number of verts we drew
  240.     vertexOffsetForBuffer += numberOfVertsToDraw;
  241.  
  242.     //Reset the number of items that we need to draw
  243.     numberOfVertsToDraw = 0;
  244.     numberOfShapesToDraw = 0;
  245.  
  246.     //Clear out vector for any new incoming data next frame
  247.     drawData.clear();
  248.  
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement