summaryrefslogtreecommitdiffstats
path: root/src/core/AssetManager.hpp
blob: b378764d4dccf40a6df5a67b926e8efb48c4d5cc (plain) (blame)
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
#pragma once

#include <experimental/filesystem>
#include <map>
#include <GL/glew.h>
#include <glm/vec4.hpp>
#include <nlohmann/json.hpp>
#include "../world/Block.hpp"
#include "../graphics/Texture.hpp"

struct TextureCoordinates {
    TextureCoordinates(float x = -1, float y = -1, float w = -1, float h = -1) : x(x), y(y), w(w), h(h) {}

    bool operator==(const TextureCoordinates &rhs) const {
        return x == rhs.x &&
               y == rhs.y &&
               w == rhs.w &&
               h == rhs.h;
    }

    explicit operator bool() const {
        return !(*this == TextureCoordinates(-1, -1, -1, -1));
    }

    float x, y, w, h;
};

struct BlockTextureId {
    //Block sides: 0 - bottom, 1 - top, 2 - north, 3 - south, 4 - west, 5 - east 6 - every side
    BlockTextureId(int id = 0, int state = 0, int side = 6) : id(id), state(state), side(side) {}

    int id:9;
    int state:4;
    int side:3;

    bool operator<(const BlockTextureId &rhs) const {
        if (id < rhs.id)
            return true;
        if (rhs.id < id)
            return false;
        return state < rhs.state;
    }
};

class AssetManager {
    Texture *textureAtlas;
    std::map<std::string, Block> assetIds;
    std::map<std::string, TextureCoordinates> assetTextures;
public:
    AssetManager();

    ~AssetManager();

    void LoadTextureResources();

    TextureCoordinates GetTextureByAssetName(std::string AssetName);

    std::string GetTextureAssetNameByBlockId(BlockTextureId block);

    GLuint GetTextureAtlas();

    void LoadIds();

    TextureCoordinates GetTextureByBlock(BlockTextureId block);
};