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

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