CUGL 1.3
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 MIT 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 
102  void parseAtlas(const std::shared_ptr<JsonValue>& json, const std::shared_ptr<Texture>& texture);
103 
116  SDL_Surface* preload(const std::string& source);
117 
135  void materialize(const std::string& key, SDL_Surface* surface, LoaderCallback callback);
136 
165  void materialize(const std::shared_ptr<JsonValue>& json, SDL_Surface* surface, LoaderCallback callback);
166 
167 
186  virtual bool read(const std::string& key, const std::string& source,
187  LoaderCallback callback, bool async) override;
188 
218  virtual bool read(const std::shared_ptr<JsonValue>& json,
219  LoaderCallback callback, bool async) override;
220 
237  virtual bool purge(const std::shared_ptr<JsonValue>& json) override;
238 
239 public:
240 #pragma mark -
241 #pragma mark Constructors
242 
248  TextureLoader();
249 
261  void dispose() override {
262  _assets.clear();
263  _loader = nullptr;
264  }
265 
278  static std::shared_ptr<TextureLoader> alloc() {
279  std::shared_ptr<TextureLoader> result = std::make_shared<TextureLoader>();
280  return (result->init() ? result : nullptr);
281  }
282 
294  static std::shared_ptr<TextureLoader> alloc(const std::shared_ptr<ThreadPool>& threads) {
295  std::shared_ptr<TextureLoader> result = std::make_shared<TextureLoader>();
296  return (result->init(threads) ? result : nullptr);
297  }
298 
299 #pragma mark -
300 #pragma mark Properties
301 
311  GLuint getMinFilter() const { return _minfilter; }
312 
322  void setMinFilter(GLuint minFilter) { _minfilter = minFilter; }
323 
333  GLuint getMagFilter() const { return _magfilter; }
334 
344  void setMagFilter(GLuint magFilter) { _magfilter = magFilter; }
345 
354  GLuint getWrapS() const { return _wraps; }
355 
364  void setWrapS(GLuint wrap) { _wraps = wrap; }
365 
374  GLuint getWrapT() const { return _wrapt; }
375 
384  void setWrapT(GLuint wrap) { _wrapt = wrap; }
385 
395  bool hasMipMaps() const { return _mipmaps; }
396 
406  void setMipMaps(bool flag) { _mipmaps = flag; }
407 
408 };
409 
410 }
411 
412 #endif /* __CU_TEXTURE_LOADER_H__ */
cugl::TextureLoader::alloc
static std::shared_ptr< TextureLoader > alloc()
Definition: CUTextureLoader.h:278
cugl::TextureLoader::setWrapT
void setWrapT(GLuint wrap)
Definition: CUTextureLoader.h:384
cugl::Loader
Definition: CULoader.h:749
cugl::TextureLoader::parseAtlas
void parseAtlas(const std::shared_ptr< JsonValue > &json, const std::shared_ptr< Texture > &texture)
cugl::TextureLoader::dispose
void dispose() override
Definition: CUTextureLoader.h:261
cugl::TextureLoader::_magfilter
GLuint _magfilter
Definition: CUTextureLoader.h:82
cugl::TextureLoader::getMinFilter
GLuint getMinFilter() const
Definition: CUTextureLoader.h:311
cugl::TextureLoader::_minfilter
GLuint _minfilter
Definition: CUTextureLoader.h:80
cugl::BaseLoader::_loader
std::shared_ptr< ThreadPool > _loader
Definition: CULoader.h:105
cugl::TextureLoader::purge
virtual bool purge(const std::shared_ptr< JsonValue > &json) override
cugl::TextureLoader::setMagFilter
void setMagFilter(GLuint magFilter)
Definition: CUTextureLoader.h:344
cugl::TextureLoader::alloc
static std::shared_ptr< TextureLoader > alloc(const std::shared_ptr< ThreadPool > &threads)
Definition: CUTextureLoader.h:294
cugl::TextureLoader::hasMipMaps
bool hasMipMaps() const
Definition: CUTextureLoader.h:395
cugl::TextureLoader::_wrapt
GLuint _wrapt
Definition: CUTextureLoader.h:86
cugl::TextureLoader
Definition: CUTextureLoader.h:73
cugl::TextureLoader::getWrapT
GLuint getWrapT() const
Definition: CUTextureLoader.h:374
cugl::Loader< Texture >::_assets
std::unordered_map< std::string, std::shared_ptr< Texture > > _assets
Definition: CULoader.h:752
cugl::TextureLoader::_mipmaps
bool _mipmaps
Definition: CUTextureLoader.h:88
cugl::TextureLoader::read
virtual bool read(const std::string &key, const std::string &source, LoaderCallback callback, bool async) override
cugl::TextureLoader::TextureLoader
TextureLoader()
cugl::TextureLoader::getMagFilter
GLuint getMagFilter() const
Definition: CUTextureLoader.h:333
cugl::TextureLoader::materialize
void materialize(const std::string &key, SDL_Surface *surface, LoaderCallback callback)
cugl::TextureLoader::preload
SDL_Surface * preload(const std::string &source)
cugl::TextureLoader::setWrapS
void setWrapS(GLuint wrap)
Definition: CUTextureLoader.h:364
cugl::TextureLoader::setMinFilter
void setMinFilter(GLuint minFilter)
Definition: CUTextureLoader.h:322
cugl::TextureLoader::_wraps
GLuint _wraps
Definition: CUTextureLoader.h:84
cugl::TextureLoader::setMipMaps
void setMipMaps(bool flag)
Definition: CUTextureLoader.h:406
cugl::TextureLoader::getWrapS
GLuint getWrapS() const
Definition: CUTextureLoader.h:354