Ejemplo n.º 1
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());
        }
    }
}
Ejemplo n.º 2
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);
    }
}
Ejemplo n.º 3
0
bool DefinitionFile::LoadPNGDef(const std::string & _filename, const std::string & pathToProcess)
{
	if (CommandLineParser::Instance()->GetVerbose())printf("* Load PNG Definition: %s\n", _filename.c_str()); 
	
	FILE * fp = fopen(_filename.c_str(), "rt");
	fscanf(fp, "%d", &frameCount);

	String path;
	String name;
	CommandLineParser::SplitFilePath(_filename, path, name);
	String nameWithoutExt = name.substr(0, name.length() - 7);
	String corespondingPngImage = path + String("/") + nameWithoutExt + String(".png");

	filename = pathToProcess + String("/") + nameWithoutExt + String(".txt");
	
	PngImageExt image;
	image.Read(corespondingPngImage.c_str());
	spriteWidth = image.GetWidth() / frameCount;
	spriteHeight = image.GetHeight();
	
//	String dirWrite = path + String("/$process/"); 
//	mkdir(dirWrite.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
	if (CommandLineParser::Instance()->GetVerbose())printf("* frameCount: %d spriteWidth: %d spriteHeight: %d\n", frameCount, spriteWidth, spriteHeight); 


	frameRects = new Rect2i[frameCount];
	for (int k = 0; k < frameCount; ++k)
	{
		PngImageExt frameX;
		frameX.Create(spriteWidth, spriteHeight);
		frameX.DrawImage(0, 0, &image, Rect2i(k * spriteWidth, 0, spriteWidth, spriteHeight));
		
		
		Rect2i reducedRect;
		frameX.FindNonOpaqueRect(reducedRect);
		if (CommandLineParser::Instance()->GetVerbose())printf("%s - reduced_rect(%d %d %d %d)\n", nameWithoutExt.c_str(), reducedRect.x, reducedRect.y, reducedRect.dx, reducedRect.dy);
		
		/*if (k == 1)
		{
			for (int y = 0; y < spriteWidth; ++y)
			{
				for (int x = 0; x < spriteWidth; ++x)
				{
					printf("%02x ", frameX.GetPixel(x, y)[3]);
				}
				printf("\n");
			}
		}*/
		
		PngImageExt frameX2;
		frameX2.Create(reducedRect.dx, reducedRect.dy);
		frameX2.DrawImage(0, 0, &frameX, reducedRect);
		
		char number[10];
		sprintf(number, "%d", k);
		String fileWrite = pathToProcess + String("/") + nameWithoutExt + String(number) + String(".png"); 
		frameX2.Write(fileWrite.c_str());		
	
		frameRects[k].x = reducedRect.x;
		frameRects[k].y = reducedRect.y;
		frameRects[k].dx = reducedRect.dx;
		frameRects[k].dy = reducedRect.dy;
	
	
		if (CommandLineParser::Instance()->IsFlagSet("--add0pixel"))
		{
			
		}else if (CommandLineParser::Instance()->IsFlagSet("--add1pixel"))
		{
			frameRects[k].dx++;
			frameRects[k].dy++;
		}
		else if (CommandLineParser::Instance()->IsFlagSet("--add2pixel"))
		{
			frameRects[k].dx+=2;
			frameRects[k].dy+=2;
		}
		else if (CommandLineParser::Instance()->IsFlagSet("--add4pixel"))
		{
			frameRects[k].dx+=4;
			frameRects[k].dy+=4;
		}else 
		{
			frameRects[k].dx++;
			frameRects[k].dy++;	
		}
	}
	

	fclose(fp);
	return true;
}