/** * * 女医ハックデータパーサ * Copyright (C) 2008 id:yuaaaa * * $Id$ */ #if PocketPC #define CrazyDraw #endif using System; using System.Collections.Generic; using System.Text; using JoyHack; using System.Drawing; using System.Drawing.Drawing2D; using System.IO; using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; using Font = System.Drawing.Font; namespace JHDrawLib { public class DrawJoyHack : IDisposable { private List lyr; private int _width, _height; private int _defaultFontSize; private int _timeCodeFix; //frames - set from datafile private int _timeUserFix; //frames - set from App private bool _strict; private drawCache[] cache = new drawCache[4]; private Device dxdevice = null; public bool _debug; public bool disable_cache; public bool debug { get { return this._debug; } set { this.clear_cache(); this._debug = value; } } public int width { get { return this._width; } set { if (value <= 0) { throw new ArgumentOutOfRangeException(); } this.clear_cache(); this._width = value; } } public int height { get { return this._height; } set { if (value <= 0) { throw new ArgumentOutOfRangeException(); } this.clear_cache(); this._height = value; } } public bool strict { get { return this._strict; } set { this._strict = value; } } public int defaultFontSize { get { return this._defaultFontSize; } set { if (value < 1 || System.Single.MaxValue < value) { throw new ArgumentOutOfRangeException(); } this.clear_cache(); this._defaultFontSize = value; } } public int timeCodeFix { get { return this._timeCodeFix; } private set { this._timeCodeFix = value; } } public int timeUserFix { get { return this._timeUserFix; } set { this._timeUserFix = value; } } public DrawJoyHack(int width, int height) { this.lyr = new List(); this.width = width; this.height = height; this.defaultFontSize = 30; this.timeCodeFix = 0; this.timeUserFix = 0; this.debug = false; this.disable_cache = false; this.strict = false; // PresentParameters。デバイスを作成する際に必須 // どのような環境でデバイスを使用するかを設定する PresentParameters pp = new PresentParameters(); // ウインドウモードなら true、フルスクリーンモードなら false を指定 pp.Windowed = true; // スワップ効果。とりあえず「Discard」を指定。 pp.SwapEffect = SwapEffect.Discard; System.Windows.Forms.Form frm = new System.Windows.Forms.Form(); // 実際にデバイスを作成します。 // 常に最高のパフォーマンスで作成を試み、 // 失敗したら下位パフォーマンスで作成するようにしている。 try { // ハードウェアによる頂点処理、ラスタライズを行う // 最高のパフォーマンスで処理を行えます。 // ビデオカードによっては実装できない処理が存在します。 this.dxdevice = new Device(0, DeviceType.Hardware, frm/*topLevelForm.Handle*/, CreateFlags.HardwareVertexProcessing, pp); } catch (DirectXException ex1) { // 作成に失敗 //Debug.WriteLine(ex1.ToString()); try { // ソフトウェアによる頂点処理、ハードウェアによるラスタライズを行う this.dxdevice = new Device(0, DeviceType.Hardware, frm/*topLevelForm.Handle*/, CreateFlags.SoftwareVertexProcessing, pp); } catch (DirectXException ex2) { // 作成に失敗 //Debug.WriteLine(ex2.ToString()); try { // ソフトウェアによる頂点処理、ラスタライズを行う // パフォーマンスはとても低いです。 // その代わり、ほとんどの処理を制限なく行えます。 this.dxdevice = new Device(0, DeviceType.Reference, frm/*topLevelForm.Handle*/, CreateFlags.SoftwareVertexProcessing, pp); } catch (DirectXException ex3) { // 作成に失敗 // 事実上デバイスは作成できません。 throw new Exception(ex3.ToString()); } } } } public void clear_cache () { this.cache = new drawCache[4]; } public void load_data(string file) { this.load_data(file, Encoding.GetEncoding(932)); } public void load_data ( string file , Encoding encode ) { lyr = new List(); this.clear_cache(); FileStream fs = new FileStream(file, FileMode.Open); StreamReader sr = new StreamReader(fs, encode); int showline = 0; //index 0... (all_showline-1) int all_showline = 2; // 1...4 string line; WordCond cond = new WordCond(); cond.color = new ShowColor( new Color[8] {Color.White, Color.Black, Color.Red, Color.White, Color.White, Color.Black, Color.Red, Color.White} ); #if PocketPC cond.mainfont = new WordFont( new Font("MS Gothic", this.defaultFontSize, FontStyle.Regular), StringAlignment.Near, 1.0D); cond.rubyfont = new WordFont( new Font("MS Gothic", this.defaultFontSize / 2, FontStyle.Regular), StringAlignment.Near, 1.0D); #else cond.mainfont = new WordFont( new Font("MS UI Gothic", this.defaultFontSize, FontStyle.Regular, GraphicsUnit.Pixel), StringAlignment.Near, 1.0D); cond.rubyfont = new WordFont( new Font("MS UI Gothic", this.defaultFontSize/2 , FontStyle.Regular, GraphicsUnit.Pixel), StringAlignment.Near, 1.0D); #endif Alignment align = Alignment.Default; Timecode prevphr_endtime = new Timecode(0); float height = -1; int linenum = 0; while ((line = sr.ReadLine()) != null) { linenum++; try { line = line.Replace("\r", "").Replace("\n", ""); if (line.IndexOf("") >= 0 ) { string timecodestr = line.Substring("".Length); if (timecodestr.Length == 0) { this.timeCodeFix = 0; } else if (timecodestr.Length == 8) { this.timeCodeFix = new Timecode(timecodestr).allFrames; } else if (timecodestr.Length == 9) { if (timecodestr.Substring(0, 1) == "-") { this.timeCodeFix = -(new Timecode(timecodestr.Substring(1, 8)).allFrames); } else { this.timeCodeFix = +(new Timecode(timecodestr.Substring(1, 8)).allFrames); } } else { throw new JoyHackDataException("不正なです: \"" + timecodestr + "\""); } continue; } if ( line.IndexOf("") >= 0 ) { string alignstr = line.Substring("".Length); byte align_i = byte.Parse(alignstr); if (align_i < 0 || 3 < align_i) { throw new JoyHackDataException("不正なです"); } align = (Alignment)align_i; continue; } if (line.IndexOf("