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

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