Skip to content

Commit

Permalink
Add some shiny 3D samples :D
Browse files Browse the repository at this point in the history
(Not all working in the public branch, we need to get cracking)
  • Loading branch information
espes committed Jun 14, 2012
1 parent 3548043 commit 7356743
Show file tree
Hide file tree
Showing 161 changed files with 29,776 additions and 11 deletions.
128 changes: 128 additions & 0 deletions Graphics3DSample/Animation/Animation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#region File Information
//-----------------------------------------------------------------------------
// Animation.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace Graphics3DSample
{
class Animation
{
#region Fields
// The texture with animation frames
Texture2D animationTexture;
// The size and structure of whole frames sheet in animationTexture. The animationTexture could
// hold animaton sequence organized in multiple rows and multiple columns, that's why animation
// engine should know how the frames are organized inside a frames sheet
Point sheetSize;
// Amount of time between frames
TimeSpan frameInterval;
// Time passed since last frame
TimeSpan nextFrame;

// Current frame in the animation sequence
public Point currentFrame;
// The size of single frame inside the animationTexture
public Point frameSize;
#endregion

#region Initialization
/// <summary>
/// Constructor of an animation class
/// </summary>
/// <param name="frameSheet">Texture with animation frames sheet</param>
/// <param name="size">Single frame size</param>
/// <param name="frameSheetSize">The whole frame sheet size</param>
/// <param name="interval">Interval between progressing to the next frame</param>
public Animation(Texture2D frameSheet, Point size, Point frameSheetSize, TimeSpan interval)
{
animationTexture = frameSheet;
frameSize = size;
sheetSize = frameSheetSize;
frameInterval = interval;
}
#endregion

#region Update and Render
/// <summary>
/// Updates the animaton progress
/// </summary>
/// <param name="gameTime"></param>
/// <param name="progressed">Returns true if animation were progressed; in such case
/// caller could updated the position of the animated character</param>
public bool Update(GameTime gameTime)
{
bool progressed;

// Check is it is a time to progress to the next frame
if (nextFrame >= frameInterval)
{
// Progress to the next frame in the row
currentFrame.X++;
// If reached end of the row advance to the next row
// and start form the first frame there
if (currentFrame.X >= sheetSize.X)
{
currentFrame.X = 0;
currentFrame.Y++;
}
// If reached last row in the frame sheet jump to the first row again - produce endless loop
if (currentFrame.Y >= sheetSize.Y)
currentFrame.Y = 0;

// Reset interval for next frame
progressed = true;
nextFrame = TimeSpan.Zero;
}
else
{
// Wait for the next frame
nextFrame += gameTime.ElapsedGameTime;
progressed = false;
}

return progressed;
}

/// <summary>
/// Rendering of the animation
/// </summary>
/// <param name="spriteBatch">SpriteBatch in which current frame will be rendered</param>
/// <param name="position">The position of current frame</param>
/// <param name="spriteEffect">SpriteEffect to apply on current frame</param>
public void Draw(SpriteBatch spriteBatch, Vector2 position, SpriteEffects spriteEffect)
{
Draw(spriteBatch, position, 1.0f, spriteEffect);
}

/// <summary>
/// Rendering of the animation
/// </summary>
/// <param name="spriteBatch">SpriteBatch in which current frame will be rendered</param>
/// <param name="position">The position of the current frame</param>
/// <param name="scale">Scale factor to apply on the current frame</param>
/// <param name="spriteEffect">SpriteEffect to apply on the current frame</param>
public void Draw(SpriteBatch spriteBatch, Vector2 position, float scale, SpriteEffects spriteEffect)
{
spriteBatch.Draw(animationTexture, position, new Rectangle(
frameSize.X * currentFrame.X,
frameSize.Y * currentFrame.Y,
frameSize.X,
frameSize.Y),
Color.White, 0f, Vector2.Zero, scale, spriteEffect, 0);
}
#endregion
}
}
87 changes: 87 additions & 0 deletions Graphics3DSample/Buttons/Button.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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 Microsoft.Xna.Framework.Input.Touch;


namespace Graphics3DSample
{
/// <summary>
/// A game component, inherits to Clickable.
/// Has associated content.
/// Has an integer Value that is incremented by click.
/// Draws content.
/// </summary>
public class Button : Clickable
{

#region Fields
readonly string asset;
Texture2D texture;
int value;

#region Public accessors
public int Value { get { return value; } }
#endregion
#endregion

#region Initialization
/// <summary>
/// Constructor
/// </summary>
/// <param name="game">The Game object</param>
/// <param name="textureName">Texture Name</param>
/// <param name="targetRectangle">Position of the component on the screen</param>
/// <param name="initialValue">Initial value</param>
public Button (Graphics3DSampleGame game, string textureName, Rectangle targetRectangle, int initialValue)
: base(game, targetRectangle)
{
asset = textureName;
value = initialValue;
}

/// <summary>
/// Load the button's texture
/// </summary>
protected override void LoadContent()
{
texture = Game.Content.Load<Texture2D>(asset);
base.LoadContent();
}
#endregion

#region Update and render
/// <summary>
/// Allows the game component to update itself
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{
HandleInput();
if (IsClicked)
++value;
base.Update(gameTime);
}

/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Draw(GameTime gameTime)
{
var color = IsTouching ? Color.Wheat : Color.White;
Game.SpriteBatch.Begin();
Game.SpriteBatch.Draw(texture, Rectangle, color);
Game.SpriteBatch.End();
base.Draw(gameTime);
}
#endregion
}
}
85 changes: 85 additions & 0 deletions Graphics3DSample/Buttons/Checkbox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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 Microsoft.Xna.Framework.Input.Touch;


namespace Graphics3DSample
{
/// <summary>
/// A game component, inherits to Clickable.
/// Has associated On and Off content.
/// Has a state of IsChecked that is switched by click.
/// Draws content according to state.
/// </summary>
public class Checkbox : Clickable
{
#region Fields
readonly string asset;
Texture2D textureOn;
bool isChecked;

#region Public accessors
public bool IsChecked { get { return isChecked; } }
#endregion
#endregion

#region Initialization
/// <summary>
///
/// </summary>
/// <param name="game">The Game object</param>
/// <param name="textureName">Texture name</param>
/// <param name="targetRectangle">Position of the component on the screen</param>
/// <param name="isChecked">Initial state of the checkbox</param>
public Checkbox(Graphics3DSampleGame game, string textureName, Rectangle targetRectangle, bool isChecked)
: base(game, targetRectangle)
{
asset = textureName;
this.isChecked = isChecked;
}

/// <summary>
/// Load the texture
/// </summary>
protected override void LoadContent()
{
textureOn = Game.Content.Load<Texture2D>(asset);
base.LoadContent();
}
#endregion

#region Update and render
/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{
HandleInput();
isChecked = IsClicked ? !isChecked : isChecked;
base.Update(gameTime);
}

/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Draw(GameTime gameTime)
{
Game.SpriteBatch.Begin();
Game.SpriteBatch.Draw(textureOn, Rectangle,
IsChecked ? Color.Yellow : Color.White);
Game.SpriteBatch.End();
base.Draw(gameTime);
}
#endregion
}
}
72 changes: 72 additions & 0 deletions Graphics3DSample/Buttons/Clickable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;


namespace Graphics3DSample
{
/// <summary>
/// A game component.
/// Has an associated rectangle.
/// Accepts touch and click inside the rectangle.
/// Has a state of IsTouching and IsClicked.
/// </summary>
public class Clickable : DrawableGameComponent
{

#region Fields
readonly Rectangle rectangle;
bool wasTouching;
bool isTouching;

#region Protected accessors
public bool IsTouching { get { return isTouching; } }
public bool IsClicked { get { return (wasTouching == true) && (isTouching == false); } }

protected Rectangle Rectangle { get { return rectangle; } }
protected new Graphics3DSampleGame Game { get { return (Graphics3DSampleGame)base.Game; } }
#endregion
#endregion

#region Initialization
/// <summary>
/// Constructor
/// </summary>
/// <param name="game">The Game oject</param>
/// <param name="targetRectangle">Position of the component on the screen</param>
public Clickable(Graphics3DSampleGame game, Rectangle targetRectangle)
: base(game)
{
rectangle = targetRectangle;
}
#endregion

#region Input handling
/// <summary>
/// Handles Input
/// </summary>
protected void HandleInput()
{
wasTouching = isTouching;
isTouching = false;

TouchCollection touches = TouchPanel.GetState();

if (touches.Count > 0)
{
var touch = touches[0];
var position = touch.Position;


Rectangle touchRect = new Rectangle((int)touch.Position.X - 5, (int)touch.Position.Y - 5,
10, 10);

if (rectangle.Intersects(touchRect))
isTouching = true;
}

}
#endregion
}
}


7 changes: 7 additions & 0 deletions Graphics3DSample/Content/AnimationDef.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<Animations>
<Definition FrameWidth="128" FrameHeight="128"
SheetRows="1" SheetColumns="6"
Speed="10"
SheetName="Textures/explosionStrip"/>
</Animations>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file added Graphics3DSample/Content/Buttons/lamp_60x60.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Graphics3DSample/Content/Buttons/lamp_60x60.xnb
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file added Graphics3DSample/Content/Buttons/textureOnOff.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Graphics3DSample/Content/Buttons/textureOnOff.xnb
Binary file not shown.
Binary file added Graphics3DSample/Content/Models/enemy.tga
Binary file not shown.
Binary file added Graphics3DSample/Content/Models/enemy_0.xnb
Binary file not shown.
Binary file added Graphics3DSample/Content/Models/spaceship.fbx
Binary file not shown.
Binary file added Graphics3DSample/Content/Models/spaceship.xnb
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file added Graphics3DSample/Content/Textures/spaceBG.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Graphics3DSample/Content/Textures/spaceBG.xnb
Binary file not shown.
Loading

0 comments on commit 7356743

Please sign in to comment.