summaryrefslogtreecommitdiffstats
path: root/src/AssetManager.hpp
blob: 3b4ad2f2cdbfb67f534419af895c3a3db0f0c0c8 (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
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
#pragma once

#include <string>
#include <vector>
#include <map>
#include <functional>
#include <memory>

#include <GL/glew.h>
#include <glm/vec4.hpp>
#include <glm/vec3.hpp>
#include <glm/mat4x4.hpp>

#include "Vector.hpp"
#include "Block.hpp"
#include "TextureAtlas.hpp"
#include "Shader.hpp"

enum FaceDirection {
	down,
	up,
	north,
	south,
	west,
	east,
	none,
};

static const Vector FaceDirectionVector[] = {
	Vector(0,-1,0),
	Vector(0,1,0),
	Vector(0,0,-1),
	Vector(0,0,1),
	Vector(-1,0,0),
	Vector(1,0,0),
	Vector(0,0,0)
};

struct ParsedFace {
	FaceDirection visibility;
	glm::mat4 transform;
	glm::vec4 texture;
	float layer;
	glm::vec3 color;	
};

struct BlockFaces {
	glm::mat4 transform;
	std::vector<ParsedFace> faces;
	bool isBlock;
	Vector faceDirectionVector[FaceDirection::none];
};

struct BlockModel {
    bool IsBlock = false;
    std::string BlockName;

    bool AmbientOcclusion = true;

    enum DisplayVariants {
        thirdperson_righthand,
        thirdperson_lefthand,
        firstperson_righthand,
        firstperson_lefthand,
        gui,
        head,
        ground,
        fixed,
        DisplayVariantsCount,
    };

    struct DisplayData {
        Vector rotation;
        Vector translation;
        Vector scale;
    };
    std::map<DisplayVariants, DisplayData> Display;

    std::map<std::string, std::string> Textures;

    struct ElementData {
        Vector from;
        Vector to;

        Vector rotationOrigin = Vector(8, 8, 8);
        enum Axis {
            x,
            y,
            z,
        } rotationAxis = Axis::x;
        int rotationAngle = 0;
        bool rotationRescale = false;

        bool shade = true;

        struct FaceData {
            struct Uv {
                int x1, y1, x2, y2;                
            } uv = { 0,0,0,0 };

            std::string texture;
            FaceDirection cullface = FaceDirection::none;
            int rotation = 0;
            bool tintIndex = false;
            
        };
        std::map<FaceDirection, FaceData> faces;
    };

    std::vector<ElementData> Elements;

	std::vector<ParsedFace> parsedFaces;
};

struct BlockStateVariant {
	std::string variantName;

	struct Model {
		std::string modelName;
		int x = 0;
		int y = 0;
		bool uvLock = false;
		int weight = 1;
	};

	std::vector<Model> models;
};

struct BlockState {
	std::map<std::string, BlockStateVariant> variants;
};

inline bool operator==(const BlockModel::ElementData::FaceData::Uv &lhs,
                       const BlockModel::ElementData::FaceData::Uv &rhs) {
    return lhs.x1 == rhs.x1 && lhs.y1 == rhs.y1 && lhs.x2 == rhs.x2 && lhs.y2 == rhs.y2;
}

struct Asset {
	virtual ~Asset() {};
};

struct AssetTreeNode {
	std::vector<std::unique_ptr<AssetTreeNode>> childs;
	std::vector<unsigned char> data;
	std::string name;
	std::unique_ptr<Asset> asset;
	AssetTreeNode *parent;
};

struct AssetBlockModel : Asset {
	BlockModel blockModel;
};

struct AssetBlockState : Asset {
	BlockState blockState;
};

struct AssetTexture : Asset {
	std::vector<unsigned char> textureData;
	unsigned int realWidth, realHeight;
	size_t id;
};

struct AssetShader : Asset {
	std::unique_ptr<NewShader> shader;
};

namespace AssetManager {
	void InitAssetManager();

	BlockFaces &GetBlockModelByBlockId(BlockId block);

    std::string GetAssetNameByBlockId(BlockId block);

	Asset *GetAssetPtr(const std::string &assetName);

	template <typename T>
	T *GetAsset(const std::string &assetName) {		
		return dynamic_cast<T*>(GetAssetPtr(assetName));
	}

	void RecursiveWalkAsset(const std::string &assetPath, std::function<void(AssetTreeNode&)> fnc);

	AssetTreeNode *GetAssetByAssetName(const std::string &assetName);
	
	GLuint GetTextureAtlasId();

	TextureCoord GetTexture(const std::string assetName);
};