portable_compressed_texture.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**************************************************************************/
  2. /* portable_compressed_texture.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef PORTABLE_COMPRESSED_TEXTURE_H
  31. #define PORTABLE_COMPRESSED_TEXTURE_H
  32. #include "scene/resources/texture.h"
  33. class BitMap;
  34. class PortableCompressedTexture2D : public Texture2D {
  35. GDCLASS(PortableCompressedTexture2D, Texture2D);
  36. public:
  37. enum DataFormat {
  38. DATA_FORMAT_UNDEFINED,
  39. DATA_FORMAT_IMAGE,
  40. DATA_FORMAT_PNG,
  41. DATA_FORMAT_WEBP,
  42. DATA_FORMAT_BASIS_UNIVERSAL,
  43. };
  44. enum CompressionMode {
  45. COMPRESSION_MODE_LOSSLESS,
  46. COMPRESSION_MODE_LOSSY,
  47. COMPRESSION_MODE_BASIS_UNIVERSAL,
  48. COMPRESSION_MODE_S3TC,
  49. COMPRESSION_MODE_ETC2,
  50. COMPRESSION_MODE_BPTC,
  51. };
  52. private:
  53. CompressionMode compression_mode = COMPRESSION_MODE_LOSSLESS;
  54. static bool keep_all_compressed_buffers;
  55. bool keep_compressed_buffer = false;
  56. Vector<uint8_t> compressed_buffer;
  57. Size2 size;
  58. Size2 size_override;
  59. bool mipmaps = false;
  60. Image::Format format = Image::FORMAT_L8;
  61. mutable RID texture;
  62. mutable Ref<BitMap> alpha_cache;
  63. bool image_stored = false;
  64. protected:
  65. Vector<uint8_t> _get_data() const;
  66. void _set_data(const Vector<uint8_t> &p_data);
  67. static void _bind_methods();
  68. public:
  69. CompressionMode get_compression_mode() const;
  70. void create_from_image(const Ref<Image> &p_image, CompressionMode p_compression_mode, bool p_normal_map = false, float p_lossy_quality = 0.8);
  71. Image::Format get_format() const;
  72. void update(const Ref<Image> &p_image);
  73. Ref<Image> get_image() const override;
  74. int get_width() const override;
  75. int get_height() const override;
  76. virtual RID get_rid() const override;
  77. bool has_alpha() const override;
  78. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;
  79. virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;
  80. virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, bool p_clip_uv = true) const override;
  81. bool is_pixel_opaque(int p_x, int p_y) const override;
  82. virtual void set_path(const String &p_path, bool p_take_over = false) override;
  83. void set_size_override(const Size2 &p_size);
  84. Size2 get_size_override() const;
  85. void set_keep_compressed_buffer(bool p_keep);
  86. bool is_keeping_compressed_buffer() const;
  87. static void set_keep_all_compressed_buffers(bool p_keep);
  88. static bool is_keeping_all_compressed_buffers();
  89. PortableCompressedTexture2D();
  90. ~PortableCompressedTexture2D();
  91. };
  92. VARIANT_ENUM_CAST(PortableCompressedTexture2D::CompressionMode)
  93. #endif // PORTABLE_COMPRESSED_TEXTURE_H