Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Vertex Struct and other vars
- struct vertex
- {
- float x;
- float y;
- float z;
- float rhw;
- D3DCOLOR color;
- };
- vertex vertices [1000];
- //===========================================================================
- #include "SpriteBatcher.h"
- //Constructor
- SpriteBatcher::SpriteBatcher()
- {
- sizeOfVertices = 0;
- numShapes = 0;
- vertIndex = 0;
- CLEAR_COLOR = D3DCOLOR_XRGB(0, 40, 100);
- }
- //De-constructor
- SpriteBatcher::~SpriteBatcher()
- {
- if(vBuffer != NULL)
- vBuffer->Release();
- }
- void SpriteBatcher::draw(float x, float y, float width, float height, D3DCOLOR color)
- {
- //Make a quad
- //V0
- vertices[vertIndex].x = x;
- vertices[vertIndex].y = y + height;
- vertices[vertIndex].z = 1.0f;
- vertices[vertIndex].rhw = 1.0f;
- vertices[vertIndex].color = color;
- //V1
- vertices[vertIndex + 1].x = x;
- vertices[vertIndex + 1].y = y;
- vertices[vertIndex + 1].z = 1.0f;
- vertices[vertIndex + 1].rhw = 1.0f;
- vertices[vertIndex + 1].color = color;
- //V2
- vertices[vertIndex + 2].x = x + width;
- vertices[vertIndex + 2].y = y;
- vertices[vertIndex + 2].z = 1.0f;
- vertices[vertIndex + 2].rhw = 1.0f;
- vertices[vertIndex + 2].color = color;
- //V3
- vertices[vertIndex + 3].x = x;
- vertices[vertIndex + 3].y = y + height;
- vertices[vertIndex + 3].z = 1.0f;
- vertices[vertIndex + 3].rhw = 1.0f;
- vertices[vertIndex + 3].color = color;
- //V4
- vertices[vertIndex + 4].x = x + width;
- vertices[vertIndex + 4].y = y;
- vertices[vertIndex + 4].z = 1.0f;
- vertices[vertIndex + 4].rhw = 1.0f;
- vertices[vertIndex + 4].color = color;
- //V5
- vertices[vertIndex + 5].x = x + width;
- vertices[vertIndex + 5].y = y + height;
- vertices[vertIndex + 5].z = 1.0f;
- vertices[vertIndex + 5].rhw = 1.0f;
- vertices[vertIndex + 5].color = color;
- //inc the number of shapes to draw (inc by 2 cause of 2 triangles)
- //inc the vert index by 6
- numShapes += 2;
- vertIndex += 6;
- }
- void SpriteBatcher::beginBatch()
- {
- //Reset everything
- sizeOfVertices = 0;
- numShapes = 0;
- vertIndex = 0;
- }
- void SpriteBatcher::endBatch(LPDIRECT3DDEVICE9 &device)
- {
- //Get everything ready for the render
- if(vertIndex > 0)
- {
- sizeOfVertices = numShapes * 3 * sizeof(vertex);
- device->SetFVF(CUSTOMFVF);
- device->CreateVertexBuffer(sizeOfVertices, 0, CUSTOMFVF, D3DPOOL_MANAGED, &vBuffer, NULL);
- render(device);
- }
- }
- void SpriteBatcher::render(LPDIRECT3DDEVICE9 &device)
- {
- //Render everything
- VOID* pVoid;
- vBuffer->Lock(0, 0, (void**) &pVoid, NULL);
- memcpy(pVoid, vertices, sizeOfVertices);
- vBuffer->Unlock();
- device->Clear(0, NULL, D3DCLEAR_TARGET, CLEAR_COLOR, 1.0f, 0);
- device->BeginScene();
- //Draw code
- device->SetStreamSource(0, vBuffer, 0, sizeof(vertex));
- device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, numShapes);
- device->EndScene();
- device->Present(NULL, NULL, NULL, NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement