Glyph.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Copyright (c) 2006-2010 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "Glyph.h"
  21. // STD
  22. #include <cstring> // For memcpy
  23. namespace love
  24. {
  25. namespace graphics
  26. {
  27. namespace opengl
  28. {
  29. Glyph::Glyph(love::font::GlyphData * data)
  30. : data(data), width((float)data->getWidth()), height((float)data->getHeight()), texture(0)
  31. {
  32. data->retain();
  33. data->getWidth();
  34. memset(vertices, 255, sizeof(vertex)*4);
  35. vertices[0].x = 0; vertices[0].y = 0;
  36. vertices[1].x = 0; vertices[1].y = height;
  37. vertices[2].x = width; vertices[2].y = height;
  38. vertices[3].x = width; vertices[3].y = 0;
  39. vertices[0].s = 0; vertices[0].t = 0;
  40. vertices[1].s = 0; vertices[1].t = 1;
  41. vertices[2].s = 1; vertices[2].t = 1;
  42. vertices[3].s = 1; vertices[3].t = 0;
  43. }
  44. Glyph::~Glyph()
  45. {
  46. if(data != 0)
  47. data->release();
  48. unload();
  49. }
  50. void Glyph::draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const
  51. {
  52. static Matrix t;
  53. t.setTransformation(x, y, angle, sx, sy, ox, oy);
  54. if(texture != 0)
  55. glBindTexture(GL_TEXTURE_2D,texture);
  56. glPushMatrix();
  57. glMultMatrixf((const GLfloat*)t.getElements());
  58. glEnableClientState(GL_VERTEX_ARRAY);
  59. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  60. glVertexPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid*)&vertices[0].x);
  61. glTexCoordPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid*)&vertices[0].s);
  62. glDrawArrays(GL_QUADS, 0, 4);
  63. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  64. glDisableClientState(GL_VERTEX_ARRAY);
  65. glPopMatrix();
  66. }
  67. bool Glyph::load()
  68. {
  69. return loadVolatile();
  70. }
  71. void Glyph::unload()
  72. {
  73. unloadVolatile();
  74. }
  75. bool Glyph::loadVolatile()
  76. {
  77. glGenTextures(1,&texture);
  78. glBindTexture(GL_TEXTURE_2D, texture);
  79. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  80. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  81. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  82. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  83. glTexImage2D(GL_TEXTURE_2D,
  84. 0,
  85. GL_RGBA,
  86. (GLsizei)width,
  87. (GLsizei)height,
  88. 0,
  89. GL_RGBA,
  90. GL_UNSIGNED_BYTE,
  91. data->getData());
  92. return true;
  93. }
  94. void Glyph::unloadVolatile()
  95. {
  96. // Delete the hardware texture.
  97. if(texture != 0)
  98. {
  99. glDeleteTextures(1, (GLuint*)&texture);
  100. texture = 0;
  101. }
  102. }
  103. } // opengl
  104. } // graphics
  105. } // love