www.xbdev.net
xbdev - software development
Saturday April 20, 2024
home | contact | Support | XNA & 360 The bits and bytes of XNA and C# for your 360... | XNA & 360 The bits and bytes of XNA and C# for your 360...

     
 

XNA & 360

The bits and bytes of XNA and C# for your 360...

 

Either you need to draw some debug information on the screen, such as the safe screen, or you want to make a full xbox 360 game with lines....then first, you need to know how to draw lines on screen.  The trickier part is making the coords map to the screen, so 0,0 is the top left and screen width/height is the bottom right.

 

So the basic shader below you can see how you would go about drawing any amount of lines to the screen!

 

 

Just to show you quickly what the simple sourcecode below does, I took a quick screenshot as shown on the right.  Simple draws 2 lines in two different colours from each corner of the screen.

 

Once you see how it works you can optimise it by making some of the variables members of a class, instead of constructing them each time.

 

 

Game.cs [Download SourceCode]

using System;

using System.Collections.Generic;

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Content;

using Microsoft.Xna.Framework.GamerServices;

using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework.Input;

 

public class GameClass : Microsoft.Xna.Framework.Game

{

    GraphicsDeviceManager graphics;

 

    public GameClass()

    {

        graphics = new GraphicsDeviceManager(this);

    }

 

    // ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE

    // ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE

    public void DrawLine2D(Vector2 from, Vector2 to, Color colour)

    {

        VertexPositionColor[]

        pnts = {new VertexPositionColor(new Vector3(from.X, from.Y, 0), colour),

                new VertexPositionColor(new Vector3(to.X,   to.Y,   0), colour)};

 

 

        VertexDeclaration

        basicEffectVertexDeclaration

        = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionColor.VertexElements);

 

        BasicEffect basicEffect;

        basicEffect = new BasicEffect(graphics.GraphicsDevice, null);

        basicEffect.Alpha = 1.0f;

        basicEffect.VertexColorEnabled = true;

        basicEffect.TextureEnabled = false;

        basicEffect.Projection = Matrix.CreateOrthographicOffCenter(0.0F,

                                            graphics.GraphicsDevice.Viewport.Width,

                                            graphics.GraphicsDevice.Viewport.Height, 0.0f,

                                            0.0F, -1.0F);

        basicEffect.View = Matrix.Identity;

        basicEffect.World = Matrix.Identity;

 

        graphics.GraphicsDevice.VertexDeclaration       = basicEffectVertexDeclaration;

        graphics.GraphicsDevice.RenderState.PointSize   = 2; // Line Thickness

        graphics.GraphicsDevice.RenderState.FillMode    = FillMode.Solid;

 

        graphics.GraphicsDevice.RenderState.CullMode = CullMode.None;

        graphics.GraphicsDevice.VertexDeclaration = basicEffectVertexDeclaration;

        basicEffect.Begin();

        foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

        {

            pass.Begin();

            graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(

                        PrimitiveType.LineList,

                        pnts, 0, 1);

 

            pass.End();

        }

        basicEffect.End();

 

    }// End DrawQuad(..)

    // ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE

    // ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE

 

    protected override void Update(GameTime gameTime)

    {

        // Allows the game to exit

        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

            this.Exit();

        base.Update(gameTime);

    }

    protected override void Draw(GameTime gameTime)

    {

        GraphicsDevice.Clear(Color.SeaShell);

 

        Vector2 s = // ScreenSize

        new Vector2( graphics.GraphicsDevice.Viewport.Width,

                     graphics.GraphicsDevice.Viewport.Height);

 

        // ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE

        // ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE ** 2DLINE

        DrawLine2D( new Vector2(0,0), new Vector2(s.X,s.Y), Color.Red);

 

        DrawLine2D( new Vector2(s.X,0), new Vector2(0,s.Y), Color.Blue);

 

 

        base.Draw(gameTime);

    }

}

 

// Program Entry Point

static class Program

{

    static void Main(string[] args)

    {

        using (GameClass game = new GameClass())

        {

            game.Run();

        }

    }

}

 

 

Hint/Tip
Just so you know, to speed things up considerably, its always a good idea to render all your lines in a single go.  This way you can render tens of thousands or millions of lines without speed drops.  To achieve this, each time you draw a line, you might want to store it in a dynamic list, and each time your list is full or each time you have finished all your scene rendering you simply draw all your lines in the list in one go!

 

An example of a whole game I wrote that simply used lines and nothing more, for the ship, rocks and explosions, see my asteroids demo and source code.

 

Happy Coding.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.