aboutsummaryrefslogtreecommitdiffstats
path: root/yage/base/spritesheet.cpp
blob: 311bb9554671f7c8978a269ea16b9ccca862c595 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* ----------------------------------------------------------------------------
 * 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/reader.h>
#include <yage/base/imageloader.h>

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

namespace yage
{

namespace details
{

bool SpriteSheetHandler::Null()
{
    return true;
}

bool SpriteSheetHandler::Bool(bool)
{
    return true;
}

bool SpriteSheetHandler::Int(int i)
{
    return handleNumber(i);
}

bool SpriteSheetHandler::Uint(unsigned u)
{
    return handleNumber(static_cast<int>(u));
}

bool SpriteSheetHandler::Int64(int64_t i)
{
    return handleNumber(static_cast<int>(i));
}

bool SpriteSheetHandler::Uint64(uint64_t u)
{
    return handleNumber(static_cast<int>(u));
}

bool SpriteSheetHandler::Double(double d)
{
    return handleNumber(static_cast<int>(d));
}

bool SpriteSheetHandler::String(const char *, SizeType, bool)
{
    return true;
}

bool SpriteSheetHandler::Key(const char *str, SizeType length, bool)
{
    current_key_ = string(str, length);
    return true;
}

bool SpriteSheetHandler::StartObject()
{
    depth_++;

    if (depth_ == 3) {
        current_image_ = current_key_;
    }

    return true;
}

bool SpriteSheetHandler::EndObject(SizeType)
{
    if (depth_ == 3) {
        map_[current_image_] = coord_;
    }
    depth_--;
    return true;
}

bool SpriteSheetHandler::StartArray()
{
    return true;
}

bool SpriteSheetHandler::EndArray(SizeType)
{
    return true;
}

SpriteMap SpriteSheetHandler::spriteMap() const
{
    return map_;
}

int SpriteSheetHandler::imageWidth() const
{
    return image_width_;
}

int SpriteSheetHandler::imageHeight() const
{
    return image_height_;
}

bool SpriteSheetHandler::handleNumber(int i)
{
    if (current_key_ == "width") {
        if (depth_ == 1) {
            image_width_ = i;
        } else {
            coord_.width = i;
        }
    } else if (current_key_ == "height") {
        if (depth_ == 1) {
            image_height_ = i;
        } else {
            coord_.height = i;
        }
    } else if (current_key_ == "x") {
        coord_.x = i;
    } else if (current_key_ == "y") {
        coord_.y = i;
    }
    return true;
}

} // namespace details

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

    SpriteSheetHandler ssHandler;
    Reader reader;
    StringStream ss(fileContents.c_str());
    reader.Parse(ss, ssHandler);

    fileLocations_ = ssHandler.spriteMap();
    texture_ = ImageLoader::loadPng(pngFileName);

    if (texture_.width != ssHandler.imageWidth())
        throw runtime_error("Texture width not equal: " +
                            to_string(texture_.width) +
                            " != " + to_string(ssHandler.imageWidth()));
    if (texture_.height != ssHandler.imageHeight())
        throw runtime_error(
            "Texture height not equal: " + to_string(texture_.height) +
            " != " + to_string(ssHandler.imageHeight()));
    assert(texture_.height == ssHandler.imageHeight());
}

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

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

    return stream.str();
}

} // namespace yage