Reputation: 391
I am trying to make a very simple game for learning purposes, but i am struggling with SharpDX. Basically what i'm trying to achieve is: Take several bitmap images and display them in a window on certain x y coordinates. After that, i will clear the window a display those images again, but on different x y coordinates (a very basic game loop basically, which means the images will be re-displayed many times per second).
I have this code:
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();
}
}
When compiled, it simply opens a window with a blue background.
Then, I have two images:
var img = new BitmapImage(new Uri("C:/img/pic1.png"));
var img1 = new BitmapImage(new Uri("C:/img/pic2.png"));
And this is where the problem lies, i am unable to figure out, how to take these two images and actually display them in the window. img
should be displayed at x10 y50 coordinates (or in other words, 10 pixels from the left side of the window, and 50 pixels from the top) and img1
at x200 y150 coordinates.
I'm sure the solution is something easy, but I just can't figure it out.
Upvotes: 0
Views: 2469
Reputation:
Inside MyGame.
Firstly, in LoadContent
method load the texture:
private Texture2D myTexture;
protected override void LoadContent()
{
this.myTexture = this.Content.Load<Texture2D>("Path to img");
}
then in 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);
}
This is the basic way to draw anything with SharpDX
.
Upvotes: 1