void DefinitionFile::LoadPNG(const String & _filename, const String & pathToProcess) { String path; String name; CommandLineParser::SplitFilePath(_filename, path, name); String nameWithoutExt = name.substr(0, name.length() - 4); String corespondingPngImage = path + String("/") + nameWithoutExt + String(".png"); filename = pathToProcess + String("/") + nameWithoutExt + String(".txt"); frameCount = 1; PngImageExt image; image.Read(corespondingPngImage.c_str()); spriteWidth = image.GetWidth(); spriteHeight = image.GetHeight(); frameRects = new Rect2i[1]; frameRects[0].x = 0; frameRects[0].y = 0; frameRects[0].dx = spriteWidth; frameRects[0].dy = spriteHeight; String fileWrite = pathToProcess + String("/") + nameWithoutExt + "0" + String(".png"); FileSystem::Instance()->CopyFile(_filename, fileWrite); }
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; }