示例#1
0
/**
 * Compile the track from an input stream.
 * @param in The input stream.
 * @throws TrackCompileExn
 */
void TrackCompiler::Compile(std::istream &in) const
{
	Parcel::ClassicRecordFile outFile;

	// Try to create the output file
	if (!outFile.CreateForWrite(outputFilename, 4, "\x8\rHoverRace track file\n\x1a")) {
		throw TrackCompileExn(boost::str(
			boost::format(_("Unable to create the output file: %s")) %
				Str::PU(outputFilename)));
	}
	log->Info(boost::str(
		boost::format(_("Compiling track (%s)")) %
			Str::PU(outputFilename)));

	// Header.
	if (!outFile.BeginANewRecord()) {
		throw TrackCompileExn(_("Unable to add a header to the output file"));
	}
	else {
		Parcel::ObjStreamPtr archivePtr(outFile.StreamOut());
		Parcel::ObjStream &archive = *archivePtr;
		CreateHeader(in, archive);
	}
	in.clear();
	in.seekg(0, std::ios::beg);

	// The track itself.
	LevelBuilder builder(log);
	if (!outFile.BeginANewRecord()) {
		throw TrackCompileExn(_("Unable to add the track to the output file"));
	}
	else {
		if (!builder.InitFromStream(in)) {
			throw TrackCompileExn(_("Track creation error"));
		}

		Parcel::ObjStreamPtr archivePtr(outFile.StreamOut());
		Parcel::ObjStream &archive = *archivePtr;
		builder.Serialize(archive);
	}
	in.clear();
	in.seekg(0, std::ios::beg);

	// The background image.
	if (!outFile.BeginANewRecord()) {
		throw TrackCompileExn(_("Unable to add a background image record"));
	}
	else {
		Parcel::ObjStreamPtr archivePtr(outFile.StreamOut());
		Parcel::ObjStream &archive = *archivePtr;
		AddBackgroundImage(in, archive);
	}
	in.clear();
	in.seekg(0, std::ios::beg);

	// The map sprite.
	if (!outFile.BeginANewRecord()) {
		throw TrackCompileExn(_("Unable to add a map record"));
	}
	else {
		Parcel::ObjStreamPtr archivePtr(outFile.StreamOut());
		Parcel::ObjStream &archive = *archivePtr;

		MapSprite mapSprite;

		int x0, x1, y0, y1;
		mapSprite.CreateMap(&builder, x0, y0, x1, y1);

		archive << x0;
		archive << x1;
		archive << y0;
		archive << y1;

		mapSprite.Serialize(archive);
	}

	log->Info(_("Compiled track successfully!"));
}