Texture.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * Copyright (c) 2006-2016 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 "Texture.h"
  21. #include "Graphics.h"
  22. namespace love
  23. {
  24. namespace graphics
  25. {
  26. love::Type Texture::type("Texture", &Drawable::type);
  27. Texture::Filter Texture::defaultFilter;
  28. Texture::Texture()
  29. : format(PIXELFORMAT_UNKNOWN)
  30. , width(0)
  31. , height(0)
  32. , filter(getDefaultFilter())
  33. , wrap()
  34. , vertices()
  35. {
  36. }
  37. Texture::~Texture()
  38. {
  39. }
  40. PixelFormat Texture::getPixelFormat() const
  41. {
  42. return format;
  43. }
  44. void Texture::draw(Graphics *gfx, const Matrix4 &m)
  45. {
  46. drawv(gfx, m, vertices);
  47. }
  48. void Texture::drawq(Graphics *gfx, Quad *quad, const Matrix4 &m)
  49. {
  50. drawv(gfx, m, quad->getVertices());
  51. }
  52. void Texture::drawv(Graphics *gfx, const Matrix4 &localTransform, const Vertex *v)
  53. {
  54. Matrix4 t = gfx->getTransform() * localTransform;
  55. Vertex verts[4] = {v[0], v[1], v[2], v[3]};
  56. t.transform(verts, v, 4);
  57. Color c = toColor(gfx->getColor());
  58. for (int i = 0; i < 4; i++)
  59. verts[i].color = c;
  60. Graphics::StreamDrawRequest req;
  61. req.formats[0] = vertex::CommonFormat::XYf_STf_RGBAub;
  62. req.indexMode = vertex::TriangleIndexMode::QUADS;
  63. req.vertexCount = 4;
  64. req.texture = this;
  65. Graphics::StreamVertexData data = gfx->requestStreamDraw(req);
  66. memcpy(data.stream[0], verts, sizeof(Vertex) * 4);
  67. }
  68. int Texture::getWidth() const
  69. {
  70. return width;
  71. }
  72. int Texture::getHeight() const
  73. {
  74. return height;
  75. }
  76. const Texture::Filter &Texture::getFilter() const
  77. {
  78. return filter;
  79. }
  80. const Texture::Wrap &Texture::getWrap() const
  81. {
  82. return wrap;
  83. }
  84. const Vertex *Texture::getVertices() const
  85. {
  86. return vertices;
  87. }
  88. void Texture::setDefaultFilter(const Filter &f)
  89. {
  90. defaultFilter = f;
  91. }
  92. const Texture::Filter &Texture::getDefaultFilter()
  93. {
  94. return defaultFilter;
  95. }
  96. bool Texture::validateFilter(const Filter &f, bool mipmapsAllowed)
  97. {
  98. if (!mipmapsAllowed && f.mipmap != FILTER_NONE)
  99. return false;
  100. if (f.mag != FILTER_LINEAR && f.mag != FILTER_NEAREST)
  101. return false;
  102. if (f.min != FILTER_LINEAR && f.min != FILTER_NEAREST)
  103. return false;
  104. if (f.mipmap != FILTER_LINEAR && f.mipmap != FILTER_NEAREST && f.mipmap != FILTER_NONE)
  105. return false;
  106. return true;
  107. }
  108. bool Texture::getConstant(const char *in, FilterMode &out)
  109. {
  110. return filterModes.find(in, out);
  111. }
  112. bool Texture::getConstant(FilterMode in, const char *&out)
  113. {
  114. return filterModes.find(in, out);
  115. }
  116. bool Texture::getConstant(const char *in, WrapMode &out)
  117. {
  118. return wrapModes.find(in, out);
  119. }
  120. bool Texture::getConstant(WrapMode in, const char *&out)
  121. {
  122. return wrapModes.find(in, out);
  123. }
  124. StringMap<Texture::FilterMode, Texture::FILTER_MAX_ENUM>::Entry Texture::filterModeEntries[] =
  125. {
  126. { "linear", FILTER_LINEAR },
  127. { "nearest", FILTER_NEAREST },
  128. { "none", FILTER_NONE },
  129. };
  130. StringMap<Texture::FilterMode, Texture::FILTER_MAX_ENUM> Texture::filterModes(Texture::filterModeEntries, sizeof(Texture::filterModeEntries));
  131. StringMap<Texture::WrapMode, Texture::WRAP_MAX_ENUM>::Entry Texture::wrapModeEntries[] =
  132. {
  133. { "clamp", WRAP_CLAMP },
  134. { "clampzero", WRAP_CLAMP_ZERO },
  135. { "repeat", WRAP_REPEAT },
  136. { "mirroredrepeat", WRAP_MIRRORED_REPEAT },
  137. };
  138. StringMap<Texture::WrapMode, Texture::WRAP_MAX_ENUM> Texture::wrapModes(Texture::wrapModeEntries, sizeof(Texture::wrapModeEntries));
  139. } // graphics
  140. } // love