Freigeben über


SO WIRD'S GEMACHT: Verwenden von Sprites

Dieser Dokumentation für die Vorschau nur ist und in späteren Versionen geändert. Leere Themen wurden als Platzhalter eingefügt.]

Sie können Sprites für Zeichnung Bilder und Text auf dem Bildschirm verwenden. In diesem Beispiel wird zeichnen und Rendern veranschaulicht.

Das Formular für dieses Codebeispiel enthält die folgenden Objekte:

Dieses Codebeispiel lädt eine Textur aus einer Datei das Sprite zu erstellen. Sie müssen eine kleine Bitmap in Ihr Projekt auf dem Gerät oder Emulator bereitgestellt werden aufnehmen.

Konstruktor des Formulars gibt Einstellungen für das Gerät PresentationParameters -Eigenschaft, erstellt ein Objekt Device und das Gerät Reset Methodenaufrufe. Er erstellt auch ein Font-Objekt.

Die folgende Tabelle beschreibt die Beispielmethoden, die das Sprite rendern.

Hinweis

Verwaltete Direct3D mobile-Anwendungen erfordern Windows Mobile 5.0 Software für Pocket PCs und Smartphones.Finden Sie unter Externe Ressourcen für .NET Compact Framework für Informationen über Windows Mobile-Software und SDKs.

Methode

Aktionen

OnDeviceReset

  • Lädt eine Textur aus einer Bitmap-Datei.

OnPaint

  1. Beginnt die Szene.

  2. Gibt Flags an SpriteFlags. Finden Sie unter "Stabiler Programmieren"Abschnitt weiter unten in diesem Thema ausführliche.

  3. Zeichnet das Sprite und den Text auf dem Bildschirm.

  4. Beendet die Szene.

Beispiel

Das folgende Codebeispiel stellt ein vollständiges Formular bereit. Es zeichnet ein Sprite mit eine bereitgestellte Bitmap.

                        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());
    }
}

Kompilieren des Codes

In diesem Beispiel sind Verweise auf die folgenden Namespaces erforderlich:

Robuste Programmierung

Erstellen Sie Sprites und Schriftarten im Konstruktor des Formulars, so dass Sie nicht benötigen neu erstellt werden, wenn das Gerät zurückgesetzt wurde.

Verwenden Sie für eine gute Leistung ein Sprite um Text zu zeichnen. Andernfalls führt das Schriftartobjekt intern Sprite Begin und End Methoden für jeden Aufruf DrawText.

Schachteln Sie wenn möglich, bei der Verwendung pro Frame 1 Sprite das Sprite innerhalb ein Aufruf an die Begin und End Methoden für ein Sprite.

Geben Sie die folgenden SpriteFlags Werte Rendern zu optimieren und Leistung zu verbessern:

  • SortTexture sortiert Bilder vor dem Zeichnen auf dem Bildschirm, sodass schneller, Wechseln von Texturen sein kann.

  • AlphaBlend rendert Schriftarten ordnungsgemäß, insbesondere für Sprites, die transparent oder durchscheinend Bereiche verfügen.

  • SortDepthBackToFront sortiert die Sprites in vorne hinten Reihenfolge ist nützlich, wenn Sie mehrere durchscheinenden oder transparenten Sprites oberhalb der jeweils anderen gezeichnet werden.

  • DoNotSaveState verbessert die Leistung für Anwendungen, die angegebene Renderingzustände nicht verwenden kann.

  • DoNotModifyRenderState optimiert die Leistung mithilfe der aktuellen Renderingzustand und für besondere Effekte verwendet werden können.

  • ObjectSpace und Billboard ermöglichen das Zeichnen von Bildern mit unterschiedlichen Spezialeffekten.

Siehe auch

Konzepte

.NET compact Framework Gewusst-wie-Themen

Weitere Ressourcen

Mobile Direct3D-Programmierung in .NET Compact Framework