SpriteBatchSample.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "SpriteBatchSample.h"
  2. #include "SamplesGame.h"
  3. #if defined(ADD_SAMPLE)
  4. ADD_SAMPLE("Graphics", "Sprite Batch", SpriteBatchSample, 8);
  5. #endif
  6. /**
  7. * Creates a triangle mesh with vertex colors.
  8. */
  9. static Mesh* createTriangleMesh()
  10. {
  11. // Calculate the vertices of the equilateral triangle.
  12. float a = 0.5f; // length of the side
  13. Vector2 p1(0.0f, a / sqrtf(3.0f));
  14. Vector2 p2(-a / 2.0f, -a / (2.0f * sqrtf(3.0f)));
  15. Vector2 p3( a / 2.0f, -a / (2.0f * sqrtf(3.0f)));
  16. // Create 3 vertices.
  17. // Each vertex has position (x, y, z) and color (red, green, blue)
  18. float vertices[] =
  19. {
  20. p1.x, p1.y, 0.0f, 1.0f, 0.0f, 0.0f,
  21. p2.x, p2.y, 0.0f, 0.0f, 1.0f, 0.0f,
  22. p3.x, p3.y, 0.0f, 0.0f, 0.0f, 1.0f,
  23. };
  24. unsigned int vertexCount = 3;
  25. VertexFormat::Element elements[] =
  26. {
  27. VertexFormat::Element(VertexFormat::POSITION, 3),
  28. VertexFormat::Element(VertexFormat::COLOR, 3)
  29. };
  30. Mesh* mesh = Mesh::createMesh(VertexFormat(elements, 2), vertexCount, false);
  31. if (mesh == NULL)
  32. {
  33. GP_ERROR("Failed to create mesh.");
  34. return NULL;
  35. }
  36. mesh->setPrimitiveType(Mesh::TRIANGLES);
  37. mesh->setVertexData(vertices, 0, vertexCount);
  38. return mesh;
  39. }
  40. SpriteBatchSample::SpriteBatchSample()
  41. : _font(NULL), _spriteBatch(NULL)
  42. {
  43. }
  44. void SpriteBatchSample::initialize()
  45. {
  46. // Create the font for drawing the framerate.
  47. _font = Font::create("res/common/arial18.gpb");
  48. // Create an orthographic projection matrix.
  49. float width = getWidth() / (float)getHeight();
  50. float height = 1.0f;
  51. Matrix::createOrthographic(width, height, -1.0f, 1.0f, &_worldViewProjectionMatrix);
  52. _spriteBatch = SpriteBatch::create("res/png/box-diffuse.png");
  53. }
  54. void SpriteBatchSample::finalize()
  55. {
  56. SAFE_DELETE(_spriteBatch);
  57. SAFE_RELEASE(_font);
  58. }
  59. void SpriteBatchSample::update(float elapsedTime)
  60. {
  61. }
  62. void SpriteBatchSample::render(float elapsedTime)
  63. {
  64. // Clear the color and depth buffers
  65. clear(CLEAR_COLOR_DEPTH, Vector4::zero(), 1.0f, 0);
  66. Rectangle dst(0, 0, 64, 64);
  67. Rectangle src(0, 0, 256, 256);
  68. _spriteBatch->start();
  69. // Just a sprite dst from src no color tint
  70. _spriteBatch->draw(dst, src);
  71. // Color tint
  72. _spriteBatch->draw(Rectangle( 64, 0, 64, 64), src, Vector4::fromColor(0xF68B28FF));
  73. _spriteBatch->draw(Rectangle(128, 0, 64, 64), src, Vector4::fromColor(0xDA2128FF));
  74. _spriteBatch->draw(Rectangle(192, 0, 64, 64), src, Vector4::fromColor(0xE21B52FF));
  75. _spriteBatch->draw(Rectangle(256, 0, 64, 64), src, Vector4::fromColor(0xE12991FF));
  76. _spriteBatch->draw(Rectangle(320, 0, 64, 64), src, Vector4::fromColor(0x9A258FFF));
  77. _spriteBatch->draw(Rectangle(384, 0, 64, 64), src, Vector4::fromColor(0x4D3F99FF));
  78. _spriteBatch->draw(Rectangle(448, 0, 64, 64), src, Vector4::fromColor(0x0073BCFF));
  79. _spriteBatch->draw(Rectangle(512, 0, 64, 64), src, Vector4::fromColor(0x00A8DFFF));
  80. _spriteBatch->draw(Rectangle(576, 0, 64, 64), src, Vector4::fromColor(0x00AFADFF));
  81. _spriteBatch->draw(Rectangle(640, 0, 64, 64), src, Vector4::fromColor(0x00A95CFF));
  82. _spriteBatch->draw(Rectangle(704, 0, 64, 64), src, Vector4::fromColor(0x8CC747FF));
  83. _spriteBatch->draw(Rectangle(768, 0, 64, 64), src, Vector4::fromColor(0xFFE710FF));
  84. // Negative height draw over top of the first one
  85. _spriteBatch->draw(Rectangle(0, 0 , 64 * 2.0f, 64 * -2.0f), src);
  86. // Scale
  87. _spriteBatch->draw(Vector3(0, 64, 0), src, Vector2(dst.width * 2.0f, dst.height * 2.0f));
  88. // rotate 90
  89. _spriteBatch->draw(Vector3(128, 64, 0), src, Vector2(128, 128), Vector4(1, 1, 1, 1), Vector2(0.5f, 0.5f), MATH_DEG_TO_RAD(90));
  90. _spriteBatch->draw(Vector3(256, 64, 0), src, Vector2(128, 128), Vector4(1, 1, 1, 1), Vector2(0.5f, 0.5f), MATH_DEG_TO_RAD(180));
  91. _spriteBatch->draw(Vector3(384, 64, 0), src, Vector2(128, 128), Vector4(1, 1, 1, 1), Vector2(0.5f, 0.5f), MATH_DEG_TO_RAD(270));
  92. _spriteBatch->draw(Vector3(512, 64, 0), src, Vector2(128, 128), Vector4(1, 1, 1, 1), Vector2(0.5f, 0.5f), MATH_DEG_TO_RAD(360));
  93. _spriteBatch->draw(Vector3(640, 64, 0), src, Vector2(128, 128), Vector4(1, 1, 1, 1), Vector2(0.5f, 0.5f), MATH_DEG_TO_RAD(0));
  94. // Lots of them now small
  95. unsigned int pointCount = 16;
  96. unsigned int x = 0;
  97. unsigned int y = 192;
  98. for (unsigned int i = 0; i < pointCount; i++)
  99. {
  100. for (unsigned int j = 0; j < pointCount; j++)
  101. {
  102. _spriteBatch->draw(Rectangle(x, y, 32, 32), src);
  103. x += 32;
  104. }
  105. x = 0;
  106. y += 32;
  107. }
  108. _spriteBatch->finish();
  109. // Draw a second batch to ensure no problems
  110. _spriteBatch->start();
  111. // 50% transparent
  112. _spriteBatch->draw(Rectangle(x + 512, y - 512, 512, 512), src, Vector4(1, 1, 1, 0.5f));
  113. _spriteBatch->finish();
  114. drawFrameRate(_font, Vector4(0, 0.5f, 1, 1), 5, 1, getFrameRate());
  115. }
  116. void SpriteBatchSample::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  117. {
  118. switch (evt)
  119. {
  120. case Touch::TOUCH_PRESS:
  121. if (x < 75 && y < 50)
  122. {
  123. // Toggle Vsync if the user touches the top left corner
  124. setVsync(!isVsync());
  125. }
  126. break;
  127. case Touch::TOUCH_RELEASE:
  128. break;
  129. case Touch::TOUCH_MOVE:
  130. break;
  131. };
  132. }