Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Aug 23rd, 2011  |  syntax: C#  |  size: 4.69 KB  |  views: 86  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11. using SerializationTest.Data;
  12. using System.Xml;
  13. using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;
  14.  
  15.  
  16. namespace SerializationTest.Modes
  17. {
  18.     /// <summary>
  19.     /// This is a game component that implements IUpdateable.
  20.     /// </summary>
  21.     public class Mode4 : Microsoft.Xna.Framework.DrawableGameComponent
  22.     {
  23.         private SpriteBatch spriteBatch;
  24.         SharedResourceList<string> listTest = new SharedResourceList<string>();
  25.         SharedResourceDictionary<int, string> dictTest = new SharedResourceDictionary<int, string>();
  26.         int currKey = 0;
  27.  
  28.         // Keyboard polling helpers
  29.         private KeyboardState currentState;
  30.         private KeyboardState previousState;
  31.  
  32.         private MouseState currentMouseState;
  33.         private MouseState previousMouseState;
  34.  
  35.         public Mode4(Game game)
  36.             : base(game)
  37.         {
  38.             // TODO: Construct any child components here
  39.         }
  40.  
  41.         /// <summary>
  42.         /// Allows the game component to perform any initialization it needs to before starting
  43.         /// to run.  This is where it can query for any required services and load content.
  44.         /// </summary>
  45.         public override void Initialize()
  46.         {
  47.             base.Initialize();
  48.             this.spriteBatch = new SpriteBatch(Game.GraphicsDevice);
  49.         }
  50.  
  51.         /// <summary>
  52.         /// Allows the game component to update itself.
  53.         /// </summary>
  54.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  55.         public override void Update(GameTime gameTime)
  56.         {
  57.             // Key checking
  58.             previousState = currentState;
  59.             currentState = Keyboard.GetState();
  60.  
  61.             previousMouseState = currentMouseState;
  62.             currentMouseState = Mouse.GetState();
  63.  
  64.             // STEP 1: User requests save / load
  65.             // When we press the "s" key
  66.             if (currentState.IsKeyUp(Keys.S) &&
  67.                 previousState.IsKeyDown(Keys.S))
  68.             {            
  69.  
  70.                 XmlWriterSettings settings = new XmlWriterSettings();
  71.                 settings.Indent = true;
  72.  
  73.                 using (XmlWriter writer = XmlWriter.Create("out.xml", settings))
  74.                 {
  75.                     IntermediateSerializer.Serialize(writer, dictTest, null);
  76.                     //IntermediateSerializer.Serialize(writer, listTest, null);
  77.                 }
  78.             }
  79.  
  80.             // When we press the "l" key
  81.             if (currentState.IsKeyUp(Keys.L) &&
  82.                 previousState.IsKeyDown(Keys.L))
  83.             {
  84.                 XmlReaderSettings settings = new XmlReaderSettings();
  85.                 using (XmlReader reader = XmlReader.Create("out.xml", settings))
  86.                 {
  87.                     this.dictTest = IntermediateSerializer.Deserialize<SharedResourceDictionary<int,string>>(reader, null);
  88.                     //this.listTest = IntermediateSerializer.Deserialize<SharedResourceList<string>>(reader, null);
  89.                 }
  90.             }
  91.  
  92.            
  93.  
  94.             // Make the age change so we can see
  95.             // stuff change with the save / load
  96.             if (currentMouseState.LeftButton == ButtonState.Released && previousMouseState.LeftButton == ButtonState.Pressed)
  97.             {
  98.                 this.dictTest.Add(currKey, "Value" + currKey);
  99.                 //this.listTest.Add("Value" + currKey);
  100.                 currKey++;
  101.             }
  102.  
  103.             base.Update(gameTime);
  104.         }
  105.  
  106.         public override void Draw(GameTime gameTime)
  107.         {
  108.             base.Draw(gameTime);
  109.             int y = 100;
  110.             this.spriteBatch.Begin();
  111.             foreach(KeyValuePair<int,string> pair in dictTest)
  112.             {
  113.                 this.spriteBatch.DrawString(
  114.                     Game.Content.Load<SpriteFont>("Font"),
  115.                     "Pair { " + pair.Key + " , " + pair.Value + " }",
  116.                     new Vector2(100, y += 30),
  117.                     Color.White);
  118.             }
  119.  
  120.             //foreach (string name in listTest)
  121.             //{
  122.             //    this.spriteBatch.DrawString(
  123.             //        Game.Content.Load<SpriteFont>("Font"),
  124.             //        name,
  125.             //        new Vector2(200, y += 30),
  126.             //        Color.White);
  127.             //}
  128.             this.spriteBatch.End();
  129.         }
  130.     }
  131. }