1 #include "xr_scene.h"
2 #include "xr_scene_shapes.h"
3 #include "xr_string_utils.h"
4 #include "xr_reader.h"
5 #include "xr_writer.h"
6 
7 using namespace xray_re;
8 
xr_shape_object(xr_scene & scene)9 xr_shape_object::xr_shape_object(xr_scene& scene):
10 	xr_custom_object(scene, TOOLS_CLASS_SHAPE) {}
11 
xr_shape_object(xr_scene & scene,tools_class_id class_id)12 xr_shape_object::xr_shape_object(xr_scene& scene, tools_class_id class_id):
13 	xr_custom_object(scene, class_id) {}
14 
~xr_shape_object()15 xr_shape_object::~xr_shape_object() {}
16 
operator ()read_shape17 struct read_shape { void operator()(shape_def& s, xr_reader& r) const {
18 	s.type = r.r_u8();
19 	r.r(s.box);
20 }};
21 
load(xr_reader & r)22 void xr_shape_object::load(xr_reader& r)
23 {
24 	uint16_t version;
25 	if (!r.r_chunk<uint16_t>(SHAPE_CHUNK_VERSION, version))
26 		xr_not_expected();
27 	xr_assert(version == SHAPE_VERSION);
28 	xr_custom_object::load(r);
29 	if (!r.find_chunk(SHAPE_CHUNK_SHAPES))
30 		xr_not_expected();
31 	r.r_seq(r.r_u32(), m_shapes, read_shape());
32 	r.debug_find_chunk();
33 }
34 
operator ()write_shape35 struct write_shape { void operator()(const shape_def& s, xr_writer& w) const {
36 	w.w_u8(s.type);
37 	w.w(s.box);
38 }};
39 
save(xr_writer & w) const40 void xr_shape_object::save(xr_writer& w) const
41 {
42 	xr_custom_object::save(w);
43 	w.w_chunk<uint16_t>(SHAPE_CHUNK_VERSION, SHAPE_VERSION);
44 	w.open_chunk(SHAPE_CHUNK_SHAPES);
45 	w.w_size_u32(m_shapes.size());
46 	w.w_seq(m_shapes, write_shape());
47 	w.close_chunk();
48 }
49 
operator ()write_shape_ini50 struct write_shape_ini { void operator()(const shape_def& s, xr_ini_writer* w, uint32_t id) const {
51 	char buffer[128];
52 	char* buf = &buffer[0];
53 	int n = xr_snprintf(buf, sizeof(buffer), "shape_type_%d", id);
54 
55 	if (n > 0)
56 		w->write(buf, s.type);
57 
58 	if (s.type == SHAPE_SPHERE)
59 	{
60 		n = xr_snprintf(buf, sizeof(buffer), "shape_center_%d", id);
61 		if (n > 0)
62 			w->write(buf, s.sphere.p);
63 
64 		n = xr_snprintf(buf, sizeof(buffer), "shape_radius_%d", id);
65 		if (n > 0)
66 			w->write(buf, s.sphere.r);
67 	}
68 	else if (s.type == SHAPE_BOX)
69 	{
70 		n = xr_snprintf(buf, sizeof(buffer), "shape_matrix_c_%d", id);
71 		if (n > 0)
72 			w->write(buf, s.box.c);
73 
74 		n = xr_snprintf(buf, sizeof(buffer), "shape_matrix_i_%d", id);
75 		if (n > 0)
76 			w->write(buf, s.box.i);
77 
78 		n = xr_snprintf(buf, sizeof(buffer), "shape_matrix_j_%d", id);
79 		if (n > 0)
80 			w->write(buf, s.box.j);
81 
82 		n = xr_snprintf(buf, sizeof(buffer), "shape_matrix_k_%d", id);
83 		if (n > 0)
84 			w->write(buf, s.box.k);
85 	}
86 }};
87 
save_v12(xr_ini_writer * w) const88 void xr_shape_object::save_v12(xr_ini_writer* w) const
89 {
90 	xr_custom_object::save_v12(w);
91 
92 	w->write("shape_type", 0);
93 
94 	w->w_ini_seq(m_shapes, write_shape_ini());
95 	w->write("shapes_count", m_shapes.size());
96 
97 	w->write("version", SHAPE_VERSION_V12);
98 }
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 
xr_scene_shapes(xr_scene & scene)102 xr_scene_shapes::xr_scene_shapes(xr_scene& scene):
103 	xr_scene_objects(scene, "shape.part", SCENE_CHUNK_SHAPES),
104 	m_flags(0) {}
105 
~xr_scene_shapes()106 xr_scene_shapes::~xr_scene_shapes() {}
107 
load(xr_reader & r)108 void xr_scene_shapes::load(xr_reader& r)
109 {
110 	uint16_t version = 0;
111 	r.r_chunk<uint16_t>(TOOLS_CHUNK_VERSION, version);
112 	xr_assert(version == 0);
113 	xr_scene_objects::load(r);
114 //	r.r_chunk<uint32_t>(SHAPES_CHUNK_COMMON_FLAGS, m_flags);
115 }
116 
save(xr_writer & w) const117 void xr_scene_shapes::save(xr_writer& w) const
118 {
119 	xr_scene_objects::save(w);
120 	w.w_chunk<uint16_t>(TOOLS_CHUNK_VERSION, 0);
121 //	w.w_chunk<uint32_t>(SHAPES_CHUNK_COMMON_FLAGS, m_flags);
122 }
123 
save_v12(xr_ini_writer * w) const124 void xr_scene_shapes::save_v12(xr_ini_writer* w) const
125 {
126 	w->open_section("main");
127 	w->write("objects_count", this->objects().size());
128 	w->write("version", 0);
129 	w->close_section();
130 
131 	scene().write_revision(w);
132 
133 	xr_scene_objects::save_v12(w);
134 }
135