Sprite でアニメーション

DirectX の Sprite を使ってアニメーションを行います。

プロジェクトの設定

  1. DirectX 3D で Form を表示 に習って、プロジェクトを作成します。
    次のファイルを格納して、プロジェクトに取り込んで下さい。
    /******************************************/
    /*★ 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();
                    }
                }
            }
        }
    }
    
  2. CreateDevice Class の領域の定義です。
    Device が DirectX の Device の定義です。
    Sprite が Sprite Object Class で device から生成します。
    texture が Texture Image Class で、ここに描画するイメージを読み込みます。
    width, height が画像の幅と高さです。
    sp_no が描画する Sprite の番号です。
        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
        
  3. Sprite を描画する Render() メソッドです。
    タイマを取得して tickCurrent に Sprite の番号を計算します。
    前回と同じ Sprite のときはパスします。
    From をクリアして sp_no の Sprite を描画します。
        // 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();
        }
        

【演習】

  1. このプログラムは girl.bmp の画像(128*216*7枚) を使うことを前提にしています。
    他の画像でもアニメーションできるようにプログラムして下さい。
    そのためには画像ファイル名と横方向と縦方向の Sprite の枚数が必要になります。
    下記の領域を修正すれば、他の画像でもアニメーション出来るようにプログラムを修正して下さい。
        const string  ImgFile = "c:\\data\\test\\girl.bmp";
        const int   WNum = 7;           //横方向の Sprite 枚数
        const int   HNum = 1;           //縦方向の Sprite 枚数
        

【NOTE】

sprite.Draw(texture, new Rectangle(sp_no*128, 0, 128, 216), Vector3.Empty, Vector3.Empty, unchecked((int)(0xFFFFFFFF))); の説明です。
Sprite を右クリックして「定義へ移動」を選択すると、メソッドの形式一覧が表示されます。
パラメータ 説明
texture 描画する texture
new Rectangle(sp_no*128,0,128,216) 送り側の矩形領域
Vector3.Empty 画像の中心座標
Vector3.Empty 描画する座標
0xFFFFFFFF Color 値(RGB)

超初心者のプログラム入門(C# Frame Work)