GL Studio C++ Runtime API
texture_palette.h
Go to the documentation of this file.
1 /*! \file
2  \brief The disti::TexturePalette class.
3 
4  \par Copyright Information
5 
6  Copyright (c) 2017 by The DiSTI Corporation.<br>
7  11301 Corporate Blvd; Suite 100<br>
8  Orlando, Florida 32817<br>
9  USA<br>
10  <br>
11  All rights reserved.<br>
12 
13  This Software contains proprietary trade secrets of DiSTI and may not be
14 reproduced, in whole or part, in any form, or by any means of electronic,
15 mechanical, or otherwise, without the written permission of DiSTI. Said
16 permission may be derived through the purchase of applicable DiSTI product
17 licenses which detail the distribution rights of this content and any
18 Derivative Works based on this or other copyrighted DiSTI Software.
19 
20  NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21 AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22 PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23 AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24 IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25 PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26 
27  LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28 IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29 INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30 DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31 INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32 INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33 OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34 EXCEED FIVE DOLLARS (US$5.00).
35 
36  The aforementioned terms and restrictions are governed by the laws of the
37 State of Florida and the United States of America.
38 
39 */
40 #ifndef _TEXTURE_PALETTE_H
41 #define _TEXTURE_PALETTE_H
42 
43 #include "disti_metadata.h"
44 #include "dynamic_array.h"
45 #include "gls_include.h"
47 #include "image.h"
48 #include <iostream>
49 
50 namespace disti
51 {
52 class FilePathClass;
53 
54 // Objects with no texture have a palette index of -1
55 // Valid texture indeces are zero through some positive number (no max)
56 #define NO_TEXTURE -1
57 
58 // When multiple textures are selected in the Texture Browser
59 // It is defined here, because it must be different than NO_TEXTURE
60 const int MULTIPLE_TEXTURES_SELECTED = -2;
61 
62 /** Current state of texture in the texture palette */
63 typedef enum
64 {
65  TEXTURE_UNUSED, /**< The texture slot is unused */
66  TEXTURE_IGNORED, /**< Texture slot does not contain a texture,
67  the user choose to ignore it. It appears UNUSED for most purposes.*/
68  TEXTURE_INVALID, /**< Texture slot contains a texture, but it isn't
69  bound to the GL context */
70  TEXTURE_VALID /**< Texture slot contains a texture, and it is
71  bound to the GL context */
72 
74 
75 /** Enumeration for an image compresion codec
76  */
78 {
79 public:
80  GLS_EXPORT DistiAttributeImageCodecEnum( CallbackMethodCallerBase* callback, const AttributeName& name, glsImageCodec* attribPtr );
81  virtual GLS_EXPORT ~DistiAttributeImageCodecEnum();
82 };
83 
84 /** A texture palette entry. Keeps track of which textures are assigned to
85  each entry in the texture palette */
87 {
88  DistiAttribDict _attribDict;
89 
90  TextureState_e status; /**< Status of the texture: UNUSED, INVALID, VALID, IGNORED */
91 
92  FilePathClass* _filePath; /**< Path to the texture file */
93  char* _returned_filename; /**< Holds a pointer to the buffer returned by Filename() */
94  bool _supportsNPOTValue;
95 
96  void SupportsAllowNPOT();
97 
98  void InitMetadata();
99 
100 public:
101  static GLS_EXPORT bool _defaultGenerateInline;
102 
103  bool generateInline; /**< True if the image is to be generated as inline code */
104  bool reverseAlpha; /**< True if the alpha channel should be reversed on load */
105  Image* texture; /**< Pointer to the texture object itself */
106  long compressedSize; /**< Size of image when compressed with currently set inline CODEC */
107  glsImageCodec codec; /**< CODEC that will be used to compress an inline image */
108  int compressionFactor; /**< Compression factor of the image, used only for JPEG */
109  int desiredLoadWidth; /**< The width that the image should load at. 0 indicates full size. (Editor only) */
110  int desiredLoadHeight; /**< The height that the image should load at. 0 indicates full size. (Editor only) */
111  int sourceImageWidth; /**< The width of the source image. (pixels) (Editor only) */
112  int sourceImageHeight; /**< The height of the source image. (pixels) (Editor only) */
113  int sourceImageMemSize; /**< The size of the source image im memory. (bytes) (Editor only) */
114  bool useGLTextureCompression; /**< True if the image should be generated to use hardware texture compression. (Editor only) */
115 
116  bool allowNPOT; /**< Allow Non-power of two texture (do not scale up) */
117 
118  GLS_EXPORT TexturePaletteEntry( void );
119  GLS_EXPORT TexturePaletteEntry( const TexturePaletteEntry& source );
120  GLS_EXPORT ~TexturePaletteEntry( void );
121  GLS_EXPORT void Initialize( void );
122 
123  GLS_EXPORT void operator=( const TexturePaletteEntry& source );
124 
125  GLS_EXPORT TextureState_e TextureStatus( void ) { return status; }
126  GLS_EXPORT const char* Filename( void ) const; // returns the filename relative to the current working directory
127  GLS_EXPORT void Filename( const char* name ); // set the filename relative to the current working directory
128  GLS_EXPORT void Invalidate( void );
129  GLS_EXPORT void Validate( void );
130  GLS_EXPORT void Ignore(); // Sets texture to NULL and status to IGNORED
131 
132  GLS_EXPORT bool IsEmpty( void ) const; // true if there is no texture object (unused or ignored)
133  GLS_EXPORT bool IsIgnored( void ) const; // true if ignored
134 
135  GLS_EXPORT std::ostream& WriteValue( std::ostream& outstr );
136  GLS_EXPORT std::istream& ReadValue( std::istream& instr );
137 };
138 
139 /** Texture Palette class. Stores a list of textures, each with its own
140  * texture handle. This the textures to be stored separately from the
141  * objects, which allows multiple objects to share the same textures.
142  */
144 {
145  /** \return true if the index passed in is a valid texture index. Doesn't imply that a texture exists at that index,
146  * merely that the index is within the array bounds of the texture palette */
147  GLS_EXPORT bool ValidIndex( int i );
148 
149  /** A texture of a single magenta pixel used for displaying textures that are not valid */
150  Image* _defaultTexture;
151 
152 public:
153  /** A dynamic array of TexturePaletteEntry */
155 
156  /** Create a new texture palette with the indicated number of entries
157  * \param size The number of entries in the palette
158  */
159  GLS_EXPORT TexturePalette( int size = 0 );
160 
161  /** Resize texture palette with the indicated number of entries
162  * \param newSize The new number of entries in the palette
163  */
164  GLS_EXPORT void PaletteSize( int newSize );
165 
166  GLS_EXPORT ~TexturePalette( void );
167 
168  GLS_EXPORT void operator=( TexturePalette& source );
169 
170  /** Returns the texture palette entry at index i */
171  GLS_EXPORT TexturePaletteEntry* Entry( int i );
172 
173  /** Gets the size of the texture palette
174  * \return The size of the texture palette
175  */
176  GLS_EXPORT unsigned int size( void ) const;
177 
178  /** Invalidates all of the textures in the palette, forcing them to be
179  * rebound the next time they are referenced by an object.
180  */
181  GLS_EXPORT void InvalidateTextures( void );
182 
183  /** Finds an unused slot in the texture palette.
184  * \return The index of the first unused slot in the palette.
185  */
186  GLS_EXPORT int FindAvailableSlot( void );
187 
188  /** Returns the filename associated with the texture at the given index.
189  * \param i The index to get the filename from
190  * \return The filename of the texture at that index
191  */
192  GLS_EXPORT const char* TextureFilename( int i );
193 
194  /** Sets the filename associated with the texture at the given index.
195  * \param i The index to get the filename from
196  * \param filename The filename to set
197  */
198  GLS_EXPORT void TextureFilename( int i, const char* filename );
199 
200  /** Sets the texture at the given index.
201  * Attempts to load the texture from disk
202  * \param i The texture index to load the texture into
203  * \param filename The filename to set
204  * \param options Image load options (e.g. reverse alpha)
205  * \param glTexCompress The GlTextureCompression setting for the new Image.
206  * \param allowNPOT If the image is allowed to be non-power of two (NPOT)
207  * \return TRUE if successful else FALSE
208  */
209  GLS_EXPORT bool SetTexture( int i, const char* filename, const Image::LoadOptions& options = Image::LoadOptions(), bool glTexCompress = false, bool allowNPOT = true );
210 
211  /** Sets the texture at the given index using a TexturePaletteEntry structure.
212  * This is generally only used in the Editor
213  * \param i
214  * \param entry Image generation options, NULL if not setting them
215  */
216  GLS_EXPORT bool SetTexture( int i, TexturePaletteEntry* entry );
217 
218  /** Sets the texture at the given index
219  * Does NOT attempt to load a file
220  * \param i The texture index to load the texture into
221  * \param image An image that will be the texture
222  * \return TRUE if successful else FALSE
223  */
224  GLS_EXPORT bool SetTexture( int i, Image* image );
225 
226  /** Sets the texture at the given index
227  * \param i The index to insert at
228  * \param image Structure containing the compressed image data
229  */
230  GLS_EXPORT bool SetInlineTexture( int i, glsInlineImage& image );
231 
232  /** Disables image sharing for the Image at the given index and
233  * ensures that the index refers to a unique Image instance.
234  * Note: If you want to disable image sharing for an Image that is not
235  * yet in a palette, you should call AllowImageSharing(false) on the Image.
236  * \param index The index of the texture to disable image sharing. The Image must already be loaded in the palette.
237  * \returns true on success
238  * \returns false on failure (Invalid index or Image could not be guarenteed unique)
239  */
240  GLS_EXPORT bool DisableImageSharing( int index );
241 
242  /** Finds a texture based on its filename
243  * \param name The filename of the texture to find
244  * \return The index if found, -1 if not found
245  */
246  GLS_EXPORT int FindTextureByName( const char* name );
247 
248  /** Gets the texture state of the texture at the given index.
249  * \param i The index to get the texture state from
250  * \return The state (UNUSED,VALID,INVALID) of the texture
251  */
252  GLS_EXPORT TextureState_e TextureState( int i );
253 
254  /** Returns the width of the texture image in pixels
255  * \param i The index of the texture to get the width of
256  * \return The width of the texture image in pixels
257  */
258  GLS_EXPORT int Width( int i );
259 
260  /** Returns the height of the texture image in pixels
261  * \param i The index of the texture to get the height of
262  * \return The height of the texture image in pixels
263  */
264  GLS_EXPORT int Height( int i );
265 
266  /** Returns the power of two width of the texture image in pixels
267  * \param i The index of the texture to get the power of two width of
268  * \return The power of two width of the texture image in pixels
269  */
270  GLS_EXPORT int TextureWidth( int i );
271 
272  /** Returns the power of two height of the texture image in pixels
273  * \param i The index of the texture to get the power of two height of
274  * \return The power of two height of the texture image in pixels
275  */
276  GLS_EXPORT int TextureHeight( int i );
277 
278  /** Returns the Texture Coordinate of the upper right corner of the image
279  * computed as (Width/TextureWidth)
280  * \param i The index of the texture
281  * \return The Texture Coordinate of the upper right corner of the image
282  */
283  GLS_EXPORT float TextureCoordX( int i );
284 
285  /** Returns the Texture Coordinate of the upper right corner of the image
286  * computed as (Height/TextureHeight)
287  * \param i The index of the texture
288  * \return The Texture Coordinate of the upper right corner of the image
289  */
290  GLS_EXPORT float TextureCoordY( int i );
291 
292  /** Invalidate the texture at the given index, causing it to be rebound
293  * the next time it is referencd
294  * \param i The index of the texture
295  */
296  GLS_EXPORT void InvalidateTexture( int i );
297 
298 #ifdef GLES
299  /** Bind the texture at the given index, unless it is already bound. Set
300  * as the texture to be used for texturing polygons
301  * \param i The index of the texture
302  * \param stateManager
303  */
304  GLS_EXPORT void BindTexture( int i, IGlsStateManager* stateManager );
305 #else
306  /** Bind the texture at the given index, unless it is already bound. Set
307  * as the texture to be used for texturing polygons
308  * \param i The index of the texture
309  */
310  GLS_EXPORT void BindTexture( int i );
311 #endif
312 
313  /** Return whether or not texture is bound
314  * \param i The index of the texture
315  * \return boolean result of whether or not texture is bound
316  */
317  GLS_EXPORT int TextureValid( int i );
318 
319  /** Delete the texture at the given index. Mark the index as unused.
320  * \param i The index of the texture
321  */
322  GLS_EXPORT void DeleteTexture( int i );
323 
324  /** Get a pointer to the texture at the given index.
325  * \param i The index of the texture
326  * \return The texture at that index. NULL if there is none.
327  */
328  GLS_EXPORT Image* Texture( int i );
329 
330  /** \return True if the image is to be generated as inline code
331  * \param i The index of the texture
332  */
333  GLS_EXPORT bool GenerateInline( int i );
334 
335  /** \return True if the image is to be generated to use OpenGL texture compression
336  * \param i The index of the texture
337  */
338  GLS_EXPORT bool UseGLTextureCompression( int i );
339 
340  /** \return True if the image is allowed to be non power of two
341  * \param i The index of the texture
342  */
343  GLS_EXPORT bool AllowNPOT( int i );
344 
345  /** \return Size of image when compressed with currently set inline CODEC
346  * \param i The index of the texture
347  */
348  GLS_EXPORT long CompressedSize( int i );
349 
350  /** \return CODEC that will be used to compress an inline image
351  * \param i The index of the texture
352  */
353  GLS_EXPORT glsImageCodec Codec( int i );
354 
355  /** \return Compression factor of the image, used only for JPEG
356  * \param i The index of the texture
357  */
358  GLS_EXPORT int CompressionFactor( int i );
359 
360  /** Set True if the image is to be generated as inline code
361  * \param i The index of the texture
362  * \param val
363  */
364  GLS_EXPORT void GenerateInline( int i, bool val );
365 
366  /** Set True if the image is to be generated to use OpenGL texture compression
367  * \param i The index of the texture
368  * \param val
369  */
370  GLS_EXPORT void UseGLTextureCompression( int i, bool val );
371 
372  /** Set True if the image is allowed to be non power of two
373  * \param i The index of the texture
374  * \param val the value to set allowing non-power of two (NPOT) functionality
375  */
376  GLS_EXPORT void AllowNPOT( int i, bool val );
377 
378  /** Set Size of image when compressed with currently set inline CODEC
379  * \param i The index of the texture
380  * \param val
381  */
382  GLS_EXPORT void CompressedSize( int i, long val );
383 
384  /** Set CODEC that will be used to compress an inline image
385  * \param i The index of the texture
386  * \param val The codec to set
387  * \param reload Whether or not to reload the image
388  */
389  GLS_EXPORT void Codec( int i, glsImageCodec val, bool reload = true );
390 
391  /** Set Compression factor of the image, used only for JPEG
392  * \param i The index of the texture
393  * \param val
394  * \param reload
395  */
396  GLS_EXPORT void CompressionFactor( int i, int val, bool reload = true );
397 
398  /** Sets the desired load size for the image.
399  * If the loaded image is larger than the specified dimensions,
400  * it will be scaled down during the load.
401  * \param i The index of the texture
402  * \param width The desired width
403  * \param height The desired height
404  */
405  GLS_EXPORT void DesiredLoadSize( int i, int width, int height );
406 
407  /** Reload a texture from disk, then downsize and compress it using the current settings
408  * of the TexturePaletteEntry
409  * \param i The index of the texture to reload
410  */
411  GLS_EXPORT void Reload( int i );
412 
413  /** Set mip-mapping state for given texture
414  * \param i The index of the texture to set mipmapping for
415  * \param val The mipmapping state to set
416  */
417  GLS_EXPORT void SetMipMap( int i, bool val );
418 
419  /** Compress using the current compression settings for the texture
420  * and scale down to the DesiredLoadSize.
421  * \param i The index of the texture
422  */
423  GLS_EXPORT void PreviewGenerated( int i );
424 
425  /** Scales down an image to the next power-of-two size where (width <= desiredWidth || height <= desiredHeight)
426  * Maintains aspect ratio.
427  * Note: This method may be removed from the TexturePalette in the future.
428  */
429  static GLS_EXPORT bool DownsizeImage( Image* image, int desiredWidth, int desiredHeight );
430 
431  /** The DistiAttributeTexturePalette class for version 2.1 and older (read only). Included for
432  * backwards compatibility with v 2.1
433  */
435  {
436  TexturePalette** _palette;
437 
438  public:
439  GLS_EXPORT DistiAttributeTexturePalette_V21( TexturePalette** palette );
440 
441  // This atttribute is never written
442  virtual bool OkToWrite() const { return false; }
443  virtual std::ostream& WriteValue( std::ostream& outstr ) { return outstr; }
444 
445  virtual GLS_EXPORT std::istream& ReadValue( std::istream& instr );
446  };
447 
448  /** The DistiAttributeTexturePalette class for version 3.0 and later
449  */
451  {
452  TexturePalette** _palette;
453 
454  public:
455  GLS_EXPORT DistiAttributeTexturePalette( TexturePalette** palette );
456  virtual GLS_EXPORT std::ostream& WriteValue( std::ostream& outstr );
457  virtual GLS_EXPORT std::istream& ReadValue( std::istream& instr );
458  virtual DISTI_EXPORT DistiAttributeBase& operator=( const DistiAttributeBase& oldClass );
459  };
460 };
461 
462 } // namespace disti
463 
464 #endif
Definition: image.h:79
glsImageCodec
Definition: image.h:62
glsImageCodec Codec(int i)
virtual std::ostream & WriteValue(std::ostream &outstr)
const char * TextureFilename(int i)
Definition: image.h:162
bool useGLTextureCompression
Definition: texture_palette.h:114
The disti metadata.
DynamicArray< TexturePaletteEntry, false > palette
Definition: texture_palette.h:154
virtual std::istream & ReadValue(std::istream &instr)
float TextureCoordX(int i)
int FindAvailableSlot(void)
TextureState_e
Definition: texture_palette.h:63
Definition: dynamic_array.h:62
static bool DownsizeImage(Image *image, int desiredWidth, int desiredHeight)
The disti::DynamicArray class. A templated array of objects capable of dynamically growing...
TexturePaletteEntry * Entry(int i)
Definition: texture_palette.h:86
bool UseGLTextureCompression(int i)
int desiredLoadWidth
Definition: texture_palette.h:109
long compressedSize
Definition: texture_palette.h:106
Definition: gls_metadata_attributes.h:1655
Definition: texture_palette.h:65
virtual bool OkToWrite() const
Definition: texture_palette.h:442
Image * Texture(int i)
Definition: image.h:272
Definition: gls_state_manager_interface.h:66
Definition: texture_palette.h:66
unsigned int size(void) const
int compressionFactor
Definition: texture_palette.h:108
A file for all GL Studio files to include.
bool SetTexture(int i, const char *filename, const Image::LoadOptions &options=Image::LoadOptions(), bool glTexCompress=false, bool allowNPOT=true)
TexturePalette(int size=0)
Definition: texture_palette.h:70
The Image class. All textures are converted internally into Images.
void SetMipMap(int i, bool val)
void InvalidateTexture(int i)
Definition: texture_palette.h:68
int TextureHeight(int i)
bool allowNPOT
Definition: texture_palette.h:116
virtual DistiAttributeBase & operator=(const DistiAttributeBase &oldClass)
int sourceImageHeight
Definition: texture_palette.h:112
void InvalidateTextures(void)
float TextureCoordY(int i)
Definition: texture_palette.h:77
void BindTexture(int i)
int sourceImageWidth
Definition: texture_palette.h:111
int sourceImageMemSize
Definition: texture_palette.h:113
Definition: disti_metadata.h:174
bool reverseAlpha
Definition: texture_palette.h:104
Definition: file_path_class.h:60
void PaletteSize(int newSize)
Definition: disti_metadata.h:667
Image * texture
Definition: texture_palette.h:105
Definition: callback_caller_base.h:55
virtual std::istream & ReadValue(std::istream &instr)
long CompressedSize(int i)
bool DisableImageSharing(int index)
Defines templated metadata classes for DisplayObjects and other uses.
bool generateInline
Definition: texture_palette.h:103
Definition: texture_palette.h:143
void PreviewGenerated(int i)
int CompressionFactor(int i)
glsImageCodec codec
Definition: texture_palette.h:107
Definition: disti_metadata.h:79
void DesiredLoadSize(int i, int width, int height)
Definition: bmpimage.h:46
TextureState_e TextureState(int i)
bool SetInlineTexture(int i, glsInlineImage &image)
void DeleteTexture(int i)
int desiredLoadHeight
Definition: texture_palette.h:110
virtual std::ostream & WriteValue(std::ostream &outstr)
Definition: texture_palette.h:443
int FindTextureByName(const char *name)
bool GenerateInline(int i)