CUGL
Cornell University Game Library
CUTextureLoader.h
1 //
2 // CUTextureLoader.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a specific implementation of the Loader class to load
6 // textures. A texture asset is identified by both its source file and its
7 // texture parameters. Hence you may wish to load a texture asset multiple
8 // times, though this is potentially wasteful regarding memory.
9 //
10 // As with all of our loaders, this loader is designed to be attached to an
11 // asset manager. In addition, this class uses our standard shared-pointer
12 // architecture.
13 //
14 // 1. The constructor does not perform any initialization; it just sets all
15 // attributes to their defaults.
16 //
17 // 2. All initialization takes place via init methods, which can fail if an
18 // object is initialized more than once.
19 //
20 // 3. All allocation takes place via static constructors which return a shared
21 // pointer.
22 //
23 //
24 // CUGL zlib License:
25 // This software is provided 'as-is', without any express or implied
26 // warranty. In no event will the authors be held liable for any damages
27 // arising from the use of this software.
28 //
29 // Permission is granted to anyone to use this software for any purpose,
30 // including commercial applications, and to alter it and redistribute it
31 // freely, subject to the following restrictions:
32 //
33 // 1. The origin of this software must not be misrepresented; you must not
34 // claim that you wrote the original software. If you use this software
35 // in a product, an acknowledgment in the product documentation would be
36 // appreciated but is not required.
37 //
38 // 2. Altered source versions must be plainly marked as such, and must not
39 // be misrepresented as being the original software.
40 //
41 // 3. This notice may not be removed or altered from any source distribution.
42 //
43 // Author: Walker White
44 // Version: 1/7/16
45 //
46 #ifndef __CU_TEXTURE_LOADER_H__
47 #define __CU_TEXTURE_LOADER_H__
48 #include <cugl/assets/CULoader.h>
49 #include <cugl/renderer/CUTexture.h>
50 
51 namespace cugl {
52 
73 class TextureLoader : public Loader<Texture> {
74 private:
76  CU_DISALLOW_COPY_AND_ASSIGN(TextureLoader);
77 
78 protected:
80  GLuint _minfilter;
82  GLuint _magfilter;
84  GLuint _wraps;
86  GLuint _wrapt;
88  bool _mipmaps;
89 
90 #pragma mark Asset Loading
91 
103  SDL_Surface* preload(const std::string& source);
104 
122  void materialize(const std::string& key, SDL_Surface* surface, LoaderCallback callback);
123 
152  void materialize(const std::shared_ptr<JsonValue>& json, SDL_Surface* surface, LoaderCallback callback);
153 
154 
173  virtual bool read(const std::string& key, const std::string& source,
174  LoaderCallback callback, bool async) override;
175 
205  virtual bool read(const std::shared_ptr<JsonValue>& json,
206  LoaderCallback callback, bool async) override;
207 
208 public:
209 #pragma mark -
210 #pragma mark Constructors
211 
217  TextureLoader();
218 
230  void dispose() override {
231  _assets.clear();
232  _loader = nullptr;
233  }
234 
247  static std::shared_ptr<TextureLoader> alloc() {
248  std::shared_ptr<TextureLoader> result = std::make_shared<TextureLoader>();
249  return (result->init() ? result : nullptr);
250  }
251 
263  static std::shared_ptr<TextureLoader> alloc(const std::shared_ptr<ThreadPool>& threads) {
264  std::shared_ptr<TextureLoader> result = std::make_shared<TextureLoader>();
265  return (result->init(threads) ? result : nullptr);
266  }
267 
268 #pragma mark -
269 #pragma mark Properties
270 
280  GLuint getMinFilter() const { return _minfilter; }
281 
291  void setMinFilter(GLuint minFilter) { _minfilter = minFilter; }
292 
302  GLuint getMagFilter() const { return _magfilter; }
303 
313  void setMagFilter(GLuint magFilter) { _magfilter = magFilter; }
314 
323  GLuint getWrapS() const { return _wraps; }
324 
333  void setWrapS(GLuint wrap) { _wraps = wrap; }
334 
343  GLuint getWrapT() const { return _wrapt; }
344 
353  void setWrapT(GLuint wrap) { _wrapt = wrap; }
354 
364  bool hasMipMaps() const { return _mipmaps; }
365 
375  void setMipMaps(bool flag) { _mipmaps = flag; }
376 
377 };
378 
379 }
380 
381 #endif /* __CU_TEXTURE_LOADER_H__ */
std::shared_ptr< ThreadPool > _loader
Definition: CULoader.h:102
bool _mipmaps
Definition: CUTextureLoader.h:88
void setWrapS(GLuint wrap)
Definition: CUTextureLoader.h:333
void materialize(const std::string &key, SDL_Surface *surface, LoaderCallback callback)
GLuint _wrapt
Definition: CUTextureLoader.h:86
GLuint getMagFilter() const
Definition: CUTextureLoader.h:302
void setMipMaps(bool flag)
Definition: CUTextureLoader.h:375
std::unordered_map< std::string, std::shared_ptr< Texture > > _assets
Definition: CULoader.h:678
virtual bool read(const std::string &key, const std::string &source, LoaderCallback callback, bool async) override
std::function< void(const std::string &key, bool success)> LoaderCallback
Definition: CULoader.h:81
GLuint getWrapS() const
Definition: CUTextureLoader.h:323
SDL_Surface * preload(const std::string &source)
GLuint _wraps
Definition: CUTextureLoader.h:84
void setWrapT(GLuint wrap)
Definition: CUTextureLoader.h:353
GLuint getWrapT() const
Definition: CUTextureLoader.h:343
static std::shared_ptr< TextureLoader > alloc()
Definition: CUTextureLoader.h:247
void setMinFilter(GLuint minFilter)
Definition: CUTextureLoader.h:291
void setMagFilter(GLuint magFilter)
Definition: CUTextureLoader.h:313
Definition: CUTextureLoader.h:73
Definition: CULoader.h:675
void dispose() override
Definition: CUTextureLoader.h:230
GLuint _magfilter
Definition: CUTextureLoader.h:82
GLuint _minfilter
Definition: CUTextureLoader.h:80
GLuint getMinFilter() const
Definition: CUTextureLoader.h:280
Definition: CUAnimationNode.h:52
static std::shared_ptr< TextureLoader > alloc(const std::shared_ptr< ThreadPool > &threads)
Definition: CUTextureLoader.h:263
bool hasMipMaps() const
Definition: CUTextureLoader.h:364