JNR
laTexture.cpp
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 
30 #include "stdafx.h"
31 #include "Core.h"
32 
33 std::map<std::string, laTexture> laTexture::_vLoadedTextures;
34 M_BOOL laTexture::_bLazyLoad = M_FALSE;
35 
36 M_TEXTURE laTexture::_id_active = 0;
37 
38 // Load a texutre
39 //
40 void laTexture::load(std::string strFile, laTextureParams params)
41 {
42  //If already existing, simply transfer from the map
43  if( _vLoadedTextures[strFile]._id )
44  {
45  laTexture t = _vLoadedTextures[strFile];
46  _id = t._id;
47  w = t.w;
48  h = t.h;
49  }
50  else
51  {
52  //Not loaded but is lazy-loading active?
53  if(_bLazyLoad)
54  {
55  //Yes. Record the filename and postopne loading
56  _strFile = strFile;
57  _params = params;
58  //_nType = nType;
59  }
60  else
61  {
62  //No lazy-loading and no pre-existing texutre => load it
63  laTexture t = ::laSystemIntegrator::getRenderer()->texLoad(strFile, params);
64  _id = t._id;
65  w = t.w;
66  h = t.h;
67  }
68 
69  //Update the map
70  _vLoadedTextures[strFile] = *this;
71  }
72 }
73 
74 // Discard texture
75 //
76 void laTexture::discard(laTexture *ptex) {
77  // TODO: Must check reference counts
78 }
79 
80 // Force the loading of a texture
81 //
82 void laTexture::loadForce()
83 {
84  if(!_id)
85  {
86  laTexture t;
87 
88  //Check if the map already has a memory-resident copy of the texture
89  if( _vLoadedTextures[_strFile]._id )
90  {
91  t = _vLoadedTextures[_strFile];
92  }
93  else
94  {
95  // Nope. Load it and update the map
96  t = ::laSystemIntegrator::getRenderer()->texLoad(_strFile, _params);
97  _vLoadedTextures[_strFile] = t;
98  }
99 
100  _id = t._id;
101  w = t.w;
102  h = t.h;
103  }
104 }
105 
106 // Force-load all textures present in the map
107 //
108 void laTexture::loadForceAll()
109 {
110  std::map<std::string, laTexture>::iterator iter;
111  laRenderer* pr = ::laSystemIntegrator::getRenderer();
112 
113  for(iter = _vLoadedTextures.begin(); iter != _vLoadedTextures.end(); iter++)
114  {
115  (*iter).second.loadForce();
116  }
117 }
Texture creation parameters used by laTexture.
Definition: laRenderer.h:45
2D Texture
Definition: laTexture.h:45
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98