UGDK  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
textureatlas.h
Go to the documentation of this file.
1 #ifndef UGDK_GRAPHIC_TEXTUREALTAS_H_
2 #define UGDK_GRAPHIC_TEXTUREALTAS_H_
3 
4 #include <ugdk/graphic.h>
5 #include <ugdk/math/integer2D.h>
7 
8 #include <memory>
9 #include <vector>
10 #include <unordered_map>
11 
12 #ifdef SWIG
13 // Nested class not supported
14 #pragma SWIG nowarn=325
15 #endif
16 
17 namespace ugdk {
18 namespace graphic {
19 
20 class TextureAtlas {
21  struct Piece {
22  Piece(const math::Integer2D& pos, const math::Integer2D& s)
23  : position(pos), size(s), trimmed_size(s)
24  , horizontal_flip(false), vertical_flip(false), rotated_90_clockwise(false) {}
25  math::Integer2D position, size, trimmed_size, offset;
26  bool horizontal_flip;
27  bool vertical_flip;
28  bool rotated_90_clockwise;
29  };
30  public:
31  class BoundPiece {
32  public:
33  BoundPiece(const TextureAtlas* atlas, const Piece* piece) : atlas_(atlas), piece_(piece) {}
35 
36  const TextureAtlas* atlas() const { return atlas_; }
37  const math::Integer2D& position() const { return piece_->position; }
38  const math::Integer2D& size() const { return piece_->size; }
39  const math::Integer2D& trimmed_size() const { return piece_->trimmed_size; }
40  const math::Integer2D& offset() const { return piece_->offset; }
41 
42  void ConvertToAtlas(float *u, float *v) const;
43  void ConvertToAtlas(float in_u, float in_v, float *out_u, float *out_v) const;
44 
45  private:
46  const TextureAtlas* atlas_;
47  const Piece* piece_;
48  };
49 
50  TextureAtlas(const graphic::GLTexture* texture, std::size_t size);
51  ~TextureAtlas();
52 
53  static TextureAtlas* LoadFromFile(const std::string& filepath);
54 
55  std::size_t AddPiece(const std::string& name, const math::Integer2D& pos, const math::Integer2D& size) {
56  if (names_.find(name) != names_.end())
57  throw system::BaseException("Piece already declared: %s", name.c_str());
58 
59  pieces_.emplace_back(pos, size);
60  std::size_t frame_number = pieces_.size() - 1;
61  names_[name] = frame_number;
62  return frame_number;
63  }
64 
65  math::Integer2D size() const;
66  size_t piece_num() const {
67  return pieces_.size();
68  }
69  const graphic::GLTexture* texture() const {
70  return texture_;
71  }
72 
73  std::size_t NameToId(const std::string& name) const {
74  return names_.at(name);
75  }
76 
77 #ifndef SWIG
78  BoundPiece PieceAt(size_t i) const {
79  return BoundPiece(this, &pieces_[i]);
80  }
81 
82  BoundPiece PieceAt(const std::string& name) const {
83  return PieceAt(NameToId(name));
84  }
85 #endif
86 
87  private:
88  const graphic::GLTexture* texture_;
89  std::vector<Piece> pieces_;
90  std::unordered_map<std::string, std::size_t> names_;
91 };
92 
93 } // namespace graphic
94 } // namespace ugdk
95 
96 #endif // UGDK_GRAPHIC_TEXTUREALTAS_H_
const math::Integer2D & size() const
Definition: textureatlas.h:38
const TextureAtlas * atlas() const
Definition: textureatlas.h:36
Definition: textureatlas.h:31
const graphic::GLTexture * texture() const
Definition: textureatlas.h:69
Definition: integer2D.h:20
BoundPiece PieceAt(size_t i) const
Definition: textureatlas.h:78
Definition: animation.h:11
BoundPiece(const TextureAtlas *atlas, const Piece *piece)
Definition: textureatlas.h:33
static TextureAtlas * LoadFromFile(const std::string &filepath)
~BoundPiece()
Definition: textureatlas.h:34
TextureAtlas(const graphic::GLTexture *texture, std::size_t size)
size_t piece_num() const
Definition: textureatlas.h:66
const math::Integer2D & position() const
Definition: textureatlas.h:37
Definition: textureatlas.h:20
BoundPiece PieceAt(const std::string &name) const
Definition: textureatlas.h:82
math::Integer2D size() const
const math::Integer2D & offset() const
Definition: textureatlas.h:40
std::size_t AddPiece(const std::string &name, const math::Integer2D &pos, const math::Integer2D &size)
Definition: textureatlas.h:55
const math::Integer2D & trimmed_size() const
Definition: textureatlas.h:39
Definition: exceptions.h:9
std::size_t NameToId(const std::string &name) const
Definition: textureatlas.h:73
void ConvertToAtlas(float *u, float *v) const