musicalglass
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion
// Tutorials in plain English by Dillinger
namespace Tutorials.Xna.SpriteClickable
{
/// <summary> This is the main hype for your game </summary>
public class GameEngine : Microsoft.Xna.Framework.Game
{
public GameEngine()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
}
/// <summary> Allows the game to perform any initialization it needs to ...</summary>
protected override void Initialize()
{
base.Initialize();
}
// Initialize Graphics Content
GraphicsDeviceManager graphics;
ContentManager content;
SpriteBatch spriteBatch;
Texture2D spriteTexture;
Color spriteColor;
// Declare a 2 space float variable for storing Sprite Vertical and Horizontal position ( X & Y )
Vector2 spritePosition = new Vector2(100f, 100f);
/// <summary> Load graphics content...</summary>
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
// Load Sprite resources
spriteBatch = new SpriteBatch(this.graphics.GraphicsDevice);
spriteTexture = content.Load<Texture2D>(@"Resources\Images\sprite1");
spriteColor = Color.White;
}
}
/// <summary> Unload your graphics content...</summary>
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent == true)
{
content.Unload();
}
}
/// <summary> Update Game Logic ... </summary>
protected override void Update(GameTime gameTime)
{
UpdateMouse();
checkMouseClick();
if (leftMouseClicked == true) // Don't bother collision checking unless mouse is clicked
{
OnMouseOver(); // Check if mouse is inside texture area
if (MouseOverSprite == true)
{
PixelCheck(); // Check if individual pixel is non Alpha
if (PixelDetected == true) spriteColor = Color.Red;
}
} else spriteColor = Color.White;
base.Update(gameTime);
}
/// <summary> Clear Screen & Draw Game ... </summary>
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.Black);
// Draw Sprite
spriteBatch.Begin();
spriteBatch.Draw(spriteTexture, spritePosition, spriteColor);
spriteBatch.End();
base.Draw(gameTime);
}
// Mouse coordinates are initialized at 0, 0
Vector2 mousePosition = Vector2.Zero;
void UpdateMouse()
{
MouseState mouseState = Mouse.GetState();
IsMouseVisible = true; // Show the mouse cursor ( default is false )
// The mouse X and Y positions are returned relative to the top-left of the game window.
mousePosition.X = mouseState.X;
mousePosition.Y = mouseState.Y;
} // end UpdateMouse
bool MouseOverSprite = false;
void OnMouseOver()
{
if ((mousePosition.X >= spritePosition.X) && mousePosition.X <= (spritePosition.X + spriteTexture.Width) &&
mousePosition.Y >= spritePosition.Y && mousePosition.Y <= (spritePosition.Y + spriteTexture.Height))
MouseOverSprite = true;
else MouseOverSprite = false;
} // end OnMouseOver
bool PixelDetected = false;
Vector2 pixelPosition = Vector2.Zero;
uint[] PixelData = new uint[1]; // Delare an Array of 1 just to store data for one pixel
void PixelCheck()
{
// Get Mouse position relative to top left of Texture
pixelPosition = mousePosition - spritePosition;
// I know. I just checked this condition at OnMouseOver or we wouldn't be here
// but just to be sure the spot we're checking is within the bounds of the texture...
if (pixelPosition.X >= 0 && pixelPosition.X < spriteTexture.Width &&
pixelPosition.Y >= 0 && pixelPosition.Y < spriteTexture.Height)
{
// Get the Texture Data within the Rectangle coords, in this case a 1 X 1 rectangle
// Store the data in pixelData Array
spriteTexture.GetData<uint>(0, new Rectangle((int)pixelPosition.X, (int)pixelPosition.Y, (1), (1)), PixelData, 0, 1);
// Check if pixel in Array is non Alpha, give or take 20
if (((PixelData[0] & 0xFF000000) >> 24) > 20)
PixelDetected = true;
else PixelDetected = false;
}
} // end PixelCheck
bool leftMouseClicked = false;
void checkMouseClick()
{
MouseState mouseState = Mouse.GetState();
if (mouseState.LeftButton == ButtonState.Pressed)
leftMouseClicked = true;
else leftMouseClicked = false;
} // end checkMouseClick
}
}