Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SpriteBatch & SpriteFont with SharpDX 2.4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SharpDX;
using SharpDX.Toolkit;
using SharpDX.Toolkit.Content;
using SharpDX.Toolkit.Graphics;
namespace SharpDX_Test
{
class Sprite
{
private string _assetName;
public Texture2D Texture;
public Vector2 Position;
public float Rotation;
public Vector2 Origin;
public Vector2 Scale;
public Sprite(string assetName)
{
_assetName = assetName;
Position = Vector2.Zero;
Rotation = 0;
Origin = Vector2.Zero;
Scale = Vector2.One;
}
public void LoadContent(ContentManager content)
{
Texture = content.Load<Texture2D>(_assetName);
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, Position, null, Color.White, Rotation, Origin, Scale, SpriteEffects.None, 1.0f);
}
}
class SharpGame : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont font;
List<Sprite> sprites;
public SharpGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
sprites = new List<Sprite>();
}
protected override void Initialize()
{
base.Initialize();
Window.Title = "SharpDX Toolkit : DeMoNiXiS testing";
}
protected override void LoadContent()
{
base.LoadContent();
spriteBatch = new SpriteBatch(GraphicsDevice);
font = Content.Load<SpriteFont>("Arial16.tkfnt");
Sprite sprite;
Random random = new Random(DateTime.Now.Millisecond);
for (int i = 0; i < 5; i++)
{
sprite = new Sprite("test.png");
sprite.LoadContent(Content);
sprite.Position = new Vector2(50 + sprite.Texture.Width + (100 * i), 50 + sprite.Texture.Height + (100 * i));
sprite.Origin = new Vector2(sprite.Texture.Width / 2, sprite.Texture.Height / 2);
sprite.Scale = new Vector2((float)(random.NextDouble() * 2.0f));
sprites.Add(sprite);
}
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
foreach (Sprite sprite in sprites)
sprite.Rotation += 0.01f;
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(GraphicsDevice.BackBuffer, Color.Black);
spriteBatch.Begin();
spriteBatch.DrawString(font, "SharpDX ToolKit Demo", new Vector2(10, 10), Color.DarkSlateBlue, 0, Vector2.Zero, new Vector2(1.5f), SpriteEffects.None, 1);
spriteBatch.DrawString(font, "The future of XNA on Microsoft Plateforms ?...", new Vector2(10, 55), Color.White);
spriteBatch.DrawString(font, "Demo by Demonixis", new Vector2(600, 560), Color.White);
foreach (Sprite sprite in sprites)
sprite.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
public class Program
{
[STAThread]
public static void Main(string[] args)
{
using (SharpGame game = new SharpGame())
{
game.Run();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment