the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 156 lines 4.0 kB view raw
1#include "stdafx.h" 2#include "TextureHolder.h" 3#include "..\Minecraft.World\StringHelpers.h" 4#include "StitchSlot.h" 5 6StitchSlot::StitchSlot(int originX, int originY, int width, int height) : originX(originX), originY(originY), width(width), height(height) 7{ 8 subSlots = NULL; 9 textureHolder = NULL; 10} 11 12TextureHolder *StitchSlot::getHolder() 13{ 14 return textureHolder; 15} 16 17int StitchSlot::getX() 18{ 19 return originX; 20} 21 22int StitchSlot::getY() 23{ 24 return originY; 25} 26 27bool StitchSlot::add(TextureHolder *textureHolder) 28{ 29 // Already holding a texture -- doesn't account for subslots. 30 if (this->textureHolder != NULL) 31 { 32 return false; 33 } 34 35 int textureWidth = textureHolder->getWidth(); 36 int textureHeight = textureHolder->getHeight(); 37 38 // We're too small to fit the texture 39 if (textureWidth > width || textureHeight > height) 40 { 41 return false; 42 } 43 44 // Exact fit! best-case-solution 45 if (textureWidth == width && textureHeight == height && subSlots == NULL) 46 { 47 // Store somehow 48 this->textureHolder = textureHolder; 49 return true; 50 } 51 52 // See if we're already divided before, if not, setup subSlots 53 if (subSlots == NULL) 54 { 55 subSlots = new vector<StitchSlot *>(); 56 57 // First slot is for the new texture 58 subSlots->push_back(new StitchSlot(originX, originY, textureWidth, textureHeight)); 59 60 int spareWidth = width - textureWidth; 61 int spareHeight = height - textureHeight; 62 63 if (spareHeight > 0 && spareWidth > 0) 64 { 65 // Space below AND right 66 // 67 // <-right-> 68 // +-----+-------+ 69 // | | | 70 // | Tex | | 71 // | | | 72 // |-----+ | ^ 73 // | | |- bottom 74 // +-------------+ v 75 // We need to add two more areas, the one with the 'biggest' dimensions should be used 76 // (In the case of this ASCII drawing, it's the 'right hand side' that should win) 77 78 // The 'fattest' area should be used (or when tied, the right hand one) 79 int right = max(height, spareWidth); 80 int bottom = max(width, spareHeight); 81 if (right >= bottom) 82 { 83 subSlots->push_back(new StitchSlot(originX, originY + textureHeight, textureWidth, spareHeight)); 84 subSlots->push_back(new StitchSlot(originX + textureWidth, originY, spareWidth, height)); 85 } 86 else 87 { 88 subSlots->push_back(new StitchSlot(originX + textureWidth, originY, spareWidth, textureHeight)); 89 subSlots->push_back(new StitchSlot(originX, originY + textureHeight, width, spareHeight)); 90 } 91 92 } 93 else if (spareWidth == 0) 94 { 95 // We just have space left below 96 // 97 // +-------------+ 98 // | | 99 // | Tex | 100 // | | 101 // |-------------+ ^ 102 // | | |- bottom 103 // +-------------+ v 104 subSlots->push_back(new StitchSlot(originX, originY + textureHeight, textureWidth, spareHeight)); 105 } 106 else if (spareHeight == 0) 107 { 108 // Only space to the right 109 // 110 // <-right-> 111 // +-----+-------+ 112 // | | | 113 // | Tex | | 114 // | | | 115 // | | | 116 // | | | 117 // +-----+-------+ 118 subSlots->push_back(new StitchSlot(originX + textureWidth, originY, spareWidth, textureHeight)); 119 } 120 } 121 122 //for (final StitchSlot subSlot : subSlots) 123 for(AUTO_VAR(it, subSlots->begin()); it != subSlots->end(); ++it) 124 { 125 StitchSlot *subSlot = *it; 126 if (subSlot->add(textureHolder)) 127 { 128 return true; 129 } 130 } 131 132 return false; 133} 134 135void StitchSlot::collectAssignments(vector<StitchSlot *> *result) 136{ 137 if (textureHolder != NULL) 138 { 139 result->push_back(this); 140 } 141 else if (subSlots != NULL) 142 { 143 //for (StitchSlot subSlot : subSlots) 144 for(AUTO_VAR(it, subSlots->begin()); it != subSlots->end(); ++it) 145 { 146 StitchSlot *subSlot = *it; 147 subSlot->collectAssignments(result); 148 } 149 } 150} 151 152//@Override 153wstring StitchSlot::toString() 154{ 155 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'}'; 156}