Advertisement
Guest User

Untitled

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