1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* Textures used by the landscape */
19 
20 #ifndef INC_C4Texture
21 #define INC_C4Texture
22 
23 #include "config/C4Constants.h"
24 #include "landscape/C4Material.h"
25 #include "landscape/C4TextureShape.h"
26 #include "graphics/C4Draw.h"
27 #include "graphics/C4Surface.h"
28 
29 class C4Texture
30 {
31 	friend class C4TextureMap;
32 public:
33 	C4Texture();
34 	~C4Texture();
35 	C4Surface * Surface32;
36 
SetAverageColor(uint32_t Color)37 	void SetAverageColor(uint32_t Color) { AvgColor = Color; }
GetAverageColor()38 	uint32_t GetAverageColor() const { return AvgColor; }
SetMaterialShape(class C4TextureShape * s)39 	void SetMaterialShape(class C4TextureShape *s) { material_shape.reset(s); }
GetMaterialShape()40 	class C4TextureShape *GetMaterialShape() const { return material_shape.get(); }
41 protected:
42 	StdStrBuf Name;
43 	uint32_t AvgColor;
44 	std::unique_ptr<class C4TextureShape> material_shape;
45 	C4Texture *Next;
46 };
47 
48 class C4TexMapEntry
49 {
50 	friend class C4TextureMap;
51 public:
52 	C4TexMapEntry();
53 private:
54 	StdCopyStrBuf Material, Texture;
55 	int32_t iMaterialIndex;
56 	C4Material *pMaterial{nullptr};
57 	C4Pattern MatPattern;
58 public:
isNull()59 	bool isNull() const { return Material.isNull(); }
GetMaterialName()60 	const char *GetMaterialName() const { return Material.getData(); }
GetTextureName()61 	const char *GetTextureName() const { return Texture.getData(); }
GetMaterialIndex()62 	int32_t GetMaterialIndex() const { return iMaterialIndex; }
GetMaterial()63 	C4Material *GetMaterial() const { return pMaterial; }
GetPattern()64 	const C4Pattern &GetPattern() const { return MatPattern; }
65 	void Clear();
66 	bool Create(const char *szMaterial, const char *szTexture);
67 	bool Init();
68 };
69 
70 class C4TextureMap
71 {
72 public:
73 	C4TextureMap();
74 	~C4TextureMap();
75 protected:
76 	C4TexMapEntry Entry[C4M_MaxTexIndex];
77 	std::vector<int32_t> Order; // drawing order in map2landscape. Reflects order in MatMap.txt file.
78 	C4Texture *FirstTexture{nullptr};
79 	bool fOverloadMaterials{false};
80 	bool fOverloadTextures{false};
81 	bool fInitialized{false}; // Set after Init() - newly added entries initialized automatically
82 public:
83 	bool fEntriesAdded{false};
84 public:
GetEntry(int32_t iIndex)85 	const C4TexMapEntry *GetEntry(int32_t iIndex) const { return Inside<int32_t>(iIndex, 0, C4M_MaxTexIndex-1) ? &Entry[iIndex] : nullptr; }
86 	void RemoveEntry(int32_t iIndex);
87 	void Clear();
88 	void StoreMapPalette(CStdPalette *, C4MaterialMap &rMaterials);
89 	static bool LoadFlags(C4Group &hGroup, const char *szEntryName, bool *pOverloadMaterials, bool *pOverloadTextures);
90 	int32_t LoadMap(C4Group &hGroup, const char *szEntryName, bool *pOverloadMaterials, bool *pOverloadTextures);
91 	int32_t Init();
92 	bool SaveMap(C4Group &hGroup, const char *szEntryName);
93 	int32_t LoadTextures(C4Group &hGroup, C4Group* OverloadFile=nullptr);
94 	bool HasTextures(C4Group &hGroup);
95 	const char *GetTexture(int32_t iIndex);
96 	void MoveIndex(BYTE byOldIndex, BYTE byNewIndex); // change index of texture
97 	int32_t GetIndex(const char *szMaterial, const char *szTexture, bool fAddIfNotExist=true, const char *szErrorIfFailed=nullptr);
98 	int32_t GetIndexMatTex(const char *szMaterialTexture, const char *szDefaultTexture = nullptr, bool fAddIfNotExist=true, const char *szErrorIfFailed=nullptr);
99 	C4Texture * GetTexture(const char *szTexture);
100 	bool CheckTexture(const char *szTexture); // return whether texture exists
101 	bool AddEntry(BYTE byIndex, const char *szMaterial, const char *szTexture);
102 	bool AddTexture(const char *szTexture, C4Surface * sfcSurface);
103 	int32_t GetTextureIndex(const char *pTexName);
104 	BYTE DefaultBkgMatTex(BYTE fg) const;
105 protected:
106 	friend class C4Landscape;
107 };
108 
109 extern C4TextureMap TextureMap;
110 
111 #endif
112