summaryrefslogtreecommitdiff
path: root/include/llmr/style/sprite.hpp
blob: 4f902d77a909df7b3a36767d6cb97f8347b7f730 (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
#ifndef LLMR_STYLE_SPRITE
#define LLMR_STYLE_SPRITE

#include <map>
#include <string>
#include <mutex>
#include <memory>
#include <atomic>

#include <llmr/util/raster.hpp>
#include <llmr/util/vec.hpp>

namespace llmr {

class Map;

class SpritePosition {
public:
    explicit SpritePosition() {}
    explicit SpritePosition(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t pixelRatio = 1);

    operator bool() const {
        return !(width == 0 && height == 0 && x == 0 && y == 0);
    }

    uint16_t x = 0, y = 0;
    uint16_t width = 0, height = 0;
    uint8_t pixelRatio = 1;
};

class Sprite : public std::enable_shared_from_this<Sprite> {
public:
    Sprite(Map &map, float pixelRatio = 1);

    void load(const std::string& base_url);

    const SpritePosition &getSpritePosition(const std::string& name) const;

    bool isLoaded() const;

public:
    const float pixelRatio;
    std::unique_ptr<util::Image> raster;

private:
    void asyncParseJSON();
    void asyncParseImage();

    static void parseJSON(std::shared_ptr<Sprite> &sprite);
    static void parseImage(std::shared_ptr<Sprite> &sprite);
    static void complete(std::shared_ptr<Sprite> &sprite);

private:
    Map &map;
    std::string body;
    std::string image;
    std::atomic<bool> loaded;
    std::map<std::string, SpritePosition> pos;
    const SpritePosition empty;
};

}

#endif