Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*!
  2. * \file src/Sprite.cpp
  3. * \brief World Sprite
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Camera.h"
  9. #include "Render.h"
  10. #include "TextureManager.h"
  11. #include "World.h"
  12. #include "Sprite.h"
  13. const static float scale = 4.0f;
  14. const static float texelScale = 256.0f;
  15. const static int texelOffset = 2;
  16. Sprite::Sprite(int tile, int x, int y, int width, int height) : texture(tile) {
  17. width >>= 8;
  18. height >>= 8;
  19. int width2 = (int)(width * scale);
  20. int height2 = (int)(height * scale);
  21. std::vector<glm::vec2> uv;
  22. uv.emplace_back(float(x + texelOffset) / texelScale, float(y + height) / texelScale);
  23. uv.emplace_back(float(x + texelOffset) / texelScale, float(y + texelOffset) / texelScale);
  24. uv.emplace_back(float(x + width) / texelScale, float(y + texelOffset) / texelScale);
  25. uv.emplace_back(float(x + width) / texelScale, float(y + height) / texelScale);
  26. uv.emplace_back(uv.at(0));
  27. uv.emplace_back(uv.at(2));
  28. std::vector<glm::vec3> vert;
  29. vert.emplace_back(float(-width2) / 2.0f, 0.0f, 0.0f);
  30. vert.emplace_back(float(-width2) / 2.0f, float(-height2), 0.0f);
  31. vert.emplace_back(float(width2) / 2.0f, float(-height2), 0.0f);
  32. vert.emplace_back(float(width2) / 2.0f, 0.0f, 0.0f);
  33. vert.emplace_back(vert.at(0));
  34. vert.emplace_back(vert.at(2));
  35. vertices.bufferData(vert);
  36. uvs.bufferData(uv);
  37. uv2D = glm::vec4(uv.at(0), uv.at(2));
  38. }
  39. void Sprite::display(glm::mat4 MVP) {
  40. Shader::drawGL(vertices, uvs, texture, MVP);
  41. }
  42. // ----------------------------------------------------------------------------
  43. void SpriteSequence::display(glm::mat4 MVP, int index) {
  44. assertGreaterThanEqual(index, 0);
  45. assertLessThan(index, length);
  46. getWorld().getSprite(start + index).display(MVP);
  47. }