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;
- numShapes = 0;
- vertCount = 0;
- idxBuffIndex = 0;
- }
- //De-constructor
- SpriteBatcher::~SpriteBatcher()
- {
- //Clean up things that need cleaning
- if(vBuffer != NULL)
- vBuffer->Release();
- if(iBuffer != NULL)
- iBuffer->Release();
- }
- void SpriteBatcher::draw(float x, float y, float width, float height, D3DCOLOR color)
- {
- //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;
- //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;
- //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;
- //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;
- //0,1,2, 2,3,0
- indices[idxBuffIndex] = 0;
- indices[idxBuffIndex + 1] = 1;
- indices[idxBuffIndex + 2] = 2;
- indices[idxBuffIndex + 3] = 3;
- indices[idxBuffIndex + 4] = 0;
- indices[idxBuffIndex + 5] = 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;
- idxBuffIndex += 6;
- }
- void SpriteBatcher::beginBatch()
- {
- //Reset everything
- numShapes = 0;
- vertCount = 0;
- idxBuffIndex = 0;
- }
- void SpriteBatcher::endBatch(LPDIRECT3DDEVICE9 &device)
- {
- //Get everything ready for the render
- if(vertCount > 0)
- {
- device->SetFVF(CUSTOMFVF);
- device->CreateVertexBuffer(vertCount * sizeof(vertex), D3DUSAGE_WRITEONLY, CUSTOMFVF, D3DPOOL_MANAGED, &vBuffer, NULL);
- device->CreateIndexBuffer(6 * sizeof(indices), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &iBuffer, NULL);
- render(device);
- }
- }
- void SpriteBatcher::render(LPDIRECT3DDEVICE9 &device)
- {
- //Render everything
- //Fill the buffers
- vBuffer->Lock(0, 0, (void**) &pVoid, NULL);
- memcpy(pVoid, vertices, vertCount * sizeof(vertex));
- vBuffer->Unlock();
- iBuffer->Lock(0, 0, (void**) &pVoid, NULL);
- memcpy(pVoid, indices, 6 * sizeof(indices));
- iBuffer->Unlock();
- //Prepare to draw the scene
- device->Clear(0, NULL, D3DCLEAR_TARGET, CLEAR_COLOR, 1.0f, 0);
- device->BeginScene();
- //Draw code
- device->SetStreamSource(0, vBuffer, 0, sizeof(vertex));
- device->SetIndices(iBuffer);
- device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, vertCount, 0, numShapes);
- //End the scene and present everything to the screen
- device->EndScene();
- device->Present(NULL, NULL, NULL, NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement