-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
140 lines (125 loc) · 4.38 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <string>
#include <filesystem>
#include <algorithm>
#include "AtlasItem.h"
#include "Atlas.h"
#include "Misc.h"
#include "thrid_party/cxxopts.hpp"
#include <fstream>
namespace fs = std::filesystem;
int substringEntryCount( const std::string &str, const std::string &substr )
{
if( str.empty() || substr.empty() || substr.length() > str.length() )
{
return 0;
}
int result = 0;
std::string::size_type pos = 0;
while( ( pos = str.find( substr, pos ) ) != std::string::npos )
{
++result;
pos += substr.length();
}
return result;
}
int main( int argc, char *argv[] )
{
cxxopts::Options options( "polybuild", "Polygon atlas builder" );
int atlasSize = 2048;
std::string outputFilename( "atlas" );
AtlasConfigType configFormat = AtlasConfigType::JSON;
bool trimAtlasSize = false;
bool drawDebugLines = false;
bool ignoreFiles = false;
std::string dirPath = ".";
// clang-format off
options
.set_width(80)
.set_tab_expansion()
.add_options()
("s,max_size", "Atlas max size", cxxopts::value<int>(atlasSize)->default_value("2048"))
("o,output", "Output atlas name", cxxopts::value<std::string>(outputFilename)->default_value("atlas"))
("c,config", "Output config type: json or lua", cxxopts::value<std::string>()->default_value("json"))
("d,dir", "Path to images directory", cxxopts::value<std::string>(dirPath)->default_value("."))
("trim", "Trim the atlas size", cxxopts::value<bool>(trimAtlasSize)->default_value("false"))
("debug", "Draw debug lines on atlas", cxxopts::value<bool>(drawDebugLines)->default_value("false"))
("ignore", "Ignore files from ignore.txt", cxxopts::value<bool>(ignoreFiles)->default_value("false"))
("help", "Print help");
// clang-format on
auto result = options.parse( argc, argv );
if( result.count( "help" ) )
{
std::cout << options.help() << std::endl;
return 0;
}
if( result.count( "config" ) )
{
auto str = result["config"].as<std::string>();
std::transform( str.begin(), str.end(), str.begin(), []( unsigned char c ) { return std::tolower( c ); } );
if( str == "lua" )
{
configFormat = AtlasConfigType::LUA;
}
}
if( result.count( "output" ) )
{
auto str = result["output"].as<std::string>();
std::transform( str.begin(), str.end(), str.begin(), []( unsigned char c ) { return std::tolower( c ); } );
if( !str.empty() )
{
outputFilename = str;
}
}
if( !( std::filesystem::exists( dirPath ) && std::filesystem::is_directory( dirPath ) ) )
{
std::cout << "Directory is not exist\n";
return 0;
}
std::set<std::string> ignoreList;
if( ignoreFiles )
{
std::string line;
std::ifstream file( "ignore.txt" );
while( std::getline( file, line ) )
{
ignoreList.insert( convertToUnixFilepath( line ) );
}
}
std::vector<AtlasItem> items;
items.reserve( 128 );
for( const auto &entry : fs::directory_iterator( dirPath ) )
{
if( substringEntryCount( entry.path().string(), ".png" ) == 1 )
{
if( ignoreFiles && !ignoreList.empty() )
{
const auto &path = convertToUnixFilepath( entry.path().string() );
if( ignoreList.find( path ) != ignoreList.end() )
{
continue;
}
}
items.emplace_back( entry.path().string() );
}
}
if( items.empty() )
{
return 0;
}
std::sort( items.begin(), items.end(), []( const AtlasItem &item1, const AtlasItem &item2 ) {
auto size1 = ( item1.GetImageSize().x * item1.GetImageSize().y ) - int( item1.GetArea() );
auto size2 = ( item2.GetImageSize().x * item2.GetImageSize().y ) - int( item2.GetArea() );
return size1 > size2;
} );
Atlas atlas( atlasSize );
atlas.SetTrimAtlasSize( trimAtlasSize );
atlas.SetAtlasName( outputFilename );
atlas.SetConfigFormat( configFormat );
atlas.SetDrawDebugLines( drawDebugLines );
for( const auto &item : items )
{
atlas.AddItem( item );
}
return 0;
}