Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "vBatcher.h"
- vBatcher::vBatcher()
- {
- //NULL the device
- batDevice = NULL;
- //Set the FVF
- CUSTOMFVF2 = D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1;
- //NULL init the buffers and related items
- vBuffer = NULL;
- iBuffer = NULL;
- vertexBufferSize = 0;
- indexBufferSize = 0;
- //Set the lock flag to NOOVERWRITE on deffault
- bufferLockFlag = D3DLOCK_NOOVERWRITE;
- bufferIsFull = false;
- //Render items defaults
- currentTexture = NULL;
- vertices = NULL;
- indices = NULL;
- currentVertexBufferPosition = 0;
- currentIndexBufferPosition = 0;
- vertexOffsetForBuffer = 0;
- numberOfVertsToDraw = 0;
- numberOfShapesToDraw = 0;
- totalAmountOfData = 0;
- bufferIsFull = false;
- }
- vBatcher::~vBatcher()
- {
- //Release everything
- if(vBuffer != NULL)
- vBuffer->Release();
- if(iBuffer != NULL)
- iBuffer->Release();
- if(currentTexture != NULL)
- currentTexture->Release();
- }
- void vBatcher::initBatcher(LPDIRECT3DDEVICE9 &device)
- {
- //Set the device and the FVF
- batDevice = device;
- batDevice->SetFVF(CUSTOMFVF2);
- //Set the MAX buffer size
- vertexBufferSize = sizeof(vertex) * 16;
- indexBufferSize = sizeof(short) * 24;
- //Make sure we can make the dynamic index buffer
- HRESULT HR = batDevice->CreateVertexBuffer(vertexBufferSize, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, CUSTOMFVF2, D3DPOOL_DEFAULT, &vBuffer, NULL);
- if(FAILED(HR))
- std::cout<<"Failed to make the vertex buffer"<<std::endl;
- else
- std::cout<<"Created vertex buffer"<<std::endl;
- //Make sure we can make the index buffer
- HR = batDevice->CreateIndexBuffer(indexBufferSize, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &iBuffer, NULL);
- if(FAILED(HR))
- std::cout<<"Failed to make the index buffer"<<std::endl;
- else
- std::cout<<"Created index buffer"<<std::endl;
- //Set the stream source for the vertex buffer and set the index buffer source
- batDevice->SetStreamSource(0, vBuffer, 0, sizeof(vertex));
- batDevice->SetIndices(iBuffer);
- }
- void vBatcher::draw(float x, float y, float width, float height, LPDIRECT3DTEXTURE9 texture)
- {
- #pragma region Vertex Data
- //Vertex 0
- quadData.verts[0].x = x;
- quadData.verts[0].y = y;
- quadData.verts[0].z = 1.0f;
- quadData.verts[0].rhw = 1.0f;
- quadData.verts[0].color = D3DCOLOR_XRGB(0, 0, 0);
- quadData.verts[0].u = 0.0f;
- quadData.verts[0].v = 0.0f;
- //Vertex 1
- quadData.verts[1].x = x + width;
- quadData.verts[1].y = y;
- quadData.verts[1].z = 1.0f;
- quadData.verts[1].rhw = 1.0f;
- quadData.verts[1].color = D3DCOLOR_XRGB(0, 0, 0);
- quadData.verts[1].u = 1.0f;
- quadData.verts[1].v = 0.0f;
- //Vertex 2
- quadData.verts[2].x = x + width;
- quadData.verts[2].y = y + height;
- quadData.verts[2].z = 1.0f;
- quadData.verts[2].rhw = 1.0f;
- quadData.verts[2].color = D3DCOLOR_XRGB(0, 0, 0);
- quadData.verts[2].u = 1.0f;
- quadData.verts[2].v = 1.0f;
- //Vertex 3
- quadData.verts[3].x = x;
- quadData.verts[3].y = y + height;
- quadData.verts[3].z = 1.0f;
- quadData.verts[3].rhw = 1.0f;
- quadData.verts[3].color = D3DCOLOR_XRGB(0, 0, 0);
- quadData.verts[3].u = 0.0f;
- quadData.verts[3].v = 1.0f;
- #pragma endregion
- #pragma region texture
- quadData.texture = texture;
- #pragma endregion
- drawData.push_back(quadData);
- }
- void vBatcher::beginBatch()
- {
- //Not really used or need. Something will be here.
- }
- void vBatcher::endBatch()
- {
- //Lock the buffers based on the current lock flag. By default on first go this is NOOVERWRITE
- vBuffer->Lock(0, 0, (void**) &vertices, bufferLockFlag);
- iBuffer->Lock(0, 0, (void**) &indices, bufferLockFlag);
- //Loop through the quad to draw
- for(std::vector<quad>::iterator i = drawData.begin(); i != drawData.end(); i++)
- {
- //Check if we have hit the max size of the buffer
- if(vertexBufferSize - totalAmountOfData == 0)
- {
- //Set the new lock flag and that we have hit the max size of the buffer
- //std::cout<<"Buffer full!"<<std::endl;
- bufferLockFlag = D3DLOCK_DISCARD;
- bufferIsFull = true;
- //Reset the vertex and index position;
- //Resset the total amount of data we are using to 0
- currentVertexBufferPosition = 0;
- currentIndexBufferPosition = 0;
- totalAmountOfData = 0;
- }
- //If the buffer is full or we have to texture swap
- if(bufferIsFull || currentTexture != (*i).texture)
- {
- //Unlock the buffers
- vBuffer->Unlock();
- iBuffer->Unlock();
- //Set the texture to the curretn texture and draw all the quads needed to be drawn
- //The Stream Source and Index buffer are set in an init method
- batDevice->SetTexture(0, currentTexture);
- //std::cout<<"Set tex: "<<currentTexture<<std::endl;
- batDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, vertexOffsetForBuffer, 0, numberOfVertsToDraw, 0, numberOfShapesToDraw);
- //Increase the offset of the buffer based on the number of vertices we drew
- vertexOffsetForBuffer += numberOfVertsToDraw;
- //Reset the number of items we need to drwa to 0
- numberOfVertsToDraw = 0;
- numberOfShapesToDraw = 0;
- //If the buffer was full; reset it
- if(bufferIsFull == true)
- {
- //Say the buffer is no longer full
- bufferIsFull = false;
- //Reset our offset on the vertex buffer so we start drawing
- //back at 0 position
- vertexOffsetForBuffer = 0;
- }
- //Set the current texture to the new texture we need
- currentTexture = (*i).texture;
- //std::cout<<"Curr tex: "<<currentTexture<<std::endl;
- //Relock the buffers based on the flag we need;
- //NOOVERWRITE if the buffer still has room
- //Otherwise it will lock DISCARD if the buffer is full
- vBuffer->Lock(0, 0, (void**) &vertices, bufferLockFlag);
- iBuffer->Lock(0, 0, (void**) &indices, bufferLockFlag);
- //Change the flag to NOOVERWRITE
- bufferLockFlag = D3DLOCK_NOOVERWRITE;
- }
- //Copy in the vertices for the quad to draw
- memcpy(vertices + currentVertexBufferPosition, (*i).verts, sizeof((*i).verts));
- //Load the indices in the index buffer
- indices[currentIndexBufferPosition] = currentVertexBufferPosition;
- indices[currentIndexBufferPosition + 1] = currentVertexBufferPosition + 1;
- indices[currentIndexBufferPosition + 2] = currentVertexBufferPosition + 2;
- indices[currentIndexBufferPosition + 3] = currentVertexBufferPosition + 3;
- indices[currentIndexBufferPosition + 4] = currentVertexBufferPosition;
- indices[currentIndexBufferPosition + 5] = currentVertexBufferPosition + 2;
- //Set the current positions based on the loaded items
- currentVertexBufferPosition += 4;
- currentIndexBufferPosition += 6;
- //INcrease the number of items we will draw
- numberOfVertsToDraw += 4;
- numberOfShapesToDraw += 2;
- //Increse the amount of data we used to draw;
- //This is based on the vertex buffer; This data for a single quad
- //So calculations are based on 4 vertexes
- totalAmountOfData += 4 * sizeof(vertex);
- //std::cout<<"Tot Data: "<<totalAmountOfData<<std::endl;
- }
- //Unlock the buffers
- vBuffer->Unlock();
- iBuffer->Unlock();
- //Set the texture to the curretn texture and draw all the quads needed to be drawn
- //The Stream Source and Index buffer are set in an init method
- batDevice->SetTexture(0, currentTexture);
- batDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, vertexOffsetForBuffer, 0, numberOfVertsToDraw, 0, numberOfShapesToDraw);
- //Increase where our buffer offset is at based on the number of verts we drew
- vertexOffsetForBuffer += numberOfVertsToDraw;
- //Reset the number of items that we need to draw
- numberOfVertsToDraw = 0;
- numberOfShapesToDraw = 0;
- //Clear out vector for any new incoming data next frame
- drawData.clear();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement