Sprite で画像を描画する

DirectX の Sprite を使って画像ファイルを描画します。
Sprite は DirectX9 から3Dに統合された2Dの描画機能です。

プロジェクトの設定

  1. 空のプロジェクトを作成して、ソースプログラムをプロジェクトに取り込んで下さい。
    詳細は DirectX 3D で Form を表示 を参照して下さい。
    前回作成したプロジェクトをフォルダーごとコピーして、フォルダー名を変更すると便利です。
    画像は適当なものを調達して下さい。
    BMP でも JPEG でもOKですが、GIF は使えません。
    /*****************************************/
    /*★ Sprite で Image を表示    前田 稔 ★*/
    /*****************************************/
    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
    
            // CreateDevice の Constructor
            public CreateDevice()
            {
                // caption の設定
                this.Text = "Sprite Image File";
            }
    
            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);
                    texture = TextureLoader.FromFile(device, "c:\\data\\test\\kishi.jpg");
                    return true;
                }
                catch (DirectXException)
                {
                    return false; 
                }
            }
    
            // Frame の描画メソッド
            private void Render()
            {
                if (device == null) return;
    
                //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.None);
                sprite.Draw(texture, Vector3.Empty, Vector3.Empty, 0xffffff);
                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. Main() メソッドから呼ばれる CreateDevice Class です。
    device が DirectX の Device の定義です。
    Sprite が Sprite Object Class で device から生成します。
    texture が Texture Image Class で、ここに描画するイメージを読み込みます。
    CreateDevice Class の Constructor ではキャプションを設定するだけです。
        public class CreateDevice : Form
        {
            Device  device = null;          // Direct Device の定義
            Sprite  sprite = null;          // Sprite Object Class
            Texture texture;                // Texture Image
    
            // CreateDevice の Constructor
            public CreateDevice()
            {
                // caption の設定
                this.Text = "Sprite Image File";
            }
        
  3. Main() から呼ばれる DirectX Sprite の初期化を行う InitializeGraphics() です。
    new Device() で DirectX の Device を取得します。
    device が取得出来たら、続いて sprite を生成します。
    FromFile() で texture に画像を入力します。
    "c:\\data\\kishi.jpg" が入力する画像ファイルの名前です。
    画像は2のベキ乗(64,128,256, ...) にサイズが調整されて格納されます。
    そのままのサイズで入力する方法は、このサイトの後のページで説明しています。
    try{ } と catch (DirectXException){ } で、実行に失敗したときは false; をリターンします。
    これが無いと失敗したときにエラーが表示されて異常終了します。
        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);
                texture = TextureLoader.FromFile(device, "c:\\data\\kishi.jpg");
                return true;
            }
            catch (DirectXException)
            {
                return false; 
            }
        }
        
  4. Frame に描画するメソッドです。
    Clear() メソッドで Frame を Blue でクリアします。
    sprite.Draw() で texture に格納した画像を描画します。
        // Frame の描画メソッド
        private void Render()
        {
            if (device == null) return;
    
            //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.None);
            sprite.Draw(texture, Vector3.Empty, Vector3.Empty, 0xffffff);
            sprite.End();
    
            //End the scene
            device.EndScene();
            device.Present();
        }
        

【演習】

  1. Color 値の意味を理解するために、次の値に設定して描画してみて下さい。
    0x808080
    0x404040
    0x00FF00
  2. 描画する座標を次のように設定して描画してみて下さい。
    new Rectangle(10, 30, 100, 60) は送り側の矩形領域です。
    sprite.Draw(texture, Vector3.Empty, new Vector3(80,60,0), 0xffffff);
    sprite.Draw(texture, new Vector3(40,20,0), Vector3.Empty, 0xffffff);
    sprite.Draw(texture, new Rectangle(10, 30, 100, 60), Vector3.Empty, new Vector3(80, 60, 0), 0xffffff);
  3. 中心座標は画像を回転したときなどの中心になる座標で、規定値は 0,0 です。
    描画する座標は、画像の中心座標の位置で、中心座標が変わると描画される位置も変わります。

【NOTE】

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

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