Image.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #ifndef LOVE_GRAPHICS_OPENGL_IMAGE_H
  21. #define LOVE_GRAPHICS_OPENGL_IMAGE_H
  22. // LOVE
  23. #include "common/config.h"
  24. #include "common/Matrix.h"
  25. #include "common/Vector.h"
  26. #include "common/StringMap.h"
  27. #include "common/math.h"
  28. #include "image/ImageData.h"
  29. #include "image/CompressedImageData.h"
  30. #include "graphics/Texture.h"
  31. #include "graphics/Volatile.h"
  32. // OpenGL
  33. #include "OpenGL.h"
  34. namespace love
  35. {
  36. namespace graphics
  37. {
  38. namespace opengl
  39. {
  40. /**
  41. * A drawable image based on OpenGL-textures. This class takes ImageData
  42. * objects and create textures on the GPU for fast drawing.
  43. *
  44. * @author Anders Ruud
  45. **/
  46. class Image : public Texture, public Volatile
  47. {
  48. public:
  49. static love::Type type;
  50. enum SettingType
  51. {
  52. SETTING_MIPMAPS,
  53. SETTING_LINEAR,
  54. SETTING_PIXELDENSITY,
  55. SETTING_MAX_ENUM
  56. };
  57. struct Settings
  58. {
  59. bool mipmaps = false;
  60. bool linear = false;
  61. float pixeldensity = 1.0f;
  62. };
  63. /**
  64. * Creates a new Image. Not that anything is ready to use
  65. * before load is called.
  66. *
  67. * @param data The data from which to load the image. Each element in the
  68. * array is a mipmap level. If more than the base level is present, all
  69. * mip levels must be present.
  70. **/
  71. Image(const std::vector<love::image::ImageData *> &data, const Settings &settings);
  72. /**
  73. * Creates a new Image with compressed image data.
  74. *
  75. * @param cdata The compressed data from which to load the image.
  76. **/
  77. Image(const std::vector<love::image::CompressedImageData *> &cdata, const Settings &settings);
  78. virtual ~Image();
  79. // Implements Volatile.
  80. bool loadVolatile() override;
  81. void unloadVolatile() override;
  82. ptrdiff_t getHandle() const override;
  83. const std::vector<StrongRef<love::image::ImageData>> &getImageData() const;
  84. const std::vector<StrongRef<love::image::CompressedImageData>> &getCompressedData() const;
  85. void setFilter(const Texture::Filter &f) override;
  86. bool setWrap(const Texture::Wrap &w) override;
  87. void setMipmapSharpness(float sharpness);
  88. float getMipmapSharpness() const;
  89. /**
  90. * Whether this Image is using a compressed texture (via CompressedImageData).
  91. **/
  92. bool isCompressed() const;
  93. /**
  94. * Re-uploads the ImageData or CompressedImageData associated with this Image to
  95. * the GPU.
  96. **/
  97. bool refresh(int xoffset, int yoffset, int w, int h);
  98. const Settings &getFlags() const;
  99. static bool isFormatSupported(PixelFormat pixelformat);
  100. static bool hasSRGBSupport();
  101. static bool getConstant(const char *in, SettingType &out);
  102. static bool getConstant(SettingType in, const char *&out);
  103. static int imageCount;
  104. private:
  105. void preload();
  106. void generateMipmaps();
  107. void loadDefaultTexture();
  108. void loadFromCompressedData();
  109. void loadFromImageData();
  110. // The ImageData from which the texture is created. May be empty if
  111. // Compressed image data was used to create the texture.
  112. // Each element in the array is a mipmap level.
  113. std::vector<StrongRef<love::image::ImageData>> data;
  114. // Or the Compressed Image Data from which the texture is created. May be
  115. // empty if raw ImageData was used to create the texture.
  116. std::vector<StrongRef<love::image::CompressedImageData>> cdata;
  117. // OpenGL texture identifier.
  118. GLuint texture;
  119. // Mipmap texture LOD bias (sharpness) value.
  120. float mipmapSharpness;
  121. // Whether this Image is using a compressed texture.
  122. bool compressed;
  123. // The settings used to initialize this Image.
  124. Settings settings;
  125. bool sRGB;
  126. // True if the image wasn't able to be properly created and it had to fall
  127. // back to a default texture.
  128. bool usingDefaultTexture;
  129. size_t textureMemorySize;
  130. static float maxMipmapSharpness;
  131. static StringMap<SettingType, SETTING_MAX_ENUM>::Entry settingTypeEntries[];
  132. static StringMap<SettingType, SETTING_MAX_ENUM> settingTypes;
  133. }; // Image
  134. } // opengl
  135. } // graphics
  136. } // love
  137. #endif // LOVE_GRAPHICS_OPENGL_IMAGE_H