Click here to Skip to main content
15,886,069 members
Articles / Game Development / XBox

XNA Billiards Visual Demo: An example of XNA content processing and 3D rendering

Rate me:
Please Sign up or sign in to vote.
4.97/5 (16 votes)
19 Nov 2007CPOL6 min read 68K   13.3K   68  
This article is an example of visual rendering and content processing with XNA, and a good start for those who want to start learning XNA.
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using XNA = Microsoft.Xna.Framework;

namespace XNADemo
{
    /// <summary>
    /// Author: I�aki Ayucar (http://graphicdna.blogspot.com)
    /// Date: 19/11/2007
    /// 
    /// This software is distributed "for free" for any non-commercial usage. The software is provided �as-is.� 
    /// You bear the risk of using it. The contributors give no express warranties, guarantees or conditions.
    /// </summary>
    public class BilliardsGAme : Microsoft.Xna.Framework.Game
    {
        public static Vector3 sPoolTarget = new Vector3(0, 0.839f, 0f);
        public static Vector3 sChairTarget = new Vector3(-3.5f, 0.839f, -3.2f);
        public static Vector3 sPoster1Target = new Vector3(3.5f, 1.7f, -2f);
        public static Vector3 sPoster2Target = new Vector3(3.5f, 1.7f, 2f);

        public GraphicsDeviceManager mGraphics;
        public ContentManager mContent;
        public Settings mSettings;                           

        public static Input.CMouseInput sMouseInput = new XNADemo.Input.CMouseInput();
        
        public XNADemo.Scene.CScene mScene = null;
        public BasicEffect mBasicEffect = null;
        public Cameras.TargetedCamera mCamera;

        public XNADemo.Utils.MySprite mCursorSprite = null;
        public XNADemo.Utils.MySprite mLogoSprite = null;
        public XNADemo.Utils.MySpriteButton mButtonChair = null;
        public XNADemo.Utils.MySpriteButton mButtonPool = null;
        public XNADemo.Utils.MySpriteButton mButtonPoster1 = null;
        public XNADemo.Utils.MySpriteButton mButtonPoster2 = null;

        /// <summary>
        /// 
        /// </summary>
        public BilliardsGAme()
        {
            mGraphics = new GraphicsDeviceManager(this);
            mContent = new ContentManager(Services);
            mSettings = new Settings();
            this.mScene = new XNADemo.Scene.CScene(this);
            
//            mInput = new MDX.Input.CMouseInput();
            sMouseInput.OnMouseMove += new XNADemo.Input.CMouseInput.CursorMoveEventHandler(mInput_MouseMove);

            this.Exiting += new EventHandler(SimBill_Exiting);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SimBill_Exiting(object sender, EventArgs e)
        {
            this.mSettings.Save("Code\\Settings\\settings.xml");
        }
        /// <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()
        {
            this.mSettings = Settings.Load("Code\\Settings\\settings.xml");

            // Crear camara
            this.mCamera = new XNADemo.Cameras.TargetedCamera();
            float aspect = (float)this.mGraphics.GraphicsDevice.Viewport.Width / (float)this.mGraphics.GraphicsDevice.Viewport.Height;
            this.mCamera.RefreshProjectionMatrix(this.mSettings.Fov, aspect, this.mSettings.NearPlane, this.mSettings.FarPlane);

            this.mBasicEffect = new BasicEffect(this.mGraphics.GraphicsDevice, null);

            sMouseInput.Initialize();

            base.Initialize();
        }
        /// <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)
        {
            this.mCamera.Update();

            sMouseInput.Update();

            this.mScene.Update(gameTime);

            this.mCursorSprite.Update(gameTime);
            this.mLogoSprite.Update(gameTime);
            this.mButtonChair.Update(gameTime);
            this.mButtonPool.Update(gameTime);
            this.mButtonPoster1.Update(gameTime);
            this.mButtonPoster2.Update(gameTime);

            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)
        {
            mGraphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            this.mScene.Draw(gameTime);

            this.mButtonChair.Draw(gameTime);
            this.mButtonPool.Draw(gameTime);
            this.mButtonPoster1.Draw(gameTime);
            this.mButtonPoster2.Draw(gameTime);

            this.mCursorSprite.Draw(gameTime);
            this.mLogoSprite.Draw(gameTime);

            base.Draw(gameTime);
        }
  
        #region Resource Loading
        /// <summary>
        /// Load your graphics content.  If loadAllContent is true, you should
        /// load content from both ResourceManagementMode pools.  Otherwise, just
        /// load ResourceManagementMode.Manual content.
        /// </summary>
        /// <param name="loadAllContent">Which type of content to load.</param>
        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {               
                this.mScene.LoadGraphicsContent();
                this.mCamera.EyePoint = new Vector3(0, 2f, 3f);
                this.mCamera.Target = sPoolTarget;
               
                this.mCursorSprite = new XNADemo.Utils.MySprite(this, this.mGraphics.GraphicsDevice);
                this.mCursorSprite.Size = new Vector2(32, 32);
                this.mCursorSprite.Texture = this.mContent.Load<Texture2D>(@"Content\UI\Cursors\Sniper");
                this.mCursorSprite.AlignMode = XNADemo.Utils.MySprite.eAlignMode.TopCenter;

                this.mLogoSprite = new XNADemo.Utils.MySprite(this, this.mGraphics.GraphicsDevice);
                this.mLogoSprite.Size = new Vector2(192, 48);
                this.mLogoSprite.Pos = new Vector2(this.mGraphics.GraphicsDevice.Viewport.Width - 10- this.mLogoSprite.Size.X, this.mGraphics.GraphicsDevice.Viewport.Height - this.mLogoSprite.Size.Y - 10);
                this.mLogoSprite.Texture = this.mContent.Load<Texture2D>(@"Content\UI\InakiAyucar");
                this.mLogoSprite.AlignMode = XNADemo.Utils.MySprite.eAlignMode.TopLeft;

                this.mButtonChair = new XNADemo.Utils.MySpriteButton(this, this.mGraphics.GraphicsDevice);
                this.mButtonChair.MouseOverColor = Color.SteelBlue;
                this.mButtonChair.Size = new Vector2(64, 64);
                this.mButtonChair.Pos = new Vector2(20, this.mGraphics.GraphicsDevice.Viewport.Height - 84);
                this.mButtonChair.Texture = this.mContent.Load<Texture2D>(@"Content\UI\TargetChair");
                this.mButtonChair.AlignMode = XNADemo.Utils.MySprite.eAlignMode.TopLeft;
                this.mButtonChair.MouseClick += new XNADemo.Utils.MySpriteButton.MouseClickDelegate(mButtonChair_MouseClick);

                this.mButtonPool = new XNADemo.Utils.MySpriteButton(this, this.mGraphics.GraphicsDevice);
                this.mButtonPool.MouseOverColor = Color.SteelBlue;
                this.mButtonPool.Size = new Vector2(64, 64);
                this.mButtonPool.Pos = new Vector2(104, this.mGraphics.GraphicsDevice.Viewport.Height - 84);
                this.mButtonPool.Texture = this.mContent.Load<Texture2D>(@"Content\UI\TargetPool");
                this.mButtonPool.AlignMode = XNADemo.Utils.MySprite.eAlignMode.TopLeft;
                this.mButtonPool.MouseClick += new XNADemo.Utils.MySpriteButton.MouseClickDelegate(mButtonPool_MouseClick);

                this.mButtonPoster1 = new XNADemo.Utils.MySpriteButton(this, this.mGraphics.GraphicsDevice);
                this.mButtonPoster1.MouseOverColor = Color.SteelBlue;
                this.mButtonPoster1.Size = new Vector2(64, 64);
                this.mButtonPoster1.Pos = new Vector2(188, this.mGraphics.GraphicsDevice.Viewport.Height - 84);
                this.mButtonPoster1.Texture = this.mContent.Load<Texture2D>(@"Content\UI\TargetPoster1");
                this.mButtonPoster1.AlignMode = XNADemo.Utils.MySprite.eAlignMode.TopLeft;
                this.mButtonPoster1.MouseClick += new XNADemo.Utils.MySpriteButton.MouseClickDelegate(mButtonPoster1_MouseClick);

                this.mButtonPoster2 = new XNADemo.Utils.MySpriteButton(this, this.mGraphics.GraphicsDevice);
                this.mButtonPoster2.MouseOverColor = Color.SteelBlue;
                this.mButtonPoster2.Size = new Vector2(64, 64);
                this.mButtonPoster2.Pos = new Vector2(272, this.mGraphics.GraphicsDevice.Viewport.Height - 84);
                this.mButtonPoster2.Texture = this.mContent.Load<Texture2D>(@"Content\UI\TargetPoster2");
                this.mButtonPoster2.AlignMode = XNADemo.Utils.MySprite.eAlignMode.TopLeft;
                this.mButtonPoster2.MouseClick += new XNADemo.Utils.MySpriteButton.MouseClickDelegate(mButtonPoster2_MouseClick);

            }
        }

        /// <summary>
        /// Unload your graphics content.  If unloadAllContent is true, you should
        /// unload content from both ResourceManagementMode pools.  Otherwise, just
        /// unload ResourceManagementMode.Manual content.  Manual content will get
        /// Disposed by the GraphicsDevice during a Reset.
        /// </summary>
        /// <param name="unloadAllContent">Which type of content to unload.</param>
        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent == true)
            {
                mContent.Unload();
            }
        }
        #endregion

        #region UI Buttons
        /// <summary>
        /// 
        /// </summary>
        void mButtonChair_MouseClick()
        {
            this.mCamera.Target = sChairTarget;
        }
        /// <summary>
        /// 
        /// </summary>
        void mButtonPool_MouseClick()
        {
            this.mCamera.Target = sPoolTarget;
        }
        /// <summary>
        /// 
        /// </summary>
        void mButtonPoster2_MouseClick()
        {
            this.mCamera.Target = sPoster2Target;
        }
        /// <summary>
        /// 
        /// </summary>
        void mButtonPoster1_MouseClick()
        {
            this.mCamera.Target = sPoster1Target;
        }
        #endregion


        #region Input Response
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pCurX"></param>
        /// <param name="pCurY"></param>
        /// <param name="pDifX"></param>
        /// <param name="pDifY"></param>
        void mInput_MouseMove(int pCurX, int pCurY, int pDifX, int pDifY)
        {
            if (this.mCursorSprite == null)
                return;
            this.mCursorSprite.Pos = new Vector2((float)pCurX, (float)pCurY);

            if (sMouseInput.Buttons[0] != ButtonState.Pressed && sMouseInput.Buttons[2] == ButtonState.Pressed)
            {
                if (sMouseInput.mCursor.LastDif.Y != 0)
                    this.mCamera.MoveForwardBackward((float)-sMouseInput.mCursor.LastDif.Y * sMouseInput.mMouseSensitivity);
            }
            else if (sMouseInput.Buttons[0] == ButtonState.Pressed ||(sMouseInput.Buttons[0] == ButtonState.Pressed && sMouseInput.Buttons[2] == ButtonState.Pressed))
            {
                if (sMouseInput.mCursor.LastDif.X != 0)
                {
                    float sensitivity = Math.Min(1f, (0.2f + this.mCamera.Dist01)) * sMouseInput.mMouseSensitivity;
                    this.mCamera.OrbitLeftRight((float)-sMouseInput.mCursor.LastDif.X * sensitivity);
                }
                if (sMouseInput.mCursor.LastDif.Y != 0)
                    this.mCamera.OrbitUpDown((float)sMouseInput.mCursor.LastDif.Y * sMouseInput.mMouseSensitivity);
            }
        }    
       
        #endregion

  
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Spain Spain
Inaki Ayucar is a Microsoft MVP in DirectX/XNA, and a software engineer involved in development since his first Spectrum 48k, in the year 1987. He is the founder and chief developer of The Simax Project (www.simaxvirt.com) and is very interested in DirectX/XNA, physics, game development, simulation, C++ and C#.

His blog is: http://graphicdna.blogspot.com

To contact Inaki: iayucar@simax.es

Comments and Discussions