/******************************************/
/*★ Sprite でアニメーション 前田 稔 ★*/
/******************************************/
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace DeviceTutorial
{
public class CreateDevice : Form
{
Device device = null; // Direct Device の定義
Sprite sprite = null; // Sprite Object Class
Texture texture; // Texture Image
string ImgFile = "c:\\data\\test\\girl.bmp";
int width, height;
int sp_no = 0; // Sprite Number
// CreateDevice の Constructor
public CreateDevice()
{
this.ClientSize = new System.Drawing.Size(256,256);
this.Text = "Sprite Image Size";
}
public bool InitializeGraphics()
{
try
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed=true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
sprite = new Sprite(device);
//Image File をロードしてサイズを取得
Bitmap bmp= new Bitmap(ImgFile);
width = bmp.Width;
height = bmp.Height;
bmp.Dispose();
Console.WriteLine("Width={0}, Height={1}", width, height);
ClientSize = new System.Drawing.Size(width/7, height);
texture = TextureLoader.FromFile(device, ImgFile, width, height, 0,
Usage.None, Format.A8R8G8B8, Pool.Managed, Filter.None, Filter.None, unchecked((int)0xFF000000));
return true;
}
catch (DirectXException)
{
return false;
}
}
// Frame の描画メソッド
private void Render()
{
if (device == null) return;
int tickCurrent = (Environment.TickCount / 200) % 7;
if (tickCurrent == sp_no) return;
sp_no = tickCurrent;
//Clear the backbuffer to a blue color
device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
//Begin the scene
device.BeginScene();
sprite.Begin(SpriteFlags.AlphaBlend);
sprite.Draw(texture, new Rectangle(sp_no*128, 0, 128, 216), Vector3.Empty, Vector3.Empty, unchecked((int)(0xFFFFFFFF)));
sprite.End();
//End the scene
device.EndScene();
device.Present();
}
// Window の描画 OnPaint() をオーバーロード
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
this.Render(); // Render on painting
}
// OnKeyPress() をオーバーロード
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)
this.Close(); // Esc was pressed
}
//☆ Main() メソッド
static void Main()
{
// using で資源の解放を確実に行う
using (CreateDevice frm = new CreateDevice())
{
if (!frm.InitializeGraphics()) // Initialize Direct3D
{
MessageBox.Show("Could not initialize Direct3D");
return;
}
frm.Show();
// メッセージループ
while(frm.Created)
{
frm.Render();
Application.DoEvents();
}
}
}
}
}
|