1 /** @file texture.h  Logical texture resource.
2  *
3  * @authors Copyright © 2003-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
4  * @authors Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
5  *
6  * @par License
7  * GPL: http://www.gnu.org/licenses/gpl.html
8  *
9  * <small>This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by the
11  * Free Software Foundation; either version 2 of the License, or (at your
12  * option) any later version. This program is distributed in the hope that it
13  * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15  * Public License for more details. You should have received a copy of the GNU
16  * General Public License along with this program; if not, write to the Free
17  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18  * 02110-1301 USA</small>
19  */
20 
21 #ifndef LIBDOOMSDAY_RESOURCE_TEXTURE_H
22 #define LIBDOOMSDAY_RESOURCE_TEXTURE_H
23 
24 #include "../libdoomsday.h"
25 
26 #include <de/Error>
27 #include <de/Observers>
28 #include <de/Vector>
29 #include <QFlag>
30 #include <QList>
31 
32 namespace res {
33 
34 class TextureManifest;
35 
36 /**
37  * Logical texture resource.
38  *
39  * @ingroup resource
40  */
41 class LIBDOOMSDAY_PUBLIC Texture
42 {
43 public:
44     DENG2_DEFINE_AUDIENCE(Deletion,         void textureBeingDeleted      (Texture const &))
45     DENG2_DEFINE_AUDIENCE(DimensionsChange, void textureDimensionsChanged (Texture const &))
46 
47     /**
48      * Classification/processing flags.
49      */
50     enum Flag
51     {
52         /// Texture is not to be drawn.
53         NoDraw              = 0x1,
54 
55         /// Texture is "custom" (i.e., not an original game resource).
56         Custom              = 0x2,
57 
58         /// Apply the monochrome filter to the processed image.
59         Monochrome          = 0x4,
60 
61         /// Apply the upscaleAndSharpen filter to the processed image.
62         UpscaleAndSharpen   = 0x8
63     };
64     Q_DECLARE_FLAGS(Flags, Flag)
65 
66     /**
67      * Image analysis identifiers.
68      */
69     enum AnalysisId
70     {
71         /// Color palette info.
72         ColorPaletteAnalysis,
73 
74         /// Brightest point for automatic light sources.
75         BrightPointAnalysis,
76 
77         /// Average color.
78         AverageColorAnalysis,
79 
80         /// Average color amplified (max component ==1).
81         AverageColorAmplifiedAnalysis,
82 
83         /// Average alpha.
84         AverageAlphaAnalysis,
85 
86         /// Average top line color.
87         AverageTopColorAnalysis,
88 
89         /// Average bottom line color.
90         AverageBottomColorAnalysis
91     };
92 
93 public:
94     /**
95      * @param manifest  Manifest derived to yield the texture.
96      */
97     Texture(TextureManifest &manifest);
98 
99     virtual ~Texture();
100 
101     /**
102      * Returns the TextureManifest derived to yield the texture.
103      */
104     TextureManifest &manifest() const;
105 
106     /**
107      * Returns a brief textual description/overview of the texture.
108      *
109      * @return Human-friendly description/overview of the texture.
110      */
111     virtual de::String description() const;
112 
113     /**
114      * Returns the world dimensions of the texture, in map coordinate space
115      * units. The DimensionsChange audience is notified whenever dimensions
116      * are changed.
117      */
118     de::Vector2ui const &dimensions() const;
119 
120     /**
121      * Convenient accessor method for returning the X axis size (width) of
122      * the world dimensions for the texture, in map coordinate space units.
123      *
124      * @see dimensions()
125      */
width()126     inline int width() const { return int(dimensions().x); }
127 
128     /**
129      * Convenient accessor method for returning the X axis size (height) of
130      * the world dimensions for the texture, in map coordinate space units.
131      *
132      * @see dimensions()
133      */
height()134     inline int height() const { return int(dimensions().y); }
135 
136     /**
137      * Change the world dimensions of the texture.
138      * @param newDimensions  New dimensions in map coordinate space units.
139      *
140      * @todo Update any Materials (and thus Surfaces) which reference this.
141      */
142     void setDimensions(de::Vector2ui const &newDimensions);
143 
144     /**
145      * Change the world width of the texture.
146      * @param newWidth  New width in map coordinate space units.
147      *
148      * @todo Update any Materials (and thus Surfaces) which reference this.
149      */
150     void setWidth(de::duint newWidth);
151 
152     /**
153      * Change the world height of the texture.
154      * @param newHeight  New height in map coordinate space units.
155      *
156      * @todo Update any Materials (and thus Surfaces) which reference this.
157      */
158     void setHeight(de::duint newHeight);
159 
160     /**
161      * Returns the world origin offset of texture in map coordinate space units.
162      */
163     de::Vector2i const &origin() const;
164 
165     /**
166      * Change the world origin offset of the texture.
167      * @param newOrigin  New origin in map coordinate space units.
168      */
169     void setOrigin(de::Vector2i const &newOrigin);
170 
171     /**
172      * Returns @c true if the texture is flagged @a flagsToTest.
173      */
isFlagged(Flags flagsToTest)174     inline bool isFlagged(Flags flagsToTest) const { return !!(flags() & flagsToTest); }
175 
176     /**
177      * Returns the flags for the texture.
178      */
179     Flags flags() const;
180 
181     /**
182      * Change the texture's flags.
183      *
184      * @param flagsToChange  Flags to change the value of.
185      * @param operation      Logical operation to perform on the flags.
186      */
187     void setFlags(Flags flagsToChange, de::FlagOp operation = de::SetFlags);
188 
189     /**
190      * Release prepared GL-textures for identified variants.
191      *
192      * @param spec  If non-zero release only for variants derived with this spec.
193      */
194     virtual void release();
195 
196     /**
197      * Destroys all analyses for the texture.
198      */
199     void clearAnalyses();
200 
201     /**
202      * Retrieve the value of an identified @a analysisId data pointer.
203      * @return  Associated data pointer value.
204      */
205     void *analysisDataPointer(AnalysisId analysisId) const;
206 
207     /**
208      * Set the value of an identified @a analysisId data pointer. Ownership of
209      * the data is not given to this instance.
210      *
211      * @note If already set the old value will be replaced (so if it points
212      *       to some dynamically constructed data/resource it is the caller's
213      *       responsibility to release it beforehand).
214      *
215      * @param analysisId  Identifier of the data being attached.
216      * @param data  Data to be attached.
217      */
218     void setAnalysisDataPointer(AnalysisId analysisId, void *data);
219 
220     /**
221      * Retrieve the value of the associated user data pointer.
222      * @return  Associated data pointer value.
223      */
224     void *userDataPointer() const;
225 
226     /**
227      * Set the user data pointer value. Ownership of the data is not given to
228      * this instance.
229      *
230      * @note If already set the old value will be replaced (so if it points
231      *       to some dynamically constructed data/resource it is the caller's
232      *       responsibility to release it beforehand).
233      *
234      * @param userData  User data pointer value.
235      */
236     void setUserDataPointer(void *userData);
237 
238 public:
239     /// Register the console commands, variables, etc..., of this module.
240     static void consoleRegister();
241 
242 private:
243     DENG2_PRIVATE(d)
244 };
245 
246 Q_DECLARE_OPERATORS_FOR_FLAGS(Texture::Flags)
247 
248 } // namespace res
249 
250 #endif // LIBDOOMSDAY_RESOURCE_TEXTURE_H
251