1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-10 04:42:38 +01:00
openrw/framework2/LoaderIPL.cpp
2013-07-02 17:29:20 +01:00

150 lines
3.5 KiB
C++

#include <renderwure/loaders/LoaderIPL.hpp>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
enum SectionTypes
{
INST,
PICK,
CULL,
ZONE,
NONE
};
/// Load the IPL data into memory
bool LoaderIPL::load(const std::string& filename)
{
std::ifstream str(filename);
if(!str.is_open())
return false;
SectionTypes section = NONE;
while(!str.eof())
{
std::string line;
getline(str, line);
line.erase(std::find_if(line.rbegin(), line.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), line.end());
if(!line.empty() && line[0] == '#')
{
// nothing, just a comment
}
else if(line == "end") // terminating a section
{
section = NONE;
}
else if(section == NONE) // starting a new section
{
if(line == "inst")
{
section = INST;
}
if(line == "pick")
{
section = PICK;
}
if(line == "cull")
{
section = CULL;
}
if(line == "zone")
{
section = ZONE;
}
}
else // regular entry
{
if(section == INST)
{
LoaderIPLInstance instance;
std::string id;
std::string model;
std::string posX, posY, posZ;
std::string scaleX, scaleY, scaleZ;
std::string rotX, rotY, rotZ, rotW;
std::stringstream strstream(line);
// read all the contents of the line
getline(strstream, id, ',');
getline(strstream, model, ',');
getline(strstream, posX, ',');
getline(strstream, posY, ',');
getline(strstream, posZ, ',');
getline(strstream, scaleX, ',');
getline(strstream, scaleY, ',');
getline(strstream, scaleZ, ',');
getline(strstream, rotX, ',');
getline(strstream, rotY, ',');
getline(strstream, rotZ, ',');
getline(strstream, rotW, ',');
// convert to our structure
instance.id = atoi(id.c_str());
instance.model = model.substr(1, model.size()-1);
instance.posX = atof(posX.c_str());
instance.posY = atof(posY.c_str());
instance.posZ = atof(posZ.c_str());
instance.scaleX = atof(scaleX.c_str());
instance.scaleY = atof(scaleY.c_str());
instance.scaleZ = atof(scaleZ.c_str());
instance.rotX = atof(rotX.c_str());
instance.rotY = atof(rotY.c_str());
instance.rotZ = atof(rotZ.c_str());
instance.rotW = atof(rotW.c_str());
centroid += glm::vec3(instance.posX, instance.posY, instance.posZ);
/*std::cout << "id: " << instance.id << std::endl;
std::cout << "model: " << instance.model << std::endl;
std::cout << "posX: " << instance.posX << std::endl;
std::cout << "posY: " << instance.posY << std::endl;
std::cout << "posZ: " << instance.posZ << std::endl;
std::cout << "rotW: " << instance.rotW << std::endl;*/
m_instances.push_back(instance);
}
else if(section == ZONE)
{
Zone zone;
std::stringstream strstream(line);
std::string value;
getline(strstream, value, ',');
zone.name = value;
getline(strstream, value, ',');
zone.type = atoi(value.c_str());
getline(strstream, value, ',');
zone.min.x = atof(value.c_str());
getline(strstream, value, ',');
zone.min.y = atof(value.c_str());
getline(strstream, value, ',');
zone.min.z = atof(value.c_str());
getline(strstream, value, ',');
zone.max.x = atof(value.c_str());
getline(strstream, value, ',');
zone.max.y = atof(value.c_str());
getline(strstream, value, ',');
zone.max.z = atof(value.c_str());
getline(strstream, value, ',');
zone.island = atoi(value.c_str());
zones.push_back(zone);
}
}
}
return true;
}