tdgame test branch by gemini.
This commit is contained in:
@@ -335,6 +335,129 @@ public:
|
||||
else os << " Polys (Cleared) : " << 0 << "\n";
|
||||
os << " colors : " << _colors.size() << "\n";
|
||||
}
|
||||
|
||||
|
||||
void writeTo(FILE* f) const {
|
||||
if (!f) return;
|
||||
|
||||
fwrite(&id, sizeof(int), 1, f);
|
||||
fwrite(&_subId, sizeof(int), 1, f);
|
||||
|
||||
size_t vCount = _vertices.size();
|
||||
fwrite(&vCount, sizeof(size_t), 1, f);
|
||||
if (vCount > 0) {
|
||||
fwrite(_vertices.data(), sizeof(Vector3f), vCount, f);
|
||||
}
|
||||
|
||||
size_t cCount = _colors.size();
|
||||
fwrite(&cCount, sizeof(size_t), 1, f);
|
||||
if (cCount > 0) {
|
||||
fwrite(_colors.data(), sizeof(Color), cCount, f);
|
||||
}
|
||||
|
||||
size_t pCount = _polys.size();
|
||||
fwrite(&pCount, sizeof(size_t), 1, f);
|
||||
for (const auto& p : _polys) {
|
||||
size_t idxCount = p.size();
|
||||
fwrite(&idxCount, sizeof(size_t), 1, f);
|
||||
if (idxCount > 0) {
|
||||
fwrite(p.data(), sizeof(int), idxCount, f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static std::shared_ptr<Mesh> readFrom(FILE* f) {
|
||||
if (!f) return nullptr;
|
||||
|
||||
int r_id, r_subId;
|
||||
if (fread(&r_id, sizeof(int), 1, f) != 1) return nullptr;
|
||||
if (fread(&r_subId, sizeof(int), 1, f) != 1) return nullptr;
|
||||
|
||||
// Read Vertices
|
||||
size_t vCount;
|
||||
if (fread(&vCount, sizeof(size_t), 1, f) != 1) return nullptr;
|
||||
std::vector<Vector3f> verts(vCount);
|
||||
if (vCount > 0) {
|
||||
fread(verts.data(), sizeof(Vector3f), vCount, f);
|
||||
}
|
||||
|
||||
// Read Colors
|
||||
size_t cCount;
|
||||
if (fread(&cCount, sizeof(size_t), 1, f) != 1) return nullptr;
|
||||
std::vector<Color> cols(cCount);
|
||||
if (cCount > 0) {
|
||||
fread(cols.data(), sizeof(Color), cCount, f);
|
||||
}
|
||||
|
||||
// Read Polys
|
||||
size_t pCount;
|
||||
if (fread(&pCount, sizeof(size_t), 1, f) != 1) return nullptr;
|
||||
std::vector<std::vector<int>> polys(pCount);
|
||||
for (size_t i = 0; i < pCount; ++i) {
|
||||
size_t idxCount;
|
||||
if (fread(&idxCount, sizeof(size_t), 1, f) != 1) return nullptr;
|
||||
polys[i].resize(idxCount);
|
||||
if (idxCount > 0) {
|
||||
fread(polys[i].data(), sizeof(int), idxCount, f);
|
||||
}
|
||||
}
|
||||
|
||||
return std::make_shared<Mesh>(r_id, verts, polys, cols, r_subId);
|
||||
}
|
||||
|
||||
// Public API to save to a filename
|
||||
bool save(const std::string& filename) const {
|
||||
FILE* f = fopen(filename.c_str(), "wb");
|
||||
if (!f) {
|
||||
std::cerr << "Mesh::save failed to open: " << filename << std::endl;
|
||||
return false;
|
||||
}
|
||||
writeTo(f);
|
||||
fclose(f);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Public API to load from a filename into this object
|
||||
bool load(const std::string& filename) {
|
||||
FILE* f = fopen(filename.c_str(), "rb");
|
||||
if (!f) {
|
||||
std::cerr << "Mesh::load failed to open: " << filename << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read into temporary variables first to ensure integrity
|
||||
int r_id, r_subId;
|
||||
if (fread(&r_id, sizeof(int), 1, f) != 1) { fclose(f); return false; }
|
||||
if (fread(&r_subId, sizeof(int), 1, f) != 1) { fclose(f); return false; }
|
||||
|
||||
size_t vCount;
|
||||
if (fread(&vCount, sizeof(size_t), 1, f) != 1) { fclose(f); return false; }
|
||||
std::vector<Vector3f> verts(vCount);
|
||||
if (vCount > 0) fread(verts.data(), sizeof(Vector3f), vCount, f);
|
||||
|
||||
size_t cCount;
|
||||
if (fread(&cCount, sizeof(size_t), 1, f) != 1) { fclose(f); return false; }
|
||||
std::vector<Color> cols(cCount);
|
||||
if (cCount > 0) fread(cols.data(), sizeof(Color), cCount, f);
|
||||
|
||||
size_t pCount;
|
||||
if (fread(&pCount, sizeof(size_t), 1, f) != 1) { fclose(f); return false; }
|
||||
std::vector<std::vector<int>> polys(pCount);
|
||||
for (size_t i = 0; i < pCount; ++i) {
|
||||
size_t idxCount;
|
||||
if (fread(&idxCount, sizeof(size_t), 1, f) != 1) { fclose(f); return false; }
|
||||
polys[i].resize(idxCount);
|
||||
if (idxCount > 0) fread(polys[i].data(), sizeof(int), idxCount, f);
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
|
||||
// Apply to current object
|
||||
this->id = r_id;
|
||||
this->_subId = r_subId;
|
||||
this->replace(verts, polys, cols);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
class Scene {
|
||||
|
||||
Reference in New Issue
Block a user