Simple game engine with complete export to scripting language
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.

TextureImage.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "TextureImage.h"
  2. #include "RenderDevice.h"
  3. #include "Utilities/Log.h"
  4. #include <cmath>
  5. #if defined(max)
  6. #define std::max max
  7. #else
  8. #define max(a,b) ( a > b ? a : b )
  9. #endif
  10. namespace BlueCore
  11. {
  12. //------------------------------------------------------------------------------
  13. TextureImage::TextureImage(RenderDevice* device, Texture *texture, float ax,
  14. float ay, float bx, float by) :
  15. _Texture(texture), _Device(device)
  16. {
  17. unsigned int texture_width = _Texture->getWidth();
  18. unsigned int texture_height = _Texture->getHeight();
  19. // greater 0, and values < 1 are fractions of texture dimemsions
  20. float e_ax= max( ax <= 1.0 ? texture_width * ax : ax, 0.0f );
  21. float e_ay= max( ay <= 1.0 ? texture_height * ay : ay, 0.0f );
  22. float e_bx= max( bx <= 1.0 ? texture_width * bx : bx, 0.0f );
  23. float e_by= max( by <= 1.0 ? texture_height * by : by, 0.0f );
  24. float width = e_bx - e_ax;
  25. float height = e_by - e_ay;
  26. // fractional values
  27. float au= max( ax > 1.0 ? ax / texture_width : ax, 0.0f );
  28. float av= max( ay > 1.0 ? ay / texture_height : ay, 0.0f );
  29. float bu= max( bx > 1.0 ? bx / texture_width : bx, 0.0f );
  30. float bv= max( by > 1.0 ? by / texture_height : by, 0.0f );
  31. // bottom left
  32. _Vertices[0] = 0.0;
  33. _Vertices[1] = 0.0;
  34. _TexCoords[0] = au;
  35. _TexCoords[1] = 1.0-bv;
  36. // top left
  37. _Vertices[2] = 0.0;
  38. _Vertices[3] = height-1;
  39. _TexCoords[2] = au;
  40. _TexCoords[3] = 1.0-av;
  41. // top right
  42. _Vertices[4] = width-1;
  43. _Vertices[5] = height-1;
  44. _TexCoords[4] = bu;
  45. _TexCoords[5] = 1.0-av;
  46. // bottom right
  47. _Vertices[6] = width-1;
  48. _Vertices[7] = 0.0;
  49. _TexCoords[6] = bu;
  50. _TexCoords[7] = 1.0-bv;
  51. _Width = (unsigned int)width;
  52. _Height = (unsigned int)height;
  53. clog << ">>> TextureImage constructed..."<< endlog;
  54. }
  55. //------------------------------------------------------------------------------
  56. TextureImage::~TextureImage()
  57. {
  58. clog << ">>> TextureImage destructed..."<< endlog;
  59. }
  60. //------------------------------------------------------------------------------
  61. void TextureImage::draw(float x, float y, int halign, int valign, float r)
  62. {
  63. if (!_Device.valid())
  64. return;
  65. _Device->begin2D();
  66. if (x < 0.0)
  67. x += _Device->getViewportWidth();
  68. else if (x < 1.0)
  69. x *= _Device->getViewportWidth();
  70. if (halign == -1)
  71. x -= _Width;
  72. else if (halign == 0)
  73. x -= _Width / 2;
  74. if (y < 0.0)
  75. y += _Device->getViewportHeight();
  76. else if (y < 1.0)
  77. y *= _Device->getViewportHeight();
  78. if (valign == -1)
  79. y -= _Height;
  80. else if (valign == 0)
  81. y -= _Height / 2;
  82. glEnable(GL_TEXTURE_2D);
  83. glBindTexture(GL_TEXTURE_2D, _Texture->getId() );
  84. glPushMatrix();
  85. glLoadIdentity();
  86. glTranslatef(x, y, 0);
  87. if (r != 0.)
  88. glRotatef(r, 0.0, 0.0, 1.0);
  89. glBegin(GL_QUADS);
  90. glTexCoord2f(_TexCoords[2], _TexCoords[3]);
  91. glVertex2f(_Vertices[2], _Vertices[3]);
  92. glTexCoord2f(_TexCoords[0], _TexCoords[1]);
  93. glVertex2f(_Vertices[0], _Vertices[1]);
  94. glTexCoord2f(_TexCoords[6], _TexCoords[7]);
  95. glVertex2f(_Vertices[6], _Vertices[7]);
  96. glTexCoord2f(_TexCoords[4], _TexCoords[5]);
  97. glVertex2f(_Vertices[4], _Vertices[5]);
  98. glEnd();
  99. glPopMatrix();
  100. _Device->end2D();
  101. }
  102. //------------------------------------------------------------------------------
  103. unsigned int TextureImage::getWidth()
  104. {
  105. return _Width;
  106. }
  107. //------------------------------------------------------------------------------
  108. unsigned int TextureImage::getHeight()
  109. {
  110. return _Height;
  111. }
  112. } // namespace BlueCore