Beispiel #1
0
 private void HandleSavestates()
 {
     InputHandler.HandleKeypress(saveStateKey, () =>
     {
         if (isRunning && !helpMenuVisible && !fileDialogVisible)
         {
             SavestateManager.Savestate(SavestateManager.selectedSlot, cpu.GetState());
             ShowMessage($"Saved state to slot {SavestateManager.selectedSlot}", 2.5f);
         }
     });
     InputHandler.HandleKeypress(loadStateKey, () =>
     {
         if (isRunning && !helpMenuVisible && !fileDialogVisible)
         {
             try
             {
                 SavestateManager.LoadSavestate(SavestateManager.selectedSlot);
                 cpu.ApplyState(SavestateManager.GetSelectedState());
                 ShowMessage($"Loaded state from slot {SavestateManager.selectedSlot}", 2.5f);
             }
             catch (SlotIsEmptyException)
             {
                 ShowMessage($"Slot {SavestateManager.selectedSlot} is empty", 2.5f);
             }
         }
     });
 }
Beispiel #2
0
        protected override void Initialize()
        {
            MyraEnvironment.Game = this;

            LoadConfig();

            messages = new List <Message>();

            _graphics.PreferredBackBufferWidth  = 640;
            _graphics.PreferredBackBufferHeight = 320;

            _graphics.ApplyChanges();

            isPaused  = false;
            isRunning = false;

            InputHandler.SetBindings(new Dictionary <int, Keys>
            {
                { 0, Keys.X },
                { 1, Keys.D1 },
                { 2, Keys.D2 },
                { 3, Keys.D3 },
                { 4, Keys.Q },
                { 5, Keys.W },
                { 6, Keys.E },
                { 7, Keys.A },
                { 8, Keys.S },
                { 9, Keys.D },
                { 10, Keys.Z },
                { 11, Keys.C },
                { 12, Keys.D4 },
                { 13, Keys.R },
                { 14, Keys.F },
                { 15, Keys.V }
            });

            pixel = new Texture2D(_graphics.GraphicsDevice, 10, 10);
            Color[] data = new Color[10 * 10];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = Color.White;
            }
            pixel.SetData(data);

            TargetElapsedTime = TimeSpan.FromSeconds(1f / cyclesPerSecond);
            IsFixedTimeStep   = true;

            cpu           = new CPU();
            cpu.hiResMode = true;
            cpu.Reset();

            apu = new APU();
            apu.Initialize();

            SavestateManager.Initialize(savestateSlots);

            RewindManager.Initialize(rewindBufferSize);

            ShowMessage("Welcome to Crispy! Press F3 to load a game, or press F1 for help.", 9999);

            base.Initialize();
        }
Beispiel #3
0
        private void HandleFunctionKeys()
        {
            InputHandler.HandleKeypress(loadRomKey, () =>
            {
                if (!fileDialogVisible && !helpMenuVisible)
                {
                    fileDialog = new FileDialog(FileDialogMode.OpenFile)
                    {
                        Filter = "*.ch8"
                    };

                    fileDialog.Closed += (s, a) =>
                    {
                        isPaused          = false;
                        fileDialogVisible = false;
                        if (!fileDialog.Result)
                        {
                            return;
                        }

                        currentRomPath = fileDialog.FilePath;

                        cpu.Reset();

                        currentRom               = File.ReadAllBytes(currentRomPath);
                        currentRomName           = Path.GetFileNameWithoutExtension(currentRomPath);
                        SavestateManager.romName = currentRomName;
                        cpu.LoadProgram(currentRom);
                        RewindManager.Initialize(rewindBufferSize);

                        isRunning = true;
                        RemoveAllMessages();
                    };
                    isPaused = true;

                    fileDialog.ShowModal();
                    fileDialogVisible = true;
                }
                else if (fileDialogVisible)
                {
                    fileDialog.Close();
                    fileDialogVisible = false;
                }
            });

            InputHandler.HandleKeypress(resetKey, () =>
            {
                if (isRunning && !helpMenuVisible && !fileDialogVisible)
                {
                    cpu.Reset();
                    cpu.LoadProgram(currentRom);
                    ShowMessage("Reset", 2.5f);
                }
            });

            InputHandler.HandleKeypress(nextSaveStateSlotKey, () =>
            {
                if (isRunning && !helpMenuVisible && !fileDialogVisible)
                {
                    SavestateManager.SelectNextSlot();
                    ShowMessage($"Selected slot {SavestateManager.selectedSlot} {(SavestateManager.IsSelectedSlotEmpty() ? "(empty)" : "")}", 2.5f);
                }
            });
            InputHandler.HandleKeypress(previousSaveStateSlotKey, () =>
            {
                if (isRunning && !helpMenuVisible && !fileDialogVisible)
                {
                    SavestateManager.SelectPreviousSlot();
                    ShowMessage($"Selected slot {SavestateManager.selectedSlot} {(SavestateManager.IsSelectedSlotEmpty() ? "(empty)" : "")}", 2.5f);
                }
            });

            InputHandler.HandleKeypress(helpKey, () =>
            {
                if (!helpMenuVisible && !fileDialogVisible)
                {
                    helpMenu = new Window
                    {
                        Title = "Welcome to Crispy!"
                    };
                    Label text = new Label
                    {
                        Text = readme,
                        VerticalAlignment = VerticalAlignment.Stretch,
                        Wrap = true
                    };
                    ScrollViewer scrollViewer = new ScrollViewer
                    {
                        Content = text
                    };

                    helpMenu.Content = scrollViewer;

                    helpMenu.Closed += (s, a) =>
                    {
                        helpMenuVisible = false;
                    };

                    helpMenu.ShowModal();
                    helpMenuVisible = true;
                }
                else if (helpMenuVisible)
                {
                    helpMenu.Close();
                    helpMenuVisible = false;
                }
            });

            InputHandler.HandleKeypress(screenshotKey, () =>
            {
                if (!helpMenuVisible && !fileDialogVisible)
                {
                    string screenshotPath = $"Screenshots/{DateTime.Now.ToString().Replace("/", "-").Replace(":", "-")}.jpg";

                    if (!File.Exists(screenshotPath))
                    {
                        try
                        {
                            File.Create(screenshotPath).Dispose();
                        }
                        catch (DirectoryNotFoundException)
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(screenshotPath));
                        }
                    }

                    using (Texture2D screenshot = TakeScreenshot())
                        SaveScreenshot(screenshot, File.OpenWrite(screenshotPath));

                    ShowMessage($"Saved screenshot as {Path.GetFileName(screenshotPath)}", 2.5f);
                }
            });
        }