hx3d  1
2D/3D Simple Game Framework
texture_atlas.cpp
1 /*
2  Texture atlas.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/graphics/texture_atlas.hpp"
22 
23 #include "hx3d/utils/log.hpp"
24 #include "hx3d/graphics/texture.hpp"
25 
26 #include <rapidjson/document.h>
27 
28 namespace hx3d {
29 namespace graphics {
30 
31 TextureAtlas::TextureAtlas(std::string pathToAtlas) {
32  loadFromJSON(pathToAtlas);
33 }
34 
36  return _regions[name];
37 }
38 
39 void TextureAtlas::loadFromJSON(std::string pathToAtlas) {
40  auto file = File::loadAsciiFile(pathToAtlas);
41  std::string content = file->toString();
42 
43  rapidjson::Document d;
44  d.Parse(content.c_str());
45 
46  std::string image = d["image"].GetString();
47  std::string type = d["type"].GetString();
48 
49  Log.Info("Loading texture atlas `%s`...", pathToAtlas.c_str());
50  Log.Info("\tImage: %s", image.c_str());
51  Log.Info("\tType: %s", type == "pure" ? "Pure" : "Animated");
52 
53  if (type == "pure") {
54  _texture = Make<Texture>(image);
55 
56  const rapidjson::Value& frames = d["frames"];
57  for (auto it = frames.Begin(); it != frames.End(); ++it) {
58  const rapidjson::Value& val = *it;
59  std::string name = val["name"].GetString();
60  int x = std::atoi(val["x"].GetString());
61  int y = std::atoi(val["y"].GetString());
62  int w = std::atoi(val["w"].GetString());
63  int h = std::atoi(val["h"].GetString());
64 
65  TextureRegion region(
66  _texture,
67  x,
68  x + w,
69  y,
70  y + h
71  );
72 
73  _regions.emplace(std::make_pair(name, region));
74  }
75 
76  } else {
77  Log.Error("/!\\ Animated atlas not supported yet.");
78  return;
79  }
80 }
81 
82 } /* graphics */
83 } /* hx3d */
TextureAtlas(std::string pathToAtlas)
Load an atlas from a path.
hx3d framework namespace
Definition: audio.hpp:26
static Ptr< File > loadAsciiFile(std::string path)
Load an ascii file from a path.
Definition: file.cpp:52
TextureRegion & getRegion(std::string name)
Get a region following a name.
void Error(const std::string fmt,...)
Write an error message.
Definition: log.cpp:72
Defines a rectangle in a texture.
static hx3d::LogImpl Log
Current log implementation.
Definition: log.hpp:91
void Info(const std::string fmt,...)
Write an info message.
Definition: log.cpp:58