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.

Sprite.cpp 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*!
  2. * \file src/Sprite.cpp
  3. * \brief 2D Billboard Collection
  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. uvBuff.emplace_back(float(x + texelOffset) / texelScale, float(y + height) / texelScale);
  22. uvBuff.emplace_back(float(x + texelOffset) / texelScale, float(y + texelOffset) / texelScale);
  23. uvBuff.emplace_back(float(x + width) / texelScale, float(y + texelOffset) / texelScale);
  24. uvBuff.emplace_back(float(x + width) / texelScale, float(y + height) / texelScale);
  25. uvBuff.emplace_back(uvBuff.at(0));
  26. uvBuff.emplace_back(uvBuff.at(2));
  27. vertexBuff.emplace_back(float(-width2) / 2.0f, 0.0f, 0.0f);
  28. vertexBuff.emplace_back(float(-width2) / 2.0f, float(-height2), 0.0f);
  29. vertexBuff.emplace_back(float(width2) / 2.0f, float(-height2), 0.0f);
  30. vertexBuff.emplace_back(float(width2) / 2.0f, 0.0f, 0.0f);
  31. vertexBuff.emplace_back(vertexBuff.at(0));
  32. vertexBuff.emplace_back(vertexBuff.at(2));
  33. uv2D = glm::vec4(uvBuff.at(0), uvBuff.at(2));
  34. }
  35. void Sprite::display(glm::mat4 MVP) {
  36. Shader::drawGL(vertexBuff, uvBuff, MVP, texture, TextureStorage::GAME);
  37. }
  38. // ----------------------------------------------------------------------------
  39. void SpriteSequence::display(glm::mat4 MVP, int index) {
  40. orAssertGreaterThanEqual(index, 0);
  41. orAssertLessThan(index, length);
  42. World::getSprite(start + index).display(MVP);
  43. }