OSDN Git Service

文字列テクスチャクラスで文字列が表示されないバグを修正。
authorくまかみ工房 <kumakamikoubou@gmail.com>
Fri, 7 Oct 2016 01:09:03 +0000 (10:09 +0900)
committerくまかみ工房 <kumakamikoubou@gmail.com>
Fri, 7 Oct 2016 01:09:03 +0000 (10:09 +0900)
FDK24/Utilities.cs
FDK24/メディア/テクスチャ.cs
FDK24/メディア/デバイスリソース.cs
FDK24/メディア/文字列テクスチャ.cs
FDK24/メディア/文字列画像.cs

index 0b34847..3c437a3 100644 (file)
@@ -99,7 +99,7 @@ namespace FDK
                /// (参考: http://qiita.com/oguna/items/c516e09ee57d931892b6 )
                /// </remarks>
                public static SharpDX.Direct3D11.ShaderResourceView CreateShaderResourceViewFromFile(
-                       SharpDX.Direct3D11.Device d3dDevice, string 画像ファイルパス, out SharpDX.Size2F ビューのサイズ, out SharpDX.Direct3D11.Texture2D テクスチャ )
+                       SharpDX.Direct3D11.Device d3dDevice, string 画像ファイルパス, out SharpDX.Size2F ビューのサイズ, out SharpDX.Direct3D11.Texture2D テクスチャ, bool target = false )
                {
                        ビューのサイズ = new SharpDX.Size2F( 0, 0 );
                        var srv = (SharpDX.Direct3D11.ShaderResourceView) null;
@@ -112,7 +112,9 @@ namespace FDK
                                        var dataBox = new[] { new SharpDX.DataBox( ロック領域.Scan0, bitmap.Width * 4, bitmap.Height ) };
                                        var textureDesc = new SharpDX.Direct3D11.Texture2DDescription() {
                                                ArraySize = 1,
-                                               BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource,
+                                               BindFlags = ( target ) ?
+                                                       SharpDX.Direct3D11.BindFlags.RenderTarget | SharpDX.Direct3D11.BindFlags.ShaderResource :
+                                                       SharpDX.Direct3D11.BindFlags.ShaderResource,
                                                CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
                                                Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                                                Height = bitmap.Height,
@@ -140,11 +142,13 @@ namespace FDK
                /// 空のテクスチャとそのシェーダーリソースビューを作成し、返す。
                /// </summary>
                public static SharpDX.Direct3D11.ShaderResourceView CreateShaderResourceView(
-                       SharpDX.Direct3D11.Device d3dDevice, SharpDX.Size2 サイズdpx, out SharpDX.Direct3D11.Texture2D テクスチャ )
+                       SharpDX.Direct3D11.Device d3dDevice, SharpDX.Size2 サイズdpx, out SharpDX.Direct3D11.Texture2D テクスチャ, bool target = false )
                {
                        var textureDesc = new SharpDX.Direct3D11.Texture2DDescription() {
                                ArraySize = 1,
-                               BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource,
+                               BindFlags = (target) ? 
+                                       SharpDX.Direct3D11.BindFlags.RenderTarget | SharpDX.Direct3D11.BindFlags.ShaderResource :
+                                       SharpDX.Direct3D11.BindFlags.ShaderResource,
                                CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
                                Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                                Height = サイズdpx.Height,
index 4eb1b03..ec7dc81 100644 (file)
@@ -57,7 +57,7 @@ namespace FDK.メディア
                                else if( this.ユーザ指定サイズdpx != SharpDX.Size2.Empty )
                                {
                                        this.ShaderResourceView = FDK.Utilities.CreateShaderResourceView(
-                                               d3dDevice, this.ユーザ指定サイズdpx, out this.Texture );
+                                               d3dDevice, this.ユーザ指定サイズdpx, out this.Texture, target: true );
 
                                        this.ShaderResourceViewSize = new SharpDX.Size2F( this.ユーザ指定サイズdpx.Width, this.ユーザ指定サイズdpx.Height );
                                }
index eb206dd..1669c73 100644 (file)
@@ -430,6 +430,11 @@ namespace FDK.メディア
                                異常状態なのでアプリを終了せよ = true;
                        }
                }
+               public void D2DContextの設定をリセットする( SharpDX.Direct2D1.DeviceContext1 context1 )
+               {
+                       context1.Transform = SharpDX.Matrix3x2.Identity;
+                       context1.PrimitiveBlend = SharpDX.Direct2D1.PrimitiveBlend.SourceOver;
+               }
 
                private float dz( float 高さdpx, float 視野角deg )
                {
index a9e081c..7174b9c 100644 (file)
@@ -37,6 +37,18 @@ namespace FDK.メディア
                        // テクスチャを解放する。
                        base.Onデバイス依存リソースの解放( dr );
                }
+               public new void 進行描画する( デバイスリソース dr, SharpDX.Matrix ワールド行列変換, SharpDX.RectangleF? 転送元矩形 = null )
+               {
+                       if( this.表示文字列.Nullまたは空である() )
+                               return;
+
+                       // 表示文字列が変更されているなら、ここで表示ビットマップの再構築を行う。
+                       if( false == string.Equals( this.表示文字列, this.前回の表示文字列 ) )
+                               this.テクスチャとビットマップを作成する( dr );
+
+                       // テクスチャを描画する。
+                       base.進行描画する( dr, ワールド行列変換, 転送元矩形 );
+               }
 
                protected string 前回の表示文字列 = null;
                protected SharpDX.DirectWrite.TextFormat テキストフォーマット = null;
@@ -52,6 +64,9 @@ namespace FDK.メディア
                        // テクスチャとビットマップを解放する。
                        this.Onデバイス依存リソースの解放( dr ); // base. じゃなくて this.
 
+                       if( this.表示文字列.Nullまたは空である() )
+                               return;
+
                        #region " テキストフォーマット/レイアウトを作成し、表示ビットマップのサイズを計算し、ユーザ指定サイズdpx に格納する。"
                        //----------------
                        this.テキストフォーマット = new SharpDX.DirectWrite.TextFormat( dr.DWriteFactory, this.フォント名, this.フォントサイズpt ) {
@@ -62,14 +77,14 @@ namespace FDK.メディア
 
                        this.テキストレイアウト = new SharpDX.DirectWrite.TextLayout(
                                dr.DWriteFactory,
-                               this.表示文字列,
+                               this.表示文字列.Nullまたは空である() ? " " : this.表示文字列,
                                this.テキストフォーマット,
                                最大サイズdpx.Width,
                                最大サイズdpx.Height );
 
                        this.ユーザ指定サイズdpx = new SharpDX.Size2(
-                               (int) this.テキストレイアウト.Metrics.WidthIncludingTrailingWhitespace,
-                               (int) this.テキストレイアウト.Metrics.Height );
+                               (int) ( this.テキストレイアウト.Metrics.WidthIncludingTrailingWhitespace + 0.5f ),
+                               (int) ( this.テキストレイアウト.Metrics.Height + 0.5f ) );
                        //----------------
                        #endregion
 
@@ -78,12 +93,10 @@ namespace FDK.メディア
 
                        #region " 作成したテクスチャとデータを共有するビットマップを作成する。"
                        //----------------
-                       using( var dxgiSurface = this.Texture.QueryInterface<SharpDX.DXGI.Surface>() )
+                       using( var dxgiSurface = this.Texture.QueryInterfaceOrNull<SharpDX.DXGI.Surface1>() )
                        {
                                var bmpProp = new SharpDX.Direct2D1.BitmapProperties1() {
-                                       DpiX = dr.D2DContext1.DotsPerInch.Width,
-                                       DpiY = dr.D2DContext1.DotsPerInch.Height,
-                                       PixelFormat = new SharpDX.Direct2D1.PixelFormat( SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied ),
+                                       PixelFormat = new SharpDX.Direct2D1.PixelFormat( dxgiSurface.Description.Format, SharpDX.Direct2D1.AlphaMode.Premultiplied ),
                                        BitmapOptions = SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw,
                                };
                                this.ビットマップターゲット = new SharpDX.Direct2D1.Bitmap1( dr.D2DContext1, dxgiSurface, bmpProp );
@@ -92,31 +105,38 @@ namespace FDK.メディア
                        #endregion
                        #region " ビットマップに文字列を描画する。"
                        //----------------
-                       var 旧ターゲット = dr.D2DContext1.Target;
-                       try
-                       {
-                               dr.D2DContext1.Target = this.ビットマップターゲット;
-                               FDK.Utilities.D2DBatchDraw( dr.D2DContext1, () => {
-
-                                       dr.D2DContext1.Clear( SharpDX.Color.Transparent );
-
-                                       dr.D2DContext1.DrawTextLayout(    // ドロップシャドウ部
-                                               new SharpDX.Vector2( 1.0f, 1.0f ),
-                                               this.テキストレイアウト,
-                                               this.黒ブラシ,
-                                               SharpDX.Direct2D1.DrawTextOptions.Clip );
-
-                                       dr.D2DContext1.DrawTextLayout(    // 本体部
-                                               new SharpDX.Vector2( 0.0f, 0.0f ),
-                                               this.テキストレイアウト,
-                                               this.白ブラシ,
-                                               SharpDX.Direct2D1.DrawTextOptions.Clip );
-
-                               } );
-                       }
-                       finally
+                       using( var 旧ターゲット = dr.D2DContext1.Target )
                        {
-                               dr.D2DContext1.Target = 旧ターゲット;
+                               try
+                               {
+                                       this.白ブラシ = new SharpDX.Direct2D1.SolidColorBrush( dr.D2DContext1, SharpDX.Color.LightGray );
+                                       this.黒ブラシ = new SharpDX.Direct2D1.SolidColorBrush( dr.D2DContext1, SharpDX.Color.Black );
+
+                                       dr.D2DContextの設定をリセットする( dr.D2DContext1 );
+                                       dr.D2DContext1.Target = this.ビットマップターゲット;
+
+                                       FDK.Utilities.D2DBatchDraw( dr.D2DContext1, () => {
+
+                                               dr.D2DContext1.Clear( SharpDX.Color.Transparent );
+
+                                               dr.D2DContext1.DrawTextLayout(    // ドロップシャドウ部
+                                                       new SharpDX.Vector2( 1.0f, 1.0f ),
+                                                       this.テキストレイアウト,
+                                                       黒ブラシ,
+                                                       SharpDX.Direct2D1.DrawTextOptions.Clip );
+
+                                               dr.D2DContext1.DrawTextLayout(    // 本体部
+                                                       new SharpDX.Vector2( 0.0f, 0.0f ),
+                                                       this.テキストレイアウト,
+                                                       白ブラシ,
+                                                       SharpDX.Direct2D1.DrawTextOptions.Clip );
+
+                                       } );
+                               }
+                               finally
+                               {
+                                       dr.D2DContext1.Target = 旧ターゲット;
+                               }
                        }
                        //----------------
                        #endregion
index 0cf1388..8cb4ac7 100644 (file)
@@ -90,6 +90,8 @@ namespace FDK.メディア
 
                        Utilities.D2DBatchDraw( dr.D2DContext1, () => {
 
+                               dr.D2DContextの設定をリセットする( dr.D2DContext1 );
+
                                // 変換行列とブレンドモードをD2Dレンダーターゲットに設定する。
                                dr.D2DContext1.Transform = 変換行列2Dpx ?? SharpDX.Matrix3x2.Identity;
                                dr.D2DContext1.PrimitiveBlend = ( 加算合成 ) ? SharpDX.Direct2D1.PrimitiveBlend.Add : SharpDX.Direct2D1.PrimitiveBlend.SourceOver;