SpriteBatchSample.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "SpriteBatchSample.h"
  2. #include "SamplesGame.h"
  3. #if defined(ADD_SAMPLE)
  4. ADD_SAMPLE("Graphics", "Sprite Batch", SpriteBatchSample, 8);
  5. #endif
  6. SpriteBatchSample::SpriteBatchSample()
  7. : _font(NULL), _spriteBatch(NULL)
  8. {
  9. }
  10. void SpriteBatchSample::initialize()
  11. {
  12. // Create the font for drawing the framerate.
  13. _font = Font::create("res/ui/arial.gpb");
  14. // Create an orthographic projection matrix.
  15. float width = getWidth() / (float)getHeight();
  16. float height = 1.0f;
  17. Matrix::createOrthographic(width, height, -1.0f, 1.0f, &_worldViewProjectionMatrix);
  18. _spriteBatch = SpriteBatch::create("res/png/logo.png");
  19. }
  20. void SpriteBatchSample::finalize()
  21. {
  22. SAFE_DELETE(_spriteBatch);
  23. SAFE_RELEASE(_font);
  24. }
  25. void SpriteBatchSample::update(float elapsedTime)
  26. {
  27. }
  28. void SpriteBatchSample::render(float elapsedTime)
  29. {
  30. // Clear the color and depth buffers
  31. clear(CLEAR_COLOR_DEPTH, Vector4::zero(), 1.0f, 0);
  32. Rectangle dst(0, 0, 64, 64);
  33. Rectangle src(0, 0, 256, 256);
  34. _spriteBatch->start();
  35. // Just a sprite dst from src no color tint
  36. _spriteBatch->draw(dst, src);
  37. // Color tint
  38. _spriteBatch->draw(Rectangle( 64, 0, 64, 64), src, Vector4::fromColor(0xF68B28FF));
  39. _spriteBatch->draw(Rectangle(128, 0, 64, 64), src, Vector4::fromColor(0xDA2128FF));
  40. _spriteBatch->draw(Rectangle(192, 0, 64, 64), src, Vector4::fromColor(0xE21B52FF));
  41. _spriteBatch->draw(Rectangle(256, 0, 64, 64), src, Vector4::fromColor(0xE12991FF));
  42. _spriteBatch->draw(Rectangle(320, 0, 64, 64), src, Vector4::fromColor(0x9A258FFF));
  43. _spriteBatch->draw(Rectangle(384, 0, 64, 64), src, Vector4::fromColor(0x4D3F99FF));
  44. _spriteBatch->draw(Rectangle(448, 0, 64, 64), src, Vector4::fromColor(0x0073BCFF));
  45. _spriteBatch->draw(Rectangle(512, 0, 64, 64), src, Vector4::fromColor(0x00A8DFFF));
  46. _spriteBatch->draw(Rectangle(576, 0, 64, 64), src, Vector4::fromColor(0x00AFADFF));
  47. _spriteBatch->draw(Rectangle(640, 0, 64, 64), src, Vector4::fromColor(0x00A95CFF));
  48. _spriteBatch->draw(Rectangle(704, 0, 64, 64), src, Vector4::fromColor(0x8CC747FF));
  49. _spriteBatch->draw(Rectangle(768, 0, 64, 64), src, Vector4::fromColor(0xFFE710FF));
  50. // Negative height draw over top of the first one
  51. _spriteBatch->draw(Rectangle(0, 0 , 64 * 2.0f, 64 * -2.0f), src);
  52. // Scale
  53. _spriteBatch->draw(Vector3(0, 64, 0), src, Vector2(dst.width * 2.0f, dst.height * 2.0f));
  54. // rotate 90
  55. _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));
  56. _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));
  57. _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));
  58. _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));
  59. _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));
  60. // Lots of them now small
  61. unsigned int pointCount = 16;
  62. unsigned int x = 0;
  63. unsigned int y = 192;
  64. for (unsigned int i = 0; i < pointCount; i++)
  65. {
  66. for (unsigned int j = 0; j < pointCount; j++)
  67. {
  68. _spriteBatch->draw(Rectangle(x, y, 32, 32), src);
  69. x += 32;
  70. }
  71. x = 0;
  72. y += 32;
  73. }
  74. _spriteBatch->finish();
  75. // Draw a second batch to ensure no problems
  76. _spriteBatch->start();
  77. // 50% transparent
  78. _spriteBatch->draw(Rectangle(x + 512, y - 512, 512, 512), src, Vector4(1, 1, 1, 0.5f));
  79. _spriteBatch->finish();
  80. drawFrameRate(_font, Vector4(0, 0.5f, 1, 1), 5, 1, getFrameRate());
  81. }
  82. void SpriteBatchSample::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  83. {
  84. switch (evt)
  85. {
  86. case Touch::TOUCH_PRESS:
  87. if (x < 75 && y < 50)
  88. {
  89. // Toggle Vsync if the user touches the top left corner
  90. setVsync(!isVsync());
  91. }
  92. break;
  93. case Touch::TOUCH_RELEASE:
  94. break;
  95. case Touch::TOUCH_MOVE:
  96. break;
  97. };
  98. }