4jcraft/targets/minecraft/client/renderer/texture/TextureMap.cpp

261 lines
9 KiB
C++

#include "minecraft/util/Log.h"
#include "TextureMap.h"
#include <wchar.h>
#include <format>
#include <utility>
#include "app/linux/LinuxGame.h"
#include "app/linux/Stubs/winapi_stubs.h"
#include "minecraft/client/BufferedImage.h"
#include "StitchSlot.h"
#include "StitchedTexture.h"
#include "Stitcher.h"
#include "Texture.h"
#include "TextureHolder.h"
#include "TextureManager.h"
#include "java/InputOutputStream/BufferedReader.h"
#include "java/InputOutputStream/InputStream.h"
#include "java/InputOutputStream/InputStreamReader.h"
#include "minecraft/client/Minecraft.h"
#include "minecraft/client/renderer/LevelRenderer.h"
#include "minecraft/client/renderer/entity/EntityRenderDispatcher.h"
#include "minecraft/client/skins/TexturePack.h"
#include "minecraft/client/skins/TexturePackRepository.h"
#include "minecraft/world/Icon.h"
#include "minecraft/world/item/Item.h"
#include "minecraft/world/level/tile/Tile.h"
const std::string TextureMap::NAME_MISSING_TEXTURE = "missingno";
TextureMap::TextureMap(int type, const std::string& name,
const std::string& path, BufferedImage* missingTexture,
bool mipmap)
: iconType(type), name(name), path(path), extension(".png") {
this->missingTexture = missingTexture;
// 4J Initialisers
missingPosition = nullptr;
stitchResult = nullptr;
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] != nullptr) {
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 != nullptr && item->getIconType() == iconType) {
item->registerIcons(this);
}
}
// Collection bucket for multiple frames per texture
std::unordered_map<TextureHolder*, std::vector<Texture*>*>
textures; // = new HashMap<TextureHolder, List<Texture>>();
Stitcher* stitcher = TextureManager::getInstance()->createStitcher(name);
for (auto it = texturesByName.begin(); it != texturesByName.end(); ++it) {
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);
std::vector<Texture*>* missingVec = new std::vector<Texture*>();
missingVec->push_back(missingTex);
textures.insert(
std::unordered_map<TextureHolder*, std::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.begin(); it != texturesToRegister.end();
++it) {
std::string name = it->first;
std::string filename = path + name + extension;
// TODO: [EB] Put the frames into a proper object, not this inside out
// hack
std::vector<Texture*>* frames =
TextureManager::getInstance()->createTextures(filename, m_mipMap);
if (frames == nullptr || frames->empty()) {
continue; // Couldn't load a texture, skip it
}
TextureHolder* holder = new TextureHolder(frames->at(0));
stitcher->addTexture(holder);
// Store frames
textures.insert(
std::unordered_map<TextureHolder*,
std::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 it = areas->begin(); it != areas->end(); ++it) {
StitchSlot* slot = *it;
TextureHolder* textureHolder = slot->getHolder();
Texture* texture = textureHolder->getTexture();
std::string textureName = texture->getName();
std::vector<Texture*>* frames = textures.find(textureHolder)->second;
StitchedTexture* stored = nullptr;
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 == nullptr) {
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
printf("Couldn't find premade icon for %s doing %s\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);
std::string animationDefinitionFile = textureName + ".txt";
TexturePack* texturePack =
Minecraft::GetInstance()->skins->getSelected();
bool requiresFallback =
!texturePack->hasFile("\\" + textureName + ".png", false);
// try {
InputStream* fileStream = texturePack->getResource(
"\\" + path + animationDefinitionFile, requiresFallback);
// Minecraft::getInstance()->getLogger().info("Found animation info
// for: " + animationDefinitionFile);
#ifndef _CONTENT_PACKAGE
printf("Found animation info for: %s\n",
animationDefinitionFile.c_str());
#endif
InputStreamReader isr(fileStream);
BufferedReader br(&isr);
stored->loadAnimationFrames(&br);
delete fileStream;
//} catch (IOException ignored) {
//}
}
}
delete areas;
missingPosition = texturesByName.find(NAME_MISSING_TEXTURE)->second;
// for (StitchedTexture texture : texturesToRegister.values())
for (auto it = texturesToRegister.begin(); it != texturesToRegister.end();
++it) {
StitchedTexture* texture = it->second;
texture->replaceWith(missingPosition);
}
stitchResult->writeAsPNG("debug.stitched_" + name + ".png");
stitchResult->updateOnGPU();
}
StitchedTexture* TextureMap::getTexture(const std::string& name) {
StitchedTexture* result = texturesByName.find(name)->second;
if (result == nullptr) result = missingPosition;
return result;
}
void TextureMap::cycleAnimationFrames() {
// for (StitchedTexture texture : animatedTextures)
for (auto it = animatedTextures.begin(); it != animatedTextures.end();
++it) {
StitchedTexture* texture = *it;
texture->cycleFrames();
}
}
Texture* TextureMap::getStitchedTexture() { return stitchResult; }
// 4J Stu - register is a reserved keyword in C++
Icon* TextureMap::registerIcon(const std::string& name) {
if (name.empty()) {
Log::info("Don't register nullptr\n");
#ifndef _CONTENT_PACKAGE
assert(0);
#endif
// new RuntimeException("Don't register null!").printStackTrace();
}
// TODO: [EB]: Why do we allow multiple registrations?
StitchedTexture* result = nullptr;
auto it = texturesToRegister.find(name);
if (it != texturesToRegister.end()) result = it->second;
if (result == nullptr) {
result = StitchedTexture::create(name);
texturesToRegister.insert(
stringStitchedTextureMap::value_type(name, result));
}
return result;
}
int TextureMap::getIconType() { return iconType; }
Icon* TextureMap::getMissingIcon() { return missingPosition; }