the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2#include "..\Minecraft.World\net.minecraft.world.h"
3#include "..\Minecraft.World\net.minecraft.world.level.tile.h"
4#include "..\Minecraft.World\net.minecraft.world.item.h"
5#include "Minecraft.h"
6#include "LevelRenderer.h"
7#include "EntityRenderDispatcher.h"
8#include "Stitcher.h"
9#include "StitchSlot.h"
10#include "StitchedTexture.h"
11#include "Texture.h"
12#include "TextureHolder.h"
13#include "TextureManager.h"
14#include "TexturePack.h"
15#include "TexturePackRepository.h"
16#include "TextureMap.h"
17
18const wstring TextureMap::NAME_MISSING_TEXTURE = L"missingno";
19
20TextureMap::TextureMap(int type, const wstring &name, const wstring &path, BufferedImage *missingTexture, bool mipmap) : iconType(type), name(name), path(path), extension(L".png")
21{
22 this->missingTexture = missingTexture;
23
24 // 4J Initialisers
25 missingPosition = NULL;
26 stitchResult = NULL;
27
28 m_mipMap = mipmap;
29}
30
31void TextureMap::stitch()
32{
33 texturesToRegister.clear();
34
35 if (iconType == Icon::TYPE_TERRAIN)
36 {
37 //for (Tile tile : Tile.tiles)
38 for(unsigned int i = 0; i < Tile::TILE_NUM_COUNT; ++i)
39 {
40 if (Tile::tiles[i] != NULL)
41 {
42 Tile::tiles[i]->registerIcons(this);
43 }
44 }
45
46 Minecraft::GetInstance()->levelRenderer->registerTextures(this);
47 EntityRenderDispatcher::instance->registerTerrainTextures(this);
48 }
49
50 //for (Item item : Item.items)
51 for(unsigned int i = 0; i < Item::ITEM_NUM_COUNT; ++i)
52 {
53 Item *item = Item::items[i];
54 if (item != NULL && item->getIconType() == iconType)
55 {
56 item->registerIcons(this);
57 }
58 }
59
60 // Collection bucket for multiple frames per texture
61 unordered_map<TextureHolder *, vector<Texture *> * > textures; // = new HashMap<TextureHolder, List<Texture>>();
62
63 Stitcher *stitcher = TextureManager::getInstance()->createStitcher(name);
64
65 for(AUTO_VAR(it,texturesByName.begin()); it != texturesByName.end(); ++it)
66 {
67 delete it->second;
68 }
69 texturesByName.clear();
70 animatedTextures.clear();
71
72 // Prep missing texture -- anything that has no resources will get pointed at this one
73 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);
74 TextureHolder *missingHolder = new TextureHolder(missingTex);
75
76 stitcher->addTexture(missingHolder);
77 vector<Texture *> *missingVec = new vector<Texture *>();
78 missingVec->push_back(missingTex);
79 textures.insert( unordered_map<TextureHolder *, vector<Texture *> * >::value_type( missingHolder, missingVec ));
80
81 // Extract frames from textures and add them to the stitchers
82 //for (final String name : texturesToRegister.keySet())
83 for(AUTO_VAR(it, texturesToRegister.begin()); it != texturesToRegister.end(); ++it)
84 {
85 wstring name = it->first;
86
87 wstring filename = path + name + extension;
88
89 // TODO: [EB] Put the frames into a proper object, not this inside out hack
90 vector<Texture *> *frames = TextureManager::getInstance()->createTextures(filename, m_mipMap);
91
92 if (frames == NULL || frames->empty())
93 {
94 continue; // Couldn't load a texture, skip it
95 }
96
97 TextureHolder *holder = new TextureHolder(frames->at(0));
98 stitcher->addTexture(holder);
99
100 // Store frames
101 textures.insert( unordered_map<TextureHolder *, vector<Texture *> * >::value_type( holder, frames ) );
102 }
103
104 // Stitch!
105 //try {
106 stitcher->stitch();
107 //} catch (StitcherException e) {
108 // throw e;
109 // TODO: [EB] Retry mechanism
110 //}
111
112 // Create the final image
113 stitchResult = stitcher->constructTexture(m_mipMap);
114
115 // Extract all the final positions and store them
116 AUTO_VAR(areas, stitcher->gatherAreas());
117 //for (StitchSlot slot : stitcher.gatherAreas())
118 for(AUTO_VAR(it, areas->begin()); it != areas->end(); ++it)
119 {
120 StitchSlot *slot = *it;
121 TextureHolder *textureHolder = slot->getHolder();
122
123 Texture *texture = textureHolder->getTexture();
124 wstring textureName = texture->getName();
125
126 vector<Texture *> *frames = textures.find(textureHolder)->second;
127
128 StitchedTexture *stored = NULL;
129
130 AUTO_VAR(itTex, texturesToRegister.find(textureName) );
131 if(itTex != texturesToRegister.end() ) stored = itTex->second;
132
133 // [EB]: What is this code for? debug warnings for when during transition?
134 bool missing = false;
135 if (stored == NULL)
136 {
137 missing = true;
138 stored = StitchedTexture::create(textureName);
139
140 if (textureName.compare(NAME_MISSING_TEXTURE)!=0)
141 {
142 //Minecraft::getInstance()->getLogger().warning("Couldn't find premade icon for " + textureName + " doing " + name);
143#ifndef _CONTENT_PACKAGE
144 wprintf(L"Couldn't find premade icon for %ls doing %ls\n", textureName.c_str(), name.c_str() );
145#endif
146 }
147 }
148
149 stored->init(stitchResult, frames, slot->getX(), slot->getY(), textureHolder->getTexture()->getWidth(), textureHolder->getTexture()->getHeight(), textureHolder->isRotated());
150
151 texturesByName.insert( stringStitchedTextureMap::value_type(textureName, stored) );
152 if (!missing) texturesToRegister.erase(textureName);
153
154 if (frames->size() > 1)
155 {
156 animatedTextures.push_back(stored);
157
158 wstring animationDefinitionFile = textureName + L".txt";
159
160 TexturePack *texturePack = Minecraft::GetInstance()->skins->getSelected();
161 bool requiresFallback = !texturePack->hasFile(L"\\" + textureName + L".png", false);
162 //try {
163 InputStream *fileStream = texturePack->getResource(L"\\" + path + animationDefinitionFile, requiresFallback);
164
165 //Minecraft::getInstance()->getLogger().info("Found animation info for: " + animationDefinitionFile);
166#ifndef _CONTENT_PACKAGE
167 wprintf(L"Found animation info for: %ls\n", animationDefinitionFile.c_str() );
168#endif
169 InputStreamReader isr(fileStream);
170 BufferedReader br(&isr);
171 stored->loadAnimationFrames(&br);
172 delete fileStream;
173 //} catch (IOException ignored) {
174 //}
175 }
176 }
177 delete areas;
178
179 missingPosition = texturesByName.find(NAME_MISSING_TEXTURE)->second;
180
181 //for (StitchedTexture texture : texturesToRegister.values())
182 for(AUTO_VAR(it, texturesToRegister.begin() ); it != texturesToRegister.end(); ++it)
183 {
184 StitchedTexture *texture = it->second;
185 texture->replaceWith(missingPosition);
186 }
187
188 stitchResult->writeAsPNG(L"debug.stitched_" + name + L".png");
189 stitchResult->updateOnGPU();
190}
191
192StitchedTexture *TextureMap::getTexture(const wstring &name)
193{
194 StitchedTexture *result = texturesByName.find(name)->second;
195 if (result == NULL) result = missingPosition;
196 return result;
197}
198
199void TextureMap::cycleAnimationFrames()
200{
201 //for (StitchedTexture texture : animatedTextures)
202 for(AUTO_VAR(it, animatedTextures.begin() ); it != animatedTextures.end(); ++it)
203 {
204 StitchedTexture *texture = *it;
205 texture->cycleFrames();
206 }
207}
208
209Texture *TextureMap::getStitchedTexture()
210{
211 return stitchResult;
212}
213
214// 4J Stu - register is a reserved keyword in C++
215Icon *TextureMap::registerIcon(const wstring &name)
216{
217 if (name.empty())
218 {
219 app.DebugPrintf("Don't register NULL\n");
220#ifndef _CONTENT_PACKAGE
221 __debugbreak();
222#endif
223 //new RuntimeException("Don't register null!").printStackTrace();
224 }
225
226 // TODO: [EB]: Why do we allow multiple registrations?
227 StitchedTexture *result = NULL;
228 AUTO_VAR(it, texturesToRegister.find(name));
229 if(it != texturesToRegister.end()) result = it->second;
230
231 if (result == NULL)
232 {
233 result = StitchedTexture::create(name);
234 texturesToRegister.insert( stringStitchedTextureMap::value_type(name, result) );
235 }
236
237 return result;
238}
239
240int TextureMap::getIconType()
241{
242 return iconType;
243}
244
245Icon *TextureMap::getMissingIcon()
246{
247 return missingPosition;
248}