aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/TextureMap.cpp
blob: 9aefbb6370a3c7e8e7a11d164b888c0956e015de (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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "stdafx.h"
#include "..\Minecraft.World\net.minecraft.world.h"
#include "..\Minecraft.World\net.minecraft.world.level.tile.h"
#include "..\Minecraft.World\net.minecraft.world.item.h"
#include "Minecraft.h"
#include "LevelRenderer.h"
#include "EntityRenderDispatcher.h"
#include "Stitcher.h"
#include "StitchSlot.h"
#include "StitchedTexture.h"
#include "Texture.h"
#include "TextureHolder.h"
#include "TextureManager.h"
#include "TexturePack.h"
#include "TexturePackRepository.h"
#include "TextureMap.h"

const wstring TextureMap::NAME_MISSING_TEXTURE = L"missingno";

TextureMap::TextureMap(int type, const wstring &name, const wstring &path, BufferedImage *missingTexture, bool mipmap) : iconType(type), name(name), path(path), extension(L".png")
{
	this->missingTexture = missingTexture;

	// 4J Initialisers
	missingPosition = NULL;
	stitchResult = NULL;

	m_mipMap = mipmap;
}

void TextureMap::stitch()
{
	texturesToRegister.clear();

	if (iconType == Icon::TYPE_TERRAIN)
	{
		//for (Tile tile : Tile.tiles)
		for(unsigned int i = 0; i < Tile::TILE_NUM_COUNT; ++i)
		{
			if (Tile::tiles[i] != NULL)
			{
				Tile::tiles[i]->registerIcons(this);
			}
		}

		Minecraft::GetInstance()->levelRenderer->registerTextures(this);
		EntityRenderDispatcher::instance->registerTerrainTextures(this);
	}

	//for (Item item : Item.items)
	for(unsigned int i = 0; i < Item::ITEM_NUM_COUNT; ++i)
	{
		Item *item = Item::items[i];
		if (item != NULL && item->getIconType() == iconType)
		{
			item->registerIcons(this);
		}
	}

	// Collection bucket for multiple frames per texture
	unordered_map<TextureHolder *, vector<Texture *> * > textures; // = new HashMap<TextureHolder, List<Texture>>();

	Stitcher *stitcher = TextureManager::getInstance()->createStitcher(name);

	for(auto& it : texturesByName)
	{
		delete it.second;
	}
	texturesByName.clear();
	animatedTextures.clear();

	// Prep missing texture -- anything that has no resources will get pointed at this one
	Texture *missingTex = TextureManager::getInstance()->createTexture(NAME_MISSING_TEXTURE, Texture::TM_CONTAINER, missingTexture->getWidth(), missingTexture->getHeight(), Texture::WM_CLAMP, Texture::TFMT_RGBA, Texture::TFLT_NEAREST, Texture::TFLT_NEAREST, m_mipMap, missingTexture);
	TextureHolder *missingHolder = new TextureHolder(missingTex);

	stitcher->addTexture(missingHolder);
	vector<Texture *> *missingVec = new vector<Texture *>();
	missingVec->push_back(missingTex);
	textures.insert( unordered_map<TextureHolder *, vector<Texture *> * >::value_type( missingHolder, missingVec ));

	// Extract frames from textures and add them to the stitchers
	//for (final String name : texturesToRegister.keySet())
	for(auto& it : texturesToRegister)
	{
		wstring name = it.first;

		wstring filename = path + name + extension;

		// TODO: [EB] Put the frames into a proper object, not this inside out hack
		vector<Texture *> *frames = TextureManager::getInstance()->createTextures(filename, m_mipMap);

		if (frames == NULL || frames->empty())
		{
			continue; // Couldn't load a texture, skip it
		}

		TextureHolder *holder = new TextureHolder(frames->at(0));
		stitcher->addTexture(holder);

		// Store frames
		textures.insert( unordered_map<TextureHolder *, vector<Texture *> * >::value_type( holder, frames ) );
	}

	// Stitch!
	//try {
		stitcher->stitch();
	//} catch (StitcherException e) {
	//	throw e;
		// TODO: [EB] Retry mechanism
	//}

	// Create the final image
	stitchResult = stitcher->constructTexture(m_mipMap);

	// Extract all the final positions and store them
    auto areas = stitcher->gatherAreas();
    //for (StitchSlot slot : stitcher.gatherAreas())
	for(auto& slot : *areas)
	{
		TextureHolder *textureHolder = slot->getHolder();

		Texture *texture = textureHolder->getTexture();
		wstring textureName = texture->getName();

		vector<Texture *> *frames = textures.find(textureHolder)->second;

		StitchedTexture *stored = NULL;

        auto itTex = texturesToRegister.find(textureName);
        if(itTex != texturesToRegister.end() ) stored = itTex->second;

		// [EB]: What is this code for? debug warnings for when during transition?
		bool missing = false;
		if (stored)
		{
			missing = true;
			stored = StitchedTexture::create(textureName);

			if (textureName.compare(NAME_MISSING_TEXTURE)!=0)
			{
				//Minecraft::getInstance()->getLogger().warning("Couldn't find premade icon for " + textureName + " doing " + name);
#ifndef _CONTENT_PACKAGE
				wprintf(L"Couldn't find premade icon for %ls doing %ls\n", textureName.c_str(), name.c_str() );
#endif
			}
		}

		stored->init(stitchResult, frames, slot->getX(), slot->getY(), textureHolder->getTexture()->getWidth(), textureHolder->getTexture()->getHeight(), textureHolder->isRotated());

		texturesByName.insert( stringStitchedTextureMap::value_type(textureName, stored) );
		if (!missing) texturesToRegister.erase(textureName);

		if (frames->size() > 1)
		{
			animatedTextures.push_back(stored);

			wstring animationDefinitionFile = textureName + L".txt";

			TexturePack *texturePack = Minecraft::GetInstance()->skins->getSelected();
			bool requiresFallback = !texturePack->hasFile(L"\\" + textureName + L".png", false);
				InputStream *fileStream = texturePack->getResource(L"\\" + path + animationDefinitionFile, requiresFallback);

				//Minecraft::getInstance()->getLogger().info("Found animation info for: " + animationDefinitionFile);
#ifndef _CONTENT_PACKAGE
				wprintf(L"Found animation info for: %ls\n", animationDefinitionFile.c_str() );
#endif
				InputStreamReader isr(fileStream);
				BufferedReader br(&isr);
				stored->loadAnimationFrames(&br);
				delete fileStream;
		}
	}
	delete areas;

	missingPosition = texturesByName.find(NAME_MISSING_TEXTURE)->second;

	//for (StitchedTexture texture : texturesToRegister.values())
	for(auto& it : texturesToRegister)
	{
		StitchedTexture *texture = it.second;
		texture->replaceWith(missingPosition);
	}

	stitchResult->writeAsPNG(L"debug.stitched_" + name + L".png");
	stitchResult->updateOnGPU();
}

StitchedTexture *TextureMap::getTexture(const wstring &name)
{
	StitchedTexture *result = texturesByName.find(name)->second;
	if (result == NULL) result = missingPosition;
	return result;
}

void TextureMap::cycleAnimationFrames()
{
	//for (StitchedTexture texture : animatedTextures)
	for(auto& texture : animatedTextures)
	{
		if ( texture )
			texture->cycleFrames();
	}
}

Texture *TextureMap::getStitchedTexture()
{
	return stitchResult;
}

// 4J Stu - register is a reserved keyword in C++
Icon *TextureMap::registerIcon(const wstring &name)
{
	if (name.empty())
	{
		app.DebugPrintf("Don't register NULL\n");
#ifndef _CONTENT_PACKAGE
		__debugbreak();
#endif
		//new RuntimeException("Don't register null!").printStackTrace();
	}

	// TODO: [EB]: Why do we allow multiple registrations?
	StitchedTexture *result = NULL;
    auto it = texturesToRegister.find(name);
    if(it != texturesToRegister.end()) result = it->second;

	if (result == NULL)
	{
		result = StitchedTexture::create(name);
		texturesToRegister.insert( stringStitchedTextureMap::value_type(name, result) );
	}

	return result;
}

int TextureMap::getIconType()
{
	return iconType;
}

Icon *TextureMap::getMissingIcon()
{
	return missingPosition;
}