Friday, June 28, 2013

III. Lord of strings - String Loading, Update and Drawing



So here's what we need..
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
This allows us do draw and load content into the game..

At the top of your class, declare a spriteFont..
so add this line..

SpriteFont font;

spritefont is the datatype of font configs..
now let's add a spriteFont into the project..



[At your game content, Right Click > add> New item > Spritefont > name it font]


If you open the spriteFont file, you'll see some settings.
You can change the font to use , size, style etc at the spritefont file..


So now at your game.cs

Lets load the font with the spritefont file.
At your load content add this line
 font = Content.Load<SpriteFont>("font");

and inside your spritebatch Begin and End, add this line..
spriteBatch.DrawString(font, "HELLO WORLD!", new Vector2(100, 100), Color.White);
so, spritebatch also allows us to draw string..
the format is (the font to use, the string, the position and the color of the text)
so we set the string to x=100,y=100
Run the GAME, and see


SOURCE CODE

public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D texture;
        Vector2 texturePosition = Vector2.Zero;
        KeyboardState currentKeyboardState, oldKeyboardState;
        SpriteFont font;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
        protected override void Initialize()
        {
            base.Initialize();
        }
        protected override void LoadContent()
        {

            spriteBatch = new SpriteBatch(GraphicsDevice);
            texture = Content.Load<Texture2D>("test");
            texturePosition = new Vector2(100, 100);
            font = Content.Load<SpriteFont>("font");
        }
        protected override void UnloadContent()
        {
        }
        protected override void Update(GameTime gameTime)
        {
            currentKeyboardState = Keyboard.GetState();

            if (currentKeyboardState.IsKeyDown(Keys.Up) && oldKeyboardState.IsKeyDown(Keys.Up))
            {
                texturePosition.Y -= 10;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Right) && oldKeyboardState.IsKeyDown(Keys.Right))
            {
                texturePosition.X += 10;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Left) && oldKeyboardState.IsKeyDown(Keys.Left))
            {
                texturePosition.X -= 10;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Down) && oldKeyboardState.IsKeyDown(Keys.Down))
            {
                texturePosition.Y += 10;
            }
            if (currentKeyboardState.IsKeyUp(Keys.O) && oldKeyboardState.IsKeyDown(Keys.O))
            {
                texturePosition = new Vector2(100, 100);
            }
            oldKeyboardState = currentKeyboardState;
            base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            spriteBatch.Draw(texture, texturePosition, Color.White);
            spriteBatch.DrawString(font, "HELLO WORLD!", new Vector2(100, 100), Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }