学习DirectX的2D游戏制作的时候,要切割小图并合并成TileMap。没找到好的切割工具,切割成小图只好用Unity,然后用TexturePacker合并。因为代码中是用索引来获取Json结构数据,因此小图需要重新命名,一个一个修改,太烦也太傻了。写个工具避免重复劳动。
一堆小图:
代码:
#include <iostream>
#include <Windows.h>
#include <ShlObj.h>
#include <vector>
#include <map>
#define SAFE_DELETE_ARRAY(p) { if(p) { delete[] p; p = nullptr; } }
#define FILE_NAME_FORMAT "%04d"
char fileFolder[MAX_PATH] = { 0 };
std::map<int, char*> numberFileMap;
std::vector<char*> unNumbeFileVector;
int fileNum = 0;
bool Initialize();
void RenameFile();
void Release();
bool IsNumber(char* str);
int main()
{
if (Initialize())
{
if (IDYES == MessageBox(NULL, fileFolder, "修改路径", MB_YESNO))
RenameFile();
}
Release();
system("pause");
return 0;
}
bool Initialize()
{
// 获取文件存放目录
BROWSEINFO bi;
memset(&bi, 0, sizeof(bi));
bi.ulFlags = BIF_BROWSEFORCOMPUTER | BIF_EDITBOX | BIF_NEWDIALOGSTYLE;
bi.lpszTitle = "请选择文件存放目录";
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
bool result = pidl != nullptr;
if (!result)
return false;
SHGetPathFromIDList(pidl, fileFolder);
pidl = nullptr;
if ('\0' == fileFolder[0])
{
MessageBox(nullptr, "获取文件存放目录有误", "错误", MB_OK | MB_ICONERROR);
return false;
}
char filePath[MAX_PATH] = { 0 };
strcpy_s(filePath, strlen(fileFolder) + 1, fileFolder);
strcat_s(filePath, strlen(filePath) + strlen("\\*") + 1, "\\*");
// 获取需要改名的文件
WIN32_FIND_DATA findFileData;
HANDLE handle = FindFirstFile(filePath, &findFileData);
if (INVALID_HANDLE_VALUE == handle)
{
MessageBox(nullptr, filePath, "错误", MB_OK | MB_ICONERROR);
return false;
}
numberFileMap.clear();
unNumbeFileVector.clear();
char preName[MAX_PATH] = { 0 };
fileNum = 0;
while (true)
{
if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
std::cout << "DIRECTORY: " << findFileData.cFileName << std::endl;
}
else
{
//std::cout << "file: " << findFileData.cFileName << std::endl;
char* tempFileName = new char[MAX_PATH] {0};
strcpy_s(tempFileName, strlen(findFileData.cFileName) + 1, findFileData.cFileName);
//char* extensionName = strchr(tempFileName, '.');
char extensionName[MAX_PATH] = { 0 };
_splitpath_s(tempFileName, nullptr, 0, nullptr, 0, nullptr, 0, extensionName, MAX_PATH);
strncpy_s(preName, strlen(tempFileName) + 1, tempFileName, strlen(tempFileName) - strlen(extensionName));
// 数字命名的和非数字命名的分开存储
if (IsNumber(preName))
{
int key = atoi(preName);
std::pair<int, char*> tempValue(key, tempFileName);
if (numberFileMap.find(key) != numberFileMap.end())
unNumbeFileVector.push_back(tempFileName);
else
numberFileMap.insert(tempValue);
}
else
{
unNumbeFileVector.push_back(tempFileName);
}
++fileNum;
}
if (!FindNextFile(handle, &findFileData))
break;
}
FindClose(handle);
std::map<int, char*>::iterator iter = numberFileMap.begin();
int key = -1;
while (iter != numberFileMap.end())
{
key = iter->first;
if (key < 0 || key > (fileNum - 1))
{
unNumbeFileVector.push_back(iter->second);
numberFileMap.erase(iter++);
}
else
++iter;
}
return true;
}
void RenameFile()
{
char preName[MAX_PATH] = { 0 };
char indexStrName[MAX_PATH] = { 0 };
char oldFileName[MAX_PATH] = { 0 };
char newFileName[MAX_PATH] = { 0 };
std::map<int, char*>::iterator mapIterator;
std::vector<char*>::iterator vectorIterator;
for (int index = 0; index < fileNum; ++index)
{
mapIterator = numberFileMap.find(index);
if (numberFileMap.end() == mapIterator)
{
vectorIterator = unNumbeFileVector.begin();
if (vectorIterator != unNumbeFileVector.end() && *vectorIterator != nullptr)
{
// Old file name
strcpy_s(oldFileName, strlen(fileFolder) + 1, fileFolder);
strcat_s(oldFileName, strlen(oldFileName) + strlen("\\") + 1, "\\");
strcat_s(oldFileName, strlen(oldFileName) + strlen(*vectorIterator) + 1, *vectorIterator);
// New file name
strcpy_s(newFileName, strlen(fileFolder) + 1, fileFolder);
strcat_s(newFileName, strlen(newFileName) + strlen("\\") + 1, "\\");
sprintf_s(indexStrName, FILE_NAME_FORMAT, index);
strcat_s(newFileName, strlen(newFileName) + strlen(indexStrName) + 1, indexStrName);
//char* extensionName = strchr(unNumbeFileVector[0], '.');
char extensionName[MAX_PATH] = { 0 };
_splitpath_s(unNumbeFileVector[0], nullptr, 0, nullptr, 0, nullptr, 0, extensionName, MAX_PATH);
strcat_s(newFileName, strlen(newFileName) + strlen(extensionName) + 1, extensionName);
std::cout << "oldFileNmae: " << oldFileName << std::endl;
std::cout << "newFileName: " << newFileName << std::endl;
// Rename
rename(oldFileName, newFileName);
SAFE_DELETE_ARRAY(*vectorIterator);
unNumbeFileVector.erase(vectorIterator);
}
}
else//判断是否按照0000格式存储
{
//char* extensionName = strchr(mapIterator->second, '.');
char extensionName[MAX_PATH] = { 0 };
_splitpath_s(mapIterator->second, nullptr, 0, nullptr, 0, nullptr, 0, extensionName, MAX_PATH);
strncpy_s(preName, strlen(mapIterator->second) + 1, mapIterator->second, strlen(mapIterator->second) - strlen(extensionName));
int length = strlen(preName) - 1;
if (length < 4)
{
// Old file name
strcpy_s(oldFileName, strlen(fileFolder) + 1, fileFolder);
strcat_s(oldFileName, strlen(oldFileName) + strlen("\\") + 1, "\\");
strcat_s(oldFileName, strlen(oldFileName) + strlen(mapIterator->second) + 1, mapIterator->second);
// New file name
strcpy_s(newFileName, strlen(fileFolder) + 1, fileFolder);
strcat_s(newFileName, strlen(newFileName) + strlen("\\") + 1, "\\");
sprintf_s(indexStrName, FILE_NAME_FORMAT, index);
strcat_s(newFileName, strlen(newFileName) + strlen(indexStrName) + 1, indexStrName);
strcat_s(newFileName, strlen(newFileName) + strlen(extensionName) + 1, extensionName);
std::cout << "oldFileNmae: " << oldFileName << std::endl;
std::cout << "newFileName: " << newFileName << std::endl;
// Rename
rename(oldFileName, newFileName);
}
}
}
}
void Release()
{
std::map<int, char*>::iterator iter = numberFileMap.begin();
while (iter != numberFileMap.end())
{
SAFE_DELETE_ARRAY(iter->second);
numberFileMap.erase(iter++);
}
}
bool IsNumber(char* str)
{
int strlength = strlen(str);
for (int i = 0; i < strlength; ++i)
{
if (str[i] < '0' || str[i] > '9')
{
if (0 == i && '-' == str[i])
{
continue;
}
return false;
}
}
return true;
}
好忧伤,已经改了3版了,为了能在TexturePackerGUI在用起来更方便。