summaryrefslogtreecommitdiff
path: root/src/style/sprite.cpp
blob: 70626ef396a39ac162b10d3a7b5ae1b5298482b5 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <llmr/style/sprite.hpp>

#include <string>
#include <llmr/platform/platform.hpp>
#include <llmr/platform/gl.hpp>

#include <png.h>

#include <rapidjson/document.h>

using namespace llmr;

SpritePosition::SpritePosition(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t pixelRatio)
    : x(x),
      y(y),
      width(width),
      height(height),
      pixelRatio(pixelRatio) {}

ImagePosition::ImagePosition(const vec2<uint16_t>& size, vec2<float> tl, vec2<float> br)
    : size(size),
      tl(tl),
      br(br) {}


Sprite::~Sprite() {
    if (img) {
        free(img);
    }
}

Sprite::operator bool() const {
    std::lock_guard<std::mutex> lock(mtx);
    return loaded;
}

void Sprite::load(const std::string& base_url, float pixelRatio) {
    std::shared_ptr<Sprite> sprite = shared_from_this();

    auto complete = [sprite]() {
        std::lock_guard<std::mutex> lock(sprite->mtx);
        if (sprite->img && sprite->pos.size()) {
            sprite->loaded = true;
            platform::restart();
            fprintf(stderr, "sprite loaded\n");
        }
    };

    std::string suffix = (pixelRatio > 1 ? "@2x" : "");

    platform::request_http(base_url + suffix + ".json", [sprite](const platform::Response & res) {
        if (res.code == 200) {
            sprite->parseJSON(res.body);
        } else {
            fprintf(stderr, "failed to load sprite\n");
        }
    }, complete);

    platform::request_http(base_url + suffix + ".png", [sprite](const platform::Response & res) {
        if (res.code == 200) {
            sprite->loadImage(res.body);
        } else {
            fprintf(stderr, "failed to load sprite image\n");
        }
    }, complete);
}


void Sprite::parseJSON(const std::string& data) {
    std::lock_guard<std::mutex> lock(mtx);

    rapidjson::Document d;
    d.Parse<0>(data.c_str());

    if (d.IsObject()) {
        for (rapidjson::Value::ConstMemberIterator itr = d.MemberBegin(); itr != d.MemberEnd(); ++itr) {
            const std::string& name = itr->name.GetString();
            const rapidjson::Value& value = itr->value;

            if (value.IsObject()) {
                uint16_t x = 0;
                uint16_t y = 0;
                uint16_t width = 0;
                uint16_t height = 0;
                uint8_t pixelRatio = 1;

                if (value.HasMember("x")) x = value["x"].GetInt();
                if (value.HasMember("y")) y = value["y"].GetInt();
                if (value.HasMember("width")) width = value["width"].GetInt();
                if (value.HasMember("height")) height = value["height"].GetInt();
                if (value.HasMember("pixelRatio")) pixelRatio = value["height"].GetInt();

                fprintf(stderr, "");

                pos.insert({ name, { x, y, width, height, pixelRatio } });
            }
        }
    }
}

struct Buffer {
    Buffer(const std::string& data)
        : data(data.data()), length(data.size()) {}
    const char *const data = 0;
    const size_t length = 0;
    size_t pos = 0;
};


void readCallback(png_structp png, png_bytep data, png_size_t length) {
    Buffer *reader = static_cast<Buffer *>(png_get_io_ptr(png));

    // Read `length` bytes into `data`.
    if (reader->pos + length > reader->length) {
        png_error(png, "Read Error");
    } else {
        memcpy(data, reader->data + reader->pos, length);
        reader->pos += length;
    }
}

void errorHandler(png_structp png, png_const_charp error_msg) {
    throw std::runtime_error(error_msg);
}

void warningHandler(png_structp png, png_const_charp error_msg) {
    fprintf(stderr, "PNG: %s\n", error_msg);
}

void Sprite::loadImage(const std::string& data) {
    std::lock_guard<std::mutex> lock(mtx);

    Buffer buffer(data);

    if (buffer.length < 8 || !png_check_sig((const png_bytep)buffer.data, 8)) {
        fprintf(stderr, "image is not a valid PNG image\n");
        return;
    }

    png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, errorHandler, warningHandler);
    assert(png);

    png_infop info = png_create_info_struct(png);
    assert(info);

    int depth, color, interlace;

    try {
        png_set_read_fn(png, (png_voidp)&buffer, readCallback);
        png_read_info(png, info);
        png_get_IHDR(png, info, (png_uint_32*)&width, (png_uint_32*)&height, &depth, &color, &interlace, nullptr, nullptr);
        bool alpha = (color & PNG_COLOR_MASK_ALPHA) || png_get_valid(png, info, PNG_INFO_tRNS);

        // From http://trac.mapnik.org/browser/trunk/src/png_reader.cpp
        if (color == PNG_COLOR_TYPE_PALETTE)
            png_set_expand(png);
        if (color == PNG_COLOR_TYPE_GRAY)
            png_set_expand(png);
        if (png_get_valid(png, info, PNG_INFO_tRNS))
            png_set_expand(png);
        if (depth == 16)
            png_set_strip_16(png);
        if (depth < 8)
            png_set_packing(png);
        if (color == PNG_COLOR_TYPE_GRAY ||
                color == PNG_COLOR_TYPE_GRAY_ALPHA)
            png_set_gray_to_rgb(png);

        if (interlace == PNG_INTERLACE_ADAM7)
            png_set_interlace_handling(png);

        // Always add an alpha channel.
        if (!alpha) {
            png_set_add_alpha(png, 0xFF, PNG_FILLER_AFTER);
        }

        double gamma;
        if (png_get_gAMA(png, info, &gamma))
            png_set_gamma(png, 2.2, gamma);

        png_read_update_info(png, info);

        png_size_t rowbytes = png_get_rowbytes(png, info);
        assert(width * 4 == rowbytes);

        img = (char *)malloc(width * height * 4);
        char *surface = img;
        assert(surface);

        png_bytep row_pointers[height];
        for (unsigned i = 0; i < height; ++i) {
            row_pointers[i] = (png_bytep)(surface + (i * rowbytes));
        }

        // Read image data
        png_read_image(png, row_pointers);

        png_read_end(png, nullptr);
    } catch (std::exception& e) {
        fprintf(stderr, "loading PNG failed: %s\n", e.what());
        png_destroy_read_struct(&png, &info, nullptr);
        if (img) {
            free(img);
            img = nullptr;
        }
        width = 0;
        height = 0;
    }
}

ImagePosition Sprite::getPosition(const std::string& name, bool repeating) {
    if (!*this) return {};

    // `repeating` indicates that the image will be used in a repeating pattern
    // repeating pattern images are assumed to have a 1px padding that mirrors the opposite edge
    // positions for repeating images are adjusted to exclude the edge
    int8_t offset = repeating ? 1 : 0;

    auto it = pos.find(name);
    if (it == pos.end()) return {};

    SpritePosition& pos = it->second;
    return {
        {
            pos.width,
            pos.height
        },
        {
            (float)(pos.x + offset) / width,
            (float)(pos.y + offset) / height
        },
        {
            (float)(pos.x + pos.width - 2 * offset) / width,
            (float)(pos.y + pos.height - 2 * offset) / height
        }
    };
}

void Sprite::bind(bool linear) {
    if (!width || !height) {
        fprintf(stderr, "trying to bind texture without dimension\n");
        return;
    }

    if (!texture) {
        glGenTextures(1, &texture);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, texture);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img);
    } else {
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, texture);
    }

    GLuint filter = linear ? GL_LINEAR : GL_NEAREST;
    if (filter != this->filter) {
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
        this->filter = filter;
    }
}