JNR
laTexture.h
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 #ifndef M_TEXTURE_H
30 #define M_TEXTURE_H
31 
33 
34 
42 
43 // Texture functionality
44 //
45 class laTexture
46 {
47  // Map all loaded texute files
48  // in order to avoid memory redundancies
49  //
50  static std::map<std::string, laTexture> _vLoadedTextures;
51 
52  //Postopne loading; This is useful when trying to load textures from a thread
53  //and the graphical implementation is not thread-safe (e.g. OpenGL)
54  //
55  static M_BOOL _bLazyLoad;
56 
57  std::string _strFile;
58  laTextureParams _params;
59 
60  //Texture identifier used by the active laRenderer
61  M_TEXTURE _id;
62  static M_TEXTURE _id_active; // currently binded texture
63 
64 public:
65 
66  //Metrics
67  unsigned w, h;
68 
69  // Get/set ID and metrics
70  //
71  M_TEXTURE id() { return _id;}
72 
73  inline void set(M_TEXTURE id, unsigned nw, unsigned nh) {
74  _id = id;
75  w = nw;
76  h = nh;
77  }
78 
79  // Constructor
80  //
81  laTexture(){
82  _strFile = "unknown.bmp";
83  //_nType = M_TEX_SIMPLE;
84 
85  _id = 0; //NOTE: Must be 0 in order for the tex to be interpreted as non-loaded;
86  w = h = 10; //NOTE: If this is 0 it could cause some issues when lazy-loading stuff
87  };
88 
89  // Load texture; These functions provide checks to ensure that
90  // each texture is loaded only once, even if it is requiested
91  // multiple times
92  //
93  void load(std::string strFile, laTextureParams params);
94  void load(std::string strFile) { load(strFile, laTextureParams()); };
95 
96  // Discard texture
97  //
98  void discard(laTexture *ptex);
99 
100  // Use texture
101  //
102  inline void laTexture::use() {
103  loadForce(); //< force load in case not already loaded
104 
105  if( _id_active != _id ) //< bind if not already bound
106  ::laSystemIntegrator::getRenderer()->texUse(this);
107  _id_active = _id;
108  }
109 
110  // Lazy-loading
111  //
112  static void toggleLazyLoad(M_BOOL bOn){
113  _bLazyLoad = bOn;
114  }
115 
116  void loadForce();
117  static void loadForceAll();
118 };
120 #endif
Texture creation parameters used by laTexture.
Definition: laRenderer.h:45
2D Texture
Definition: laTexture.h:45