www.xbdev.net
xbdev - software development
Friday December 27, 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...

 

2D is still used, and the SpriteBatch class is pretty good, but what if you want to go drawing 2d lines and 2d textures using the screen coords?  Well I thought I'd throw together a simple function which shows you how to use the BasicEffect and the default Texture vertex so you can draw textures to the screen.

 

I find it can be a very useful tool, either if you want to write a splash screen or just see whats in some textures for debuggings.

 

In the source code below the whole example is in a single function, which when called with a texture2d will draw the texture to screen.  Just pass in the full screen with and height to fill the full screen.

I didn't spent to much time on this, so just grabbed a quick test image...one of a gremlin and then added it to the project and drawed it in the top left corner... as you can see from the screenshot on the right.

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;

    // ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D **

    // ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D **

    Texture2D testImage;

 

    public GameClass()

    {

        graphics = new GraphicsDeviceManager(this);

        Content.RootDirectory = "Content";

    }

    protected override void LoadContent()

    {

        // ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D **

        // ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D **

        testImage = Content.Load<Texture2D>("myImage");

    }

 

    // ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D **

    // ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D **

    // ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D **

    public void DrawQuad(Texture2D tex, float x, float y, float w, float h)

    {

        VertexPositionTexture[]

        verts ={new VertexPositionTexture(new Vector3(x,   y,   0), new Vector2(0.0f, 0.0f)),

                new VertexPositionTexture(new Vector3(x+w, y,   0), new Vector2(1.0f, 0.0f)),

                new VertexPositionTexture(new Vector3(x+w, y+h, 0), new Vector2(1.0f, 1.0f)),

                new VertexPositionTexture(new Vector3(x,   y+h, 0), new Vector2(0.0f, 1.0f))};

 

 

        VertexDeclaration

        basicEffectVertexDeclaration

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

 

        BasicEffect basicEffect;

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

        basicEffect.Alpha = 1.0f;

        basicEffect.VertexColorEnabled = false;

        basicEffect.TextureEnabled = true;

        basicEffect.Texture = tex;

        basicEffect.EnableDefaultLighting();

        basicEffect.LightingEnabled = 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.SamplerStates[0].AddressU = TextureAddressMode.Wrap;

        graphics.GraphicsDevice.SamplerStates[0].AddressV = TextureAddressMode.Wrap;

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

        graphics.GraphicsDevice.VertexDeclaration = basicEffectVertexDeclaration;

        basicEffect.Begin();

        foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

        {

            pass.Begin();

            graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(

                        PrimitiveType.TriangleFan,

                        verts, 0, 2);

 

            pass.End();

        }

        basicEffect.End();

 

    }// End DrawQuad(..)

    // ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D **

    // ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D **

    // ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D **

 

    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.CornflowerBlue);

 

        // ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D **

        // ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D ** TEXTURE2D **

        DrawQuad(testImage, 40, 40, 300, 300);

 

        base.Draw(gameTime);

    }

}

 

// Program Entry Point

static class Program

{

    static void Main(string[] args)

    {

        using (GameClass game = new GameClass())

        {

            game.Run();

        }

    }

}

 

 

Won't be long before your modifying the code to draw sprites, splash screens..loading screens, fades...etc etc...

 

:)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
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.