XNA Application Anti Aliasing

336 views Asked by At

I'm building a business intelligence tool using the XNA framework to construct the graphics. I'm only using 2D objects, and I'm constructing my 2D objects from scratch using Texture2D and SpriteFont.

Currently I sit with the problem that my objects look 'rough around the edges' on a big display. I know that anti aliasing technology can get rid of these 'rough edges', however I have not been able to successfully implemented it on my custom made Texture2D objects.

What is the best way to use anti aliasing in XNA in the context of my development efforts?

Note that I'm not an XNA expert, I know zero about 3D modelling in XNA, and I have researched other questions.

My XNA framework code looks as follows:

public class Game1 : Microsoft.Xna.Framework.Game
{
    public GraphicsDeviceManager graphics;
    public SpriteBatch spriteBatch;

    public bool drawn = false;

    public Timer timer;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        Settings.LoadSettings();

        if (Settings.GetSettings("FullScreen") == "True") { graphics.IsFullScreen = true; } else { graphics.IsFullScreen = false; }
        graphics.PreferredBackBufferWidth = Convert.ToInt32(Settings.GetSettings("ScreenWidth"));
        graphics.PreferredBackBufferHeight = Convert.ToInt32(Settings.GetSettings("ScreenHeight"));

        graphics.PreferMultiSampling = true;
        graphics.PreferredBackBufferFormat = SurfaceFormat.Color;
        graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;

        //Update LOS every 6 minutes
        timer = new Timer(360000);
        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        timer.Enabled = true;

        this.IsMouseVisible = true;

        //System.Windows.Forms.Form MyGameForm = (System.Windows.Forms.Form)System.Windows.Forms.Form.FromHandle(Window.Handle);
        //MyGameForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        var form = (System.Windows.Forms.Form)System.Windows.Forms.Control.FromHandle(this.Window.Handle);
        form.Location = new System.Drawing.Point(0, 0);


        base.Initialize();

        this.TargetElapsedTime = TimeSpan.FromSeconds(0.1);
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.

        graphics.GraphicsDevice.PresentationParameters.MultiSampleCount = 2;

        spriteBatch = new SpriteBatch(GraphicsDevice);

        // TODO: use this.Content to load your game content here
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here

        if (drawn == true) { SuppressDraw(); }

        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        //MouseState mouseState = Mouse.GetState();
        //mouseState.LeftButton = ButtonState.Pressed

        //This boolean will pause the 'game' so that it does not update every x milli seconds
        drawn = true;

        //Create the interface with a black background
        GraphicsDevice.Clear(Color.Black);


        // Add pointers to create global references to XNA
        Pointers.graphics = this.graphics;
        Pointers.spriteBatch = this.spriteBatch;
        Pointers.content = this.Content;

        //Run data initialiazations and sql queries to retrieve all data
        Objects.ObjectsData.CleareObjectsData();
        Data.SqlData.SetDates();
        Data.SqlData.GetAllData();
        Data.DataFunctions.RunDataFunctions();
        LocationRef.InitializeLocationRef();

        //Create 2D Objects

        Sections.Drilling.CreateDrillingObjects();
        Sections.BlastShovels.CreateBlastShovelsObjects();
        Sections.Pits.CreatePitObjects();
        Sections.Trucks.CreateTrucksObjects();
        //Sections.StocksCrushers.CreateStocksCrushersObjects();
        Sections.Other.CreateOtherObjects();

        //Begin processing of 2D objects
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null);

        foreach (var x in Objects.ObjectsData.textObjects) { x.DrawObject(); }
        foreach (var x in Objects.ObjectsData.circleObjects) { x.DrawObject(); }
        foreach (var x in Objects.ObjectsData.rectangleObjects) { x.DrawObject(); }
        foreach (var x in Objects.ObjectsData.triangleObjects) { x.DrawObject(); }
        foreach (var x in Objects.ObjectsData.trapesiumObjects) { x.DrawObject(); }

        //End processing of 2D objects
        spriteBatch.End();

        base.Draw(gameTime);
    }

    //Timer used to refresh the LOS
    public void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        drawn = false;
        System.Media.SystemSounds.Beep.Play();
    }
}

An example object class:

public class Circle
{
    public Texture2D graphic;
    public SpriteFont font;

    public Vector2 graphicCoor;
    public Vector2 fontCoor;

    Color graphicColor;
    Color fontColor;

    public string fontText;
    public string fontType;

    public string objectID;

    public Circle(int xCord, int yCord, int radius, Color graphicColor_, string fontType_, string fontText_, Color fontColor_, string ID)
    {

        objectID = ID;

        graphicColor = graphicColor_;
        fontColor = fontColor_;

        fontText = fontText_;
        fontType = fontType_;

        graphic = new Texture2D(Pointers.graphics.GraphicsDevice, radius, radius);
        font = Pointers.content.Load<SpriteFont>(fontType_);

        graphicCoor = new Vector2(xCord, yCord);

        Vector2 FontOrigin = font.MeasureString(fontText_);
        //fontCoor = new Vector2(xCord + (int)FontOrigin.Length(), yCord + radius/2);
        fontCoor = new Vector2(xCord + (radius - (int)FontOrigin.X) / 2, yCord + (radius - (int)FontOrigin.Y) / 2);

        //Set color data

        Color[] colorData = new Color[radius * radius];

        float diam = radius / 2f;
        float diamsq = diam * diam;

        for (int x = 0; x < radius; x++)
        {
            for (int y = 0; y < radius; y++)
            {
                int index = x * radius + y;
                Vector2 pos = new Vector2(x - diam, y - diam);
                if (pos.LengthSquared() <= diamsq)
                {
                    colorData[index] = graphicColor_;
                }
                else
                {
                    colorData[index] = Color.Transparent;
                }
            }

            graphic.SetData(colorData);

        }
    }

    public void DrawObject()
    {
        Pointers.spriteBatch.Draw(graphic, graphicCoor, Color.White);
        Pointers.spriteBatch.DrawString(font, fontText, fontCoor, fontColor, 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 0.5f);
    }
}
0

There are 0 answers