2014-09-13 22 views
0

我想爲學習目的做一個非常簡單的遊戲,但我正在與SharpDX苦苦掙扎。 基本上我試圖實現的是:取幾個位圖圖像,並在某個xy座標的窗口中顯示它們。之後,我將清除窗口再次顯示這些圖像,但在不同的xy座標上(基本上是一個非常基本的遊戲循環,這意味着圖像將每秒重新顯示多次)。如何在SharpDX中顯示位圖?

我有這樣的代碼:

using SharpDX; 
using SharpDX.Toolkit; 

internal sealed class MyGame : Game 
{ 
    private readonly GraphicsDeviceManager _graphicsDeviceManager; 

    public MyGame() 
    { 
     _graphicsDeviceManager = new GraphicsDeviceManager(this); 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 


     base.Draw(gameTime); 
    } 
} 
------- 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     using (var game = new MyGame()) 
      game.Run(); 
    } 
} 

在編譯時,它就會打開一個藍色背景的窗口。

然後,我有兩個圖像:

var img = new BitmapImage(new Uri("C:/img/pic1.png")); 
var img1 = new BitmapImage(new Uri("C:/img/pic2.png")); 

這就是問題所在,我無法弄清楚,如何利用這兩個圖像,實際上在窗口中顯示出來。 img應在x200 y150座標處顯示x10 y50座標(或換句話說,距窗口左側10像素,頂部50像素),img1

我確定解決方案很簡單,但我無法弄清楚。

回答

1

Inside MyGame

首先,在LoadContent方法加載質地:

private Texture2D myTexture; 

protected override void LoadContent()   
{ 
    this.myTexture = this.Content.Load<Texture2D>("Path to img"); 
} 

然後在Draw;

protected override void Draw(GameTime gameTime) 
{ 
    var sprite = new SpriteBatch(this.GraphicsDevice); 
    sprite.Begin(); 

    sprite.Draw(this.myTexture, 
        new Rectangle(10, 50, // position: x and y coordiantes in pixels 
            /* width and height below 
              - do not remeber order: */ 
            25, 25), 
        Color.White); 

    sprite.End(); 

    base.Draw(gameTime); 
} 

這是用SharpDX繪製任何東西的基本方法。