OSDN Git Service

デバイスリソース クラスに、D3D11デバッグ情報の出力を追加。
[strokestylet/CsWin10Desktop3.git] / StrokeStyleT / StrokeStyleT.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using System.Linq;
5 using System.Windows.Forms;
6
7 namespace SST
8 {
9         class StrokeStyleT : FDK.ApplicationFormBase
10         {
11                 // グローバルリソース (static) 
12                 public static SST.フォルダ フォルダ => StrokeStyleT.bs_フォルダ;
13                 public static FDK.入力.Keyboard キーボード入力 => StrokeStyleT.bs_キーボード入力;
14                 public static FDK.入力.MidiIn MIDI入力 => StrokeStyleT.bs_MIDI入力;
15                 public static FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice Wasapiデバイス => StrokeStyleT.bs_Wasapiデバイス;
16                 public static Random 乱数 => StrokeStyleT.bs_乱数;
17                 public static SST.ユーザ.ユーザ管理 ユーザ管理 => StrokeStyleT.bs_ユーザ管理;
18                 public static SST.曲.曲ツリー管理 曲ツリー管理 => StrokeStyleT.bs_曲ツリー管理;
19                 public static SSTFormat.スコア 演奏スコア { get; set; } = null;
20
21                 public static void すべての入力デバイスをポーリングする()
22                 {
23                         // hack: 追加の入力デバイスができたら、ここにポーリングコードを追加すること。
24                         StrokeStyleT.キーボード入力?.ポーリングする();
25                         StrokeStyleT.MIDI入力?.ポーリングする();
26                 }
27
28                 // get only static property の初期化。
29                 static StrokeStyleT()
30                 {
31                         // フォルダ変数を真っ先に登録する。(ほかのメンバのコンストラクタでフォルダ変数を利用できるようにするため。)
32                         StrokeStyleT.bs_フォルダ = new SST.フォルダ();
33                         SST.フォルダ.フォルダ変数を追加する( "Static", StrokeStyleT.フォルダ.StaticFolder );
34                         SST.フォルダ.フォルダ変数を追加する( "AppData", StrokeStyleT.フォルダ.AppDataFolder );
35                         SST.フォルダ.フォルダ変数を追加する( "User", null );
36
37                         // その他の static の生成。
38                         StrokeStyleT.bs_ユーザ管理 = new ユーザ.ユーザ管理();
39                         StrokeStyleT.bs_曲ツリー管理 = new 曲.曲ツリー管理();
40                 }
41
42                 public override void 初期化する()
43                 {
44                         Debug.Assert( null == this.デバイスリソース );  // 作成される前である。
45                         FDK.Log.現在のスレッドに名前をつける( "GUI" );
46
47                         this.Text = $"StrokeStyle<T> {System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()}";
48                         this.ClientSize = new System.Drawing.Size( 1920, 1080 );        // 物理画面サイズpx
49                         this.設計画面サイズdpx = new SharpDX.Size2F( 1920, 1080 ); // 設計画面サイズdpx
50
51                         #region " System.Stopwatch が高解像度タイマを使わないならエラー。"
52                         //-----------------
53                         if( false == System.Diagnostics.Stopwatch.IsHighResolution )
54                                 throw new SSTException( "このシステムは、高解像度タイマをサポートしていません。" );
55                         //-----------------
56                         #endregion
57                         #region " MediaFoundation を起動する。"
58                         //-----------------
59                         SharpDX.MediaFoundation.MediaManager.Startup();
60                         //-----------------
61                         #endregion
62                         #region " Sleep 精度を上げる。"
63                         //-----------------
64                         StrokeStyleT.timeBeginPeriod( 1 );
65                         //-----------------
66                         #endregion
67
68                         #region " ユーザを初期化する。"
69                         //-----------------
70                         FDK.Log.Info( "ユーザ情報を初期化します。" );
71                         StrokeStyleT.ユーザ管理.UsersXmlを読み込む();
72
73                         // ユーザ別の初期化。
74                         foreach( var ユーザ in StrokeStyleT.ユーザ管理.ユーザリスト )
75                                 ユーザ.SourcesXmlを読み込む();
76                         //-----------------
77                         #endregion
78                         #region " WASAPI デバイスを初期化する。"
79                         //----------------
80                         StrokeStyleT.bs_Wasapiデバイス = new FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice();
81                         StrokeStyleT.bs_Wasapiデバイス.初期化する( 7.0f );
82                         //----------------
83                         #endregion
84                         #region " キーボード入力 を初期化する。"
85                         //-----------------
86                         FDK.Log.Info( "キーボード入力デバイスを初期化します。" );
87                         StrokeStyleT.bs_キーボード入力 = new FDK.入力.Keyboard( this.Handle );
88                         //-----------------
89                         #endregion
90                         #region " MIDI入力 を初期化する。"
91                         //-----------------
92                         FDK.Log.Info( "MIDI入力デバイスを初期化します。" );
93                         StrokeStyleT.bs_MIDI入力 = new FDK.入力.MidiIn();
94                         //-----------------
95                         #endregion
96
97                         this.現在のステージ = this.最初のダミーステージ;
98                 }
99                 public override void 終了する()
100                 {
101                         Debug.Assert( null == this.デバイスリソース );  // 解放された後である。
102
103                         #region " ステージを終了し、解放する。"
104                         //----------------
105                         //if( this.現在のステージ.活性化している )
106                         //      this.現在のステージ.非活性化する( this.デバイスリソース ); → デバイスリソース解放後なので、ここで非活性化してはならない。
107                         this.最初のダミーステージ  = null;
108                         this.起動ステージ = null;
109                         this.タイトルステージ = null;
110                         this.ログインステージ = null;
111                         this.選曲ステージ = null;
112                         this.曲読込ステージ = null;
113                         this.演奏ステージ = null;
114                         this.結果ステージ = null;
115                         //----------------
116                         #endregion
117                         #region " MIDI入力 を解放する。"
118                         //-----------------
119                         FDK.Log.Info( "MIDI入力デバイスを解放します。" );
120                         FDK.Utilities.解放する( ref StrokeStyleT.bs_MIDI入力 );
121                         //-----------------
122                         #endregion
123                         #region " キーボード入力 を解放する。"
124                         //-----------------
125                         FDK.Log.Info( "キーボード入力デバイスを解放します。" );
126                         FDK.Utilities.解放する( ref StrokeStyleT.bs_キーボード入力 );
127                         //-----------------
128                         #endregion
129                         #region " WASAPIデバイスを解放する。"
130                         //----------------
131                         FDK.Utilities.解放する( ref StrokeStyleT.bs_Wasapiデバイス );
132                         //----------------
133                         #endregion
134                         #region " MediaFoundation を終了する。"
135                         //-----------------
136                         SharpDX.MediaFoundation.MediaManager.Shutdown();
137                         //-----------------
138                         #endregion
139                 }
140                 public override void シーンを描画する()
141                 {
142                         #region " 描画を準備する。"
143                         //----------------
144                         var d3dDevice = (SharpDX.Direct3D11.Device) null;
145                         using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.デバイスリソース.DXGIDeviceManager, out d3dDevice ) )
146                         {
147                                 // 既定のD3Dレンダーターゲットビューを黒でクリアする。
148                                 d3dDevice.ImmediateContext.ClearRenderTargetView(
149                                         this.デバイスリソース.D3DRenderTargetView, SharpDX.Color4.Black );
150
151                                 // 深度バッファを 1.0f でクリアする。
152                                 d3dDevice.ImmediateContext.ClearDepthStencilView(
153                                         this.デバイスリソース.D3DDepthStencilView,
154                                         SharpDX.Direct3D11.DepthStencilClearFlags.Depth,
155                                         depth: 1.0f,
156                                         stencil: 0 );
157                         }
158                         //----------------
159                         #endregion
160
161                         // 現在のステージを進行描画する。
162                         this.現在のステージ?.進行描画する( this.デバイスリソース );
163
164                         // 表示する。
165                         this.デバイスリソース.SwapChain.Present( 1, SharpDX.DXGI.PresentFlags.None );
166
167                         if( null != this.現在のステージ )
168                         {
169                                 // ステージの状態チェックを行い、必要あれば遷移、またはアプリを終了する。
170                                 switch( this.現在のステージ.GetType().Name )
171                                 {
172                                         case nameof( ステージ.ステージ ):
173                                                 #region " 最初のダミーステージ "
174                                                 //----------------
175                                                 // 起動ステージへ遷移する。
176                                                 this.起動ステージ.活性化する( this.デバイスリソース );
177                                                 this.現在のステージ = this.起動ステージ;
178                                                 //----------------
179                                                 #endregion
180                                                 break;
181
182                                         case nameof( ステージ.起動.起動ステージ ):
183                                                 #region " 終了 → タイトルステージへ。"
184                                                 //---------------
185                                                 if( this.起動ステージ.現在のフェーズ == ステージ.起動.起動ステージ.フェーズ.終了 )
186                                                 {
187                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
188                                                         this.現在のステージ = this.タイトルステージ;
189                                                         this.現在のステージ.活性化する( this.デバイスリソース );
190                                                 }
191                                                 //---------------
192                                                 #endregion
193                                                 break;
194
195                                         case nameof( ステージ.タイトル.タイトルステージ ):
196                                                 #region " 確定 → ログインステージへ。"
197                                                 //---------------
198                                                 if( this.タイトルステージ.現在のフェーズ == ステージ.タイトル.タイトルステージ.フェーズ.確定 )
199                                                 {
200                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
201                                                         this.現在のステージ = this.ログインステージ;
202                                                         this.現在のステージ.活性化する( this.デバイスリソース );
203                                                 }
204                                                 //---------------
205                                                 #endregion
206                                                 #region " キャンセル → アプリを終了する。"
207                                                 //---------------
208                                                 else if( this.タイトルステージ.現在のフェーズ == ステージ.タイトル.タイトルステージ.フェーズ.キャンセル )
209                                                 {
210                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
211                                                         this.現在のステージ = null;
212                                                         this.アプリを終了せよ = true;
213                                                 }
214                                                 //---------------
215                                                 #endregion
216                                                 break;
217
218                                         case nameof( ステージ.ログイン.ログインステージ ):
219                                                 #region " 確定 → ログイン処理を行って、選曲ステージへ。"
220                                                 //---------------
221                                                 if( this.ログインステージ.現在のフェーズ == ステージ.ログイン.ログインステージ.フェーズ.確定 )
222                                                 {
223                                                         // ログイン処理(1) ユーザリストに対してユーザを選択する。
224                                                         StrokeStyleT.ユーザ管理.ユーザを選択する( this.ログインステージ.ユーザインデックス );
225                                                         FDK.Log.Info( $"ユーザが選択されました。[{StrokeStyleT.ユーザ管理.現在選択されているユーザ.名前}]" );
226
227                                                         // ログイン処理(2) 選択ユーザの曲ツリーを構築する。
228                                                         StrokeStyleT.ユーザ管理.現在選択されているユーザ.曲を検索して曲ツリーを構築する();
229
230                                                         // ログイン処理(3) 構築した曲ツリーを曲リスト管理に登録する。
231                                                         StrokeStyleT.曲ツリー管理.現在の管理対象ツリー = StrokeStyleT.ユーザ管理.現在選択されているユーザ.曲ツリーのルートノード;
232
233                                                         // 選曲ステージへ。
234                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
235                                                         this.現在のステージ = this.選曲ステージ;
236                                                         this.現在のステージ.活性化する( this.デバイスリソース );
237                                                 }
238                                                 //---------------
239                                                 #endregion
240                                                 #region " キャンセル → タイトルステージへ。"
241                                                 //---------------
242                                                 else if( this.ログインステージ.現在のフェーズ == ステージ.ログイン.ログインステージ.フェーズ.キャンセル )
243                                                 {
244                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
245                                                         this.現在のステージ = this.タイトルステージ;
246                                                         this.現在のステージ.活性化する( this.デバイスリソース );
247                                                 }
248                                                 //---------------
249                                                 #endregion
250                                                 break;
251
252                                         case nameof( ステージ.選曲.選曲ステージ ):
253                                                 #region " 曲確定 → 曲読込ステージへ。"
254                                                 //---------------
255                                                 if( this.選曲ステージ.現在のフェーズ == ステージ.選曲.選曲ステージ.フェーズ.曲確定 )
256                                                 {
257                                                         // 曲ノードが選択されていることを確認。
258                                                         Trace.Assert( null != StrokeStyleT.曲ツリー管理.現在選択されているノード, "[バグあり] 選択曲が null です。" );
259                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
260                                                         this.現在のステージ = this.曲読込ステージ;
261                                                         this.現在のステージ.活性化する( this.デバイスリソース );
262                                                 }
263                                                 //---------------
264                                                 #endregion
265                                                 #region " キャンセル → アプリを終了する。"
266                                                 //---------------
267                                                 else if( this.選曲ステージ.現在のフェーズ == ステージ.選曲.選曲ステージ.フェーズ.キャンセル )
268                                                 {
269                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
270                                                         this.現在のステージ = null;
271                                                         this.アプリを終了せよ = true;
272                                                 }
273                                                 //---------------
274                                                 #endregion
275                                                 break;
276
277                                         case nameof( ステージ.曲読込.曲読込ステージ ):
278                                                 #region " 終了 → 演奏ステージへ。"
279                                                 //--------------------
280                                                 if( this.曲読込ステージ.現在のフェーズ == ステージ.曲読込.曲読込ステージ.フェーズ.終了 )
281                                                 {
282                                                         // 演奏スコアインスタンスが作成されていることを確認。
283                                                         Debug.Assert( null != StrokeStyleT.演奏スコア );
284
285                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
286                                                         this.現在のステージ = this.演奏ステージ;
287                                                         this.現在のステージ.活性化する( this.デバイスリソース );
288                                                 }
289                                                 //--------------------
290                                                 #endregion
291                                                 break;
292
293                                         case nameof( ステージ.演奏.演奏ステージ ):
294                                                 #region " 演奏終了 → 結果ステージへ。"
295                                                 //--------------------
296                                                 if( this.演奏ステージ.現在のフェーズ == ステージ.演奏.演奏ステージ.フェーズ.クリアor失敗 )
297                                                 {
298                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
299                                                         this.現在のステージ = this.結果ステージ;
300                                                         this.現在のステージ.活性化する( this.デバイスリソース );
301                                                 }
302                                                 //--------------------
303                                                 #endregion
304                                                 #region " キャンセル → 選曲ステージへ。"
305                                                 //--------------------
306                                                 if( this.演奏ステージ.現在のフェーズ == ステージ.演奏.演奏ステージ.フェーズ.キャンセル )
307                                                 {
308                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
309                                                         this.現在のステージ = this.選曲ステージ;
310                                                         this.現在のステージ.活性化する( this.デバイスリソース );
311                                                 }
312                                                 //--------------------
313                                                 #endregion
314                                                 break;
315
316                                         case nameof( ステージ.結果.結果ステージ ):
317                                                 #region " 終了 → 選曲ステージへ。"
318                                                 //--------------------
319                                                 if( this.結果ステージ.現在のフェーズ == ステージ.結果.結果ステージ.フェーズ.終了 )
320                                                 {
321                                                         this.現在のステージ.非活性化する( this.デバイスリソース );
322                                                         this.現在のステージ = this.選曲ステージ;
323                                                         this.現在のステージ.活性化する( this.デバイスリソース );
324                                                 }
325                                                 //--------------------
326                                                 #endregion
327                                                 break;
328                                 }
329                         }
330                 }
331                 protected override void OnSizeChanged( EventArgs e )
332                 {
333                         // すべてのサイズ依存リソースを解放する。
334                         this.現在のステージ?.非活性化する( this.デバイスリソース );
335
336                         // デバイス等の変更処理を行う。
337                         base.OnSizeChanged( e );
338
339                         // すべてのサイズ依存リソースを作成し直す。
340                         this.現在のステージ?.活性化する( this.デバイスリソース );
341                 }
342
343                 // 各ステージの、唯一のインスタンス。
344                 protected SST.ステージ.ステージ 最初のダミーステージ = new ステージ.ステージ();
345                 protected SST.ステージ.起動.起動ステージ 起動ステージ = new ステージ.起動.起動ステージ();
346                 protected SST.ステージ.タイトル.タイトルステージ タイトルステージ = new ステージ.タイトル.タイトルステージ();
347                 protected SST.ステージ.ログイン.ログインステージ ログインステージ = new ステージ.ログイン.ログインステージ();
348                 protected SST.ステージ.選曲.選曲ステージ 選曲ステージ = new ステージ.選曲.選曲ステージ();
349                 protected SST.ステージ.曲読込.曲読込ステージ 曲読込ステージ = new ステージ.曲読込.曲読込ステージ();
350                 protected SST.ステージ.演奏.演奏ステージ 演奏ステージ = new ステージ.演奏.演奏ステージ();
351                 protected SST.ステージ.結果.結果ステージ 結果ステージ = new ステージ.結果.結果ステージ();
352
353                 private SST.ステージ.ステージ 現在のステージ = null;
354
355                 #region " バックストア。"
356                 //----------------
357                 private static SST.フォルダ bs_フォルダ = null;
358                 private static FDK.入力.Keyboard bs_キーボード入力 = null;
359                 private static FDK.入力.MidiIn bs_MIDI入力 = null;
360                 private static FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice bs_Wasapiデバイス = null;
361                 private static readonly Random bs_乱数 = new Random( DateTime.Now.Millisecond );
362                 private static SST.ユーザ.ユーザ管理 bs_ユーザ管理 = null;
363                 private static SST.曲.曲ツリー管理 bs_曲ツリー管理 = null;
364                 //----------------
365                 #endregion
366
367                 #region " Win32 API "
368                 //-----------------
369                 [System.Runtime.InteropServices.DllImport( "winmm.dll", EntryPoint = "timeBeginPeriod" )]
370                 private static extern uint timeBeginPeriod( uint uMilliseconds );
371
372                 [System.Runtime.InteropServices.DllImport( "winmm.dll", EntryPoint = "timeEndPeriod" )]
373                 private static extern uint timeEndPeriod( uint uMilliseconds );
374                 //-----------------
375                 #endregion
376         }
377 }