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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*!
  2. * \file src/Sprite.cpp
  3. * \brief World Sprite
  4. *
  5. * \author xythobuz
  6. */
  7. #include "glm/glm.hpp"
  8. #include "global.h"
  9. #include "Camera.h"
  10. #include "Game.h"
  11. #include "Render.h"
  12. #include "TextureManager.h"
  13. #include "Sprite.h"
  14. SpriteSequence::SpriteSequence(int32_t objectID) {
  15. id = objectID;
  16. }
  17. void SpriteSequence::add(Sprite s) {
  18. sprites.push_back(s);
  19. }
  20. unsigned long SpriteSequence::size() {
  21. return sprites.size();
  22. }
  23. Sprite& SpriteSequence::get(unsigned long index) {
  24. assert(index < sprites.size());
  25. return sprites.at(index);
  26. }
  27. // ----------------------------------------------------------------------------
  28. Sprite::Sprite(uint16_t tile, uint8_t x, uint8_t y, uint16_t width, uint16_t height) {
  29. /*!
  30. * \fixme TODO Can't do translation/visibility-check when rendering here.
  31. * We don't know xyz of this Sprite because it could be used by
  32. * different items at other coordinates in the world.
  33. * Do translation/visibility-check at item/room level.
  34. */
  35. const float scale = 4.0f;
  36. const float texelScale = 256.0f;
  37. width >>= 8;
  38. height >>= 8;
  39. int width2 = (int)(width * scale);
  40. int height2 = (int)(height * scale);
  41. vertex[0][0] = -width2 / 2.0f;
  42. vertex[1][0] = -width2 / 2.0f;
  43. vertex[2][0] = width2 / 2.0f;
  44. vertex[3][0] = width2 / 2.0f;
  45. vertex[0][1] = 0;
  46. vertex[1][1] = -height2;
  47. vertex[2][1] = -height2;
  48. vertex[3][1] = 0;
  49. vertex[0][2] = 0;
  50. vertex[1][2] = 0;
  51. vertex[2][2] = 0;
  52. vertex[3][2] = 0;
  53. texel[3][0] = (float)(x + width) / texelScale;
  54. texel[3][1] = (float)(y + height) / texelScale;
  55. texel[2][0] = (float)(x + width) / texelScale;
  56. texel[2][1] = (float)(y) / texelScale;
  57. texel[1][0] = (float)(x) / texelScale;
  58. texel[1][1] = (float)(y) / texelScale;
  59. texel[0][0] = (float)(x) / texelScale;
  60. texel[0][1] = (float)(y + height) / texelScale;
  61. texture = tile;
  62. //radius = width2 / 2.0f;
  63. }
  64. void Sprite::display() {
  65. /*
  66. //if (!Render::isVisible(pos[0], pos[1], pos[2], radius))
  67. // return;
  68. glPushMatrix();
  69. //glTranslated(pos[0], pos[1], pos[2]);
  70. // Sprites must always face camera, because they have no depth =)
  71. glRotated(glm::degrees(Camera::getRadianYaw()), 0, 1, 0);
  72. switch (Render::getMode()) {
  73. // No vertex lighting on sprites, as far as I see in specs
  74. // So just draw normal texture, no case 2
  75. case RenderMode::Solid:
  76. glBegin(GL_TRIANGLE_STRIP);
  77. glColor3f(texel[0][0], texel[0][1], 0.5);
  78. glVertex3fv(vertex[0]);
  79. glColor3f(texel[1][0], texel[1][1], 0.5);
  80. glVertex3fv(vertex[1]);
  81. glColor3f(texel[3][0], texel[3][1], 0.5);
  82. glVertex3fv(vertex[3]);
  83. glColor3f(texel[2][0], texel[2][1], 0.5);
  84. glVertex3fv(vertex[2]);
  85. glEnd();
  86. break;
  87. case RenderMode::Wireframe:
  88. glColor3ubv(CYAN);
  89. glBegin(GL_LINE_LOOP);
  90. glVertex3fv(vertex[0]);
  91. glVertex3fv(vertex[1]);
  92. glVertex3fv(vertex[2]);
  93. glVertex3fv(vertex[3]);
  94. glEnd();
  95. glColor3ubv(WHITE);
  96. break;
  97. default:
  98. getTextureManager().bindTextureId(texture);
  99. glBegin(GL_TRIANGLE_STRIP);
  100. glTexCoord2fv(texel[0]);
  101. glVertex3fv(vertex[0]);
  102. glTexCoord2fv(texel[1]);
  103. glVertex3fv(vertex[1]);
  104. glTexCoord2fv(texel[3]);
  105. glVertex3fv(vertex[3]);
  106. glTexCoord2fv(texel[2]);
  107. glVertex3fv(vertex[2]);
  108. glEnd();
  109. }
  110. glPopMatrix();
  111. */
  112. }
  113. void Sprite::display(float x, float y, float w, float h) {
  114. /*
  115. float z = 0.0f;
  116. getTextureManager().bindTextureId(texture);
  117. glBegin(GL_QUADS);
  118. glTexCoord2fv(texel[1]);
  119. glVertex3f(x, y, z);
  120. glTexCoord2fv(texel[2]);
  121. glVertex3f(x + w, y, z);
  122. glTexCoord2fv(texel[3]);
  123. glVertex3f(x + w, y + h, z);
  124. glTexCoord2fv(texel[0]);
  125. glVertex3f(x, y + h, z);
  126. glEnd();
  127. */
  128. }