コード例 #1
0
ファイル: texturepacker.cpp プロジェクト: gideros/gideros
	virtual bool packInto(int pwidth, int pheight, int border)
	{
		if (border)
		for (size_t i = 0; i < rects_.size(); ++i)
		{
			rects_[i].width += border * 2;
			rects_[i].height += border * 2;
		}

		packHelper2(pwidth, pheight);

		if (result_.size() != rects_.size()) return false;

		if (border)
		for (size_t i = 0; i < result_.size(); ++i)
		{
			result_[i].width -= 2 * border;
			result_[i].height -= 2 * border;
			result_[i].x += border;
			result_[i].y += border;
		}
		return true;
	}
コード例 #2
0
ファイル: texturepacker.cpp プロジェクト: JFonS/gideros
	virtual void packTextures(int* pwidth, int* pheight, int border, bool forceSquare)
	{
		for (size_t i = 0; i < rects_.size(); ++i)
		{
			rects_[i].width += border * 2;
			rects_[i].height += border * 2;
		}

		int maxWidth = 0;
		int maxHeight = 0;
		int totalArea = 0;
		for (size_t i = 0; i < rects_.size(); ++i)
		{
			int width = rects_[i].width;
			int height = rects_[i].height;

			maxWidth = std::max(maxWidth, width);
			maxHeight = std::max(maxHeight, height);
			totalArea += width * height;
		}

		int max = 11;		// 2^11 = 2048
		for (int i = 0; i < max * 2 + 1; ++i)
		{
			int jmax = max - abs(max - i) + 1;
			int jstart = (i < max) ? 0 : (i - max);
			for (int j = 0; j < jmax; ++j)
			{
				int x = (jmax / 2) + offset(j) + jstart;		// j + jstart
				int y = i - x;

				if (forceSquare && (x != y))
					continue;

				int width = 1 << x;
				int height = 1 << y;
				int area = width * height;

				if (width < maxWidth || height < maxHeight || area < totalArea)
					continue;

				packHelper2(width, height);

				if (result_.size() == rects_.size())
				{
					*pwidth = width;
					*pheight = height;

					for (size_t i = 0; i < result_.size(); ++i)
					{
						result_[i].width -= 2 * border;
						result_[i].height -= 2 * border;
						result_[i].x += border;
						result_[i].y += border;
					}

					return;
				}
			}
		}
	}