Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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