FPLBase
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
texture_atlas.h
Go to the documentation of this file.
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef FPLBASE_TEXTURE_ATLAS_H
16 #define FPLBASE_TEXTURE_ATLAS_H
17 
18 #include <map>
19 #include <string>
20 #include <vector>
21 
22 #include "fplbase/config.h" // Must come first.
23 #include "fplbase/asset.h"
24 
25 namespace fplbase {
26 
27 /// @file
28 /// @addtogroup fplbase_texture_atlas
29 /// @{
30 using mathfu::vec4;
31 
32 class Texture;
33 
34 /// @class TextureAtlas
35 /// @brief Texture coordinate dictionary.
36 ///
37 /// A TextureAtlas supports sprite sheets by containing one texture and a list
38 /// of subtexture bounding boxes, which can also be indexed by name using the
39 /// index_map. Subtexture bounding boxes are returned in normalized texture
40 /// coordinates, and take the form (u, v, width, height).
41 ///
42 /// @warning This is will very likely be refactored.
43 class TextureAtlas : public Asset {
44  public:
45  TextureAtlas() : atlas_texture_(nullptr) {}
46  ~TextureAtlas() { Delete(); }
47 
48  /// @brief Delete the texture associated with this atlas.
49  void Delete() {
50  atlas_texture_->Delete();
51  atlas_texture_ = nullptr;
52  }
53 
54  /// @brief Get the bounds of a subtexture associated with name.
55  ///
56  /// @param name Name of the subtexture to lookup.
57  /// @returns Bounds of the subtexture or nullptr if the specified name isn't
58  /// found.
59  const vec4 *GetBounds(const std::string &name) {
60  auto index_iter = index_map_.find(name);
61  if (index_iter != index_map_.end()) {
62  return &subtexture_bounds_[index_iter->second];
63  }
64  return nullptr;
65  }
66 
67  /// @brief Get the texture associated with this atlas.
68  /// @return Pointer to the texture associated with this atlas.
69  const Texture *atlas_texture() const { return atlas_texture_; }
70  /// @brief Get the texture associated with this atlas.
71  /// @return Pointer to the texture associated with this atlas.
72  Texture *atlas_texture() { return atlas_texture_; }
73  /// @brief Set the texture associated with this atlas.
75  atlas_texture_ = atlas_texture;
76  }
77 
78  /// @brief Get a vector of the bounds of each subtexture in this atlas.
79  ///
80  /// Each element of the vector consists of (offsetx, offsety, sizex, sizey)
81  /// where offsetx / offsety are coordinates relative to the underlying
82  /// atlased texture and sizex / sizey are the dimensions of the subtexture.
83  ///
84  /// @returns Vector of subtexture bounds.
85  const std::vector<vec4> &subtexture_bounds() const {
86  return subtexture_bounds_;
87  }
88 
89  /// @brief Get a vector of the bounds of each subtexture in this atlas.
90  ///
91  /// Each element of the vector consists of (offsetx, offsety, sizex, sizey)
92  /// where offsetx / offsety are coordinates relative to the underlying
93  /// atlased texture and sizex / sizey are the dimensions of the subtexture.
94  ///
95  /// @returns Vector of subtexture bounds.
96  std::vector<vec4> &subtexture_bounds() { return subtexture_bounds_; }
97 
98  /// @brief Get a map of subtexture names to subtexture offsets.
99  ///
100  /// Each entry in the map can be used to lookup the subtexture bounds in
101  /// the vector returned by @ref subtexture_bounds().
102  ///
103  /// @return A map of subtexture names to indices in vector returned by
104  /// @ref subtexture_bounds().
105  const std::map<std::string, size_t> &index_map() const { return index_map_; }
106 
107  /// @brief Get a map of subtexture names to subtexture offsets.
108  ///
109  /// Each entry in the map can be used to lookup the subtexture bounds in
110  /// the vector returned by @ref subtexture_bounds().
111  ///
112  /// @return A map of subtexture names to indices in vector returned by
113  /// @ref subtexture_bounds().
114  std::map<std::string, size_t> &index_map() { return index_map_; }
115 
116  /// @brief Load a texture atlas file. Used by the more convenient AssetManager
117  /// interface, but can be used without it.
118  static TextureAtlas *LoadTextureAtlas(const char *filename,
119  TextureFormat format,
120  TextureFlags flags,
121  const TextureLoaderFn &tlf);
122  private:
123  // Texture being used by this atlas.
124  Texture *atlas_texture_;
125  // List of bounds (offsetx, offsety, sizex, sizey) of each subtexture.
126  std::vector<vec4> subtexture_bounds_;
127  // Map of subtexture names to indices into subtexture_bounds_.
128  std::map<std::string, size_t> index_map_;
129 };
130 
131 /// @}
132 } // namespace fplbase
133 
134 #endif // FPLBASE_TEXTURE_ATLAS_H
void set_atlas_texture(Texture *atlas_texture)
Set the texture associated with this atlas.
Definition: texture_atlas.h:74
void Delete()
Delete the Texture stored in id_, and reset id_ to 0.
static TextureAtlas * LoadTextureAtlas(const char *filename, TextureFormat format, TextureFlags flags, const TextureLoaderFn &tlf)
Load a texture atlas file. Used by the more convenient AssetManager interface, but can be used withou...
Abstraction for a texture object loaded on the GPU.
Definition: texture.h:104
Base class of all assets that may be managed by Assetmanager.
Definition: asset.h:26
std::vector< vec4 > & subtexture_bounds()
Get a vector of the bounds of each subtexture in this atlas.
Definition: texture_atlas.h:96
TextureFormat
Definition: texture.h:36
Texture * atlas_texture()
Get the texture associated with this atlas.
Definition: texture_atlas.h:72
const vec4 * GetBounds(const std::string &name)
Get the bounds of a subtexture associated with name.
Definition: texture_atlas.h:59
const std::map< std::string, size_t > & index_map() const
Get a map of subtexture names to subtexture offsets.
Definition: texture_atlas.h:105
std::map< std::string, size_t > & index_map()
Get a map of subtexture names to subtexture offsets.
Definition: texture_atlas.h:114
TextureFlags
Flags affecting loading and sampler modes for a texture.
Definition: texture.h:52
const Texture * atlas_texture() const
Get the texture associated with this atlas.
Definition: texture_atlas.h:69
const std::vector< vec4 > & subtexture_bounds() const
Get a vector of the bounds of each subtexture in this atlas.
Definition: texture_atlas.h:85
Texture coordinate dictionary.
Definition: texture_atlas.h:43
std::function< Texture *(const char *filename, TextureFormat format, TextureFlags flags)> TextureLoaderFn
used by some functions to allow the texture loading mechanism to be specified by the caller...
Definition: texture.h:431
void Delete()
Delete the texture associated with this atlas.
Definition: texture_atlas.h:49