CompressedData.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * Copyright (c) 2006-2013 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 "CompressedData.h"
  21. namespace love
  22. {
  23. namespace image
  24. {
  25. CompressedData::CompressedData()
  26. : type(TYPE_UNKNOWN)
  27. {
  28. }
  29. CompressedData::~CompressedData()
  30. {
  31. }
  32. int CompressedData::getSize() const
  33. {
  34. // Adding up the total size for all mipmap levels would make more sense, but
  35. // it's probably better for getSize() to match getData() so no bad memory
  36. // accesses happen...
  37. if (dataImages.size() > 0)
  38. return dataImages[0].size;
  39. else
  40. return 0;
  41. }
  42. void *CompressedData::getData() const
  43. {
  44. // Data for different mipmap levels is not stored contiguously in memory, so
  45. // getData() won't work properly for CompressedData.
  46. if (dataImages.size() > 0 && dataImages[0].size > 0)
  47. return (void *) dataImages[0].data;
  48. else
  49. return 0;
  50. }
  51. int CompressedData::getMipmapCount() const
  52. {
  53. return dataImages.size();
  54. }
  55. int CompressedData::getSize(int miplevel) const
  56. {
  57. checkMipmapLevelExists(miplevel);
  58. return dataImages[miplevel].size;
  59. }
  60. void *CompressedData::getData(int miplevel) const
  61. {
  62. checkMipmapLevelExists(miplevel);
  63. return (void *) &dataImages[miplevel].data[0];
  64. }
  65. int CompressedData::getWidth(int miplevel) const
  66. {
  67. checkMipmapLevelExists(miplevel);
  68. return dataImages[miplevel].width;
  69. }
  70. int CompressedData::getHeight(int miplevel) const
  71. {
  72. checkMipmapLevelExists(miplevel);
  73. return dataImages[miplevel].height;
  74. }
  75. CompressedData::TextureType CompressedData::getType() const
  76. {
  77. return type;
  78. }
  79. void CompressedData::checkMipmapLevelExists(int miplevel) const
  80. {
  81. if (miplevel < 0 || miplevel >= (int) dataImages.size())
  82. throw love::Exception("Mipmap level %d does not exist", miplevel);
  83. }
  84. bool CompressedData::getConstant(const char *in, CompressedData::TextureType &out)
  85. {
  86. return types.find(in, out);
  87. }
  88. bool CompressedData::getConstant(CompressedData::TextureType in, const char *&out)
  89. {
  90. return types.find(in, out);
  91. }
  92. StringMap<CompressedData::TextureType, CompressedData::TYPE_MAX_ENUM>::Entry CompressedData::typeEntries[] =
  93. {
  94. {"unknown", CompressedData::TYPE_UNKNOWN},
  95. {"dxt1", CompressedData::TYPE_DXT1},
  96. {"dxt3", CompressedData::TYPE_DXT3},
  97. {"dxt5", CompressedData::TYPE_DXT5},
  98. {"bc5", CompressedData::TYPE_BC5},
  99. {"bc5s", CompressedData::TYPE_BC5s},
  100. };
  101. StringMap<CompressedData::TextureType, CompressedData::TYPE_MAX_ENUM> CompressedData::types(CompressedData::typeEntries, sizeof(CompressedData::typeEntries));
  102. } // image
  103. } // love