4jcraft/Minecraft.Client/Textures/Stitching/StitchSlot.cpp
MatthewBeshay a0fdc643d1 Merge branch 'upstream-dev' into cleanup/nullptr-replacement
# Conflicts:
#	Minecraft.Client/Network/PlayerChunkMap.cpp
#	Minecraft.Client/Network/PlayerList.cpp
#	Minecraft.Client/Network/ServerChunkCache.cpp
#	Minecraft.Client/Platform/Common/Consoles_App.cpp
#	Minecraft.Client/Platform/Common/DLC/DLCManager.cpp
#	Minecraft.Client/Platform/Common/GameRules/LevelGenerationOptions.cpp
#	Minecraft.Client/Platform/Common/GameRules/LevelRuleset.cpp
#	Minecraft.Client/Platform/Common/Tutorial/Tutorial.cpp
#	Minecraft.Client/Platform/Common/Tutorial/TutorialTask.cpp
#	Minecraft.Client/Platform/Common/UI/IUIScene_CreativeMenu.cpp
#	Minecraft.Client/Platform/Common/UI/UIComponent_Panorama.cpp
#	Minecraft.Client/Platform/Common/UI/UIController.cpp
#	Minecraft.Client/Platform/Common/UI/UIController.h
#	Minecraft.Client/Platform/Extrax64Stubs.cpp
#	Minecraft.Client/Platform/Windows64/4JLibs/inc/4J_Input.h
#	Minecraft.Client/Platform/Windows64/4JLibs/inc/4J_Storage.h
#	Minecraft.Client/Player/EntityTracker.cpp
#	Minecraft.Client/Player/ServerPlayer.cpp
#	Minecraft.Client/Rendering/EntityRenderers/PlayerRenderer.cpp
#	Minecraft.Client/Textures/Packs/DLCTexturePack.cpp
#	Minecraft.Client/Textures/Stitching/StitchedTexture.cpp
#	Minecraft.Client/Textures/Stitching/TextureMap.cpp
#	Minecraft.Client/Textures/Textures.cpp
#	Minecraft.World/Blocks/NotGateTile.cpp
#	Minecraft.World/Blocks/PressurePlateTile.cpp
#	Minecraft.World/Blocks/TileEntities/PotionBrewing.cpp
#	Minecraft.World/Enchantments/EnchantmentHelper.cpp
#	Minecraft.World/Entities/HangingEntity.cpp
#	Minecraft.World/Entities/LeashFenceKnotEntity.cpp
#	Minecraft.World/Entities/LivingEntity.cpp
#	Minecraft.World/Entities/Mobs/Boat.cpp
#	Minecraft.World/Entities/Mobs/Minecart.cpp
#	Minecraft.World/Entities/Mobs/Witch.cpp
#	Minecraft.World/Entities/SyncedEntityData.cpp
#	Minecraft.World/Items/LeashItem.cpp
#	Minecraft.World/Items/PotionItem.cpp
#	Minecraft.World/Level/BaseMobSpawner.cpp
#	Minecraft.World/Level/CustomLevelSource.cpp
#	Minecraft.World/Level/Level.cpp
#	Minecraft.World/Level/Storage/DirectoryLevelStorage.cpp
#	Minecraft.World/Level/Storage/McRegionLevelStorage.cpp
#	Minecraft.World/Level/Storage/RegionFileCache.cpp
#	Minecraft.World/Player/Player.cpp
#	Minecraft.World/WorldGen/Biomes/BiomeCache.cpp
#	Minecraft.World/WorldGen/Features/RandomScatteredLargeFeature.cpp
#	Minecraft.World/WorldGen/Layers/BiomeOverrideLayer.cpp
2026-03-30 16:28:40 +11:00

140 lines
5 KiB
C++

#include "../../Platform/stdafx.h"
#include "../TextureHolder.h"
#include "../../../Minecraft.World/Util/StringHelpers.h"
#include "StitchSlot.h"
StitchSlot::StitchSlot(int originX, int originY, int width, int height)
: originX(originX), originY(originY), width(width), height(height) {
subSlots = nullptr;
textureHolder = nullptr;
}
TextureHolder* StitchSlot::getHolder() { return textureHolder; }
int StitchSlot::getX() { return originX; }
int StitchSlot::getY() { return originY; }
bool StitchSlot::add(TextureHolder* textureHolder) {
// Already holding a texture -- doesn't account for subslots.
if (this->textureHolder != nullptr) {
return false;
}
int textureWidth = textureHolder->getWidth();
int textureHeight = textureHolder->getHeight();
// We're too small to fit the texture
if (textureWidth > width || textureHeight > height) {
return false;
}
// Exact fit! best-case-solution
if (textureWidth == width && textureHeight == height && subSlots == nullptr) {
// Store somehow
this->textureHolder = textureHolder;
return true;
}
// See if we're already divided before, if not, setup subSlots
if (subSlots == nullptr) {
subSlots = new std::vector<StitchSlot*>();
// First slot is for the new texture
subSlots->push_back(
new StitchSlot(originX, originY, textureWidth, textureHeight));
int spareWidth = width - textureWidth;
int spareHeight = height - textureHeight;
if (spareHeight > 0 && spareWidth > 0) {
// Space below AND right
//
// <-right->
// +-----+-------+
// | | |
// | Tex | |
// | | |
// |-----+ | ^
// | | |- bottom
// +-------------+ v
// We need to add two more areas, the one with the 'biggest'
// dimensions should be used (In the case of this ASCII drawing,
// it's the 'right hand side' that should win)
// The 'fattest' area should be used (or when tied, the right hand
// one)
int right = std::max(height, spareWidth);
int bottom = std::max(width, spareHeight);
if (right >= bottom) {
subSlots->push_back(new StitchSlot(originX,
originY + textureHeight,
textureWidth, spareHeight));
subSlots->push_back(new StitchSlot(
originX + textureWidth, originY, spareWidth, height));
} else {
subSlots->push_back(new StitchSlot(originX + textureWidth,
originY, spareWidth,
textureHeight));
subSlots->push_back(new StitchSlot(
originX, originY + textureHeight, width, spareHeight));
}
} else if (spareWidth == 0) {
// We just have space left below
//
// +-------------+
// | |
// | Tex |
// | |
// |-------------+ ^
// | | |- bottom
// +-------------+ v
subSlots->push_back(new StitchSlot(originX, originY + textureHeight,
textureWidth, spareHeight));
} else if (spareHeight == 0) {
// Only space to the right
//
// <-right->
// +-----+-------+
// | | |
// | Tex | |
// | | |
// | | |
// | | |
// +-----+-------+
subSlots->push_back(new StitchSlot(originX + textureWidth, originY,
spareWidth, textureHeight));
}
}
// for (final StitchSlot subSlot : subSlots)
for (auto it = subSlots->begin(); it != subSlots->end(); ++it) {
StitchSlot* subSlot = *it;
if (subSlot->add(textureHolder)) {
return true;
}
}
return false;
}
void StitchSlot::collectAssignments(std::vector<StitchSlot*>* result) {
if (textureHolder != nullptr) {
result->push_back(this);
} else if (subSlots != nullptr) {
// for (StitchSlot subSlot : subSlots)
for (auto it = subSlots->begin(); it != subSlots->end(); ++it) {
StitchSlot* subSlot = *it;
subSlot->collectAssignments(result);
}
}
}
//@Override
std::wstring StitchSlot::toString() {
return L"Slot{originX=" + _toString(originX) + L", originY=" +
_toString(originY) + L", width=" + _toString(width) + L", height=" +
_toString(height) + L", texture=" + _toString(textureHolder) +
L", subSlots=" + _toString(subSlots) + L'}';
}