Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

Cannot change of file .spritefont

$
0
0

@Tanakhon_Kaewkunha wrote:

I am following this tutorial -www.youtube.com/watch?v=jr8sktSwn7E&index=5&list=PLHJE4y54mpC5hrlDv8yFHPfrSNhqFoA0h

In tutorial 5, i'm already created file Obitron.spritefont like this.

When i changed FontName from Arial to
Obitron

after run the program i got these error

Error The command ""C:\Program Files (x86)\MSBuild\MonoGame\v3.0\Tools\MGCB.exe" /@:"C:\Users\Tkaewkunha\Documents\Visual Studio 2015\Projects\Game1\Game1\Content\Content.mgcb" /platform:Windows /outputDir:"C:\Users\Tkaewkunha\Documents\Visual Studio 2015\Projects\Game1\Game1\Content\bin\Windows" /intermediateDir:"C:\Users\Tkaewkunha\Documents\Visual Studio 2015\Projects\Game1\Game1\Content\obj\Windows" /quiet" exited with code 1. Game1

and this

Error Index was out of range. Must be non-negative and less than the size of the

This is the class that calling SpriteFont

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Game1
{
    public class Image
    {
        public float Alpha;
        public string Text, FontName, Path;
        public Vector2 Position, Scale;
        public Rectangle SourceRect;

        [XmlIgnore]
        public Texture2D Texture;

        Vector2 origin;
        ContentManager content;
        RenderTarget2D renderTarget;
        SpriteFont font;

        public Image()
        {
            Path = String.Empty;
            FontName = "Fonts/Obitron";
            Position = Vector2.Zero;
            Scale = Vector2.One;
            Alpha = 1.0f;
            SourceRect = Rectangle.Empty;
        }
        public void LoadContent()
        {
            content = new ContentManager(
                ScreenManager.Instance.Content.ServiceProvider, "Content");
            if (Path != String.Empty)
                Texture = content.Load<Texture2D>(Path);

            font = content.Load<SpriteFont>(FontName);

            Vector2 dimensions = Vector2.Zero;

            if (Texture != null)
                dimensions.X += Texture.Width;

            dimensions.X += font.MeasureString(Text).X;

            if (Texture != null)
                dimensions.Y = Math.Max(Texture.Height, font.MeasureString(Text).Y);
            else
                dimensions.Y = font.MeasureString(Text).Y;

            if (SourceRect == Rectangle.Empty)
                SourceRect = new Rectangle(0, 0, (int)dimensions.X, (int)dimensions.Y);

            renderTarget = new RenderTarget2D(ScreenManager.Instance.GraphicsDevice,
                (int)dimensions.X, (int)dimensions.Y);

            ScreenManager.Instance.GraphicsDevice.SetRenderTarget(renderTarget);
            ScreenManager.Instance.GraphicsDevice.Clear(Color.Transparent);
            ScreenManager.Instance.SpriteBatch.Begin();
            if (Texture != null)
                ScreenManager.Instance.SpriteBatch.Draw(Texture, Vector2.Zero, Color.White);
            ScreenManager.Instance.SpriteBatch.DrawString(font, Text, Vector2.Zero, Color.White);
            ScreenManager.Instance.SpriteBatch.End();
  
            Texture = renderTarget;
            ScreenManager.Instance.GraphicsDevice.SetRenderTarget(null);
        }
        public void UnloadContent() {
            content.Unload();
        }
        public void Update(GameTime gameTime)
        {

        }
        public void Draw(SpriteBatch spriteBatch)
        {
            origin = new Vector2(SourceRect.Width / 2,SourceRect.Height/2);
            spriteBatch.Draw(Texture, Position + origin, SourceRect, Color.White * Alpha, 
                0.0f, origin, Scale, SpriteEffects.None, 0.0f);

        }
    }
}

Any suggestion?
Thank you.

Posts: 14

Participants: 4

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles