1 /**
2 Copyright: Copyright (c) 2017-2018 Andrey Penechko.
3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 module voxelman.graphics.resourcemanager;
7 
8 import std.path : buildPath;
9 import voxelman.math;
10 import voxelman.graphics;
11 
12 enum string fontPath = "font";
13 
14 final class ResourceManager
15 {
16 	TextureAtlas texAtlas;
17 	FontManager fontManager;
18 	string resourcePath;
19 
20 	// used for solid coloring
21 	// allows rendering filled polygons together with textured polygons
22 	ivec2 whitePixelPos;
23 
24 	this(string resourcePath)
25 	{
26 		this.resourcePath = resourcePath;
27 
28 		texAtlas = new TextureAtlas(256);
29 		fontManager = new FontManager(buildPath(resourcePath, fontPath), texAtlas);
30 		findOrAddWhitePixel();
31 	}
32 
33 	SpriteRef[string] loadNamedSpriteSheet(string name, ivec2 spriteSize) {
34 		return .loadNamedSpriteSheet(buildPath(resourcePath, name), texAtlas, spriteSize);
35 	}
36 	SpriteRef[string] loadNamedSpriteSheet(string name, TextureAtlas texAtlas, ivec2 spriteSize) {
37 		return .loadNamedSpriteSheet(buildPath(resourcePath, name), texAtlas, spriteSize);
38 	}
39 
40 	SpriteRef[] loadIndexedSpriteSheet(string name, ivec2 spriteSize) {
41 		return .loadIndexedSpriteSheet(buildPath(resourcePath, name), texAtlas, spriteSize);
42 	}
43 	SpriteRef[] loadIndexedSpriteSheet(string name, TextureAtlas texAtlas, ivec2 spriteSize) {
44 		return .loadIndexedSpriteSheet(buildPath(resourcePath, name), texAtlas, spriteSize);
45 	}
46 
47 	SpriteSheetAnimationRef loadAnimation(string name)
48 	{
49 		return .loadSpriteSheetAnimation(buildPath(resourcePath, name), texAtlas);
50 	}
51 
52 	SpriteRef loadSprite(string name)
53 	{
54 		return .loadSprite(buildPath(resourcePath, name), texAtlas);
55 	}
56 
57 	void findOrAddWhitePixel()
58 	{
59 		import dlib.image.color : Color4f;
60 		foreach (col, x, y; texAtlas.bitmap)
61 		{
62 			if (col == Color4f(1, 1, 1))
63 			{
64 				whitePixelPos = ivec2(x, y);
65 				return;
66 			}
67 		}
68 
69 		whitePixelPos = texAtlas.insert(ivec2(1, 1));
70 		texAtlas.bitmap[whitePixelPos.x, whitePixelPos.y] = Color4f(1, 1, 1);
71 	}
72 }