方法 : スプライトを使用します。

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

図面のイメージと画面にテキスト スプライトを使えます。 この例では描画とレンダリングを示します。

このコード例フォームが、次のオブジェクト。

  • Device オブジェクトを返します。

  • Direct3D Font オブジェクト。

  • Texture オブジェクトを返します。

  • Sprite オブジェクトを返します。

このコード例は、スプライトを作成するファイルからテクスチャをロードします。 デバイスまたはエミュレーターに配置するプロジェクトに、小さいビットマップを追加する必要があります。

フォームのコンストラクターでは、デバイスの PresentationParameters プロパティの設定を指定 Device オブジェクトを作成し、デバイスの Reset メソッドを呼び出します。 Font オブジェクトを構築してもします。

次の表は、スプライトをレンダリングするサンプル メソッドです。

注意

マネージの Direct3D モバイル アプリケーションが、Pocket PC やスマートフォン Windows Mobile 5. 0 ソフトウェアを必要です。.NET の外部のリソース フレームワークを最適化します。 Windows Mobile ソフトウェアおよび SDK についてを参照してください。

メソッド

アクション

OnDeviceReset

  • ビットマップ ファイルからテクスチャをロードします。

OnPaint

  1. シーンを開始します。

  2. SpriteFlags フラグを指定します。 「堅牢なプログラミング」を参照このトピックの詳細で後述する「。

  3. 画面に、スプライトとテキストを描画します。

  4. シーンを終了します。

使用例

完全な形を次のコード例に示します。 指定されたビットマップを使用して、スプライトを描画します。

                        Class Sprites
    Inherits Form
    ' The objects that will be used to show    ' the uses of the Sprite classPrivate device As Device
    Private d3dFont As Microsoft.WindowsMobile.DirectX.Direct3D.Font
    Private sprite As Sprite
    Private texture As Texture


    PublicSubNew() 
        Dim present As PresentParameters
        Dim gdiFont As System.Drawing.Font

        Me.Text = "Using Sprites"
        ' Give the application a way to be closed.        ' This must be done before the device is created        ' as it will cause the hwnd of the Form to change.Me.MinimizeBox = False

        present = New PresentParameters()
        present.Windowed = True
        present.SwapEffect = SwapEffect.Discard

        device = New Device(0, DeviceType.Default, Me, CreateFlags.None, present)
        AddHandler device.DeviceReset, AddressOf OnDeviceReset

        ' Construct a new Sprite.        ' Sprites do not need to be recreated        ' when a device is reset.
        sprite = New Sprite(device)

        gdiFont = New System.Drawing.Font(FontFamily.GenericSansSerif, 10F, FontStyle.Regular)

        ' Construct a new font. Fonts do not need        ' to be recreated when a device is reset.
        d3dFont = New Microsoft.WindowsMobile.DirectX.Direct3D.Font(device, gdiFont)

        OnDeviceReset(Nothing, EventArgs.Empty)

    EndSubPrivateSub OnDeviceReset(ByVal sender AsObject, ByVal e As EventArgs) 
        ' Textures must be recreated whenever a device is reset        ' no matter what pool they are created in.
        texture = TextureLoader.FromFile(device, "image.bmp")

    EndSubProtectedOverridesSub OnPaintBackground(ByVal e As PaintEventArgs) 
        ' Do nothing.EndSubProtectedOverridesSub OnPaint(ByVal e As PaintEventArgs)
        ' Begin the scene and clear the back buffer to black
        device.BeginScene()
        device.Clear(ClearFlags.Target, Color.Black, 1.0F, 0)

        ' When using sprites it is important to        ' specify sprite flags passed to Sprite.Begin
        sprite.Begin(SpriteFlags.SortTexture Or SpriteFlags.AlphaBlend)

        ' Draw an image to the screen using Sprite.DrawDim spriteY AsInteger = 5

        sprite.Draw(texture, Vector3.Empty, New Vector3(0, spriteY, 0), Color.White.ToArgb())
        spriteY += texture.GetLevelDescription(0).Height + 5

        ' Draw a portion of an image to the screen        ' using Sprite.Draw. This shall be drawn such        ' that the image is modulated with the color green.
        sprite.Draw(texture, New Rectangle(4, 4, 24, 24), Vector3.Empty, New Vector3(0, spriteY, 0), Color.Green)

        spriteY += 30

        ' Draw text to the screen. Using a sprite to draw text        ' to the  screen is essential for good performance.        ' Otherwise the font object will perform a        ' Sprite.Begin/Sprite.End internally for        ' each call to Font.DrawText. This can cause severe        ' performance problems.
        spriteY = 150

        d3dFont.DrawText(sprite, "This is text.", 5, spriteY, Color.Red)
        spriteY += d3dFont.Description.Height + 5

        d3dFont.DrawText(sprite, "This is another line of text.", 5, spriteY, Color.Green)
        spriteY += d3dFont.Description.Height + 5

        d3dFont.DrawText(sprite, "Only one call to Sprite.Begin.", 5, spriteY, Color.Blue)

        ' End drawing using this sprite. This will cause the        ' sprites to be flushed to the graphics driver and will        ' reset the transformation matrices, textures states,        ' and renderstates if the SpriteFlags specified in Begin        ' call for that to happen.
        sprite.End()

        ' Finish the scene and present it on the screen.
        device.EndScene()
        device.Present()

    EndSubSharedSub Main() 
        Application.Run(New Sprites())

    EndSubEndClass
                        class Sprites : Form
{
    // The objects that will be used to show// the uses of the Sprite classprivate Device device;
    private Microsoft.WindowsMobile.DirectX.Direct3D.Font d3dFont;
    private Sprite sprite;
    private Texture texture;

    public Sprites()
    {
        PresentParameters present;
        System.Drawing.Font gdiFont;

        this.Text = "Using Sprites";

        // Give the application a way to be closed.// This must be done before the device is created// as it will cause the hwnd of the Form to change.this.MinimizeBox = false;

        present = new PresentParameters();
        present.Windowed = true;
        present.SwapEffect = SwapEffect.Discard;

        device = new Device(0, DeviceType.Default, this,
            CreateFlags.None, present);
        device.DeviceReset += new EventHandler(OnDeviceReset);

        // Construct a new Sprite.// Sprites do not need to be recreated// when a device is reset.
        sprite = new Sprite(device);

        gdiFont = new System.Drawing.Font
            (FontFamily.GenericSansSerif,
        10.0f, FontStyle.Regular);

        // Construct a new font. Fonts do not need// to be recreated when a device is reset.
        d3dFont= new Microsoft.WindowsMobile.DirectX.Direct3D.Font
            (device, gdiFont);

        OnDeviceReset(null, EventArgs.Empty);
    }

    privatevoid OnDeviceReset(object sender, EventArgs e)
    {
        // Textures must be recreated whenever a device is reset// no matter what pool they are created in.
        texture = TextureLoader.FromFile(device, "image.bmp");
    }

    protectedoverridevoid OnPaintBackground(PaintEventArgs e)
    {
        // Do nothing.
    }

    protectedoverridevoid OnPaint(PaintEventArgs e)
    {
        // Begin the scene and clear the back buffer to black
        device.BeginScene();
        device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);

        // When using sprites it is important to// specify sprite flags passed to Sprite.Begin

        sprite.Begin(SpriteFlags.SortTexture | SpriteFlags.AlphaBlend);

        // Draw an image to the screen using Sprite.Drawint spriteY = 5;

        sprite.Draw(texture, Vector3.Empty, new Vector3(0,
            spriteY, 0),
            Color.White.ToArgb());
        spriteY += texture.GetLevelDescription(0).Height + 5;

        // Draw a portion of an image to the screen// using Sprite.Draw. This shall be drawn such// that the image is modulated with the color green.

        sprite.Draw(texture, new Rectangle(4, 4, 24, 24),
            Vector3.Empty,
            new Vector3(0, spriteY, 0), Color.Green);

        spriteY+= 30;

        // Draw text to the screen. Using a sprite to draw text// to the  screen is essential for good performance.// Otherwise the font object will perform a// Sprite.Begin/Sprite.End internally for// each call to Font.DrawText. This can cause severe// performance problems.

        spriteY = 150;

        d3dFont.DrawText(sprite, "This is text.",
            5, spriteY, Color.Red);
        spriteY += d3dFont.Description.Height + 5;

        d3dFont.DrawText(sprite, "This is another line of text.",
            5, spriteY, Color.Green);
        spriteY += d3dFont.Description.Height + 5;

        d3dFont.DrawText(sprite, "Only one call to Sprite.Begin.",
            5, spriteY, Color.Blue);

        // End drawing using this sprite. This will cause the// sprites to be flushed to the graphics driver and will// reset the transformation matrices, textures states,// and renderstates if the SpriteFlags specified in Begin// call for that to happen.
        sprite.End();

        // Finish the scene and present it on the screen.
        device.EndScene();
        device.Present();
    }

    staticvoid Main()
    {
        Application.Run(new Sprites());
    }
}

コードのコンパイル方法

この例では、次の名前空間への参照が必要です。

堅牢性の高いプログラム

デバイスがリセットされる再作成する必要はありませんようににフォームのコンストラクターでスプライトとフォントを作成します。

優れたパフォーマンスには、スプライトを使用してテキストの描画します。 それ以外の場合は、フォント オブジェクト内部スプライト BeginEnd メソッドに対して実行 DrawText を呼び出すたびします。

可能であれば、フレームごとに 1 つのスプライトを使用する場合は、スプライト Begin とするスプライトの End メソッドに 1 回の呼び出し内の入れ子にします。

レンダリングを最適化してパフォーマンスを向上する、次の SpriteFlags 値を指定します。

  • SortTexture は高速化できるテクスチャを切り替えるように画面に描画する前にイメージを並べ替えます。

  • AlphaBlend を透明または半透明の領域を持つスプライトの特にフォントを正しくレンダリングします。

  • SortDepthBackToFront はスプライトは互いの上に描画されるいくつかの半透明、または透明のスプライトをした場合に便利です - ライトバック前面に順序で並べ替えます。

  • DoNotSaveState は指定されたレンダリング ステートを使用できないアプリケーションのパフォーマンスが向上します。

  • DoNotModifyRenderState 現在のレンダリング ステートを使用してパフォーマンスを最適化され、特殊効果で使用されることができます。

  • ObjectSpace および Billboard はさまざまな特殊効果と描画 (イメージを使用します。

参照

概念

.NET コンパクトなフレームワーク方法を説明したトピックの検索

その他の技術情報

.NET Compact Framework でモバイル Direct3D プログラミング