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 | |
31 | #ifndef PORTABLE_COMPRESSED_TEXTURE_H |
32 | #define PORTABLE_COMPRESSED_TEXTURE_H |
33 | |
34 | #include "scene/resources/texture.h" |
35 | |
36 | class BitMap; |
37 | |
38 | class PortableCompressedTexture2D : public Texture2D { |
39 | GDCLASS(PortableCompressedTexture2D, Texture2D); |
40 | |
41 | public: |
42 | enum CompressionMode { |
43 | COMPRESSION_MODE_LOSSLESS, |
44 | COMPRESSION_MODE_LOSSY, |
45 | COMPRESSION_MODE_BASIS_UNIVERSAL, |
46 | COMPRESSION_MODE_S3TC, |
47 | COMPRESSION_MODE_ETC2, |
48 | COMPRESSION_MODE_BPTC, |
49 | }; |
50 | |
51 | private: |
52 | CompressionMode compression_mode = COMPRESSION_MODE_LOSSLESS; |
53 | static bool keep_all_compressed_buffers; |
54 | bool keep_compressed_buffer = false; |
55 | Vector<uint8_t> compressed_buffer; |
56 | Size2 size; |
57 | Size2 size_override; |
58 | bool mipmaps = false; |
59 | Image::Format format = Image::FORMAT_L8; |
60 | |
61 | mutable RID texture; |
62 | mutable Ref<BitMap> alpha_cache; |
63 | |
64 | bool image_stored = false; |
65 | |
66 | protected: |
67 | Vector<uint8_t> _get_data() const; |
68 | void _set_data(const Vector<uint8_t> &p_data); |
69 | |
70 | static void _bind_methods(); |
71 | |
72 | public: |
73 | CompressionMode get_compression_mode() const; |
74 | void create_from_image(const Ref<Image> &p_image, CompressionMode p_compression_mode, bool p_normal_map = false, float p_lossy_quality = 0.8); |
75 | |
76 | Image::Format get_format() const; |
77 | |
78 | void update(const Ref<Image> &p_image); |
79 | Ref<Image> get_image() const override; |
80 | |
81 | int get_width() const override; |
82 | int get_height() const override; |
83 | |
84 | virtual RID get_rid() const override; |
85 | |
86 | bool has_alpha() const override; |
87 | 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; |
88 | 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; |
89 | 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; |
90 | |
91 | bool is_pixel_opaque(int p_x, int p_y) const override; |
92 | |
93 | virtual void set_path(const String &p_path, bool p_take_over = false) override; |
94 | |
95 | void set_size_override(const Size2 &p_size); |
96 | Size2 get_size_override() const; |
97 | |
98 | void set_keep_compressed_buffer(bool p_keep); |
99 | bool is_keeping_compressed_buffer() const; |
100 | |
101 | static void set_keep_all_compressed_buffers(bool p_keep); |
102 | static bool is_keeping_all_compressed_buffers(); |
103 | |
104 | PortableCompressedTexture2D(); |
105 | ~PortableCompressedTexture2D(); |
106 | }; |
107 | |
108 | VARIANT_ENUM_CAST(PortableCompressedTexture2D::CompressionMode) |
109 | |
110 | #endif // PORTABLE_COMPRESSED_TEXTURE_H |
111 | |