- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/PendulumGame/Screens/PendulumVideoScreen.cs

    00001 ﻿#region File Description
    00002 //-----------------------------------------------------------------------------
    00003 // PendulumVideoScreen.cs
    00004 //-----------------------------------------------------------------------------
    00005 #endregion
    00006
    00007 namespace PendulumGame
    00008 {
    00009     using System;
    00010     using Microsoft.Xna.Framework;
    00011     using Microsoft.Xna.Framework.Content;
    00012     using Microsoft.Xna.Framework.Graphics;
    00013     using Microsoft.Xna.Framework.Media;
    00014     using Microsoft.Xna.Framework.Input;
    00015
    00016     using NewGamePhysics.StateManager;
    00017     using NewGamePhysics.Utilities;
    00018
    00022     class PendulumVideoScreen : GameScreen
    00023     {
    00024         #region Fields
    00025
    00029         ContentManager content;
    00030
    00034         Video videoFile;
    00035
    00039         bool videoStarted = false;
    00040
    00044         Effect effect;
    00045
    00049         Color referenceColor;
    00050
    00051         #endregion
    00052
    00053         #region Initialization
    00054
    00058         public PendulumVideoScreen()
    00059         {
    00060             TransitionOnTime = TimeSpan.FromSeconds(0.5);
    00061             TransitionOffTime = TimeSpan.FromSeconds(0.5);
    00062         }
    00063
    00071         public override void LoadContent()
    00072         {
    00073             if (content == null)
    00074             {
    00075                 content = new ContentManager(ScreenManager.Game.Services, "Content");
    00076             }
    00077
    00078             // Pick random video
    00079             int videoNumber = PendulumGame.State.PhysicalRandomNumberGenerator.Next(0, 12);
    00080
    00081             videoFile = content.Load<Video>(@"Videos\Pendulum" + videoNumber);
    00082             effect = content.Load<Effect>(@"Effects\ColorChromaKey");
    00083
    00084             // Determine chromakey reference color by averaging
    00085             // the color over the reference image
    00086             Texture2D backgroundReference =
    00087                 content.Load<Texture2D>(@"Textures\PendulumBackgroundReference");
    00088             Color[] colors = TextureHelpers.TextureToColors(backgroundReference);
    00089             ulong rSum = 0;
    00090             ulong gSum = 0;
    00091             ulong bSum = 0;
    00092             ulong num = (ulong)colors.Length;
    00093             foreach (Color color in colors)
    00094             {
    00095                 rSum += (ulong)color.R;
    00096                 gSum += (ulong)color.G;
    00097                 bSum += (ulong)color.B;
    00098             }
    00099
    00100             this.referenceColor = new Color(
    00101                 (byte)(rSum / num),
    00102                 (byte)(gSum / num),
    00103                 (byte)(bSum / num));
    00104         }
    00105
    00109         public override void UnloadContent()
    00110         {
    00111             content.Unload();
    00112         }
    00113
    00114
    00115         #endregion
    00116
    00121         public override void HandleInput(InputState input)
    00122         {
    00123             if (input == null)
    00124             {
    00125                 throw new ArgumentNullException("input");
    00126             }
    00127
    00128             if (input.IsInputCancel(null) || input.IsDisconnected(null))
    00129             {
    00130                 ScreenManager.AddScreen(new PendulumPauseMenuScreen(), null);
    00131             }
    00132             else
    00133             {
    00134                 PlayerIndex playerIndex;
    00135
    00136                 // Back to main menu
    00137                 if (input.IsMenuCancel(null, out playerIndex) ||
    00138                     input.IsMenuSelect(null, out playerIndex))
    00139                 {
    00140                     PendulumLoadingScreen.Load(
    00141                         ScreenManager, false, null,
    00142                         new PendulumBackgroundScreen(),
    00143                         new PendulumMainMenuScreen());
    00144                 }
    00145             }
    00146         }
    00147
    00148         #region Update and Draw
    00149
    00157         public override void Update(GameTime gameTime, bool otherScreenHasFocus,
    00158                                                        bool coveredByOtherScreen)
    00159         {
    00160             if (!videoStarted)
    00161             {
    00162                 ScreenManager.VideoPlayer.Play(videoFile);
    00163                 videoStarted = true;
    00164             }
    00165             else
    00166             {
    00167                 if (ScreenManager.VideoPlayer.State == MediaState.Stopped)
    00168                 {
    00169                     // Return to main menu
    00170                     PendulumLoadingScreen.Load(ScreenManager, false, null,
    00171                         new PendulumBackgroundScreen(),
    00172                         new PendulumMainMenuScreen());
    00173                 }
    00174             }
    00175
    00176             // If the game is transitioning on or off, fade it out to black.
    00177             if (TransitionPosition > 0)
    00178             {
    00179                 ScreenManager.FadeBackBufferToBlack(255 - TransitionAlpha);
    00180             }
    00181
    00182             base.Update(gameTime, otherScreenHasFocus, false);
    00183         }
    00184
    00185
    00189         public override void Draw(GameTime gameTime)
    00190         {
    00191             SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
    00192             Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
    00193             Rectangle fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height);
    00194             byte fade = TransitionAlpha;
    00195
    00196             // Render the video in it's orginal resolution
    00197             // to the screen using SpriteBatch
    00198             if (ScreenManager.VideoPlayer.State == MediaState.Playing)
    00199             {
    00200                 spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
    00201                 effect.Begin();
    00202                 float threshold = 0.3f;
    00203                 effect.Parameters["referenceColor"].SetValue(this.referenceColor.ToVector3());
    00204                 effect.Parameters["threshold"].SetValue(threshold);
    00205                 foreach (EffectPass pass in effect.CurrentTechnique.Passes)
    00206                 {
    00207                     pass.Begin();
    00208                     spriteBatch.Draw(
    00209                         ScreenManager.VideoPlayer.GetTexture(),
    00210                         new Rectangle(
    00211                             (viewport.Width - videoFile.Width) / 2,
    00212                             (viewport.Height - videoFile.Height) / 2,
    00213                             videoFile.Width,
    00214                             videoFile.Height),
    00215                         new Color(fade, fade, fade));
    00216                     pass.End();
    00217                 }
    00218                 effect.End();
    00219                 spriteBatch.End();
    00220             }
    00221         }
    00222
    00223         #endregion
    00224     }
    00225 }
    00226