/
utility_object_compiler.cpp
346 lines (280 loc) · 11 KB
/
utility_object_compiler.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "graphics.hpp"
#include <boost/shared_ptr.hpp>
#include <map>
#include <vector>
#include <sstream>
#include "asserts.hpp"
#include "custom_object_type.hpp"
#include "filesystem.hpp"
#include "foreach.hpp"
#include "formatter.hpp"
#include "frame.hpp"
#include "geometry.hpp"
#include "json_parser.hpp"
#include "module.hpp"
#include "string_utils.hpp"
#include "surface.hpp"
#include "surface_cache.hpp"
#include "unit_test.hpp"
#include "variant_utils.hpp"
#include "IMG_savepng.h"
namespace {
const int TextureImageSize = 1024;
struct animation_area {
explicit animation_area(variant node) : anim(new frame(node)), is_particle(false)
{
width = 0;
height = 0;
foreach(const frame::frame_info& f, anim->frame_layout()) {
width += f.area.w();
if(f.area.h() > height) {
height = f.area.h();
}
}
src_image = node["image"].as_string();
dst_image = -1;
}
boost::shared_ptr<frame> anim;
int width, height;
std::string src_image;
int dst_image;
rect dst_area;
bool is_particle;
};
typedef boost::shared_ptr<animation_area> animation_area_ptr;
bool operator==(const animation_area& a, const animation_area& b)
{
return a.src_image == b.src_image && a.anim->area() == b.anim->area() && a.anim->pad() == b.anim->pad() && a.anim->num_frames() == b.anim->num_frames() && a.anim->num_frames_per_row() == b.anim->num_frames_per_row();
}
bool animation_area_height_compare(animation_area_ptr a, animation_area_ptr b)
{
if(a->is_particle != b->is_particle) {
return a->is_particle;
}
return a->height > b->height;
}
struct output_area {
explicit output_area(int n) : image_id(n)
{
area = rect(0, 0, TextureImageSize, TextureImageSize);
}
int image_id;
rect area;
};
rect use_output_area(const output_area& input, int width, int height, std::vector<output_area>& areas)
{
ASSERT_LE(width, input.area.w());
ASSERT_LE(height, input.area.h());
rect result(input.area.x(), input.area.y(), width, height);
if(input.area.h() > height) {
areas.push_back(output_area(input.image_id));
areas.back().area = rect(input.area.x(), input.area.y() + height, input.area.w(), input.area.h() - height);
}
if(input.area.w() > width) {
areas.push_back(output_area(input.image_id));
areas.back().area = rect(input.area.x() + width, input.area.y(), input.area.w() - width, height);
}
return result;
}
}
namespace graphics {
void set_alpha_for_transparent_colors_in_rgba_surface(SDL_Surface* s, int options=0);
}
UTILITY(compile_objects)
{
using graphics::surface;
int num_output_images = 0;
std::vector<output_area> output_areas;
output_areas.push_back(output_area(num_output_images++));
std::map<variant, std::string> nodes_to_files;
std::vector<variant> objects;
std::vector<animation_area_ptr> animation_areas;
std::map<variant, animation_area_ptr> nodes_to_animation_areas;
std::vector<variant> animation_containing_nodes;
std::vector<std::string> no_compile_images;
variant gui_node = json::parse_from_file(module::map_file("data/gui.cfg"));
animation_containing_nodes.push_back(gui_node);
std::map<std::string, variant> gui_nodes;
std::vector<std::string> gui_files;
module::get_files_in_dir("data/gui", &gui_files);
foreach(const std::string& gui, gui_files) {
if(gui[0] == '.') {
continue;
}
gui_nodes[gui] = json::parse_from_file(module::map_file("data/gui/" + gui));
animation_containing_nodes.push_back(gui_nodes[gui]);
if(gui_nodes[gui].has_key("no_compile_image")) {
std::vector<std::string> images = util::split(gui_nodes[gui][variant("no_compile_image")].as_string());
no_compile_images.insert(no_compile_images.end(), images.begin(), images.end());
}
}
std::vector<const_custom_object_type_ptr> types = custom_object_type::get_all();
foreach(const_custom_object_type_ptr type, types) {
const std::string* path = custom_object_type::get_object_path(type->id() + ".cfg");
//skip any experimental stuff so it isn't compiled
const std::string Experimental = "experimental";
if(std::search(path->begin(), path->end(), Experimental.begin(), Experimental.end()) != path->end()) {
continue;
}
std::cerr << "OBJECT: " << type->id() << " -> " << *path << "\n";
variant obj_node = json::parse_from_file(*path);
obj_node = custom_object_type::merge_prototype(obj_node);
obj_node.remove_attr(variant("prototype"));
objects.push_back(obj_node);
nodes_to_files[obj_node] = "data/compiled/objects/" + type->id() + ".cfg";
if(obj_node.has_key("no_compile_image")) {
std::vector<std::string> images = util::split(obj_node["no_compile_image"].as_string());
no_compile_images.insert(no_compile_images.end(), images.begin(), images.end());
}
animation_containing_nodes.push_back(obj_node);
foreach(variant v, obj_node["particle_system"].as_list()) {
animation_containing_nodes.push_back(v);
}
//add nested objects -- disabled for now until we find bugs in it.
/*
for(wml::node::child_iterator i = obj_node->begin_child("object_type"); i != obj_node->end_child("object_type"); ++i) {
animation_containing_nodes.push_back(i->second);
}
*/
}
foreach(variant node, animation_containing_nodes) {
foreach(const variant_pair& p, node.as_map()) {
std::string attr_name = p.first.as_string();
if(attr_name != "animation" && attr_name != "framed_gui_element" && attr_name != "section") {
continue;
}
foreach(const variant& v, p.second.as_list()) {
animation_area_ptr anim(new animation_area(v));
if(anim->src_image.empty() || v.has_key(variant("palettes")) || std::find(no_compile_images.begin(), no_compile_images.end(), anim->src_image) != no_compile_images.end()) {
continue;
}
animation_areas.push_back(anim);
foreach(animation_area_ptr area, animation_areas) {
if(*area == *anim) {
anim = area;
break;
}
}
if(attr_name == "particle_system") {
anim->is_particle = true;
}
if(anim != animation_areas.back()) {
animation_areas.pop_back();
}
nodes_to_animation_areas[v] = anim;
}
}
}
std::sort(animation_areas.begin(), animation_areas.end(), animation_area_height_compare);
foreach(animation_area_ptr anim, animation_areas) {
ASSERT_LOG(anim->width <= 1024 && anim->height <= 1024,
"Bad animation area " << anim->width << "x" << anim->height << " for " << anim->src_image << ". Must be 1024x1024 or less.");
int match = -1;
int match_diff = -1;
for(int n = 0; n != output_areas.size(); ++n) {
if(anim->width <= output_areas[n].area.w() && anim->height <= output_areas[n].area.h()) {
const int diff = output_areas[n].area.w()*output_areas[n].area.h() - anim->width*anim->height;
if(match == -1 || diff < match_diff) {
match = n;
match_diff = diff;
}
}
}
if(match == -1) {
match = output_areas.size();
output_areas.push_back(output_area(num_output_images++));
}
output_area match_area = output_areas[match];
output_areas.erase(output_areas.begin() + match);
rect area = use_output_area(match_area, anim->width, anim->height, output_areas);
anim->dst_image = match_area.image_id;
anim->dst_area = area;
}
std::vector<surface> surfaces;
for(int n = 0; n != num_output_images; ++n) {
surfaces.push_back(surface(SDL_CreateRGBSurface(SDL_SWSURFACE,TextureImageSize,TextureImageSize,32,SURFACE_MASK)));
}
foreach(animation_area_ptr anim, animation_areas) {
foreach(animation_area_ptr other, animation_areas) {
if(anim == other || anim->dst_image != other->dst_image) {
continue;
}
ASSERT_LOG(rects_intersect(anim->dst_area, other->dst_area) == false, "RECTANGLES CLASH: " << anim->dst_image << " " << anim->dst_area << " vs " << other->dst_area);
}
ASSERT_INDEX_INTO_VECTOR(anim->dst_image, surfaces);
surface dst = surfaces[anim->dst_image];
surface src = graphics::surface_cache::get(anim->src_image);
ASSERT_LOG(src.get() != NULL, "COULD NOT LOAD IMAGE: '" << anim->src_image << "'");
int xdst = 0;
for(int f = 0; f != anim->anim->num_frames(); ++f) {
const frame::frame_info& info = anim->anim->frame_layout()[f];
const int x = f%anim->anim->num_frames_per_row();
const int y = f/anim->anim->num_frames_per_row();
const rect& base_area = anim->anim->area();
const int xpos = base_area.x() + (base_area.w()+anim->anim->pad())*x;
const int ypos = base_area.y() + (base_area.h()+anim->anim->pad())*y;
SDL_Rect blit_src = {xpos + info.x_adjust, ypos + info.y_adjust, info.area.w(), info.area.h()};
SDL_Rect blit_dst = {anim->dst_area.x() + xdst,
anim->dst_area.y(),
info.area.w(), info.area.h()};
xdst += info.area.w();
ASSERT_GE(blit_dst.x, anim->dst_area.x());
ASSERT_GE(blit_dst.y, anim->dst_area.y());
ASSERT_LE(blit_dst.x + blit_dst.w, anim->dst_area.x() + anim->dst_area.w());
ASSERT_LE(blit_dst.y + blit_dst.h, anim->dst_area.y() + anim->dst_area.h());
SDL_SetAlpha(src.get(), 0, SDL_ALPHA_OPAQUE);
SDL_BlitSurface(src.get(), &blit_src, dst.get(), &blit_dst);
}
}
for(int n = 0; n != num_output_images; ++n) {
std::ostringstream fname;
fname << "images/compiled-" << n << ".png";
graphics::set_alpha_for_transparent_colors_in_rgba_surface(surfaces[n].get());
IMG_SavePNG((module::get_module_path() + fname.str()).c_str(), surfaces[n].get(), -1);
}
typedef std::pair<variant, animation_area_ptr> anim_pair;
foreach(const anim_pair& a, nodes_to_animation_areas) {
variant node = a.first;
animation_area_ptr anim = a.second;
std::ostringstream fname;
fname << "compiled-" << anim->dst_image << ".png";
node.add_attr_mutation(variant("image"), variant(fname.str()));
node.remove_attr_mutation(variant("x"));
node.remove_attr_mutation(variant("y"));
node.remove_attr_mutation(variant("w"));
node.remove_attr_mutation(variant("h"));
node.remove_attr_mutation(variant("pad"));
const frame::frame_info& first_frame = anim->anim->frame_layout().front();
rect r(anim->dst_area.x() - first_frame.x_adjust, anim->dst_area.y() - first_frame.y_adjust, anim->anim->area().w(), anim->anim->area().h());
node.add_attr_mutation(variant("rect"), r.write());
int xpos = anim->dst_area.x();
std::vector<int> v;
foreach(const frame::frame_info& f, anim->anim->frame_layout()) {
ASSERT_EQ(f.area.w() + f.x_adjust + f.x2_adjust, anim->anim->area().w());
ASSERT_EQ(f.area.h() + f.y_adjust + f.y2_adjust, anim->anim->area().h());
v.push_back(f.x_adjust);
v.push_back(f.y_adjust);
v.push_back(f.x2_adjust);
v.push_back(f.y2_adjust);
v.push_back(xpos);
v.push_back(anim->dst_area.y());
v.push_back(f.area.w());
v.push_back(f.area.h());
xpos += f.area.w();
}
std::vector<variant> vs;
foreach(int n, v) {
vs.push_back(variant(n));
}
node.add_attr_mutation(variant("frame_info"), variant(&vs));
}
for(std::map<variant, std::string>::iterator i = nodes_to_files.begin(); i != nodes_to_files.end(); ++i) {
variant node = i->first;
module::write_file(i->second, node.write_json());
}
module::write_file("data/compiled/gui.cfg", gui_node.write_json());
for(std::map<std::string, variant>::iterator i = gui_nodes.begin();
i != gui_nodes.end(); ++i) {
module::write_file("data/compiled/gui/" + i->first, i->second.write_json());
}
}