// load game data from a storage device public static GameData Load(StorageDevice device, string name) { // return a new game data object on failure GameData data = new GameData(); // get a container reference (hard drive or memory card) //StorageContainer container = device.OpenContainer(GameName); StorageContainer container = OpenContainer(device, GameName); // build the filename for the save game //string filename = Path.Combine(container.Path, name + ".xml"); string filename = name + ".xml"; // only attempt a load if we know the file is actually there //if (File.Exists(filename)) //{ // // open the file and read our game data // FileStream stream = File.Open(filename, FileMode.Open); // XmlSerializer serializer = new XmlSerializer(typeof(GameData)); // data = (GameData)serializer.Deserialize(stream); // stream.Close(); //} if (container.FileExists(filename)) { using (Stream stream = container.OpenFile(filename, FileMode.Open)) { XmlSerializer serializer = new XmlSerializer(typeof(GameData)); data = (GameData)serializer.Deserialize(stream); stream.Close(); } } // dispose of the container. if you forget to call dispose, // your game will have issues if you try to load or save again. container.Dispose(); // return the game data object return data; }
// save game data to a storage device public static void Save(StorageDevice device, GameData data, string name) { // get a container reference (hard drive or memory card) //StorageContainer container = device.OpenContainer(GameName); StorageContainer container = OpenContainer(device, GameName); // build the filename for the save game //string filename = Path.Combine(container.Path, name + ".xml"); // open the file and write our game data //FileStream stream = File.Open(filename, FileMode.Create); //XmlSerializer serializer = new XmlSerializer(typeof(GameData)); //serializer.Serialize(stream, data); //stream.Close(); using (Stream stream = container.OpenFile(name + ".xml", FileMode.Create)) { XmlSerializer serializer = new XmlSerializer(typeof(GameData)); serializer.Serialize(stream, data); stream.Close(); } // dispose of the container. data isn't truely saved until // you make this call. if you forget to call dispose, your // game will have issues if you try to load or save again. container.Dispose(); }
protected void ProcessInput(GamePadState pad1, KeyboardState key1) { // is the player moving left or right? if (pad1.ThumbSticks.Left.X < 0) { m_cursor.X += pad1.ThumbSticks.Left.X * 3.0f; } else if (key1.IsKeyDown(Keys.Left)) { m_cursor.X -= 3.0f; } else if (pad1.ThumbSticks.Left.X > 0) { m_cursor.X += pad1.ThumbSticks.Left.X * 3.0f; } else if (key1.IsKeyDown(Keys.Right)) { m_cursor.X += 3.0f; } // is the player moving up or down? if (pad1.ThumbSticks.Left.Y < 0) { m_cursor.Y -= pad1.ThumbSticks.Left.Y * 3.0f; } else if (key1.IsKeyDown(Keys.Down)) { m_cursor.Y += 3.0f; } else if (pad1.ThumbSticks.Left.Y > 0) { m_cursor.Y -= pad1.ThumbSticks.Left.Y * 3.0f; } else if (key1.IsKeyDown(Keys.Up)) { m_cursor.Y -= 3.0f; } // is the player pressing the clear button? if (pad1.Buttons.Start == ButtonState.Pressed || key1.IsKeyDown(Keys.Enter)) { // clear our list of stamps m_data.Stamps.Clear(); } // is the player pressing the action button? if (pad1.Buttons.A == ButtonState.Pressed || key1.IsKeyDown(Keys.Space)) { // only register a save or load if there's not // a save or load currently in progress if (m_resultStorage == null) { // clear the button press states m_pressedSave = false; m_pressedLoad = false; // is player pressing the save button? if (InRect(m_cursor, m_rectSave, m_posButtonSave)) { m_pressedSave = true; } // is player pressing the load button? else if (InRect(m_cursor, m_rectLoad, m_posButtonLoad)) { m_pressedLoad = true; } // add a new stamp to our list else if (!m_data.Stamps.Contains(m_cursor)) { m_data.Stamps.Add(m_cursor); } } } // the player isn't pressing the action button else { // there is no load or save in progress if (m_resultStorage == null) { // the player just released the action button if (m_pressedLoad || m_pressedSave) { // show the storage guide on Xbox, has no // effect on Windows m_resultStorage = //Guide.BeginShowStorageDeviceSelector(null, null); StorageDevice.BeginShowSelector(null, null); } } // there is a load or save in progress else { // has the player selected a device? if (m_resultStorage.IsCompleted) { // get a reference to the selected device m_storage = //Guide.EndShowStorageDeviceSelector(m_resultStorage); StorageDevice.EndShowSelector(m_resultStorage); // save was requested, save our data if (m_pressedSave) { GameStorage.Save(m_storage, m_data, "test01"); } // load was requested, load our data else if (m_pressedLoad) { m_data = GameStorage.Load(m_storage, "test01"); } // reset up our load / save state data m_storage = null; m_resultStorage = null; m_pressedSave = false; m_pressedLoad = false; } } } }