aboutsummaryrefslogtreecommitdiffstats
path: root/yage/base/spritesheet.cpp
blob: d53f64aadd38410a8bf429049bfaf4552dc833fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* ----------------------------------------------------------------------------
 * spritesheet.cpp
 *
 * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
 * MIT License, see LICENSE file for more details.
 * ----------------------------------------------------------------------------
 */

#include "spritesheet.h"

#include <cassert>
#include <fstream>
#include <sstream>
#include <stdexcept>

#include <rapidjson/document.h>
#include <yage/base/imageloader.h>

using namespace std;
using namespace rapidjson;
using namespace yage::details;

namespace yage
{

SpriteSheet::SpriteSheet(string pngFileName, string jsonFileName)
{
    string fileContents = fileContent(jsonFileName);
}

string SpriteSheet::fileContent(string jsonFileName) const
{
    ifstream inputFile(jsonFileName);

    stringstream stream;
    stream << inputFile.rdbuf();

    return stream.str();
}

SpriteMap SpriteSheet::parseJson(int &width, int &height, const string &jsonContent) const
{
    SpriteMap spriteMap;
    Document jsonAtlas;
    jsonAtlas.Parse(jsonContent.c_str());

    width = jsonAtlas["width"].GetInt();
    height = jsonAtlas["height"].GetInt();

    for (auto &texture : jsonAtlas["sprites"].GetObject()) {
        spriteMap[texture.name.GetString()] = Coordinate();
        for (auto &value : texture.value.GetObject()) {
            /// @todo add the coordinate to the map
        }
    }

    return spriteMap;
}

} // namespace yage