-
Notifications
You must be signed in to change notification settings - Fork 9
Drawing
Drawing primitives in Sharpex2D is pretty simple. Any object implementing or inheriting the IDrawable interface (Scene
s for example) has a Draw
method that gets a SpriteBatch passed in.
public override void Draw(Sharpex2D.Rendering.SpriteBatch spriteBatch, Sharpex2D.GameTime gameTime)
{
}
By overriding this method, we can unse the SpriteBatch to draw different primitive figures.
public override void Draw(Sharpex2D.Rendering.SpriteBatch spriteBatch, Sharpex2D.GameTime gameTime)
{
spriteBatch.FillRectangle(Color.Red, new Rectangle(10, 10, 100, 50));
spriteBatch.DrawRectangle(new Pen(Color.DarkGreen, 3f), new Rectangle(400, 180, 30, 30));
spriteBatch.FillEllipse(Color.Blue, new Ellipse(50, 75, new Vector2(320, 240)));
spriteBatch.DrawEllipse(new Pen(Color.Black, 2f), new Ellipse(60, 60, new Vector2(500, 400)));
spriteBatch.DrawPolygon(new Pen(Color.Green, 7f), new Polygon(
new Vector2(50,50),
new Vector2(300, 50),
new Vector2(200,140),
new Vector2(300,240),
new Vector2(140, 140)
));
spriteBatch.FillPolygon(Color.Lime, new Polygon(
new Vector2(400, 10),
new Vector2(500, 30),
new Vector2(600, 110),
new Vector2(300, 160),
new Vector2(300, 110)
));
}
Note: DrawArc
is only supported by GDI at the moment
Drawing images is nearly as simgle as drawing primitives. The only difference is that we need to load an image prior to drawing it.
To do so, navigate to a LoadContent
method that enables you to load the graphic into a variable that you can use in your Draw
method. This is usually the LoadContent
method of the scene you want to draw the graphics in.
LoadContent
gets a ContentManager
passed in which has a Function Load
. Have a look at its signature:
public T Load<T>(string asset) where T : IContent
So Load
can load object types that implement IContent
from a given file (string asset
). asset
is a path relative to Application Path/Content. Sharpex2D usually creates this directory with the first start of a program. Navigate to your project's output dir (for VS, this is usually Documents/Visual Studio Version/Projects/Solution Name/Project Name/bin/debug/). If there's no "Content" directory, create it. Get an image (I'll use this one by megupets) and copy it to your "Content" directory.
Back in VS, go back to your LoadContent
method and load the image into a Texture2D variable. You can now use the DrawTexture
method of spriteBatch
to draw your Texture2D.
class IntroScene :Scene
{
Texture2D bear;
public override void Update(Sharpex2D.GameTime gameTime)
{
}
public override void Draw(Sharpex2D.Rendering.SpriteBatch spriteBatch, Sharpex2D.GameTime gameTime)
{
spriteBatch.DrawTexture(bear, new Vector2(50, 50));
}
public override void Initialize()
{
}
public override void LoadContent(Sharpex2D.Content.ContentManager content)
{
bear = content.Load<Texture2D>("bear.png");
}
}
Drawing text needs a Font
defines font type, font size and font style (like bold or italic). Add a Font
variable to your scene and instanciate it in LoadContent
. You can then use SpriteBatch.DrawString
to draw a string with that font.
class IntroScene :Scene
{
Texture2D bear;
Font font;
public override void Update(Sharpex2D.GameTime gameTime)
{
}
public override void Draw(Sharpex2D.Rendering.SpriteBatch spriteBatch, Sharpex2D.GameTime gameTime)
{
spriteBatch.DrawTexture(bear, new Vector2(50, 50));
spriteBatch.DrawString("I'm a bear!", font, new Vector2(60, 150), Color.White);
}
public override void Initialize()
{
}
public override void LoadContent(Sharpex2D.Content.ContentManager content)
{
bear = content.Load<Texture2D>("bear.png");
font = new Font("Arial", 16f, TypefaceStyle.Regular);
}
}