void TextureToolWindow::updateControls(String texName) { try { bool exists = TextureManager::getSingleton().resourceExists(texName); if (!exists) { mTxt->setCaption(convertToMyGUIString("Texture not found:\n" + texName)); mBtnSavePNG->setEnabled(false); return; } TexturePtr tex = TextureManager::getSingleton().getByName(texName); if (tex.isNull()) { mTxt->setCaption(convertToMyGUIString("Error loading texture:\n" + texName)); mBtnSavePNG->setEnabled(false); return; } String str = "#aa0000" + texName + "#000000\n"; str += "#00aa00res: #000000" + TOSTRING(tex->getWidth()) + " x " + TOSTRING(tex->getHeight()) + " pixels\n"; str += "#00aa00size: #000000" + formatBytes(tex->getSize()) + "\n"; str += "#00aa00format: #000000" + PixelUtil::getFormatName(tex->getFormat()) + "\n"; if (tex->getNumFaces() > 1) str += "#00aa00faces: #000000" + TOSTRING(tex->getNumFaces()) + "\n"; if (tex->getFSAA() > 0) str += "#00aa00FSAA: #000000" + TOSTRING(tex->getFSAA()) + "\n"; if (tex->getNumMipmaps() > 0) str += "#00aa00mipmaps: #000000" + TOSTRING(tex->getNumMipmaps()) + "\n"; String typeStr = ""; switch (tex->getTextureType()) { case TEX_TYPE_1D: typeStr = "1D"; break; case TEX_TYPE_2D: typeStr = "2D"; break; case TEX_TYPE_3D: typeStr = "3D"; break; case TEX_TYPE_CUBE_MAP: typeStr = "Cube Map"; break; } str += "#00aa00type: #000000" + typeStr + "\n"; String usageStr = ""; if (tex->getUsage() & TU_STATIC) usageStr += "static,\n"; if (tex->getUsage() & TU_DYNAMIC) usageStr += "dynamic,\n"; if (tex->getUsage() & TU_WRITE_ONLY) usageStr += "write only,\n"; if (tex->getUsage() & TU_STATIC_WRITE_ONLY) usageStr += "static write only,\n"; if (tex->getUsage() & TU_DYNAMIC_WRITE_ONLY) usageStr += "dynamic write only,\n"; if (tex->getUsage() & TU_DYNAMIC_WRITE_ONLY_DISCARDABLE) usageStr += "dynamic write only discardable,\n"; if (tex->getUsage() & TU_AUTOMIPMAP) usageStr += "automipmap,\n"; if (tex->getUsage() & TU_RENDERTARGET) usageStr += "rendertarget,\n"; if (tex->getUsage() & TU_DEFAULT) usageStr += "default\n"; str += "#00aa00usage: #000000" + usageStr + "\n"; if (tex->getDepth() > 1) str += "#00aa00depth: #000000" + TOSTRING(tex->getDepth()) + "\n"; mTxt->setCaption(convertToMyGUIString(str)); mImage->setImageTexture(texName); mBtnSavePNG->setEnabled(true); } catch (Exception& e) { UTFString str = "Exception while opening texture:" + e.getFullDescription(); RoR::App::GetConsole()->putMessage(Console::CONSOLE_MSGTYPE_INFO, Console::CONSOLE_MSGTYPE_INFO, str, "error.png"); } }
BorderImagePtr BorderImage::loadFromOTML(const OTMLNodePtr& borderImageNode) { Rect leftBorder; Rect rightBorder; Rect topBorder; Rect bottomBorder; Rect topLeftCorner; Rect topRightCorner; Rect bottomLeftCorner; Rect bottomRightCorner; Rect center; Rect subRect; int top, bottom, left, right, border; Size size; Point offset; // load texture std::string source = borderImageNode->at("source")->value(); TexturePtr texture = g_textures.getTexture(source); // load basic border confs size = texture->getSize(); size = borderImageNode->valueAt("size", size); offset = borderImageNode->valueAt("offset", offset); border = borderImageNode->valueAt("border", 0); subRect = Rect(offset, size); // load border margins top = bottom = left = right = border; top = borderImageNode->valueAt("border.top", top); bottom = borderImageNode->valueAt("border.bottom", bottom); left = borderImageNode->valueAt("border.left", left); right = borderImageNode->valueAt("border.right", right); // calculates border coords leftBorder = Rect(subRect.left(), subRect.top() + top, left, subRect.height() - top - bottom); rightBorder = Rect(subRect.right() - right + 1, subRect.top() + top, right, subRect.height() - top - bottom); topBorder = Rect(subRect.left() + left, subRect.top(), subRect.width() - right - left, top); bottomBorder = Rect(subRect.left() + left, subRect.bottom() - bottom + 1, subRect.width() - right - left, bottom); topLeftCorner = Rect(subRect.left(), subRect.top(), left, top); topRightCorner = Rect(subRect.right() - right + 1, subRect.top(), right, top); bottomLeftCorner = Rect(subRect.left(), subRect.bottom() - bottom + 1, left, bottom); bottomRightCorner = Rect(subRect.right() - right + 1, subRect.bottom() - bottom + 1, right, bottom); center = Rect(subRect.left() + left, subRect.top() + top, subRect.width() - right - left, subRect.height() - top - bottom); // load individual border conf if supplied /* leftBorder = borderImageNode->valueAt("left border", leftBorder); rightBorder = borderImageNode->valueAt("right border", rightBorder); topBorder = borderImageNode->valueAt("top border", topBorder); bottomBorder = borderImageNode->valueAt("bottom border", bottomBorder); topLeftCorner = borderImageNode->valueAt("top left corner", topLeftCorner); topRightCorner = borderImageNode->valueAt("top right corner", topRightCorner); bottomLeftCorner = borderImageNode->valueAt("bottom left corner", bottomLeftCorner); bottomRightCorner = borderImageNode->valueAt("bottom right corner", bottomRightCorner); center = borderImageNode->valueAt("center", center); */ return BorderImagePtr(new BorderImage(texture, leftBorder, rightBorder, topBorder, bottomBorder, topLeftCorner, topRightCorner, bottomLeftCorner, bottomRightCorner, center)); }
bool LayerComponent::init(const std::string& filename, const Vector2f& offset) { FileInputStream mapStream; if (!mapStream.open(filename)) { err() << "loadMap: could not open file " << filename << endl; return false; } // convert FileInputStream to char* mapBuffer char* mapBuffer = new char[mapStream.getSize() + 1]; mapStream.read(mapBuffer, mapStream.getSize()); mapBuffer[mapStream.getSize()] = '\0'; // now lets load a NLTmxMap NLTmxMap* tilemap = NLLoadTmxMap(mapBuffer); delete mapBuffer; err() << "Load tilemap with size: " << tilemap->width << ", " << tilemap->height << " and tilesize: " << tilemap->tileWidth << ", " << tilemap->tileHeight << std::endl; // load textures for every tileset for (auto tileset : tilemap->tilesets) { err() << "Load tileset: " << tileset->name << " width filename: " << tileset->filename << " and tilesize: " << tileset->tileWidth << ", " << tileset->tileHeight << std::endl; auto texture = make_shared<Texture>(); if (!texture->loadFromFile(m_resourcePath + tileset->filename)) err() << "Could not load texture " << m_resourcePath + tileset->filename << endl; m_tilesetTexture[tileset->name] = texture; } // go through all layers m_layers.resize(tilemap->layers.size()); for (int layerIdx = 0; layerIdx < (int)tilemap->layers.size(); layerIdx++) { NLTmxMapLayer* layer = tilemap->layers[layerIdx]; err() << "Load layer: " << layer->name << " with width: " << layer->width << " and height: " << layer->height << std::endl; int size = layer->width * layer->height; // go over all elements in the layer for (int i = 0; i < size; i++) { int grid = layer->data[i]; if (grid == 0) continue; // get tileset and tileset texture NLTmxMapTileset* tileset = tilemap->getTilesetForGrid(grid); Vector2i tileSize(tilemap->tileWidth, tilemap->tileHeight); TexturePtr texture = m_tilesetTexture[tileset->name]; assert(texture != nullptr); // horizontal tile count in tileset texture int tileCountX = texture->getSize().x / tileSize.x; // calcualte position of tile Vector2f position; position.x = (i % layer->width) * (float)tileSize.x; position.y = (i / layer->width) * (float)tileSize.y; position += offset; // calculate 2d idx of tile in tileset texture int idx = grid - tileset->firstGid; int idxX = idx % tileCountX; int idxY = idx / tileCountX; // calculate source area of tile in tileset texture IntRect source(idxX * tileSize.x, idxY * tileSize.y, tileSize.x, tileSize.y); // TODO create tile and put it into a layer auto sprite = make_shared<Sprite>(); sprite->setTexture(*texture); sprite->setTextureRect(source); sprite->setPosition(position.x, position.y); m_layers[layerIdx].push_back(sprite); } } // go through all object layers for (auto group : tilemap->groups) { // go over all objects per layer for (auto object : group->objects) { Vector2f position(object->x, object->y); position += offset; FloatRect bounds(position.x, position.y, object->width, object->height); // TODO create object if (object->type == "Sprite") { auto sprite = loadSprite(object); m_objects[object->name] = sprite; } } } return true; }