the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 196 lines 6.6 kB view raw
1#include "stdafx.h" 2#include "Stitcher.h" 3#include "Texture.h" 4#include "TexturePack.h" 5#include "TexturePackRepository.h" 6#include "Minecraft.h" 7#include "TextureManager.h" 8#include "..\Minecraft.World\StringHelpers.h" 9 10TextureManager *TextureManager::instance = NULL; 11 12void TextureManager::createInstance() 13{ 14 instance = new TextureManager(); 15} 16 17TextureManager *TextureManager::getInstance() 18{ 19 return instance; 20} 21 22TextureManager::TextureManager() 23{ 24 nextID = 0; 25} 26 27int TextureManager::createTextureID() 28{ 29 return nextID++; 30} 31 32Texture *TextureManager::getTexture(const wstring &name) 33{ 34 if (stringToIDMap.find(name) != stringToIDMap.end()) 35 { 36 return idToTextureMap.find(stringToIDMap.find(name)->second)->second; 37 } 38 39 return NULL; 40} 41 42void TextureManager::registerName(const wstring &name, Texture *texture) 43{ 44 stringToIDMap.insert( stringIntMap::value_type( name, texture->getManagerId() ) ); 45 46 if (idToTextureMap.find(texture->getManagerId()) == idToTextureMap.end()) 47 { 48 idToTextureMap.insert( intTextureMap::value_type( texture->getManagerId(), texture) ); 49 } 50} 51 52void TextureManager::registerTexture(Texture *texture) 53{ 54 for(AUTO_VAR(it, idToTextureMap.begin()); it != idToTextureMap.end(); ++it) 55 { 56 if(it->second == texture) 57 { 58 //Minecraft.getInstance().getLogger().warning("TextureManager.registerTexture called, but this texture has " + "already been registered. ignoring."); 59 app.DebugPrintf("TextureManager.registerTexture called, but this texture has already been registered. ignoring."); 60 return; 61 } 62 } 63 64 idToTextureMap.insert( intTextureMap::value_type( texture->getManagerId(), texture ) ); 65} 66 67void TextureManager::unregisterTexture(const wstring &name, Texture *texture) 68{ 69 AUTO_VAR(it, idToTextureMap.find(texture->getManagerId())); 70 if(it != idToTextureMap.end()) idToTextureMap.erase(it); 71 72 AUTO_VAR(it2, stringToIDMap.find(name)); 73 if(it2 != stringToIDMap.end()) stringToIDMap.erase(it2); 74} 75 76Stitcher *TextureManager::createStitcher(const wstring &name) 77{ 78 int maxTextureSize = Minecraft::maxSupportedTextureSize(); 79 80 return new Stitcher(name, maxTextureSize, maxTextureSize, true); 81} 82 83vector<Texture *> *TextureManager::createTextures(const wstring &filename, bool mipmap) 84{ 85 vector<Texture *> *result = new vector<Texture *>(); 86 TexturePack *texturePack = Minecraft::GetInstance()->skins->getSelected(); 87 //try { 88 int mode = Texture::TM_CONTAINER; // Most important -- so it doesn't get uploaded to videoram 89 int clamp = Texture::WM_WRAP; // 4J Stu - Don't clamp as it causes issues with how we signal non-mipmmapped textures to the pixel shader //Texture::WM_CLAMP; 90 int format = Texture::TFMT_RGBA; 91 int minFilter = Texture::TFLT_NEAREST; 92 int magFilter = Texture::TFLT_NEAREST; 93 94 MemSect(32); 95 wstring drive = L""; 96 97 98 if(texturePack->hasFile(L"res/" + filename,false)) 99 { 100 drive = texturePack->getPath(true); 101 } 102 else 103 { 104#ifdef __PS3__ 105 if(app.GetBootedFromDiscPatch()) 106 { 107 const char *pchTextureName=wstringtofilename(filename); 108 char *pchUsrDir = app.GetBDUsrDirPath(pchTextureName); 109 wstring wstr (pchUsrDir, pchUsrDir+strlen(pchUsrDir)); 110 drive= wstr + L"\\Common\\res\\TitleUpdate\\"; 111 } 112 else 113#endif 114 { 115 drive = Minecraft::GetInstance()->skins->getDefault()->getPath(true); 116 } 117 } 118 119 //BufferedImage *image = new BufferedImage(texturePack->getResource(L"/" + filename),false,true,drive); //ImageIO::read(texturePack->getResource(L"/" + filename)); 120 121 BufferedImage *image = texturePack->getImageResource(filename, false, true, drive); 122 MemSect(0); 123 int height = image->getHeight(); 124 int width = image->getWidth(); 125 126 wstring texName = getTextureNameFromPath(filename); 127 128 if (isAnimation(filename, texturePack)) 129 { 130 // TODO: Read this information from the animation file later 131 int frameWidth = width; 132 int frameHeight = width; 133 134 // This could end as 0 frames 135 int frameCount = height / frameWidth; 136 for (int i = 0; i < frameCount; i++) 137 { 138 BufferedImage *subImage = image->getSubimage(0, frameHeight * i, frameWidth, frameHeight); 139 Texture *texture = createTexture(texName, mode, frameWidth, frameHeight, clamp, format, minFilter, magFilter, mipmap || image->getData(1) != NULL, subImage); 140 delete subImage; 141 result->push_back(texture); 142 } 143 } 144 else 145 { 146 // TODO: Remove this hack -- fix proper rotation support (needed for 'off-aspect textures') 147 if (width == height) 148 { 149 result->push_back(createTexture(texName, mode, width, height, clamp, format, minFilter, magFilter, mipmap || image->getData(1) != NULL, image)); 150 } 151 else 152 { 153 //Minecraft.getInstance().getLogger().warning("TextureManager.createTexture: Skipping " + filename + " because of broken aspect ratio and not animation"); 154#ifndef _CONTENT_PACKAGE 155 wprintf(L"TextureManager.createTexture: Skipping %ls because of broken aspect ratio and not animation\n", filename.c_str()); 156#endif 157 } 158 } 159 delete image; 160 161 162 //return result; 163 //} catch (FileNotFoundException e) { 164 // Minecraft.getInstance().getLogger().warning("TextureManager.createTexture called for file " + filename + ", but that file does not exist. Ignoring."); 165 //} catch (IOException e) { 166 // Minecraft.getInstance().getLogger().warning("TextureManager.createTexture encountered an IOException when " + "trying to read file " + filename + ". Ignoring."); 167 //} 168 return result; 169} 170 171wstring TextureManager::getTextureNameFromPath(const wstring &filename) 172{ 173 File file(filename); 174 return file.getName().substr(0, file.getName().find_last_of(L'.')); 175} 176 177bool TextureManager::isAnimation(const wstring &filename, TexturePack *texturePack) 178{ 179 wstring dataFileName = L"/" + filename.substr(0, filename.find_last_of(L'.')) + L".txt"; 180 bool hasOriginalImage = texturePack->hasFile(L"/" + filename, false); 181 return Minecraft::GetInstance()->skins->getSelected()->hasFile(dataFileName, !hasOriginalImage); 182} 183 184Texture *TextureManager::createTexture(const wstring &name, int mode, int width, int height, int wrap, int format, int minFilter, int magFilter, bool mipmap, BufferedImage *image) 185{ 186 Texture *newTex = new Texture(name, mode, width, height, wrap, format, minFilter, magFilter, image, mipmap); 187 registerTexture(newTex); 188 return newTex; 189} 190 191Texture *TextureManager::createTexture(const wstring &name, int mode, int width, int height, int format, bool mipmap) 192{ 193 // 4J Stu - Don't clamp as it causes issues with how we signal non-mipmmapped textures to the pixel shader 194 //return createTexture(name, mode, width, height, Texture::WM_CLAMP, format, Texture::TFLT_NEAREST, Texture::TFLT_NEAREST, mipmap, NULL); 195 return createTexture(name, mode, width, height, Texture::WM_WRAP, format, Texture::TFLT_NEAREST, Texture::TFLT_NEAREST, mipmap, NULL); 196}