Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SpriteBatcher.h"
- //Constructor
- SpriteBatcher::SpriteBatcher()
- {
- //Init base vars
- CLEAR_COLOR = D3DCOLOR_XRGB(0, 40, 100);
- vBuffer = NULL;
- iBuffer = NULL;
- //Set the drawing vars
- numShapes = 0;
- vertCount = 0;
- idxBuffCount = 0;
- renderCount = 0;
- setTexture = true;
- }
- //De-constructor
- SpriteBatcher::~SpriteBatcher()
- {
- //Clean up things that need cleaning
- if(vBuffer != NULL)
- vBuffer->Release();
- if(iBuffer != NULL)
- iBuffer->Release();
- if(currentTexture != NULL)
- currentTexture->Release();
- }
- void SpriteBatcher::setBatcherDevice(LPDIRECT3DDEVICE9 &device)
- {
- batDevice = device;
- batDevice->SetFVF(CUSTOMFVF);
- }
- void SpriteBatcher::draw(float x, float y, float width, float height, D3DCOLOR color, LPDIRECT3DTEXTURE9 texture)
- {
- //If we go over call the eend batch so we don't explode
- if(currentTexture == NULL)
- {
- //std::cout<<"Init texture set; Texture: "<<texture<<std::endl;
- currentTexture = texture;
- }
- else if(currentTexture != texture) //If we use a different texure end the batch
- {
- //std::cout<<"Texture change; Change from Texture: "<<currentTexture<<std::endl;
- endBatch();
- currentTexture = texture;
- //std::cout<<"Texture is now: "<<currentTexture<<std::endl;
- }
- #pragma region Filling the vertex and index buffer
- //Make a quad
- //V0
- vertices[vertCount].x = x;
- vertices[vertCount].y = y;
- vertices[vertCount].z = 1.0f;
- vertices[vertCount].rhw = 1.0f;
- vertices[vertCount].color = color;
- vertices[vertCount].u = 0.0f;
- vertices[vertCount].v = 0.0f;
- //V1
- vertices[vertCount + 1].x = x + width;
- vertices[vertCount + 1].y = y;
- vertices[vertCount + 1].z = 1.0f;
- vertices[vertCount + 1].rhw = 1.0f;
- vertices[vertCount + 1].color = color;
- vertices[vertCount + 1].u = 1.0f;
- vertices[vertCount + 1].v = 0.0f;
- //V2
- vertices[vertCount + 2].x = x + width;
- vertices[vertCount + 2].y = y + height;
- vertices[vertCount + 2].z = 1.0f;
- vertices[vertCount + 2].rhw = 1.0f;
- vertices[vertCount + 2].color = color;
- vertices[vertCount + 2].u = 1.0f;
- vertices[vertCount + 2].v = 1.0f;
- //V3
- vertices[vertCount + 3].x = x;
- vertices[vertCount + 3].y = y + height;
- vertices[vertCount + 3].z = 1.0f;
- vertices[vertCount + 3].rhw = 1.0f;
- vertices[vertCount + 3].color = color;
- vertices[vertCount + 3].u = 0.0f;
- vertices[vertCount + 3].v = 1.0f;
- //0,1,2, 2,3,0
- indices[idxBuffCount] = vertCount;
- indices[idxBuffCount + 1] = vertCount + 1;
- indices[idxBuffCount + 2] = vertCount + 2;
- indices[idxBuffCount + 3] = vertCount + 3;
- indices[idxBuffCount + 4] = vertCount;
- indices[idxBuffCount + 5] = vertCount +2;
- //inc the number of shapes to draw (inc by 2 cause of 2 triangles)
- //inc the vert index by 4
- numShapes += 2;
- vertCount += 4;
- idxBuffCount += 6;
- #pragma endregion
- }
- void SpriteBatcher::draw(float x, float y, float width, float height, D3DCOLOR color, float rotation, LPDIRECT3DTEXTURE9 texture)
- {
- //If we go over call the end batch so we don't explode
- if(currentTexture == NULL)
- {
- //std::cout<<"Init texture set; Texture: "<<texture<<std::endl;
- currentTexture = texture;
- }
- else if(currentTexture != texture) //If we use a different texure end the batch
- {
- //std::cout<<"Texture change; Change from Texture: "<<currentTexture<<std::endl;
- endBatch();
- currentTexture = texture;
- //std::cout<<"Texture is now: "<<currentTexture<<std::endl;
- }
- #pragma region Rotation Calc
- float rotXpo = x + (width/2);
- float rotYpo = y + (height/2);
- float ang = rotation * D3DX_PI/180;
- //Upper left corner
- float ulX = ((x-rotXpo) * cos(ang) - (y-rotYpo) * sin(ang)) + rotXpo;
- float ulY = ((x-rotXpo) * sin(ang) + (y-rotYpo) * cos(ang)) + rotYpo;
- //Upper right corner
- float urX = (((x+width)-rotXpo) * cos(ang) - (y-rotYpo) * sin(ang)) + rotXpo;
- float urY = (((x+width)-rotXpo) * sin(ang) + (y-rotYpo) * cos(ang)) + rotYpo;
- //Bottom left corner
- float blX = ((x-rotXpo) * cos(ang) - ((y+height)-rotYpo) * sin(ang)) + rotXpo;
- float blY = ((x-rotXpo) * sin(ang) + ((y+height)-rotYpo) * cos(ang)) + rotYpo;
- //Bottom right corner
- float brX = (((x+width)-rotXpo) * cos(ang) - ((y+height)-rotYpo) * sin(ang)) + rotXpo;
- float brY = (((x+width)-rotXpo) * sin(ang) + ((y+height)-rotYpo) * cos(ang)) + rotYpo;
- #pragma endregion
- #pragma region Filling the vertex and index buffer
- //Make a quad
- //V0
- vertices[vertCount].x = ulX;
- vertices[vertCount].y = ulY;
- vertices[vertCount].z = 1.0f;
- vertices[vertCount].rhw = 1.0f;
- vertices[vertCount].color = color;
- vertices[vertCount].u = 0.0f;
- vertices[vertCount].v = 0.0f;
- //V1
- vertices[vertCount + 1].x = urX;
- vertices[vertCount + 1].y = urY;
- vertices[vertCount + 1].z = 1.0f;
- vertices[vertCount + 1].rhw = 1.0f;
- vertices[vertCount + 1].color = color;
- vertices[vertCount + 1].u = 1.0f;
- vertices[vertCount + 1].v = 0.0f;
- //V2
- vertices[vertCount + 2].x = brX;
- vertices[vertCount + 2].y = brY;
- vertices[vertCount + 2].z = 1.0f;
- vertices[vertCount + 2].rhw = 1.0f;
- vertices[vertCount + 2].color = color;
- vertices[vertCount + 2].u = 1.0f;
- vertices[vertCount + 2].v = 1.0f;
- //V3
- vertices[vertCount + 3].x = blX;
- vertices[vertCount + 3].y = blY;
- vertices[vertCount + 3].z = 1.0f;
- vertices[vertCount + 3].rhw = 1.0f;
- vertices[vertCount + 3].color = color;
- vertices[vertCount + 3].u = 0.0f;
- vertices[vertCount + 3].v = 1.0f;
- //0,1,2, 2,3,0
- indices[idxBuffCount] = vertCount;
- indices[idxBuffCount + 1] = vertCount + 1;
- indices[idxBuffCount + 2] = vertCount + 2;
- indices[idxBuffCount + 3] = vertCount + 3;
- indices[idxBuffCount + 4] = vertCount;
- indices[idxBuffCount + 5] = vertCount +2;
- //inc the number of shapes to draw (inc by 2 cause of 2 triangles)
- //inc the vert index by 4
- numShapes += 2;
- vertCount += 4;
- idxBuffCount += 6;
- #pragma endregion
- }
- void SpriteBatcher::beginBatch()
- {
- renderCount = 0;
- //std::cout<<"Begin batch!"<<std::endl;
- }
- void SpriteBatcher::endBatch()
- {
- //Get everything ready for the render
- if(vertCount > 0)
- {
- batDevice->CreateVertexBuffer(vertCount * sizeof(vertex), D3DUSAGE_WRITEONLY, CUSTOMFVF, D3DPOOL_MANAGED, &vBuffer, NULL);
- batDevice->CreateIndexBuffer(idxBuffCount * sizeof(short), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &iBuffer, NULL);
- render();
- resetCounts();
- renderCount++;
- }
- std::cout<<renderCount<<std::endl;
- }
- void SpriteBatcher::render()
- {
- //Render everything that needs to be drawn
- #pragma region Vertex and Index buffers
- //Fill / prepare the vertex buffer
- vBuffer->Lock(0, 0, (void**) &pVoid, NULL);
- memcpy(pVoid, vertices, vertCount * sizeof(vertex));
- vBuffer->Unlock();
- //Fill / prepare the index buffer
- iBuffer->Lock(0, 0, (void**) &pVoid, NULL);
- memcpy(pVoid, indices, idxBuffCount * sizeof(short));
- iBuffer->Unlock();
- #pragma endregion
- //Prepare to draw the scene
- //Draw code
- batDevice->SetStreamSource(0, vBuffer, 0, sizeof(vertex));
- batDevice->SetIndices(iBuffer);
- //std::cout<<"Texture set: "<<currentTexture<<std::endl;
- batDevice->SetTexture(0, currentTexture);
- batDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, vertCount, 0, numShapes);
- }
- void SpriteBatcher::resetCounts()
- {
- //Reset all variables that are using in drawing
- numShapes = 0;
- vertCount = 0;
- idxBuffCount = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement