Inheritance: IGraphicDevice
Example #1
0
        public void OpenRom(string romPath)
        {
            _romFileName = romPath;
            if (File.Exists(romPath))
            {
                #region Check if archive

                var extension = Path.GetExtension(romPath);
                if (extension != null && extension.ToLower() != ".nes")
                {
                    try
                    {
                        _extractor = new SevenZipExtractor(romPath);
                    }
                    catch
                    {
                    }
                    if (_extractor.ArchiveFileData.Count == 1)
                    {
                        if (
                            _extractor.ArchiveFileData[0].FileName.Substring(
                                _extractor.ArchiveFileData[0].FileName.Length - 4, 4).ToLower() == ".nes")
                        {
                            _extractor.ExtractArchive(Path.GetTempPath());
                            romPath = Path.GetTempPath() + _extractor.ArchiveFileData[0].FileName;
                        }
                    }
                    else
                    {
                        var ar = new ArchivesForm(_extractor.ArchiveFileData.Select(file => file.FileName).ToArray());
                        ar.ShowDialog(this);
                        if (ar.Ok)
                        {
                            string[] fil = { ar.SelectedRom };
                            _extractor.ExtractFiles(Path.GetTempPath(), fil);
                            romPath = Path.GetTempPath() + ar.SelectedRom;
                        }
                        else
                        {
                            return;
                        }
                    }
                }

                #endregion

                #region Check the rom

                var header = new NesHeaderReader(romPath);
                if (header.ValidRom)
                {
                    if (!header.SupportedMapper())
                    {
                        MessageBox.Show("Can't load rom:\n" + romPath +
                                        "\n\nUNSUPPORTED MAPPER # " +
                                        header.MemoryMapper);
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("Can't load rom:\n" + romPath +
                                    "\n\nRom is damaged or not an INES format file");
                    return;
                }

                #endregion

                //Exit current thread
                if (_engine != null)
                {
                    _engine.ShutDown();
                    _engine = null;
                }
                if (_gameThread != null)
                {
                    _gameThread.Abort();
                }
                //Start new nes !!
                if (Program.Settings.PaletteFormat == null)
                    Program.Settings.PaletteFormat = new PaletteFormat();
                _engine = new NesEngine(Program.Settings.TV, Program.Settings.PaletteFormat);
                _engine.PauseToggle += NesPauseToggle;
                if (_engine.LoadRom(romPath))
                {
                    #region Setup input

                    var manager = new InputManager(Handle);
                    var joy1 = new Joypad(manager);
                    joy1.A.Input = Program.Settings.CurrentControlProfile.Player1A;
                    joy1.B.Input = Program.Settings.CurrentControlProfile.Player1B;
                    joy1.Up.Input = Program.Settings.CurrentControlProfile.Player1Up;
                    joy1.Down.Input = Program.Settings.CurrentControlProfile.Player1Down;
                    joy1.Left.Input = Program.Settings.CurrentControlProfile.Player1Left;
                    joy1.Right.Input = Program.Settings.CurrentControlProfile.Player1Right;
                    joy1.Select.Input = Program.Settings.CurrentControlProfile.Player1Select;
                    joy1.Start.Input = Program.Settings.CurrentControlProfile.Player1Start;
                    var joy2 = new Joypad(manager);
                    joy2.A.Input = Program.Settings.CurrentControlProfile.Player2A;
                    joy2.B.Input = Program.Settings.CurrentControlProfile.Player2B;
                    joy2.Up.Input = Program.Settings.CurrentControlProfile.Player2Up;
                    joy2.Down.Input = Program.Settings.CurrentControlProfile.Player2Down;
                    joy2.Left.Input = Program.Settings.CurrentControlProfile.Player2Left;
                    joy2.Right.Input = Program.Settings.CurrentControlProfile.Player2Right;
                    joy2.Select.Input = Program.Settings.CurrentControlProfile.Player2Select;
                    joy2.Start.Input = Program.Settings.CurrentControlProfile.Player2Start;
                    _engine.SetupInput(manager, joy1, joy2);

                    #endregion

                    #region Output

                    //Set the size
                    switch (Program.Settings.Size.ToLower())
                    {
                        case "x1":
                            Size = new Size(265, 305);
                            FormBorderStyle = FormBorderStyle.FixedDialog;
                            statusStrip1.SizingGrip = false;
                            break;
                        case "x2":
                            Size = new Size(521, 529);
                            FormBorderStyle = FormBorderStyle.FixedDialog;
                            statusStrip1.SizingGrip = false;
                            break;
                        case "stretch":
                            FormBorderStyle = FormBorderStyle.Sizable;
                            statusStrip1.SizingGrip = true;
                            break;
                    }
                    //The output devices
                    var mon = new SoundDeviceGeneral16(statusStrip1) { Stereo = Program.Settings.Stereo };
                    switch (Program.Settings.GFXDevice)
                    {
                        case GraphicDevices.Gdi:
                            var gdi = new VideoGdi(Program.Settings.TV, panel1);
                            _engine.SetupOutput(gdi, mon);
                            break;
                        case GraphicDevices.GdiHiRes:
                            var gdih = new VideoGdiHiRes(Program.Settings.TV, panel1,
                                                         _engine.Memory.Map.Cartridge.RomPath,
                                                         _engine.Memory.Map.Cartridge.ChrPages);
                            _engine.SetupOutput(gdih, mon);
                            break;
                        case GraphicDevices.SlimDx:
                            var sl = new VideoSlimDx(Program.Settings.TV, panel1, _engine.Memory.Map.Cartridge.Multi);
                            _engine.SetupOutput(sl, mon);
                            break;
                        default:
                            Program.Settings.GFXDevice = GraphicDevices.SlimDx;
                            var sl1 = new VideoSlimDx(Program.Settings.TV, panel1, _engine.Memory.Map.Cartridge.Multi);
                            _engine.SetupOutput(sl1, mon);
                            break;
                    }
                    if (_engine.Ppu.OutputDevice.SupportFullScreen)
                        _engine.Ppu.OutputDevice.FullScreen = Program.Settings.Fullscreen;
                    //Audio
                    _engine.SoundEnabled = Program.Settings.SoundEnabled;
                    _engine.Apu.Square1Enabled = Program.Settings.Square1;
                    _engine.Apu.Square2Enabled = Program.Settings.Square2;
                    _engine.Apu.DMCEnabled = Program.Settings.DMC;
                    _engine.Apu.TriangleEnabled = Program.Settings.Triangle;
                    _engine.Apu.NoiseEnabled = Program.Settings.Noize;
                    _engine.Apu.VRC6P1Enabled = Program.Settings.VRC6Pulse1;
                    _engine.Apu.VRC6P2Enabled = Program.Settings.VRC6Pulse2;
                    _engine.Apu.VRC6SawToothEnabled = Program.Settings.VRC6Sawtooth;
                    _engine.Apu.SetVolume(Program.Settings.Volume);

                    #endregion

                    #region Misc

                    _engine.Ppu.NoLimiter = Program.Settings.NoLimiter;
                    _engine.AutoSaveSram = Program.Settings.AutoSaveSRAM;

                    #endregion

                    //Launch
                    _myThreadCreator = _engine.Run;
                    _gameThread = new Thread(_myThreadCreator);
                    _gameThread.Start();
                    timer_FPS.Start();
                    StatusLabel4_status.BackColor = Color.Green;
                    StatusLabel4_status.Text = "ON";
                    //Add to the recent
                    AddRecent(romPath);
                    RefreshRecents();
                    //Set the name
                    Text = "My Nes - " + Path.GetFileNameWithoutExtension(romPath);
                }
                else
                {
                    MessageBox.Show("Can't load rom:\n" + romPath +
                                    "\n\nRom is damaged or not an INES format file !!");
                    if (_engine != null)
                    {
                        _engine.ShutDown();
                        _engine = null;
                    }
                    if (_gameThread != null)
                    {
                        _gameThread.Abort();
                    }
                    return;
                }
            }
        }
Example #2
0
 private void VideoToolStripMenuItemClick(object sender, EventArgs e)
 {
     //Pause
     if (_engine != null)
         _engine.Pause();
     //Show the dialog
     var optionForm = new VideoOptionForm(_engine.Memory.Map.Cartridge.Multi);
     optionForm.ShowDialog(this);
     //Applay the options if the Nes is null
     if (_engine != null)
     {
         _engine.SetupTv(Program.Settings.TV, Program.Settings.PaletteFormat);
         //Set the size
         switch (Program.Settings.Size.ToLower())
         {
             case "x1":
                 Size = new Size(265, 305);
                 FormBorderStyle = FormBorderStyle.FixedDialog;
                 statusStrip1.SizingGrip = false;
                 break;
             case "x2":
                 Size = new Size(521, 529);
                 FormBorderStyle = FormBorderStyle.FixedDialog;
                 statusStrip1.SizingGrip = false;
                 break;
             case "stretch":
                 FormBorderStyle = FormBorderStyle.Sizable;
                 statusStrip1.SizingGrip = true;
                 break;
         }
         switch (Program.Settings.GFXDevice)
         {
             case GraphicDevices.Gdi:
                 var gdi = new VideoGdi(Program.Settings.TV, panel1);
                 _engine.Ppu.OutputDevice = gdi;
                 break;
             case GraphicDevices.GdiHiRes:
                 var gdih = new VideoGdiHiRes(Program.Settings.TV, panel1, "", 0);
                 _engine.Ppu.OutputDevice = gdih;
                 break;
             case GraphicDevices.SlimDx:
                 var sli = new VideoSlimDx(Program.Settings.TV, panel1, _engine.Memory.Map.Cartridge.Multi);
                 _engine.Ppu.OutputDevice = sli;
                 break;
         }
         //Set fullscreen if available
         if (_engine.Ppu.OutputDevice.SupportFullScreen)
             _engine.Ppu.OutputDevice.FullScreen = Program.Settings.Fullscreen;
         _engine.Resume();
     }
 }