Advertisement
noodleBowl

SpriteBatcher.cpp

Sep 26th, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 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.  
  11. //Set the drawing vars
  12. numShapes = 0;
  13. vertCount = 0;
  14. idxBuffCount = 0;
  15. renderCount = 0;
  16. setTexture = true;
  17. }
  18.  
  19. //De-constructor
  20. SpriteBatcher::~SpriteBatcher()
  21. {
  22. //Clean up things that need cleaning
  23. if(vBuffer != NULL)
  24. vBuffer->Release();
  25.  
  26. if(iBuffer != NULL)
  27. iBuffer->Release();
  28.  
  29. if(currentTexture != NULL)
  30. currentTexture->Release();
  31. }
  32.  
  33. void SpriteBatcher::setBatcherDevice(LPDIRECT3DDEVICE9 &device)
  34. {
  35. batDevice = device;
  36. batDevice->SetFVF(CUSTOMFVF);
  37. }
  38.  
  39. void SpriteBatcher::draw(float x, float y, float width, float height, D3DCOLOR color, LPDIRECT3DTEXTURE9 texture)
  40. {
  41. //If we go over call the eend batch so we don't explode
  42. if(currentTexture == NULL)
  43. {
  44. //std::cout<<"Init texture set; Texture: "<<texture<<std::endl;
  45. currentTexture = texture;
  46. }
  47. else if(currentTexture != texture) //If we use a different texure end the batch
  48. {
  49. //std::cout<<"Texture change; Change from Texture: "<<currentTexture<<std::endl;
  50. endBatch();
  51.  
  52. currentTexture = texture;
  53. //std::cout<<"Texture is now: "<<currentTexture<<std::endl;
  54. }
  55.  
  56. #pragma region Filling the vertex and index buffer
  57.  
  58. //Make a quad
  59. //V0
  60. vertices[vertCount].x = x;
  61. vertices[vertCount].y = y;
  62. vertices[vertCount].z = 1.0f;
  63. vertices[vertCount].rhw = 1.0f;
  64. vertices[vertCount].color = color;
  65. vertices[vertCount].u = 0.0f;
  66. vertices[vertCount].v = 0.0f;
  67.  
  68. //V1
  69. vertices[vertCount + 1].x = x + width;
  70. vertices[vertCount + 1].y = y;
  71. vertices[vertCount + 1].z = 1.0f;
  72. vertices[vertCount + 1].rhw = 1.0f;
  73. vertices[vertCount + 1].color = color;
  74. vertices[vertCount + 1].u = 1.0f;
  75. vertices[vertCount + 1].v = 0.0f;
  76.  
  77. //V2
  78. vertices[vertCount + 2].x = x + width;
  79. vertices[vertCount + 2].y = y + height;
  80. vertices[vertCount + 2].z = 1.0f;
  81. vertices[vertCount + 2].rhw = 1.0f;
  82. vertices[vertCount + 2].color = color;
  83. vertices[vertCount + 2].u = 1.0f;
  84. vertices[vertCount + 2].v = 1.0f;
  85.  
  86.  
  87. //V3
  88. vertices[vertCount + 3].x = x;
  89. vertices[vertCount + 3].y = y + height;
  90. vertices[vertCount + 3].z = 1.0f;
  91. vertices[vertCount + 3].rhw = 1.0f;
  92. vertices[vertCount + 3].color = color;
  93. vertices[vertCount + 3].u = 0.0f;
  94. vertices[vertCount + 3].v = 1.0f;
  95.  
  96. //0,1,2, 2,3,0
  97. indices[idxBuffCount] = vertCount;
  98. indices[idxBuffCount + 1] = vertCount + 1;
  99. indices[idxBuffCount + 2] = vertCount + 2;
  100. indices[idxBuffCount + 3] = vertCount + 3;
  101. indices[idxBuffCount + 4] = vertCount;
  102. indices[idxBuffCount + 5] = vertCount +2;
  103.  
  104. //inc the number of shapes to draw (inc by 2 cause of 2 triangles)
  105. //inc the vert index by 4
  106. numShapes += 2;
  107. vertCount += 4;
  108. idxBuffCount += 6;
  109.  
  110. #pragma endregion
  111.  
  112. }
  113.  
  114. void SpriteBatcher::draw(float x, float y, float width, float height, D3DCOLOR color, float rotation, LPDIRECT3DTEXTURE9 texture)
  115. {
  116. //If we go over call the end batch so we don't explode
  117. if(currentTexture == NULL)
  118. {
  119. //std::cout<<"Init texture set; Texture: "<<texture<<std::endl;
  120. currentTexture = texture;
  121. }
  122. else if(currentTexture != texture) //If we use a different texure end the batch
  123. {
  124. //std::cout<<"Texture change; Change from Texture: "<<currentTexture<<std::endl;
  125. endBatch();
  126.  
  127. currentTexture = texture;
  128. //std::cout<<"Texture is now: "<<currentTexture<<std::endl;
  129. }
  130.  
  131.  
  132. #pragma region Rotation Calc
  133.  
  134. float rotXpo = x + (width/2);
  135. float rotYpo = y + (height/2);
  136. float ang = rotation * D3DX_PI/180;
  137.  
  138. //Upper left corner
  139. float ulX = ((x-rotXpo) * cos(ang) - (y-rotYpo) * sin(ang)) + rotXpo;
  140. float ulY = ((x-rotXpo) * sin(ang) + (y-rotYpo) * cos(ang)) + rotYpo;
  141.  
  142. //Upper right corner
  143. float urX = (((x+width)-rotXpo) * cos(ang) - (y-rotYpo) * sin(ang)) + rotXpo;
  144. float urY = (((x+width)-rotXpo) * sin(ang) + (y-rotYpo) * cos(ang)) + rotYpo;
  145.  
  146. //Bottom left corner
  147. float blX = ((x-rotXpo) * cos(ang) - ((y+height)-rotYpo) * sin(ang)) + rotXpo;
  148. float blY = ((x-rotXpo) * sin(ang) + ((y+height)-rotYpo) * cos(ang)) + rotYpo;
  149.  
  150. //Bottom right corner
  151. float brX = (((x+width)-rotXpo) * cos(ang) - ((y+height)-rotYpo) * sin(ang)) + rotXpo;
  152. float brY = (((x+width)-rotXpo) * sin(ang) + ((y+height)-rotYpo) * cos(ang)) + rotYpo;
  153.  
  154. #pragma endregion
  155.  
  156. #pragma region Filling the vertex and index buffer
  157.  
  158. //Make a quad
  159. //V0
  160. vertices[vertCount].x = ulX;
  161. vertices[vertCount].y = ulY;
  162. vertices[vertCount].z = 1.0f;
  163. vertices[vertCount].rhw = 1.0f;
  164. vertices[vertCount].color = color;
  165. vertices[vertCount].u = 0.0f;
  166. vertices[vertCount].v = 0.0f;
  167.  
  168. //V1
  169. vertices[vertCount + 1].x = urX;
  170. vertices[vertCount + 1].y = urY;
  171. vertices[vertCount + 1].z = 1.0f;
  172. vertices[vertCount + 1].rhw = 1.0f;
  173. vertices[vertCount + 1].color = color;
  174. vertices[vertCount + 1].u = 1.0f;
  175. vertices[vertCount + 1].v = 0.0f;
  176.  
  177. //V2
  178. vertices[vertCount + 2].x = brX;
  179. vertices[vertCount + 2].y = brY;
  180. vertices[vertCount + 2].z = 1.0f;
  181. vertices[vertCount + 2].rhw = 1.0f;
  182. vertices[vertCount + 2].color = color;
  183. vertices[vertCount + 2].u = 1.0f;
  184. vertices[vertCount + 2].v = 1.0f;
  185.  
  186.  
  187. //V3
  188. vertices[vertCount + 3].x = blX;
  189. vertices[vertCount + 3].y = blY;
  190. vertices[vertCount + 3].z = 1.0f;
  191. vertices[vertCount + 3].rhw = 1.0f;
  192. vertices[vertCount + 3].color = color;
  193. vertices[vertCount + 3].u = 0.0f;
  194. vertices[vertCount + 3].v = 1.0f;
  195.  
  196. //0,1,2, 2,3,0
  197. indices[idxBuffCount] = vertCount;
  198. indices[idxBuffCount + 1] = vertCount + 1;
  199. indices[idxBuffCount + 2] = vertCount + 2;
  200. indices[idxBuffCount + 3] = vertCount + 3;
  201. indices[idxBuffCount + 4] = vertCount;
  202. indices[idxBuffCount + 5] = vertCount +2;
  203.  
  204. //inc the number of shapes to draw (inc by 2 cause of 2 triangles)
  205. //inc the vert index by 4
  206. numShapes += 2;
  207. vertCount += 4;
  208. idxBuffCount += 6;
  209.  
  210. #pragma endregion
  211.  
  212. }
  213.  
  214.  
  215. void SpriteBatcher::beginBatch()
  216. {
  217. renderCount = 0;
  218. //std::cout<<"Begin batch!"<<std::endl;
  219. }
  220.  
  221. void SpriteBatcher::endBatch()
  222. {
  223. //Get everything ready for the render
  224. if(vertCount > 0)
  225. {
  226. batDevice->CreateVertexBuffer(vertCount * sizeof(vertex), D3DUSAGE_WRITEONLY, CUSTOMFVF, D3DPOOL_MANAGED, &vBuffer, NULL);
  227. batDevice->CreateIndexBuffer(idxBuffCount * sizeof(short), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &iBuffer, NULL);
  228. render();
  229. resetCounts();
  230. renderCount++;
  231. }
  232.  
  233. std::cout<<renderCount<<std::endl;
  234. }
  235.  
  236. void SpriteBatcher::render()
  237. {
  238. //Render everything that needs to be drawn
  239.  
  240. #pragma region Vertex and Index buffers
  241.  
  242. //Fill / prepare the vertex buffer
  243. vBuffer->Lock(0, 0, (void**) &pVoid, NULL);
  244. memcpy(pVoid, vertices, vertCount * sizeof(vertex));
  245. vBuffer->Unlock();
  246.  
  247. //Fill / prepare the index buffer
  248. iBuffer->Lock(0, 0, (void**) &pVoid, NULL);
  249. memcpy(pVoid, indices, idxBuffCount * sizeof(short));
  250. iBuffer->Unlock();
  251.  
  252. #pragma endregion
  253.  
  254. //Prepare to draw the scene
  255.  
  256. //Draw code
  257. batDevice->SetStreamSource(0, vBuffer, 0, sizeof(vertex));
  258. batDevice->SetIndices(iBuffer);
  259.  
  260. //std::cout<<"Texture set: "<<currentTexture<<std::endl;
  261. batDevice->SetTexture(0, currentTexture);
  262.  
  263. batDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, vertCount, 0, numShapes);
  264.  
  265. }
  266.  
  267. void SpriteBatcher::resetCounts()
  268. {
  269. //Reset all variables that are using in drawing
  270. numShapes = 0;
  271. vertCount = 0;
  272. idxBuffCount = 0;
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement