/NES EMU 7/NES EMU 7/NES EMU 7/SharpNES.cs
C# | 427 lines | 326 code | 65 blank | 36 comment | 43 complexity | 4b39904d31cd88bdc95417b49cdc7105 MD5 | raw file
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.GamerServices;
- using TouchThumbsticks;
- //using XNAExtras;
-
- namespace XNASharpNES
- {
- public class SharpNES : Microsoft.Xna.Framework.Game
- {
- #region Entry Point
-
-
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- static void Main()
- {
- using (SharpNES game = new SharpNES())
- {
- game.Run();
- }
- }
-
-
- #endregion
-
- #region Menu Constants
- private static readonly Color BackgroundColor = Color.White;
- private static readonly Color TitleColor = Color.White;
- private static readonly Color MenuItemColor = Color.Black;
- private static readonly Color SelectedItemColor = Color.DarkGreen;
-
- private const int NumMenuItems = 24;
-
- private Vector2 MenuPosition;
- private int MenuItemHeight;
- #endregion
-
- private SpriteFont MenuSpriteFont;
- private GraphicsDeviceManager graphics;
- private ContentManager content;
- private SpriteBatch spriteBatch;
- private Texture2D targetTexture;
- private Texture2D backgroundTexture;
- private Texture2D helpBackgroundTexture;
- private GamePadHelper menuPad;
- private Rectangle screenRectangle;
- private NesEngine myEngine = new NesEngine();
- private IGameComponent s;
- private static FpsCounter FPS = null;
- //public static TitleContainer saveDevice = null;
- private string[] roms;
- private bool waitForBackRelease = false;
- private bool running = false;
- private int topMenuRom;
- private int selectedRom;
- // a texture used to draw the virtual thumbsticks on the screen
- private Texture2D thumbstick;
- private Texture2D controllerTex;
-
-
- Object stateobj;
-
- public static FpsCounter FpsCounter
- {
- get
- {
- return FPS;
- }
- }
-
- public SharpNES()
- {
-
- graphics = new GraphicsDeviceManager(this);
- graphics.SynchronizeWithVerticalRetrace = false;
- TargetElapsedTime = TimeSpan.FromSeconds(1 / 30f);
- content = new ContentManager(Services, "Content");
- IsFixedTimeStep = false;
- FPS = new FpsCounter(this);
- menuPad = new GamePadHelper(20);
- Components.Add(FPS);
-
- #if WINDOWS_PHONE
- graphics.IsFullScreen = true;
- #endif
-
- LoadCart("smb3.nes");
- }
-
- void GetDevice(IAsyncResult result)
- {
- //saveDevice = Guide.EndShowStorageDeviceSelector(result);
- //if (saveDevice.IsConnected)
- {
- List<string> files = new List<string>();
- //files.AddRange(Directory.GetFiles());
- int i = 0;
- while (i < files.Count)
- {
- if (files[i].ToString().ToLower().EndsWith(".nes"))
- i++;
- else
- files.RemoveAt(i);
- }
- roms = (string[])files.ToArray();
-
- Array.Sort(roms);
-
- string lastRom = LoadRomName();
- selectedRom = Array.IndexOf(roms, lastRom);
- if (selectedRom < 0)
- {
- selectedRom = 0;
- }
-
- topMenuRom = 0;
- }
- }
-
- protected override void Initialize()
- {
- //For some reason LoadContent() Won't do the job :/
- spriteBatch = new SpriteBatch(graphics.GraphicsDevice); ;
- screenRectangle = new Rectangle(
- 0, 0,
- graphics.PreferredBackBufferWidth,
- graphics.PreferredBackBufferHeight
- );
- targetTexture = new Texture2D(
- graphics.GraphicsDevice,
- 256, 224, false,
- SurfaceFormat.Bgr565
- );
-
- //Menu Stuff
- backgroundTexture = content.Load<Texture2D>("bg") as Texture2D;
- helpBackgroundTexture = content.Load<Texture2D>("help") as Texture2D;
- thumbstick = content.Load<Texture2D>("thumbstick");
- controllerTex = content.Load<Texture2D>("nescontroller4ad");
- LoadFonts();
- }
-
- protected override void LoadContent()
- {
-
- }
-
- private void LoadFonts()
- {
- MenuItemHeight = 16 * 4 / 3;
- MenuPosition = new Vector2(graphics.PreferredBackBufferWidth / 8, 100 + MenuItemHeight);
- MenuSpriteFont = content.Load<SpriteFont>("Verdana");
- }
-
-
- protected override void UnloadContent() { }
-
- protected override void Update(GameTime gameTime)
- {
- if (running)
- {
- UpdateGame();
- }
- else
- {
- UpdateMenu();
- }
-
- base.Update(gameTime);
- }
-
- protected override void Draw(GameTime gameTime)
- {
- if (running)
- {
- DrawGame();
- }
- else
- {
- DrawMenu();
- }
- base.Draw(gameTime);
- }
-
- private void UpdateGame()
- {
- GamePadState pad = GamePad.GetState(PlayerIndex.One);
-
- if (((pad.Buttons.Back == ButtonState.Pressed) &&
- (pad.Buttons.RightShoulder == ButtonState.Pressed)) || Keyboard.GetState().IsKeyDown(Keys.Escape))
- {
- StopCart();
- waitForBackRelease = true;
- return;
- }
-
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
-
- myEngine.RunCart();
- myEngine.RunCart();
- }
-
- private void DrawGame()
- {
- GraphicsDevice.Textures[0] = null;
-
- targetTexture.SetData<short>(
- myEngine.myPPU.offscreenBuffer,
- 256 * 8,
- targetTexture.Width * targetTexture.Height
- );
-
- spriteBatch.Begin(0, BlendState.Opaque);
- spriteBatch.Draw(targetTexture, screenRectangle, Color.White);
- DrawDebug();
-
- // begin a new batch without the camera transformation for our UI
-
- // if the user is touching the screen and the thumbsticks have positions,
- // draw our thumbstick sprite so the user knows where the centers are
- if (VirtualThumbsticks.LeftThumbstickCenter.HasValue)
- {
- spriteBatch.Draw(
- thumbstick,
- VirtualThumbsticks.LeftThumbstickCenter.Value - new Vector2(thumbstick.Width / 2f, thumbstick.Height / 2f),
- Color.Green);
- }
-
- if (VirtualThumbsticks.RightThumbstickCenter.HasValue)
- {
- //spriteBatch.Draw(
- // thumbstick,
- // VirtualThumbsticks.RightThumbstickCenter.Value - new Vector2(thumbstick.Width / 2f, thumbstick.Height / 2f),
- // Color.Blue);
- }
-
- spriteBatch.Draw(controllerTex, Joypad.aButtonRect, new Rectangle(625, 200, 85, 85), Color.White);
-
- spriteBatch.Draw(controllerTex, Joypad.bButtonRect, new Rectangle(520, 200, 85, 85), Color.White);
-
- spriteBatch.Draw(controllerTex, Joypad.startButtonRect, new Rectangle(385, 226, 75, 35), Color.White);
-
- spriteBatch.Draw(controllerTex, Joypad.selectButtonRect, new Rectangle(385, 226, 75, 35), Color.White);
-
- spriteBatch.End();
- }
-
- private void UpdateMenu()
- {
- menuPad.Update();
- GamePadState pad = GamePad.GetState(PlayerIndex.One);
- if (waitForBackRelease)
- {
- // Since the back button is used to leave the game,
- // we need to wait here for it to be released.
- if (menuPad.BackIsPressed)
- {
- return;
- }
-
- menuPad.Reset();
- waitForBackRelease = false;
- }
-
- if (menuPad.BackIsPressed)
- {
- Quit();
- }
-
- if (roms != null && roms.Length > 0)
- {
-
- if (menuPad.StartIsPressed || menuPad.AIsPressed || Keyboard.GetState().IsKeyDown(Keys.A))
- {
- LoadCart(roms[selectedRom]);
- }
- else if (menuPad.UpWasPressed || menuPad.LeftWasPressed || Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.Up))
- {
- selectedRom = Math.Max(selectedRom - 1, 0);
-
- if (selectedRom < topMenuRom)
- {
- topMenuRom--;
- }
- }
- else if (menuPad.DownWasPressed || menuPad.RightWasPressed || Keyboard.GetState().IsKeyDown(Keys.Down) || Keyboard.GetState().IsKeyDown(Keys.Right))
- {
- selectedRom = Math.Min(selectedRom + 1, roms.Length - 1);
-
- if (selectedRom >= topMenuRom + NumMenuItems)
- {
- topMenuRom++;
- }
- }
- }
- }
-
- private void DrawDebug()
- {
- //#if DEBUG
- spriteBatch.DrawString( MenuSpriteFont,
- FpsCounter.FPS.ToString() + "fps" + "\n",
- //"Real Resolution: " + graphics.PreferredBackBufferWidth + " x " + graphics.PreferredBackBufferHeight + "\n" +
- //"Rendered Resolution: " + Window.ClientBounds.Width + " x " + Window.ClientBounds.Height + "\n" +
- //"ROM Loaded: " + roms[selectedRom],
- new Vector2( 50, 50 ), Color.Black );
- //#endif
- }
-
- private void DrawMenu()
- {
- GamePadState pad = GamePad.GetState(PlayerIndex.One);
- graphics.GraphicsDevice.Clear(BackgroundColor);
- spriteBatch.Begin();
- spriteBatch.Draw(backgroundTexture, new Vector2(0, 0), Color.White);
- DrawDebug();
- if (roms != null)
- {
- if (roms.Length == 0)
- {
- spriteBatch.DrawString(MenuSpriteFont, "No ROMs Available.", MenuPosition, Color.Black);
- }
- else
- {
- DrawRomNames();
- }
- if (pad.Buttons.LeftShoulder == ButtonState.Pressed)
- {
- DrawHelp();
- }
- }
- spriteBatch.End();
- }
-
- private void DrawHelp()
- {
- spriteBatch.Draw(helpBackgroundTexture, new Vector2(0, 0), Color.White);
- }
-
- private void DrawRomNames()
- {
- for (int index = 0; index < NumMenuItems; index++)
- {
- int currentMenuRom = topMenuRom + index;
- Color itemColor = (currentMenuRom == selectedRom) ? SelectedItemColor : MenuItemColor;
-
- if (roms.Length > currentMenuRom)
- {
- spriteBatch.DrawString(
- MenuSpriteFont,
- Path.GetFileNameWithoutExtension(roms[currentMenuRom]),
- new Vector2(
- MenuPosition.X,
- MenuPosition.Y + index * MenuItemHeight
- ),
- itemColor
- );
- }
- }
- }
-
- private void SaveRomName(string rom)
- {
- //using (TitleContainer.OpenStream("SharpNES"))
- {
- //using (StreamWriter writer = File.CreateText(Path.Combine(container.Path, "LastRom.txt")))
- {
- // writer.Write(rom);
- }
- }
- }
-
- private string LoadRomName()
- {
- string result = String.Empty;
-
- //using (StorageContainer container = saveDevice.OpenContainer("SharpNES"))
- {
- //string filename = Path.Combine(container.Path, "LastRom.txt");
-
- //if (File.Exists(filename))
- {
- // using (StreamReader reader = File.OpenText(filename))
- // result = reader.ReadLine();
- }
- }
-
- return result;
- }
-
- private void LoadCart(string filename)
- {
- SaveRomName(filename);
- myEngine.LoadCart(filename);
- myEngine.LoadRam();
- myEngine.StartCart();
- running = true;
- }
-
- private void StopCart()
- {
- //using (StorageContainer container = saveDevice.OpenContainer("SharpNES"))
- {
- //myEngine.SaveRamDirectory = container.Path;
- myEngine.StopCart(); // Writes SaveRam
- }
- running = false;
- }
-
- private void Quit()
- {
- StopCart();
- this.Exit();
- }
- }
- }