bate's blog

調べたこと実装したことなどを取りとめもなく書きます。

バイナリ出力

修復したら使えるようになった。

XMLのデータをロードしてバイナリに出力してみた。

private void button_export_binary_Click(object sender, EventArgs e)
{
	// save as binary
	// 情報の取得
	string basename = textBox_name.Text;
	string filename = basename + ".eff";

	UInt16 body_size = 0;
 	Int16 emitter_rotX = (Int16)numUpDown_emitter_rotX.Value;
	body_size += sizeof(Int16);
	Int16 emitter_rotY = (Int16)numUpDown_emitter_rotY.Value;
	body_size += sizeof(Int16);
	Int16 emitter_rotZ = (Int16)numUpDown_emitter_rotZ.Value;
	body_size += sizeof(Int16);
	UInt16 emit_num = (UInt16)numUpDown_emit_num.Value;
	body_size += sizeof(UInt16);
	UInt16 emit_interval = (UInt16)numUpDown_emit_interval.Value;
	body_size += sizeof(UInt16);
	UInt16 life = (UInt16)numUpDown_life.Value;
	body_size += sizeof(UInt16);

	// 保存する
	saveFileDialog.FileName = filename;
	saveFileDialog.InitialDirectory = @"D:\text\xml";
	if (saveFileDialog.ShowDialog() == DialogResult.OK)
	{
		string save_path = saveFileDialog.FileName;
		// バイナリ形式でファイルに書き出し。
		using (BinaryWriter w = new BinaryWriter(File.OpenWrite(save_path)))
		{
			// file format
			byte[] buf = new byte[4];
			Encoding.UTF8.GetBytes("EFF").CopyTo(buf, 0);
			w.Write(buf);
			// version
			w.Write((float)3.0);
			// file name
			buf = new byte[32];
			Encoding.UTF8.GetBytes(basename).CopyTo(buf, 0);
			w.Write(buf);
			// body size
			w.Write((UInt16)body_size);

			w.Write(emitter_rotX);
			w.Write(emitter_rotY);
			w.Write(emitter_rotZ);
			w.Write(emit_num);
			w.Write(emit_interval);
			w.Write(life);
		}
	}
}