Main.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using TestGame.Controls;
using TestGame.GameStates;
namespace TestGame
{
public class Main : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
InputHandler inputHandler;
public SpriteBatch spriteBatch;
SpriteFont spriteFont;
MainMenu mainMenu;
Vector2 position;
Texture2D backgroundImage;
public Main()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
inputHandler = new InputHandler();
mainMenu = new MainMenu();
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
}
protected override void Initialize()
{
this.IsMouseVisible = true;
base.Initialize();
mainMenu.MenuInitialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
spriteFont = Content.Load<SpriteFont>(@"Fonts\MainFont");
backgroundImage = Content.Load<Texture2D>(@"Backgrounds\titlescreen");
mainMenu.MenuLoadContent();
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
inputHandler.Update();
if (inputHandler.currentKeyState.IsKeyDown(Keys.Escape))
this.Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
spriteBatch.Draw(backgroundImage, new Vector2(0, 0), Color.White);
mainMenu.MenuDraw();
spriteBatch.End();
base.Draw(gameTime);
}
}
}
MainMenu.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace TestGame.GameStates
{
public class MainMenu : Main
{
enum buttonState { hover, up, released, down }
const int numberOfButtons = 4, newGameButtonIndex = 0, loadGameButtonIndex = 1, optionsButtonIndex = 2, quitButtonIndex = 3, buttonHeight = 48, buttonWidth = 80;
Color backgroundButtonColor;
Color[] buttonColor = new Color[numberOfButtons];
Rectangle[] buttonRect = new Rectangle[numberOfButtons];
buttonState[] buttonSt = new buttonState[numberOfButtons];
Texture2D[] buttonTexture = new Texture2D[numberOfButtons];
double[] buttonTimer = new double[numberOfButtons];
bool mousePressed, previouslyPressed = false;
int mouseX, mouseY;
double frameTime;
int buttonPadding;
public void MenuInitialize()
{
for (int i = 0; i < numberOfButtons; i++)
{
buttonSt = buttonState.up;
buttonColor = Color.White;
buttonTimer = 0.0;
buttonRect = new Rectangle(0, buttonPadding, buttonWidth, buttonHeight);
buttonPadding += buttonHeight;
}
}
public void MenuLoadContent()
{
buttonTexture[newGameButtonIndex] = Content.Load<Texture2D>(@"Sprites\desktop");
buttonTexture[loadGameButtonIndex] = Content.Load<Texture2D>(@"Sprites\desktop");
buttonTexture[optionsButtonIndex] = Content.Load<Texture2D>(@"Sprites\desktop");
buttonTexture[quitButtonIndex] = Content.Load<Texture2D>(@"Sprites\desktop");
}
public void MenuDraw()
{
for (int i = 0; i < numberOfButtons; i++)
{
spriteBatch.Draw(buttonTexture, buttonRect, buttonColor);
}
}
}
}