Advertisement
Guest User

SpriteBatcher.cpp

a guest
Sep 17th, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #include "SpriteBatcher.h"
  2.  
  3. //Constructor
  4. SpriteBatcher::SpriteBatcher()
  5. {
  6. //Init base vars
  7. CLEAR_COLOR = D3DCOLOR_XRGB(0, 40, 100);
  8. vBuffer = NULL;
  9. iBuffer = NULL;
  10. numShapes = 0;
  11. vertCount = 0;
  12. idxBuffIndex = 0;
  13. }
  14.  
  15. //De-constructor
  16. SpriteBatcher::~SpriteBatcher()
  17. {
  18. //Clean up things that need cleaning
  19. if(vBuffer != NULL)
  20. vBuffer->Release();
  21.  
  22. if(iBuffer != NULL)
  23. iBuffer->Release();
  24. }
  25.  
  26. void SpriteBatcher::draw(float x, float y, float width, float height, D3DCOLOR color)
  27. {
  28. //Make a quad
  29.  
  30. //V0
  31. vertices[vertCount].x = x;
  32. vertices[vertCount].y = y;
  33. vertices[vertCount].z = 1.0f;
  34. vertices[vertCount].rhw = 1.0f;
  35. vertices[vertCount].color = color;
  36.  
  37. //V1
  38. vertices[vertCount + 1].x = x + width;
  39. vertices[vertCount + 1].y = y;
  40. vertices[vertCount + 1].z = 1.0f;
  41. vertices[vertCount + 1].rhw = 1.0f;
  42. vertices[vertCount + 1].color = color;
  43.  
  44. //V2
  45. vertices[vertCount + 2].x = x + width;
  46. vertices[vertCount + 2].y = y + height;
  47. vertices[vertCount + 2].z = 1.0f;
  48. vertices[vertCount + 2].rhw = 1.0f;
  49. vertices[vertCount + 2].color = color;
  50.  
  51. //V3
  52. vertices[vertCount + 3].x = x;
  53. vertices[vertCount + 3].y = y + height;
  54. vertices[vertCount + 3].z = 1.0f;
  55. vertices[vertCount + 3].rhw = 1.0f;
  56. vertices[vertCount + 3].color = color;
  57.  
  58. //0,1,2, 2,3,0
  59. indices[idxBuffIndex] = 0;
  60. indices[idxBuffIndex + 1] = 1;
  61. indices[idxBuffIndex + 2] = 2;
  62. indices[idxBuffIndex + 3] = 3;
  63. indices[idxBuffIndex + 4] = 0;
  64. indices[idxBuffIndex + 5] = 2;
  65.  
  66. //inc the number of shapes to draw (inc by 2 cause of 2 triangles)
  67. //inc the vert index by 4
  68. numShapes += 2;
  69. vertCount += 4;
  70. idxBuffIndex += 6;
  71. }
  72.  
  73. void SpriteBatcher::beginBatch()
  74. {
  75. //Reset everything
  76. numShapes = 0;
  77. vertCount = 0;
  78. idxBuffIndex = 0;
  79. }
  80.  
  81. void SpriteBatcher::endBatch(LPDIRECT3DDEVICE9 &device)
  82. {
  83. //Get everything ready for the render
  84. if(vertCount > 0)
  85. {
  86. device->SetFVF(CUSTOMFVF);
  87. device->CreateVertexBuffer(vertCount * sizeof(vertex), D3DUSAGE_WRITEONLY, CUSTOMFVF, D3DPOOL_MANAGED, &vBuffer, NULL);
  88. device->CreateIndexBuffer(6 * sizeof(indices), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &iBuffer, NULL);
  89. render(device);
  90. }
  91. }
  92.  
  93. void SpriteBatcher::render(LPDIRECT3DDEVICE9 &device)
  94. {
  95. //Render everything
  96.  
  97. //Fill the buffers
  98. vBuffer->Lock(0, 0, (void**) &pVoid, NULL);
  99. memcpy(pVoid, vertices, vertCount * sizeof(vertex));
  100. vBuffer->Unlock();
  101.  
  102. iBuffer->Lock(0, 0, (void**) &pVoid, NULL);
  103. memcpy(pVoid, indices, 6 * sizeof(indices));
  104. iBuffer->Unlock();
  105.  
  106. //Prepare to draw the scene
  107. device->Clear(0, NULL, D3DCLEAR_TARGET, CLEAR_COLOR, 1.0f, 0);
  108. device->BeginScene();
  109.  
  110. //Draw code
  111. device->SetStreamSource(0, vBuffer, 0, sizeof(vertex));
  112. device->SetIndices(iBuffer);
  113. device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, vertCount, 0, numShapes);
  114.  
  115. //End the scene and present everything to the screen
  116. device->EndScene();
  117. device->Present(NULL, NULL, NULL, NULL);
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement