post-parser completion clean up (wip folder)
This commit is contained in:
parent
111cdc6339
commit
dd5da5d4a7
@ -1,215 +0,0 @@
|
||||
|
||||
|
||||
#define KEYWORD_ENTITY "entity"
|
||||
#define KEYWORD_BRUSH "brush"
|
||||
#define KEYWORD_VERTICES "vertices"
|
||||
#define KEYWORD_FACES "faces"
|
||||
#define KEYWORD_GLOBAL "global"
|
||||
#define KEYWORD_PREFAB "prefab"
|
||||
|
||||
#include "oopless-parser.hpp"
|
||||
#include "brushdef.hpp"
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
// place holding functions to test proper control flow.
|
||||
bool write(const struct TBrush &geometry,
|
||||
ofstream &fout,
|
||||
void (*b) (stringstream &, const vector<TPlanePoints> &)) {
|
||||
bool ok = true;
|
||||
if (geometry.m_Vertices.size() < 1) {
|
||||
cerr << "error: geometry has no vertices" << endl;
|
||||
ok = false;
|
||||
}
|
||||
if (geometry.m_Faces.size() < 1) {
|
||||
cerr << "error: geometry has no faces"<< endl;
|
||||
ok = false;
|
||||
}
|
||||
if (ok) {
|
||||
fout << GetBrushString(geometry, b);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
void write(const vector<string> &entity, ofstream &fo) {
|
||||
;
|
||||
}
|
||||
|
||||
// entities will vary widely depending on their type and available flags.
|
||||
// for now, acquire entity data, and handle their conversion in the write methods.
|
||||
vector<string> get_entity(ifstream &f) {
|
||||
vector<string> output;
|
||||
string line;
|
||||
unsigned int pos = f.tellg();
|
||||
while (getline(f, line)) {
|
||||
if (line.find(KEYWORD_ENTITY) != string::npos ||
|
||||
line.find(KEYWORD_BRUSH) != string::npos ||
|
||||
line.find(KEYWORD_PREFAB) != string::npos) {
|
||||
f.seekg(pos);
|
||||
return output;
|
||||
} else {
|
||||
output.push_back(line);
|
||||
}
|
||||
pos = f.tellg();
|
||||
}
|
||||
}
|
||||
|
||||
vector<Eigen::Vector3f> parse_vertices(ifstream &f) {
|
||||
#define FIRSTCH(x) x[0]
|
||||
vector<Eigen::Vector3f> output;
|
||||
string line;
|
||||
unsigned int pos = f.tellg();
|
||||
while(getline(f, line)) {
|
||||
if (line.find(KEYWORD_FACES) != string::npos ||
|
||||
line.find(KEYWORD_BRUSH) != string::npos ||
|
||||
line.find(KEYWORD_ENTITY) != string::npos ||
|
||||
line.find(KEYWORD_PREFAB) != string::npos) {
|
||||
f.seekg(pos);
|
||||
return output;
|
||||
} else {
|
||||
Eigen::Vector3f v;
|
||||
stringstream ss(line);
|
||||
string vdata;
|
||||
unsigned int i = 0;
|
||||
while (ss >> vdata) {
|
||||
if (isdigit(FIRSTCH(vdata)) || FIRSTCH(vdata) == '-') {
|
||||
double dvalue = stod(vdata);
|
||||
v[i] = (float)dvalue;
|
||||
}
|
||||
// note: this prevents the index growing out of vector capacity
|
||||
// alternatively can throw an exception when that happens instead
|
||||
// to make it clear that an illegal line has been hit.
|
||||
i = (i + 1) % 3;
|
||||
}
|
||||
output.push_back(v);
|
||||
}
|
||||
pos = f.tellg();
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
vector<struct TFace> parse_face(ifstream &f) {
|
||||
#define FIRSTCH(x) x[0]
|
||||
#define SECONDCH(x) x[1]
|
||||
// it is possible for the next line to be unrelated to faces
|
||||
// so it is needed to reset the stream prior to reading
|
||||
// the new label, e.g '\tbrush'.
|
||||
vector<struct TFace> output;
|
||||
string line;
|
||||
unsigned int pos = f.tellg();
|
||||
while(getline(f, line)) {
|
||||
if (line.find(KEYWORD_VERTICES) != string::npos ||
|
||||
line.find(KEYWORD_BRUSH) != string::npos ||
|
||||
line.find(KEYWORD_ENTITY) != string::npos ||
|
||||
line.find(KEYWORD_PREFAB) != string::npos ||
|
||||
line.find(KEYWORD_GLOBAL) != string::npos) {
|
||||
f.seekg(pos);
|
||||
return output;
|
||||
} else {
|
||||
struct TFace x;
|
||||
float *f = &x.m_fXOffset;;
|
||||
stringstream ss(line);
|
||||
string fdata;
|
||||
bool hex = false;
|
||||
unsigned int i = 0;
|
||||
while (ss >> fdata) {
|
||||
if (i < 5) {
|
||||
if (isdigit(FIRSTCH(fdata)) || FIRSTCH(fdata) == '-') {
|
||||
double dvalue = stod(fdata);
|
||||
*f = (float)dvalue;
|
||||
f++;
|
||||
}
|
||||
// note: if there is a non-digit in the first 5 fields
|
||||
// then it qualifies as an illegal line.
|
||||
} else if (4 < i && i < 8) {
|
||||
x.m_Indices.push_back(stoi(fdata));
|
||||
} else if (i == 8) {
|
||||
// field #8 may either be the unidentified hex digit
|
||||
// i.e there are 3 indices only,
|
||||
// or it could be another index.
|
||||
if (fdata.length() > 1 && SECONDCH(fdata) == 'x') {
|
||||
// this is the unidentified hex digit.
|
||||
// just it signify the texture at the end.
|
||||
hex = true;
|
||||
} else {
|
||||
x.m_Indices.push_back(stoi(fdata));
|
||||
}
|
||||
} else if ((i == 9 && hex) || i == 10) {
|
||||
// it is a texture if it is field #9
|
||||
// and the hex digit is already encountered
|
||||
// or it is field #10.
|
||||
x.m_Material = fdata;
|
||||
}
|
||||
i++;
|
||||
} // end, per field iteration
|
||||
output.push_back(x);
|
||||
}
|
||||
pos = f.tellg();
|
||||
} // end, per line iteration
|
||||
// the odd case when the map file ends with a face indent.
|
||||
return output;
|
||||
}
|
||||
|
||||
struct TBrush parse_brush(ifstream &f) {
|
||||
struct TBrush output;
|
||||
string l;
|
||||
getline(f, l);
|
||||
// wip: note must return the entire brush, adjust control flow
|
||||
if (l.find(KEYWORD_VERTICES) != string::npos) {
|
||||
output.m_Vertices = parse_vertices(f);
|
||||
}
|
||||
getline(f, l);
|
||||
if (l.find(KEYWORD_FACES) != string::npos) {
|
||||
output.m_Faces = parse_face(f);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
void parse_prefab(ifstream &f,
|
||||
ofstream &out,
|
||||
void (*b) (stringstream &, const vector<TPlanePoints> &)) {
|
||||
string l;
|
||||
getline(f, l);
|
||||
if (l.find(KEYWORD_BRUSH) != string::npos) {
|
||||
write(parse_brush(f), out, b);
|
||||
} else if (l.find(KEYWORD_ENTITY) != string::npos) {
|
||||
write(get_entity(f), out);
|
||||
}
|
||||
}
|
||||
|
||||
// note: can't use a single function pointer for variadic overloaded write function?
|
||||
bool convertmap(const char * const infile,
|
||||
const char * const outfile,
|
||||
void (*brushdef) (stringstream &, const vector<TPlanePoints> &)) {
|
||||
ifstream fin;
|
||||
fin.open(infile);
|
||||
if (!fin.good()){
|
||||
cerr << "error: failed to open input file" << endl;
|
||||
return false;
|
||||
}
|
||||
ofstream fout;
|
||||
fout.open(outfile);
|
||||
if (!fout.good()) {
|
||||
cerr << "error: failed to open output file" << endl;
|
||||
return false;
|
||||
}
|
||||
fout << "{\n"
|
||||
<< "\"classname\" \"worldspawn\"" << endl;
|
||||
string line;
|
||||
while (getline(fin, line)) {
|
||||
if (line.find(KEYWORD_PREFAB) != string::npos ||
|
||||
line.find(KEYWORD_GLOBAL) != string::npos) {
|
||||
parse_prefab(fin, fout, brushdef);
|
||||
} else if (line.find(KEYWORD_ENTITY) != string::npos) {
|
||||
write(get_entity(fin), fout);
|
||||
} else if (line.find(KEYWORD_BRUSH) != string::npos) {
|
||||
write(parse_brush(fin), fout, brushdef);
|
||||
}
|
||||
}
|
||||
fout << "}" << endl;
|
||||
fin.close();
|
||||
fout.close();
|
||||
return true;
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
|
||||
#ifndef __OOPLESS_PARSER__
|
||||
#define __OOPLESS_PARSER__
|
||||
|
||||
#include "planes.h"
|
||||
#include "worldspawn.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
|
||||
|
||||
|
||||
bool write(const struct TBrush &,
|
||||
std::ofstream &,
|
||||
void (*f) (std::stringstream &, const std::vector<TPlanePoints> &));
|
||||
|
||||
void write(const std::vector<std::string> &, std::ofstream &);
|
||||
|
||||
std::vector<std::string> get_entity(std::ifstream &);
|
||||
|
||||
std::vector<Eigen::Vector3f> parse_vertices(std::ifstream &);
|
||||
|
||||
std::vector<TFace> parse_face(std::ifstream &);
|
||||
|
||||
struct TBrush parse_brush(std::ifstream &);
|
||||
|
||||
void parse_prefab(std::ifstream &,
|
||||
std::ofstream &,
|
||||
void (*f) (std::stringstream &, const std::vector<TPlanePoints> &));
|
||||
|
||||
bool convertmap(const char * const,
|
||||
const char * const,
|
||||
void (*f) (std::stringstream &, const std::vector<TPlanePoints> &));
|
||||
|
||||
#endif
|
@ -1,77 +0,0 @@
|
||||
|
||||
#include "oopless-parser.hpp"
|
||||
#include "brushdef.hpp"
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void print_TFace(const TFace x, int i) {
|
||||
cout << "----------------------------" << endl;
|
||||
cout << "TFace #" << i << endl;
|
||||
cout << "\tOffset X: " << x.m_fXOffset << endl;
|
||||
cout << "\tOffset Y: " << x.m_fYOffset << endl;
|
||||
cout << "\tScale X: " << x.m_fXScale << endl;
|
||||
cout << "\tScale Y: " << x.m_fYScale << endl;
|
||||
cout << "\tRotation: " << x.m_fRotation << endl;
|
||||
for (unsigned int i = 0; i < x.m_Indices.size(); ++i) {
|
||||
cout << x.m_Indices[i] << ", ";
|
||||
}
|
||||
cout << "\n\tMaterial: " << x.m_Material << endl;
|
||||
cout << "----------------------------" << endl;
|
||||
}
|
||||
|
||||
// jump to a line in fstream
|
||||
// this is to set the stream position to test individual parsing functions.
|
||||
void gotoline(ifstream &f, unsigned int n) {
|
||||
string t;
|
||||
for (unsigned int i = 0; i < n; ++i) {
|
||||
getline(f, t);
|
||||
}
|
||||
}
|
||||
|
||||
void test_parsevertices (const char * const fn, const int ln) {
|
||||
ifstream fin;
|
||||
fin.open(fn);
|
||||
string line;
|
||||
gotoline(fin, ln);
|
||||
vector<Eigen::Vector3f> x = parse_vertices(fin);
|
||||
cout << "size has a size of " << x.size() << endl;
|
||||
// display data retrieved.
|
||||
for (unsigned int i = 0; i < x.size(); ++i) {
|
||||
for (unsigned int j = 0; j < 3; ++j) {
|
||||
cout << x[i][j] << "\t";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
getline(fin, line);
|
||||
cout << "check stream state \n current line: " << endl;
|
||||
cout << line << endl;
|
||||
fin.close();
|
||||
}
|
||||
|
||||
// test if faces are parsing correctly
|
||||
void test_parseface(const char * const fn, const int ln) {
|
||||
ifstream fin;
|
||||
fin.open(fn);
|
||||
string line;
|
||||
gotoline(fin, ln);
|
||||
vector<TFace> x = parse_face(fin);
|
||||
cout << "size has a size of " << x.size() << endl;
|
||||
// display data retrieved.
|
||||
for (unsigned int i = 0; i < x.size(); ++i) {
|
||||
print_TFace(x[i], i);
|
||||
}
|
||||
// parse_face will rollback to a position if it comes across a keyword
|
||||
// check the line that fin will retrieve.
|
||||
getline(fin, line);
|
||||
cout << "check stream state \n current line: " << endl;
|
||||
cout << line << endl;
|
||||
fin.close();
|
||||
}
|
||||
|
||||
int main() {
|
||||
//loadmap("empty.map", "empty-test.map");
|
||||
test_parsevertices("empty.map", 10);
|
||||
test_parseface("empty.map", 19);
|
||||
test_parseface("pocket-infinity.map", 16);
|
||||
convertmap("empty.map", "test-empty.map", &brushdef_net);
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
//
|
||||
// Author: Michael Cameron
|
||||
// Email: chronokun@hotmail.com
|
||||
//
|
||||
|
||||
#ifndef __WORLDSPAWN_H__
|
||||
#define __WORLDSPAWN_H__
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <Eigen/Core>
|
||||
|
||||
struct TFace
|
||||
{
|
||||
float m_fXOffset;
|
||||
float m_fYOffset;
|
||||
float m_fXScale;
|
||||
float m_fYScale;
|
||||
float m_fRotation;
|
||||
std::vector<int> m_Indices;
|
||||
std::string m_Material;
|
||||
};
|
||||
|
||||
struct TBrush
|
||||
{
|
||||
std::vector<Eigen::Vector3f> m_Vertices;
|
||||
std::vector<TFace> m_Faces;
|
||||
|
||||
};
|
||||
|
||||
struct TWorldSpawn
|
||||
{
|
||||
std::vector<TBrush> m_Brushes;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user