XNAプログラミング

スプライト描画

スプライト描画とは、つまるところ2D描画機能です。

さっそく、サンプルプログラムを作成してみます。

■準備

・画像ファイルをxnbファイルに変換する

XNA GameStudio(Visual C#)で表示する画像ファイルをxnbファイルに変換しておく。

■サンプルコード

#light

open Microsoft.Xna.Framework
open Microsoft.Xna.Framework.Content
open Microsoft.Xna.Framework.Graphics
open System

type MyGame = class
    inherit Game
        
    val mutable graphics : GraphicsDeviceManager
    val mutable spriteBatch : SpriteBatch
    val mutable texture : Texture2D
    
    new() as this =
        {
            graphics = null
            spriteBatch = null
            texture = null
        }
        then
            this.graphics <- new GraphicsDeviceManager(this)
            this.Content.RootDirectory <- "Content"

    override this.Initialize() =
        base.Initialize()

    override this.LoadContent() =
        this.spriteBatch <- new SpriteBatch(this.GraphicsDevice)   
        this.texture <- this.Content.Load("Texture")

    override this.Update(gameTime) =
        base.Update(gameTime)

    override this.Draw(gameTime) =
        let gd = this.graphics.GraphicsDevice
        gd.Clear(Color.Blue)
        
        this.spriteBatch.Begin()
        this.spriteBatch.Draw(this.texture, Vector2.Zero, Color.White)
        this.spriteBatch.End()
        base.Draw(gameTime)

    end
    
let main() =
    let game = new MyGame()
    game.Run()

[<STAThread>]
do main()

■実行結果

inserted by FC2 system