Пример #1
0
ulAssetManager::ulAssetManager(std::string artFilePath)
{
    // Should probably find a better way of doing this bit
    int len = 256;
    char pBuf[len];
    char szTmp[32];
    sprintf(szTmp, "/proc/%d/exe", getpid());
    int bytes = std::min((int)readlink(szTmp, pBuf, len), len - 1);
    if(bytes >= 0)
    {
        pBuf[bytes] = '\0';
    }

    progDir = std::string(pBuf);
    progDir = progDir.substr(0, progDir.length() - 10);

    std::ifstream artFile(progDir + artFilePath);
    if (!artFile.is_open())
    {
        std::cerr<<"Could not open art definition file ("<<progDir + artFilePath<<")"<<std::endl;
    }
    else
    {
        std::string jsonString((std::istreambuf_iterator<char>(artFile)),
                         std::istreambuf_iterator<char>());
        doc.Parse(jsonString.c_str());
        assert(!doc.HasParseError());
    }
}
// Load line and art data from files
int ConstellationMgr::loadLinesAndArt(const string &skyCultureDir)
{

	string fileName = skyCultureDir + "/constellationship.fab";
	string artfileName = skyCultureDir + "/constellationsart.fab";
	string boundaryfileName = skyCultureDir + "/boundaries.dat";

	std::ifstream inf(fileName.c_str());

	if (!inf.is_open()) {
		Log.write("Constellation_Mgr::loadLinesAndArt can't open constellation data file "+ fileName, cLog::eLOG_TYPE_ERROR);
		return -1;
	}

	// delete existing data, if any
	//~ if( SharedData::Instance()->DB() ) {
		//~ dbCursor<ObjectRecord> cursor(dbCursorForUpdate);
		//~ dbQuery q;
		//~ q = "type=",ObjectRecord::OBJECT_CONSTELLATION;
		//~ if( cursor.select(q) )
			//~ cursor.removeAllSelected();
		//~ SharedData::Instance()->DB()->commit();
	//~ }

	vector < Constellation * >::iterator iter;
	for (iter = asterisms.begin(); iter != asterisms.end(); ++iter) {
		delete(*iter);
	}
	asterisms.clear();
	selected.clear();

	Constellation *cons = NULL;

	string record;
	int line=0;
	while (!inf.eof() && std::getline(inf, record)) {
		line++;
		if (record.size()!=0 && record[0]=='#')
			continue;
		cons = new Constellation;

		if (cons->read(record, hipStarMgr)) {
			asterisms.push_back(cons);
		} else {
			//cerr << "ERROR on line " << line << " of " << fileName.c_str() << endl;
			Log.write("ConstellationMgr::loadLinesAndArt on line " + Utility::intToString(line) + " of " + fileName, cLog::eLOG_TYPE_ERROR);
			delete cons;
		}
	}
	inf.close();

	// Set current states
	setFlagArt(flagArt);
	setFlagLines(flagLines);
	setFlagNames(flagNames);
	setFlagBoundaries(flagBoundaries);


	FILE *fic = fopen(artfileName.c_str(), "r");
	if (!fic) {
//		cerr << "Can't open " << artfileName.c_str() << endl;
		Log.write("ConstellationMgr::loadLinesAndArt Can't open " + artfileName, cLog::eLOG_TYPE_ERROR);
		return 0; // no art, but still loaded constellation data
	}
	fclose(fic);

	// Read the constellation art file with the following format :
	// ShortName texture_file x1 y1 hp1 x2 y2 hp2
	// Where :
	// shortname is the international short name (i.e "Lep" for Lepus)
	// texture_file is the graphic file of the art texture
	// x1 y1 are the x and y texture coordinates in pixels of the star of hipparcos number hp1
	// x2 y2 are the x and y texture coordinates in pixels of the star of hipparcos number hp2
	// The coordinate are taken with (0,0) at the top left corner of the image file
	string shortname;
	string texfile;
	unsigned int x1, y1, x2, y2, x3, y3, hp1, hp2, hp3;
	float fx1, fy1, fx2, fy2, fx3, fy3; // read floats to allow proportional image points to allow image sizes to vary as needed
	int texW, texH; // art texture dimensions

	// Read in constellation art information
	// Note: Stellarium 0.10.3 allows more than 3 alignment points, here only first 3 are used.

	ifstream artFile(artfileName.c_str());
	if (!artFile.is_open()) {
		//cerr << "Can't open file" << artFile << endl;
		Log.write("ConstellationMgr::loadLinesAndArt Can't open " + artfileName, cLog::eLOG_TYPE_ERROR);
		return 0;
	}

	while (!artFile.eof() && std::getline(artFile, record)) {

		if ( record != "") {
			stringstream in(record);
			in >> shortname >> texfile;

			if(in.fail()) {
				//cerr << "Error parsing constellation art record:\n" << record << endl;
				Log.write("ConstellationMgr::loadLinesAndArt Error parsing constellation art record " + record, cLog::eLOG_TYPE_ERROR);
				continue;
			}

			// TODO add better error checking
			if(shortname!="" && shortname[0]!='#') {
				in >> fx1 >> fy1 >> hp1;
				in >> fx2 >> fy2 >> hp2;
				in >> fx3 >> fy3 >> hp3;
			} else {
				continue;