Textureが表示できない
Posted: 2009年1月21日(水) 21:24
はじめまして
C#のManagedDirectXですがアドバイスいただければと思い質問させてください
シューティングゲームを作りたいと思い
最初はSpriteで描画していました。
速度的な問題がありDevice描画に変更しようと思い
ついでにその描画方法がどれほどの速度が出るか調べたかったのでベンチマークテストを作ろうと思ったのですが…
何故か(自分のせいなのは分かっています)テクスチャが描画されません。
(もしかしたら変なところに描画されているのかも…)
色々なサイトのソースを試したのですが…
関係ありそうなコードだけ下に(その他はFPS描画などです。)
どこかおかしいところ(修正するところ)はありませんか。
ここのFPS計算のソースもそのまま入ってますが
VC# XPです。
C#のManagedDirectXですがアドバイスいただければと思い質問させてください
シューティングゲームを作りたいと思い
最初はSpriteで描画していました。
速度的な問題がありDevice描画に変更しようと思い
ついでにその描画方法がどれほどの速度が出るか調べたかったのでベンチマークテストを作ろうと思ったのですが…
何故か(自分のせいなのは分かっています)テクスチャが描画されません。
(もしかしたら変なところに描画されているのかも…)
色々なサイトのソースを試したのですが…
関係ありそうなコードだけ下に(その他はFPS描画などです。)
どこかおかしいところ(修正するところ)はありませんか。
ここのFPS計算のソースもそのまま入ってますが
VC# XPです。
class Loop { Device device; Texture tex; //終了させて良いか?というフラグ bool Cont = true; public Bitmap bmp; int width, height; public Thread th; public Loop(Device device) { this.device = device; bmp = new Bitmap(@"C:\enemy.png"); SetTexture(out tex, bmp); //テクスチャ初期設定[ vert = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, device, Usage.None, CustomVertex.PositionTextured.Format, Pool.Managed); verts = new CustomVertex.PositionTextured[4]; th = new Thread(Run); th.Start(); } //Texture描画 void DrawTexture(Texture tex,int x,int y) { verts[0] = new CustomVertex.PositionTextured(x - width / 2, y - height / 2, 0.0f, 0.0f, 0.0f); verts[1] = new CustomVertex.PositionTextured(x + width / 2, y - height / 2, 0.0f, 1.0f, 0.0f); verts[2] = new CustomVertex.PositionTextured(x - width / 2, y + height / 2, 0.0f, 0.0f, 1.0f); verts[3] = new CustomVertex.PositionTextured(x + width / 2, y + height / 2, 0.0f, 1.0f, 1.0f); device.SetTexture(0, tex); DrawRectPrim(verts); } void DrawRectPrim(CustomVertex.PositionTextured[/url] verts) { if (device == null) return; GraphicsStream stm = vert.Lock(0, 0, 0); stm.Write(verts); vert.Unlock(); device.SetStreamSource(0,vert,0); device.VertexFormat = CustomVertex.PositionTextured.Format; device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); } public void Run() { while(true) { //デバイス消失の確認(省略) //ループ // 描画内容を単色でクリア this.device.Clear(ClearFlags.Target, Color.White, 1.0f, 0); device.BeginScene(); DrawTexture(tex, 200, 200); device.EndScene(); device.Present(); Sleep(); } } void Sleep() { //省略 } /// <summary> /// テクスチャ生成 /// </summary> /// <param name="img">貼り付けるBitMap</param> protected void SetTexture(out Texture tex,Bitmap img) { int w = 1, h = 1; width = img.Width; height = img.Height; while (w < width) { w = w * 2; } while (h < height) { h = h * 2; } width = w; height = h; //サイズを考慮したbitmapへ~ Bitmap bmp = new Bitmap(w, h); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.Transparent); g.DrawImage(img, 0, 0); g.Dispose(); tex = new Texture(device, w, h, 0, Usage.Dynamic, Format.A8R8G8B8, Pool.Default); SurfaceDescription desc = tex.GetLevelDescription(0); UInt32[,] buf = (UInt32[,])tex.LockRectangle(typeof(UInt32), 0,LockFlags.None, desc.Height, desc.Width); //Bitmap→byte BitmapData bmpdata = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite,PixelFormat.Format32bppArgb); IntPtr ptr = bmpdata.Scan0; byte[/url] temp = new byte[bmpdata.Width * bmpdata.Height * 4]; //ptr→tempコピー Marshal.Copy(ptr, temp, 0, bmpdata.Width * bmpdata.Height * 4); //アンロック bmp.UnlockBits(bmpdata); for (int i = 0; i < bmpdata.Height; i++) { for (int j = 0; j < bmp.Width; j++) { //byte*4→UInt32 //縦横逆にする buf[i, j] = (UInt32)temp[i * bmpdata.Width * 4 + j * 4] + (UInt32)temp[i * bmpdata.Width * 4 + j * 4 + 1] * 256 + (UInt32)temp[i * bmpdata.Width * 4 + j * 4 + 2] * 256 * 256 + (UInt32)temp[i * bmpdata.Width * 4 + j * 4 + 3] * 256 * 256 * 256; } } tex.UnlockRectangle(0); }