Example #1
0
void TexturePacker::PackToTextures(const char * excludeFolder, const char* outputPath, std::list<DefinitionFile*> & defsList)
{
    lastPackedPacker = 0;
    for (std::list<DefinitionFile*>::iterator dfi = defsList.begin(); dfi != defsList.end(); ++dfi)
    {
        DefinitionFile * defFile = *dfi;
        for (int frame = 0; frame < defFile->frameCount; ++frame)
        {
            SizeSortItem sortItem;
            sortItem.imageSize = defFile->frameRects[frame].dx * defFile->frameRects[frame].dy;
            sortItem.defFile = defFile;
            sortItem.frameIndex = frame;
            sortVector.push_back(sortItem);
        }
    }

//	for (int i = 0; i < sortVector.size(); ++i)
//	{
//		DefinitionFile * defFile = sortVector[i].defFile;
//		int frame = sortVector[i].frameIndex;
//		printf("[SinglePack] before sort: %s frame: %d w:%d h:%d\n", defFile->filename.c_str(), frame, defFile->frameRects[frame].dx, defFile->frameRects[frame].dy);
//	}

    std::sort(sortVector.begin(), sortVector.end(), sortFn);

//	for (int i = 0; i < sortVector.size(); ++i)
//	{
//		DefinitionFile * defFile = sortVector[i].defFile;
//		int frame = sortVector[i].frameIndex;
//		printf("[SinglePack] after sort: %s frame: %d w:%d h:%d\n", defFile->filename.c_str(), frame, defFile->frameRects[frame].dx, defFile->frameRects[frame].dy);
//	}

    // try to pack for each resolution
    int bestResolution = (maxTextureSize + 1) * (maxTextureSize + 1);
    int bestXResolution, bestYResolution;

    if (CommandLineParser::Instance()->GetVerbose())
        printf("* Packing tries started: ");

    bool isPvr = false;
    if (CommandLineParser::Instance()->IsFlagSet("--pvr"))
        isPvr = true;

    for (int yResolution = 8; yResolution <= maxTextureSize; yResolution *= 2)
        for (int xResolution = 8; xResolution <= maxTextureSize; xResolution *= 2)
        {
            if ((isPvr) && (xResolution != yResolution))continue;

            if ((onlySquareTextures) && (xResolution != yResolution))continue;

            Rect2i textureRect = Rect2i(0, 0, xResolution, yResolution);

            if (xResolution * yResolution < bestResolution)
                if (TryToPack(textureRect, defsList))
                {
                    bestResolution = xResolution * yResolution;
                    bestXResolution = xResolution;
                    bestYResolution = yResolution;
                }
        }
    if (CommandLineParser::Instance()->GetVerbose())
        printf("\n");
    if (bestResolution != (maxTextureSize + 1) * (maxTextureSize + 1))
    {
        std::string textureName = std::string(outputPath) + std::string("texture");
        if (CommandLineParser::Instance()->GetVerbose())
            printf("* Writing final texture (%d x %d): %s\n", bestXResolution, bestYResolution , textureName.c_str());

        PngImageExt finalImage;
        finalImage.Create(bestXResolution, bestYResolution);

        // Writing
        for (std::list<DefinitionFile*>::iterator dfi = defsList.begin(); dfi != defsList.end(); ++dfi)
        {
            DefinitionFile * defFile = *dfi;
            for (int frame = 0; frame < defFile->frameCount; ++frame)
            {
                Rect2i *destRect = lastPackedPacker->SearchRectForPtr(&defFile->frameRects[frame]);
                if (!destRect)printf("*** ERROR Can't find rect for frame\n");

                char name[256];
                std::string withoutExt = defFile->filename.substr(0, defFile->filename.length() - 4);
                snprintf(name, 256, "%s%d.png", withoutExt.c_str(), frame);

                PngImageExt image;
                image.Read(name);
                finalImage.DrawImage(destRect->x, destRect->y, &image);

                if (CommandLineParser::Instance()->IsFlagSet("--debug"))
                {
                    finalImage.DrawRect(*destRect, 0xFF0000FF);
                }
            }

            if (!WriteDefinition(excludeFolder, outputPath, "texture", defFile))
            {
                printf("* ERROR: failed to write definition\n");
            }
        }
        char textureExtension[5] = "png";
        if (CommandLineParser::Instance()->IsFlagSet("--pvr"))strcpy(textureExtension, "pvr");

        textureName += std::string(".") + textureExtension;
        finalImage.Write(textureName.c_str());
    } else
    {
        //
        PackToMultipleTextures(excludeFolder, outputPath, defsList);
    }
}
Example #2
0
void TexturePacker::PackToTexturesSeparate(const char * excludeFolder, const char* outputPath, std::list<DefinitionFile*> & defsList)
{
    lastPackedPacker = 0;
    int textureIndex = 0;
    for (std::list<DefinitionFile*>::iterator dfi = defsList.begin(); dfi != defsList.end(); ++dfi)
    {
        sortVector.clear();

        DefinitionFile * defFile = *dfi;
        for (int frame = 0; frame < defFile->frameCount; ++frame)
        {
            SizeSortItem sortItem;
            sortItem.imageSize = defFile->frameRects[frame].dx * defFile->frameRects[frame].dy;
            sortItem.defFile = defFile;
            sortItem.frameIndex = frame;
            sortVector.push_back(sortItem);
        }
        std::sort(sortVector.begin(), sortVector.end(), sortFn);


        // try to pack for each resolution
        int bestResolution = (maxTextureSize + 1) * (maxTextureSize + 1);
        int bestXResolution, bestYResolution;

        if (CommandLineParser::Instance()->GetVerbose())
            printf("* Packing tries started: ");

        for (int yResolution = 8; yResolution <= maxTextureSize; yResolution *= 2)
            for (int xResolution = 8; xResolution <= maxTextureSize; xResolution *= 2)
            {
                Rect2i textureRect = Rect2i(0, 0, xResolution, yResolution);

                if (xResolution * yResolution < bestResolution)
                    if (TryToPack(textureRect, defsList))
                    {
                        bestResolution = xResolution * yResolution;
                        bestXResolution = xResolution;
                        bestYResolution = yResolution;
                    }
            }
        if (CommandLineParser::Instance()->GetVerbose())
            printf("\n");
        if (bestResolution != (maxTextureSize + 1) * (maxTextureSize + 1))
        {
            char textureNameWithIndex[50];
            sprintf(textureNameWithIndex, "texture%d", textureIndex++);
            std::string textureName = std::string(outputPath) + std::string(textureNameWithIndex);
            if (CommandLineParser::Instance()->GetVerbose())
                printf("* Writing final texture (%d x %d): %s\n", bestXResolution, bestYResolution , textureName.c_str());

            PngImageExt finalImage;
            finalImage.Create(bestXResolution, bestYResolution);

            // Writing
            for (int frame = 0; frame < defFile->frameCount; ++frame)
            {
                Rect2i *destRect = lastPackedPacker->SearchRectForPtr(&defFile->frameRects[frame]);
                if (!destRect)printf("*** ERROR Can't find rect for frame\n");

                char name[256];
                std::string withoutExt = defFile->filename.substr(0, defFile->filename.length() - 4);
                snprintf(name, 256, "%s%d.png", withoutExt.c_str(), frame);

                PngImageExt image;
                image.Read(name);
                finalImage.DrawImage(destRect->x, destRect->y, &image);
            }

            if (!WriteDefinition(excludeFolder, outputPath, textureNameWithIndex, defFile))
            {
                printf("* ERROR: failed to write definition\n");
            }
            char textureExtension[5] = "png";
            if (CommandLineParser::Instance()->IsFlagSet("--pvr"))strcpy(textureExtension, "pvr");
            textureName += std::string(".") + textureExtension;

            finalImage.Write(textureName.c_str());
        }
    }
}