Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk/FDK17プロジェクト/コード/04.グラフィック/CTexture.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 574 - (hide annotations) (download)
Fri Apr 26 15:19:46 2019 UTC (4 years, 11 months ago) by kairera0467
File size: 43058 byte(s)
#35379 テクスチャの致命的な解放漏れを修正
#xxxxx 曲選択画面で選択中の曲を移動した時に一部の変数が初期化されていなかったのを修正
#xxxxx DTXMania rev:693bf14b0d83efc770235c788117190d08a4e531の変更(CTextureの解放漏れログ)を取り込み
1 kairera0467 2 using System;
2     using System.Collections.Generic;
3     using System.Text;
4     using System.Drawing;
5     using System.Drawing.Imaging;
6     using System.IO;
7     using System.Diagnostics;
8     using SlimDX;
9     using SlimDX.Direct3D9;
10    
11     namespace FDK
12     {
13     public class CTexture : IDisposable
14     {
15     // プロパティ
16     public bool b加算合成
17     {
18     get;
19     set;
20     }
21     public float fZ軸中心回転
22     {
23     get;
24     set;
25     }
26     public int n透明度
27     {
28     get
29     {
30     return this._透明度;
31     }
32     set
33     {
34     if( value < 0 )
35     {
36     this._透明度 = 0;
37     }
38     else if( value > 0xff )
39     {
40     this._透明度 = 0xff;
41     }
42     else
43     {
44     this._透明度 = value;
45     }
46     }
47     }
48     public Size szテクスチャサイズ
49     {
50     get;
51     private set;
52     }
53     public Size sz画像サイズ
54     {
55     get;
56 kairera0467 430 protected set;
57 kairera0467 2 }
58     public Texture texture
59     {
60     get;
61     private set;
62     }
63     public Format Format
64     {
65     get;
66     protected set;
67     }
68     public Vector3 vc拡大縮小倍率;
69 kairera0467 574 public string filename;
70 kairera0467 2
71 kairera0467 193 // 画面が変わるたび以下のプロパティを設定し治すこと。
72 kairera0467 2
73 kairera0467 193 public static Size sz論理画面 = Size.Empty;
74     public static Size sz物理画面 = Size.Empty;
75     public static Rectangle rc物理画面描画領域 = Rectangle.Empty;
76     /// <summary>
77     /// <para>論理画面を1とする場合の物理画面の倍率。</para>
78     /// <para>論理値×画面比率=物理値。</para>
79     /// </summary>
80     public static float f画面比率 = 1.0f;
81 kairera0467 2
82     // コンストラクタ
83    
84     public CTexture()
85     {
86     this.sz画像サイズ = new Size( 0, 0 );
87     this.szテクスチャサイズ = new Size( 0, 0 );
88     this._透明度 = 0xff;
89     this.texture = null;
90 kairera0467 574 this.bSlimDXTextureDispose完了済み = true;
91 kairera0467 2 this.cvPositionColoredVertexies = null;
92     this.b加算合成 = false;
93     this.fZ軸中心回転 = 0f;
94     this.vc拡大縮小倍率 = new Vector3( 1f, 1f, 1f );
95 kairera0467 574 this.filename = ""; // DTXMania rev:693bf14b0d83efc770235c788117190d08a4e531
96 kairera0467 2 // this._txData = null;
97     }
98    
99     /// <summary>
100     /// <para>指定されたビットマップオブジェクトから Managed テクスチャを作成する。</para>
101     /// <para>テクスチャのサイズは、BITMAP画像のサイズ以上、かつ、D3D9デバイスで生成可能な最小のサイズに自動的に調節される。
102     /// その際、テクスチャの調節後のサイズにあわせた画像の拡大縮小は行わない。</para>
103     /// <para>その他、ミップマップ数は 1、Usage は None、Pool は Managed、イメージフィルタは Point、ミップマップフィルタは
104     /// None、カラーキーは 0xFFFFFFFF(完全なる黒を透過)になる。</para>
105     /// </summary>
106     /// <param name="device">Direct3D9 デバイス。</param>
107     /// <param name="bitmap">作成元のビットマップ。</param>
108     /// <param name="format">テクスチャのフォーマット。</param>
109     /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
110     public CTexture( Device device, Bitmap bitmap, Format format )
111     : this()
112     {
113     try
114     {
115     this.Format = format;
116     this.sz画像サイズ = new Size( bitmap.Width, bitmap.Height );
117     this.szテクスチャサイズ = this.t指定されたサイズを超えない最適なテクスチャサイズを返す( device, this.sz画像サイズ );
118     this.rc全画像 = new Rectangle( 0, 0, this.sz画像サイズ.Width, this.sz画像サイズ.Height );
119    
120     using( var stream = new MemoryStream() )
121     {
122     bitmap.Save( stream, ImageFormat.Bmp );
123     stream.Seek( 0L, SeekOrigin.Begin );
124     int colorKey = unchecked( (int) 0xFF000000 );
125     this.texture = Texture.FromStream( device, stream, this.szテクスチャサイズ.Width, this.szテクスチャサイズ.Height, 1, Usage.None, format, poolvar, Filter.Point, Filter.None, colorKey );
126 kairera0467 574 this.bSlimDXTextureDispose完了済み = false;
127 kairera0467 2 }
128     }
129     catch ( Exception e )
130     {
131     this.Dispose();
132     throw new CTextureCreateFailedException( "ビットマップからのテクスチャの生成に失敗しました。(" + e.Message + ")" );
133     }
134     }
135    
136     /// <summary>
137     /// <para>空の Managed テクスチャを作成する。</para>
138     /// <para>テクスチャのサイズは、指定された希望サイズ以上、かつ、D3D9デバイスで生成可能な最小のサイズに自動的に調節される。
139     /// その際、テクスチャの調節後のサイズにあわせた画像の拡大縮小は行わない。</para>
140     /// <para>テクスチャのテクセルデータは未初期化。(おそらくゴミデータが入ったまま。)</para>
141     /// <para>その他、ミップマップ数は 1、Usage は None、イメージフィルタは Point、ミップマップフィルタは None、
142     /// カラーキーは 0x00000000(透過しない)になる。</para>
143     /// </summary>
144     /// <param name="device">Direct3D9 デバイス。</param>
145     /// <param name="n幅">テクスチャの幅(希望値)。</param>
146     /// <param name="n高さ">テクスチャの高さ(希望値)。</param>
147     /// <param name="format">テクスチャのフォーマット。</param>
148     /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
149     public CTexture( Device device, int n, int n高さ, Format format )
150     : this( device, n, n高さ, format, Pool.Managed )
151     {
152     }
153    
154     /// <summary>
155     /// <para>指定された画像ファイルから Managed テクスチャを作成する。</para>
156     /// <para>利用可能な画像形式は、BMP, JPG, PNG, TGA, DDS, PPM, DIB, HDR, PFM のいずれか。</para>
157     /// </summary>
158     /// <param name="device">Direct3D9 デバイス。</param>
159     /// <param name="strファイル名">画像ファイル名。</param>
160     /// <param name="format">テクスチャのフォーマット。</param>
161     /// <param name="b黒を透過する">画像の黒(0xFFFFFFFF)を透過させるなら true。</param>
162     /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
163     public CTexture( Device device, string strファイル名, Format format, bool b黒を透過する )
164     : this( device, strファイル名, format, b黒を透過する, Pool.Managed )
165     {
166     }
167     public CTexture( Device device, byte[] txData, Format format, bool b黒を透過する )
168     : this( device, txData, format, b黒を透過する, Pool.Managed )
169     {
170     }
171     public CTexture( Device device, Bitmap bitmap, Format format, bool b黒を透過する )
172     : this( device, bitmap, format, b黒を透過する, Pool.Managed )
173     {
174     }
175    
176     /// <summary>
177     /// <para>空のテクスチャを作成する。</para>
178     /// <para>テクスチャのサイズは、指定された希望サイズ以上、かつ、D3D9デバイスで生成可能な最小のサイズに自動的に調節される。
179     /// その際、テクスチャの調節後のサイズにあわせた画像の拡大縮小は行わない。</para>
180     /// <para>テクスチャのテクセルデータは未初期化。(おそらくゴミデータが入ったまま。)</para>
181     /// <para>その他、ミップマップ数は 1、Usage は None、イメージフィルタは Point、ミップマップフィルタは None、
182     /// カラーキーは 0x00000000(透過しない)になる。</para>
183     /// </summary>
184     /// <param name="device">Direct3D9 デバイス。</param>
185     /// <param name="n幅">テクスチャの幅(希望値)。</param>
186     /// <param name="n高さ">テクスチャの高さ(希望値)。</param>
187     /// <param name="format">テクスチャのフォーマット。</param>
188     /// <param name="pool">テクスチャの管理方法。</param>
189     /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
190     public CTexture( Device device, int n, int n高さ, Format format, Pool pool )
191     : this( device, n, n高さ, format, pool, Usage.None )
192     {
193     }
194    
195     public CTexture( Device device, int n, int n高さ, Format format, Pool pool, Usage usage )
196     : this()
197     {
198     try
199     {
200     this.Format = format;
201     this.sz画像サイズ = new Size( n, n高さ );
202     this.szテクスチャサイズ = this.t指定されたサイズを超えない最適なテクスチャサイズを返す( device, this.sz画像サイズ );
203     this.rc全画像 = new Rectangle( 0, 0, this.sz画像サイズ.Width, this.sz画像サイズ.Height );
204    
205     using ( var bitmap = new Bitmap( 1, 1 ) )
206     {
207     using ( var graphics = Graphics.FromImage( bitmap ) )
208     {
209     graphics.FillRectangle( Brushes.Black, 0, 0, 1, 1 );
210     }
211     using ( var stream = new MemoryStream() )
212     {
213     bitmap.Save( stream, ImageFormat.Bmp );
214     stream.Seek( 0L, SeekOrigin.Begin );
215     #if TEST_Direct3D9Ex
216     pool = poolvar;
217     #endif
218     // 中で更にメモリ読み込みし直していて無駄なので、Streamを使うのは止めたいところ
219     this.texture = Texture.FromStream( device, stream, n, n高さ, 1, usage, format, pool, Filter.Point, Filter.None, 0 );
220 kairera0467 574 this.bSlimDXTextureDispose完了済み = false;
221 kairera0467 2 }
222     }
223     }
224     catch
225     {
226     this.Dispose();
227     throw new CTextureCreateFailedException( string.Format( "テクスチャの生成に失敗しました。\n({0}x{1}, {2})", n, n高さ, format ) );
228     }
229     }
230    
231     /// <summary>
232     /// <para>画像ファイルからテクスチャを生成する。</para>
233     /// <para>利用可能な画像形式は、BMP, JPG, PNG, TGA, DDS, PPM, DIB, HDR, PFM のいずれか。</para>
234     /// <para>テクスチャのサイズは、画像のサイズ以上、かつ、D3D9デバイスで生成可能な最小のサイズに自動的に調節される。
235     /// その際、テクスチャの調節後のサイズにあわせた画像の拡大縮小は行わない。</para>
236     /// <para>その他、ミップマップ数は 1、Usage は None、イメージフィルタは Point、ミップマップフィルタは None になる。</para>
237     /// </summary>
238     /// <param name="device">Direct3D9 デバイス。</param>
239     /// <param name="strファイル名">画像ファイル名。</param>
240     /// <param name="format">テクスチャのフォーマット。</param>
241     /// <param name="b黒を透過する">画像の黒(0xFFFFFFFF)を透過させるなら true。</param>
242     /// <param name="pool">テクスチャの管理方法。</param>
243     /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
244     public CTexture( Device device, string strファイル名, Format format, bool b黒を透過する, Pool pool )
245     : this()
246     {
247     MakeTexture( device, strファイル名, format, b黒を透過する, pool );
248     }
249     public void MakeTexture( Device device, string strファイル名, Format format, bool b黒を透過する, Pool pool )
250     {
251     if ( !File.Exists( strファイル名 ) ) // #27122 2012.1.13 from: ImageInformation では FileNotFound 例外は返ってこないので、ここで自分でチェックする。わかりやすいログのために。
252     throw new FileNotFoundException( string.Format( "ファイルが存在しません。\n[{0}]", strファイル名 ) );
253    
254     Byte[] _txData = File.ReadAllBytes( strファイル名 );
255 kairera0467 574 this.filename = Path.GetFileName( strファイル名 );
256 kairera0467 2 MakeTexture( device, _txData, format, b黒を透過する, pool );
257     }
258    
259     public CTexture( Device device, byte[] txData, Format format, bool b黒を透過する, Pool pool )
260     : this()
261     {
262     MakeTexture( device, txData, format, b黒を透過する, pool );
263     }
264     public void MakeTexture( Device device, byte[] txData, Format format, bool b黒を透過する, Pool pool )
265     {
266     try
267     {
268     var information = ImageInformation.FromMemory( txData );
269     this.Format = format;
270     this.sz画像サイズ = new Size( information.Width, information.Height );
271     this.rc全画像 = new Rectangle( 0, 0, this.sz画像サイズ.Width, this.sz画像サイズ.Height );
272     int colorKey = ( b黒を透過する ) ? unchecked( (int) 0xFF000000 ) : 0;
273     this.szテクスチャサイズ = this.t指定されたサイズを超えない最適なテクスチャサイズを返す( device, this.sz画像サイズ );
274     #if TEST_Direct3D9Ex
275     pool = poolvar;
276     #endif
277     // lock ( lockobj )
278     // {
279     //Trace.TraceInformation( "CTexture() start: " );
280     this.texture = Texture.FromMemory( device, txData, this.sz画像サイズ.Width, this.sz画像サイズ.Height, 1, Usage.None, format, pool, Filter.Point, Filter.None, colorKey );
281 kairera0467 574 this.bSlimDXTextureDispose完了済み = false;
282 kairera0467 2 //Trace.TraceInformation( "CTexture() end: " );
283     // }
284     }
285     catch
286     {
287     this.Dispose();
288     // throw new CTextureCreateFailedException( string.Format( "テクスチャの生成に失敗しました。\n{0}", strファイル名 ) );
289     throw new CTextureCreateFailedException( string.Format( "テクスチャの生成に失敗しました。\n" ) );
290     }
291     }
292    
293     public CTexture( Device device, Bitmap bitmap, Format format, bool b黒を透過する, Pool pool )
294     : this()
295     {
296     MakeTexture( device, bitmap, format, b黒を透過する, pool );
297     }
298     public void MakeTexture( Device device, Bitmap bitmap, Format format, bool b黒を透過する, Pool pool )
299     {
300     try
301     {
302     this.Format = format;
303     this.sz画像サイズ = new Size( bitmap.Width, bitmap.Height );
304     this.rc全画像 = new Rectangle( 0, 0, this.sz画像サイズ.Width, this.sz画像サイズ.Height );
305     int colorKey = ( b黒を透過する ) ? unchecked( (int) 0xFF000000 ) : 0;
306     this.szテクスチャサイズ = this.t指定されたサイズを超えない最適なテクスチャサイズを返す( device, this.sz画像サイズ );
307     #if TEST_Direct3D9Ex
308     pool = poolvar;
309     #endif
310     //Trace.TraceInformation( "CTExture() start: " );
311     unsafe // Bitmapの内部データ(a8r8g8b8)を自前でゴリゴリコピーする
312     {
313     int tw =
314     #if TEST_Direct3D9Ex
315     288; // 32の倍数にする(グラフによっては2のべき乗にしないとダメかも)
316     #else
317     this.sz画像サイズ.Width;
318     #endif
319     #if TEST_Direct3D9Ex
320     this.texture = new Texture( device, tw, this.sz画像サイズ.Height, 1, Usage.Dynamic, format, Pool.Default );
321     #else
322     this.texture = new Texture( device, this.sz画像サイズ.Width, this.sz画像サイズ.Height, 1, Usage.None, format, pool );
323     #endif
324     BitmapData srcBufData = bitmap.LockBits( new Rectangle( 0, 0, this.sz画像サイズ.Width, this.sz画像サイズ.Height ), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb );
325     DataRectangle destDataRectangle = texture.LockRectangle( 0, LockFlags.Discard ); // None
326     #if TEST_Direct3D9Ex
327     byte[] filldata = null;
328     if ( tw > this.sz画像サイズ.Width )
329     {
330     filldata = new byte[ (tw - this.sz画像サイズ.Width) * 4 ];
331     }
332     for ( int y = 0; y < this.sz画像サイズ.Height; y++ )
333     {
334     IntPtr src_scan0 = (IntPtr) ( (Int64) srcBufData.Scan0 + y * srcBufData.Stride );
335     destDataRectangle.Data.WriteRange( src_scan0, this.sz画像サイズ.Width * 4 );
336     if ( tw > this.sz画像サイズ.Width )
337     {
338     destDataRectangle.Data.WriteRange( filldata );
339     }
340     }
341     #else
342     IntPtr src_scan0 = (IntPtr) ( (Int64) srcBufData.Scan0 );
343     destDataRectangle.Data.WriteRange( src_scan0, this.sz画像サイズ.Width * 4 * this.sz画像サイズ.Height );
344     #endif
345     texture.UnlockRectangle( 0 );
346     bitmap.UnlockBits( srcBufData );
347 kairera0467 574 this.bSlimDXTextureDispose完了済み = false;
348 kairera0467 2 }
349     //Trace.TraceInformation( "CTExture() End: " );
350     }
351     catch
352     {
353     this.Dispose();
354     // throw new CTextureCreateFailedException( string.Format( "テクスチャの生成に失敗しました。\n{0}", strファイル名 ) );
355     throw new CTextureCreateFailedException( string.Format( "テクスチャの生成に失敗しました。\n" ) );
356     }
357     }
358     // メソッド
359    
360     /// <summary>
361     /// テクスチャを 2D 画像と見なして描画する。
362     /// </summary>
363     /// <param name="device">Direct3D9 デバイス。</param>
364     /// <param name="x">描画位置(テクスチャの左上位置の X 座標[dot])。</param>
365     /// <param name="y">描画位置(テクスチャの左上位置の Y 座標[dot])。</param>
366     public void t2D描画( Device device, int x, int y )
367     {
368     this.t2D描画( device, x, y, 1f, this.rc全画像 );
369     }
370     public void t2D描画( Device device, int x, int y, Rectangle rc画像内の描画領域 )
371     {
372     this.t2D描画( device, x, y, 1f, rc画像内の描画領域 );
373     }
374 kairera0467 153 public void t2D描画( Device device, float x, float y )
375     {
376     this.t2D描画( device, (int)x, (int)y, 1f, this.rc全画像 );
377     }
378     public void t2D描画( Device device, float x, float y, Rectangle rc画像内の描画領域 )
379     {
380     this.t2D描画( device, (int)x, (int)y, 1f, rc画像内の描画領域 );
381     }
382 kairera0467 2 public void t2D描画( Device device, int x, int y, float depth, Rectangle rc画像内の描画領域 )
383     {
384 kairera0467 153 if (this.texture == null)
385     return;
386 kairera0467 2
387     this.tレンダリングステトの設定( device );
388    
389     if( this.fZ軸中心回転 == 0f )
390     {
391     #region [ (A) 回転なし ]
392     //-----------------
393     float f補正値X = -0.5f; // -0.5 は座標とピクセルの誤差を吸収するための座標補正値。(MSDN参照)
394     float f補正値Y = -0.5f; //
395     float w = rc画像内の描画領域.Width;
396     float h = rc画像内の描画領域.Height;
397     float fU = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width );
398     float fU = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width );
399     float fV = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height );
400     float fV = ( (float) rc画像内の描画領域.Bottom ) / ( (float) this.szテクスチャサイズ.Height );
401     this.color4.Alpha = ( (float) this._透明度 ) / 255f;
402     int color = this.color4.ToArgb();
403    
404     if( this.cvTransformedColoredVertexies == null )
405     this.cvTransformedColoredVertexies = new TransformedColoredTexturedVertex[ 4 ];
406    
407     // #27122 2012.1.13 from: 以下、マネージドオブジェクト(=ガベージ)の量産を抑えるため、new は使わず、メンバに値を1つずつ直接上書きする。
408    
409     this.cvTransformedColoredVertexies[ 0 ].Position.X = x + f補正値X;
410     this.cvTransformedColoredVertexies[ 0 ].Position.Y = y + f補正値Y;
411     this.cvTransformedColoredVertexies[ 0 ].Position.Z = depth;
412     this.cvTransformedColoredVertexies[ 0 ].Position.W = 1.0f;
413     this.cvTransformedColoredVertexies[ 0 ].Color = color;
414     this.cvTransformedColoredVertexies[ 0 ].TextureCoordinates.X = fU;
415     this.cvTransformedColoredVertexies[ 0 ].TextureCoordinates.Y = fV;
416    
417     this.cvTransformedColoredVertexies[ 1 ].Position.X = ( x + ( w * this.vc拡大縮小倍率.X ) ) + f補正値X;
418     this.cvTransformedColoredVertexies[ 1 ].Position.Y = y + f補正値Y;
419     this.cvTransformedColoredVertexies[ 1 ].Position.Z = depth;
420     this.cvTransformedColoredVertexies[ 1 ].Position.W = 1.0f;
421     this.cvTransformedColoredVertexies[ 1 ].Color = color;
422     this.cvTransformedColoredVertexies[ 1 ].TextureCoordinates.X = fU;
423     this.cvTransformedColoredVertexies[ 1 ].TextureCoordinates.Y = fV;
424    
425     this.cvTransformedColoredVertexies[ 2 ].Position.X = x + f補正値X;
426     this.cvTransformedColoredVertexies[ 2 ].Position.Y = ( y + ( h * this.vc拡大縮小倍率.Y ) ) + f補正値Y;
427     this.cvTransformedColoredVertexies[ 2 ].Position.Z = depth;
428     this.cvTransformedColoredVertexies[ 2 ].Position.W = 1.0f;
429     this.cvTransformedColoredVertexies[ 2 ].Color = color;
430     this.cvTransformedColoredVertexies[ 2 ].TextureCoordinates.X = fU;
431     this.cvTransformedColoredVertexies[ 2 ].TextureCoordinates.Y = fV;
432    
433     this.cvTransformedColoredVertexies[ 3 ].Position.X = ( x + ( w * this.vc拡大縮小倍率.X ) ) + f補正値X;
434     this.cvTransformedColoredVertexies[ 3 ].Position.Y = ( y + ( h * this.vc拡大縮小倍率.Y ) ) + f補正値Y;
435     this.cvTransformedColoredVertexies[ 3 ].Position.Z = depth;
436     this.cvTransformedColoredVertexies[ 3 ].Position.W = 1.0f;
437     this.cvTransformedColoredVertexies[ 3 ].Color = color;
438     this.cvTransformedColoredVertexies[ 3 ].TextureCoordinates.X = fU;
439     this.cvTransformedColoredVertexies[ 3 ].TextureCoordinates.Y = fV;
440    
441     device.SetTexture( 0, this.texture );
442     device.VertexFormat = TransformedColoredTexturedVertex.Format;
443     device.DrawUserPrimitives( PrimitiveType.TriangleStrip, 0, 2, this.cvTransformedColoredVertexies );
444     //-----------------
445     #endregion
446     }
447     else
448     {
449     #region [ (B) 回転あり ]
450     //-----------------
451     float f補正値X = ( ( rc画像内の描画領域.Width % 2 ) == 0 ) ? -0.5f : 0f; // -0.5 は座標とピクセルの誤差を吸収するための座標補正値。(MSDN参照)
452     float f補正値Y = ( ( rc画像内の描画領域.Height % 2 ) == 0 ) ? -0.5f : 0f; // 3D(回転する)なら補正はいらない。
453     float f中央X = ( (float) rc画像内の描画領域.Width ) / 2f;
454     float f中央Y = ( (float) rc画像内の描画領域.Height ) / 2f;
455     float fU = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width );
456     float fU = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width );
457     float fV = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height );
458     float fV = ( (float) rc画像内の描画領域.Bottom ) / ( (float) this.szテクスチャサイズ.Height );
459     this.color4.Alpha = ( (float) this._透明度 ) / 255f;
460     int color = this.color4.ToArgb();
461    
462     if( this.cvPositionColoredVertexies == null )
463     this.cvPositionColoredVertexies = new PositionColoredTexturedVertex[ 4 ];
464    
465     // #27122 2012.1.13 from: 以下、マネージドオブジェクト(=ガベージ)の量産を抑えるため、new は使わず、メンバに値を1つずつ直接上書きする。
466    
467     this.cvPositionColoredVertexies[ 0 ].Position.X = -f中央X + f補正値X;
468     this.cvPositionColoredVertexies[ 0 ].Position.Y = f中央Y + f補正値Y;
469     this.cvPositionColoredVertexies[ 0 ].Position.Z = depth;
470     this.cvPositionColoredVertexies[ 0 ].Color = color;
471     this.cvPositionColoredVertexies[ 0 ].TextureCoordinates.X = fU;
472     this.cvPositionColoredVertexies[ 0 ].TextureCoordinates.Y = fV;
473    
474     this.cvPositionColoredVertexies[ 1 ].Position.X = f中央X + f補正値X;
475     this.cvPositionColoredVertexies[ 1 ].Position.Y = f中央Y + f補正値Y;
476     this.cvPositionColoredVertexies[ 1 ].Position.Z = depth;
477     this.cvPositionColoredVertexies[ 1 ].Color = color;
478     this.cvPositionColoredVertexies[ 1 ].TextureCoordinates.X = fU;
479     this.cvPositionColoredVertexies[ 1 ].TextureCoordinates.Y = fV;
480    
481     this.cvPositionColoredVertexies[ 2 ].Position.X = -f中央X + f補正値X;
482     this.cvPositionColoredVertexies[ 2 ].Position.Y = -f中央Y + f補正値Y;
483     this.cvPositionColoredVertexies[ 2 ].Position.Z = depth;
484     this.cvPositionColoredVertexies[ 2 ].Color = color;
485     this.cvPositionColoredVertexies[ 2 ].TextureCoordinates.X = fU;
486     this.cvPositionColoredVertexies[ 2 ].TextureCoordinates.Y = fV;
487    
488     this.cvPositionColoredVertexies[ 3 ].Position.X = f中央X + f補正値X;
489     this.cvPositionColoredVertexies[ 3 ].Position.Y = -f中央Y + f補正値Y;
490     this.cvPositionColoredVertexies[ 3 ].Position.Z = depth;
491     this.cvPositionColoredVertexies[ 3 ].Color = color;
492     this.cvPositionColoredVertexies[ 3 ].TextureCoordinates.X = fU;
493     this.cvPositionColoredVertexies[ 3 ].TextureCoordinates.Y = fV;
494    
495     int n描画領域内X = x + ( rc画像内の描画領域.Width / 2 );
496     int n描画領域内Y = y + ( rc画像内の描画領域.Height / 2 );
497     var vc3移動量 = new Vector3( n描画領域内X - ( ( (float) device.Viewport.Width ) / 2f ), -( n描画領域内Y - ( ( (float) device.Viewport.Height ) / 2f ) ), 0f );
498    
499     var matrix = Matrix.Identity * Matrix.Scaling( this.vc拡大縮小倍率 );
500     matrix *= Matrix.RotationZ( this.fZ軸中心回転 );
501     matrix *= Matrix.Translation( vc3移動量 );
502     device.SetTransform( TransformState.World, matrix );
503    
504     device.SetTexture( 0, this.texture );
505     device.VertexFormat = PositionColoredTexturedVertex.Format;
506     device.DrawUserPrimitives( PrimitiveType.TriangleStrip, 2, this.cvPositionColoredVertexies );
507     //-----------------
508     #endregion
509     }
510     }
511 kairera0467 67 public void t2D上下反転描画( Device device, int x, int y )
512 kairera0467 66 {
513     this.t2D上下反転描画( device, x, y, 1f, this.rc全画像 );
514     }
515     public void t2D上下反転描画( Device device, int x, int y, Rectangle rc画像内の描画領域 )
516     {
517     this.t2D上下反転描画( device, x, y, 1f, rc画像内の描画領域 );
518     }
519     public void t2D上下反転描画( Device device, int x, int y, float depth, Rectangle rc画像内の描画領域 )
520     {
521 kairera0467 147 if( this.texture == null )
522 kairera0467 66 throw new InvalidOperationException( "テクスチャは生成されていません。" );
523 kairera0467 2
524 kairera0467 66 this.tレンダリングステトの設定( device );
525    
526 kairera0467 67 float fx = x * CTexture.f画面比率 + CTexture.rc物理画面描画領域.X - 0.5f; // -0.5 は座標とピクセルの誤差を吸収するための座標補正値。(MSDN参照)
527     float fy = y * CTexture.f画面比率 + CTexture.rc物理画面描画領域.Y - 0.5f; //
528 kairera0467 66 float w = rc画像内の描画領域.Width * this.vc拡大縮小倍率.X * CTexture.f画面比率;
529     float h = rc画像内の描画領域.Height * this.vc拡大縮小倍率.Y * CTexture.f画面比率;
530     float fU = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width );
531     float fU = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width );
532     float fV = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height );
533     float fV = ( (float) rc画像内の描画領域.Bottom ) / ( (float) this.szテクスチャサイズ.Height );
534     this.color4.Alpha = ( (float) this._透明度 ) / 255f;
535     int color = this.color4.ToArgb();
536    
537 kairera0467 67 if( this.cvTransformedColoredVertexies == null )
538     this.cvTransformedColoredVertexies = new TransformedColoredTexturedVertex[ 4 ];
539    
540 kairera0467 66 // 以下、マネージドオブジェクトの量産を抑えるため new は使わない。
541    
542     this.cvTransformedColoredVertexies[ 0 ].TextureCoordinates.X = fU; // 左上 → 左下
543     this.cvTransformedColoredVertexies[ 0 ].TextureCoordinates.Y = fV;
544     this.cvTransformedColoredVertexies[ 0 ].Position.X = fx;
545     this.cvTransformedColoredVertexies[ 0 ].Position.Y = fy;
546     this.cvTransformedColoredVertexies[ 0 ].Position.Z = depth;
547     this.cvTransformedColoredVertexies[ 0 ].Position.W = 1.0f;
548     this.cvTransformedColoredVertexies[ 0 ].Color = color;
549    
550     this.cvTransformedColoredVertexies[ 1 ].TextureCoordinates.X = fU; // 右上 → 右下
551     this.cvTransformedColoredVertexies[ 1 ].TextureCoordinates.Y = fV;
552     this.cvTransformedColoredVertexies[ 1 ].Position.X = fx + w;
553     this.cvTransformedColoredVertexies[ 1 ].Position.Y = fy;
554     this.cvTransformedColoredVertexies[ 1 ].Position.Z = depth;
555     this.cvTransformedColoredVertexies[ 1 ].Position.W = 1.0f;
556     this.cvTransformedColoredVertexies[ 1 ].Color = color;
557    
558     this.cvTransformedColoredVertexies[ 2 ].TextureCoordinates.X = fU; // 左下 → 左上
559     this.cvTransformedColoredVertexies[ 2 ].TextureCoordinates.Y = fV;
560     this.cvTransformedColoredVertexies[ 2 ].Position.X = fx;
561     this.cvTransformedColoredVertexies[ 2 ].Position.Y = fy + h;
562     this.cvTransformedColoredVertexies[ 2 ].Position.Z = depth;
563     this.cvTransformedColoredVertexies[ 2 ].Position.W = 1.0f;
564     this.cvTransformedColoredVertexies[ 2 ].Color = color;
565    
566     this.cvTransformedColoredVertexies[ 3 ].TextureCoordinates.X = fU; // 右下 → 右上
567     this.cvTransformedColoredVertexies[ 3 ].TextureCoordinates.Y = fV;
568     this.cvTransformedColoredVertexies[ 3 ].Position.X = fx + w;
569     this.cvTransformedColoredVertexies[ 3 ].Position.Y = fy + h;
570     this.cvTransformedColoredVertexies[ 3 ].Position.Z = depth;
571     this.cvTransformedColoredVertexies[ 3 ].Position.W = 1.0f;
572     this.cvTransformedColoredVertexies[ 3 ].Color = color;
573    
574     device.SetTexture( 0, this.texture );
575     device.VertexFormat = TransformedColoredTexturedVertex.Format;
576     device.DrawUserPrimitives( PrimitiveType.TriangleStrip, 2, this.cvTransformedColoredVertexies );
577     }
578     public void t2D上下反転描画( Device device, Point pt )
579     {
580     this.t2D上下反転描画( device, pt.X, pt.Y, 1f, this.rc全画像 );
581     }
582     public void t2D上下反転描画( Device device, Point pt, Rectangle rc画像内の描画領域 )
583     {
584     this.t2D上下反転描画( device, pt.X, pt.Y, 1f, rc画像内の描画領域 );
585     }
586     public void t2D上下反転描画( Device device, Point pt, float depth, Rectangle rc画像内の描画領域 )
587     {
588     this.t2D上下反転描画( device, pt.X, pt.Y, depth, rc画像内の描画領域 );
589     }
590    
591 kairera0467 2 public static Vector3 t論理画面座標をワルド座標へ変換する(int x, int y)
592     {
593     return CTexture.t論理画面座標をワルド座標へ変換する(new Vector3((float)x, (float)y, 0f));
594     }
595     public static Vector3 t論理画面座標をワルド座標へ変換する(float x, float y)
596     {
597     return CTexture.t論理画面座標をワルド座標へ変換する(new Vector3(x, y, 0f));
598     }
599     public static Vector3 t論理画面座標をワルド座標へ変換する(Point pt論理画面座標)
600     {
601     return CTexture.t論理画面座標をワルド座標へ変換する(new Vector3(pt論理画面座標.X, pt論理画面座標.Y, 0.0f));
602     }
603     public static Vector3 t論理画面座標をワルド座標へ変換する(Vector2 v2論理画面座標)
604     {
605     return CTexture.t論理画面座標をワルド座標へ変換する(new Vector3(v2論理画面座標, 0f));
606     }
607     public static Vector3 t論理画面座標をワルド座標へ変換する(Vector3 v3論理画面座標)
608     {
609     return new Vector3(
610     (v3論理画面座標.X - (CTexture.sz論理画面.Width / 2.0f)) * CTexture.f画面比率,
611     (-(v3論理画面座標.Y - (CTexture.sz論理画面.Height / 2.0f)) * CTexture.f画面比率),
612     v3論理画面座標.Z);
613     }
614    
615     /// <summary>
616     /// テクスチャを 3D 画像と見なして描画する。
617     /// </summary>
618     public void t3D描画( Device device, Matrix mat )
619     {
620     this.t3D描画( device, mat, this.rc全画像 );
621     }
622     public void t3D描画( Device device, Matrix mat, Rectangle rc画像内の描画領域 )
623     {
624     if( this.texture == null )
625     return;
626    
627     float x = ( (float) rc画像内の描画領域.Width ) / 2f;
628     float y = ( (float) rc画像内の描画領域.Height ) / 2f;
629     float z = 0.0f;
630     float fU = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width );
631     float fU = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width );
632     float fV = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height );
633     float fV = ( (float) rc画像内の描画領域.Bottom ) / ( (float) this.szテクスチャサイズ.Height );
634     this.color4.Alpha = ( (float) this._透明度 ) / 255f;
635     int color = this.color4.ToArgb();
636    
637     if( this.cvPositionColoredVertexies == null )
638     this.cvPositionColoredVertexies = new PositionColoredTexturedVertex[ 4 ];
639    
640     // #27122 2012.1.13 from: 以下、マネージドオブジェクト(=ガベージ)の量産を抑えるため、new は使わず、メンバに値を1つずつ直接上書きする。
641    
642     this.cvPositionColoredVertexies[ 0 ].Position.X = -x;
643     this.cvPositionColoredVertexies[ 0 ].Position.Y = y;
644     this.cvPositionColoredVertexies[ 0 ].Position.Z = z;
645     this.cvPositionColoredVertexies[ 0 ].Color = color;
646     this.cvPositionColoredVertexies[ 0 ].TextureCoordinates.X = fU;
647     this.cvPositionColoredVertexies[ 0 ].TextureCoordinates.Y = fV;
648    
649     this.cvPositionColoredVertexies[ 1 ].Position.X = x;
650     this.cvPositionColoredVertexies[ 1 ].Position.Y = y;
651     this.cvPositionColoredVertexies[ 1 ].Position.Z = z;
652     this.cvPositionColoredVertexies[ 1 ].Color = color;
653     this.cvPositionColoredVertexies[ 1 ].TextureCoordinates.X = fU;
654     this.cvPositionColoredVertexies[ 1 ].TextureCoordinates.Y = fV;
655    
656     this.cvPositionColoredVertexies[ 2 ].Position.X = -x;
657     this.cvPositionColoredVertexies[ 2 ].Position.Y = -y;
658     this.cvPositionColoredVertexies[ 2 ].Position.Z = z;
659     this.cvPositionColoredVertexies[ 2 ].Color = color;
660     this.cvPositionColoredVertexies[ 2 ].TextureCoordinates.X = fU;
661     this.cvPositionColoredVertexies[ 2 ].TextureCoordinates.Y = fV;
662    
663     this.cvPositionColoredVertexies[ 3 ].Position.X = x;
664     this.cvPositionColoredVertexies[ 3 ].Position.Y = -y;
665     this.cvPositionColoredVertexies[ 3 ].Position.Z = z;
666     this.cvPositionColoredVertexies[ 3 ].Color = color;
667     this.cvPositionColoredVertexies[ 3 ].TextureCoordinates.X = fU;
668     this.cvPositionColoredVertexies[ 3 ].TextureCoordinates.Y = fV;
669    
670     this.tレンダリングステトの設定( device );
671    
672     device.SetTransform( TransformState.World, mat );
673     device.SetTexture( 0, this.texture );
674     device.VertexFormat = PositionColoredTexturedVertex.Format;
675     device.DrawUserPrimitives( PrimitiveType.TriangleStrip, 2, this.cvPositionColoredVertexies );
676     }
677    
678 kairera0467 193 public void t3D左上基準描画( Device device, Matrix mat )
679     {
680     this.t3D左上基準描画( device, mat, this.rc全画像 );
681     }
682     /// <summary>
683     /// ○覚書
684     /// SlimDX.Matrix mat = SlimDX.Matrix.Identity;
685     /// mat *= SlimDX.Matrix.Translation( x, y, z );
686     /// 「mat =」ではなく「mat *=」であることを忘れないこと。
687     /// </summary>
688     public void t3D左上基準描画( Device device, Matrix mat, Rectangle rc画像内の描画領域 )
689     {
690     //とりあえず補正値などは無し。にしても使う機会少なさそうだなー・・・・
691     if( this.texture == null )
692     return;
693    
694     float x = 0.0f;
695     float y = 0.0f;
696     float z = 0.0f;
697     float fU = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width );
698     float fU = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width );
699     float fV = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height );
700     float fV = ( (float) rc画像内の描画領域.Bottom ) / ( (float) this.szテクスチャサイズ.Height );
701     this.color4.Alpha = ( (float) this._透明度 ) / 255f;
702     int color = this.color4.ToArgb();
703    
704     if( this.cvPositionColoredVertexies == null )
705     this.cvPositionColoredVertexies = new PositionColoredTexturedVertex[ 4 ];
706    
707     // #27122 2012.1.13 from: 以下、マネージドオブジェクト(=ガベージ)の量産を抑えるため、new は使わず、メンバに値を1つずつ直接上書きする。
708    
709     this.cvPositionColoredVertexies[ 0 ].Position.X = -x;
710     this.cvPositionColoredVertexies[ 0 ].Position.Y = y;
711     this.cvPositionColoredVertexies[ 0 ].Position.Z = z;
712     this.cvPositionColoredVertexies[ 0 ].Color = color;
713     this.cvPositionColoredVertexies[ 0 ].TextureCoordinates.X = fU;
714     this.cvPositionColoredVertexies[ 0 ].TextureCoordinates.Y = fV;
715    
716     this.cvPositionColoredVertexies[ 1 ].Position.X = x;
717     this.cvPositionColoredVertexies[ 1 ].Position.Y = y;
718     this.cvPositionColoredVertexies[ 1 ].Position.Z = z;
719     this.cvPositionColoredVertexies[ 1 ].Color = color;
720     this.cvPositionColoredVertexies[ 1 ].TextureCoordinates.X = fU;
721     this.cvPositionColoredVertexies[ 1 ].TextureCoordinates.Y = fV;
722    
723     this.cvPositionColoredVertexies[ 2 ].Position.X = -x;
724     this.cvPositionColoredVertexies[ 2 ].Position.Y = -y;
725     this.cvPositionColoredVertexies[ 2 ].Position.Z = z;
726     this.cvPositionColoredVertexies[ 2 ].Color = color;
727     this.cvPositionColoredVertexies[ 2 ].TextureCoordinates.X = fU;
728     this.cvPositionColoredVertexies[ 2 ].TextureCoordinates.Y = fV;
729    
730     this.cvPositionColoredVertexies[ 3 ].Position.X = x;
731     this.cvPositionColoredVertexies[ 3 ].Position.Y = -y;
732     this.cvPositionColoredVertexies[ 3 ].Position.Z = z;
733     this.cvPositionColoredVertexies[ 3 ].Color = color;
734     this.cvPositionColoredVertexies[ 3 ].TextureCoordinates.X = fU;
735     this.cvPositionColoredVertexies[ 3 ].TextureCoordinates.Y = fV;
736    
737     this.tレンダリングステトの設定( device );
738    
739     device.SetTransform( TransformState.World, mat );
740     device.SetTexture( 0, this.texture );
741     device.VertexFormat = PositionColoredTexturedVertex.Format;
742     device.DrawUserPrimitives( PrimitiveType.TriangleStrip, 2, this.cvPositionColoredVertexies );
743     }
744    
745 kairera0467 153 #region [ IDisposable 実装 ]
746 kairera0467 2 //-----------------
747     public void Dispose()
748     {
749 kairera0467 574 this.Dispose(true);
750     GC.SuppressFinalize(this);
751     }
752     protected void Dispose(bool disposeManagedObjects)
753     {
754     if (this.bDispose完了済み)
755     return;
756    
757     if (disposeManagedObjects)
758 kairera0467 2 {
759 kairera0467 574 // (A) Managed リソースの解放
760     // テクスチャの破棄 (SharpDXのテクスチャは、SharpDX側で管理されるため、FDKからはmanagedリソースと見做す)
761     if (this.texture != null)
762 kairera0467 2 {
763     this.texture.Dispose();
764     this.texture = null;
765 kairera0467 574 this.bSlimDXTextureDispose完了済み = true;
766 kairera0467 2 }
767 kairera0467 574 }
768 kairera0467 2
769 kairera0467 574 // (B) Unamanaged リソースの解放
770    
771    
772     this.bDispose完了済み = true;
773     }
774     ~CTexture()
775     {
776     // ファイナライザの動作時にtextureのDisposeがされていない場合は、
777     // CTextureのDispose漏れと見做して警告をログ出力する
778     if (!this.bSlimDXTextureDispose完了済み)
779     {
780     Trace.TraceWarning("CTexture: Dispose漏れを検出しました。(Size=({0}, {1}), filename={2})", sz画像サイズ.Width, sz画像サイズ.Height, filename );
781 kairera0467 2 }
782 kairera0467 574 this.Dispose(false);
783 kairera0467 2 }
784     //-----------------
785     #endregion
786    
787    
788     // その他
789    
790     #region [ private ]
791     //-----------------
792     private int _透明度;
793 kairera0467 574 private bool bDispose完了済み, bSlimDXTextureDispose完了済み;
794 kairera0467 2 private PositionColoredTexturedVertex[] cvPositionColoredVertexies;
795 kairera0467 67 protected TransformedColoredTexturedVertex[] cvTransformedColoredVertexies = new TransformedColoredTexturedVertex[]
796     {
797     new TransformedColoredTexturedVertex(),
798     new TransformedColoredTexturedVertex(),
799     new TransformedColoredTexturedVertex(),
800     new TransformedColoredTexturedVertex(),
801     };
802 kairera0467 2 private const Pool poolvar = // 2011.4.25 yyagi
803     #if TEST_Direct3D9Ex
804     Pool.Default;
805     #else
806     Pool.Managed;
807     #endif
808     // byte[] _txData;
809     static object lockobj = new object();
810    
811     private void tレンダリングステトの設定( Device device )
812     {
813     if( this.b加算合成 )
814     {
815     device.SetRenderState( RenderState.AlphaBlendEnable, true );
816     device.SetRenderState( RenderState.SourceBlend, SlimDX.Direct3D9.Blend.SourceAlpha ); // 5
817     device.SetRenderState( RenderState.DestinationBlend, SlimDX.Direct3D9.Blend.One ); // 2
818     }
819     else
820     {
821     device.SetRenderState( RenderState.AlphaBlendEnable, true );
822     device.SetRenderState( RenderState.SourceBlend, SlimDX.Direct3D9.Blend.SourceAlpha ); // 5
823     device.SetRenderState( RenderState.DestinationBlend, SlimDX.Direct3D9.Blend.InverseSourceAlpha ); // 6
824     }
825     }
826     private Size t指定されたサイズを超えない最適なテクスチャサイズを返す( Device device, Size sz指定サイズ )
827     {
828     bool b条件付きでサイズは2の累乗でなくてもOK = ( device.Capabilities.TextureCaps & TextureCaps.NonPow2Conditional ) != 0;
829     bool bサイズは2の累乗でなければならない = ( device.Capabilities.TextureCaps & TextureCaps.Pow2 ) != 0;
830     bool b正方形でなければならない = ( device.Capabilities.TextureCaps & TextureCaps.SquareOnly ) != 0;
831     int n最大幅 = device.Capabilities.MaxTextureWidth;
832     int n最大高 = device.Capabilities.MaxTextureHeight;
833     var szサイズ = new Size( sz指定サイズ.Width, sz指定サイズ.Height );
834    
835     if( bサイズは2の累乗でなければならない && !b条件付きでサイズは2の累乗でなくてもOK )
836     {
837     // 幅を2の累乗にする
838     int n = 1;
839     do
840     {
841     n *= 2;
842     }
843     while( n <= sz指定サイズ.Width );
844     sz指定サイズ.Width = n;
845    
846     // 高さを2の累乗にする
847     n = 1;
848     do
849     {
850     n *= 2;
851     }
852     while( n <= sz指定サイズ.Height );
853     sz指定サイズ.Height = n;
854     }
855    
856     if( sz指定サイズ.Width > n最大幅 )
857     sz指定サイズ.Width = n最大幅;
858    
859     if( sz指定サイズ.Height > n最大高 )
860     sz指定サイズ.Height = n最大高;
861    
862     if( b正方形でなければならない )
863     {
864     if( szサイズ.Width > szサイズ.Height )
865     {
866     szサイズ.Height = szサイズ.Width;
867     }
868     else if( szサイズ.Width < szサイズ.Height )
869     {
870     szサイズ.Width = szサイズ.Height;
871     }
872     }
873    
874     return szサイズ;
875     }
876    
877    
878     // 2012.3.21 さらなる new の省略作戦
879    
880 kairera0467 430 protected Rectangle rc全画像; // テクスチャ作ったらあとは不変
881     protected Color4 color4 = new Color4( 1f, 1f, 1f, 1f ); // アルファ以外は不変
882 kairera0467 2 //-----------------
883     #endregion
884     }
885     }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26