C#搞個跨平台的桌面NES遊戲模擬器

 

支持Windows,Mac,Linux    NES模擬器內核源碼來自 //github.com/colinvella/EmuNes   他這邊的源碼功能很完善了的,支持視頻錄製,手柄,金手指等等。現在移植到cpf來實現跨平台測試,不過這邊的移植測試里並沒有把所有功能移植完整。

 

 

 

 

 

 

 移植這個,主要就是圖形繪製和音頻播放適配。

需要開啟代碼優化才能有足夠的幀數,否則會很卡。

 

 繪製和控制的代碼主要在 NesVideoPanel 類里

 

將遊戲畫面繪製出來

        protected override void OnRender(DrawingContext dc)
        {
            base.OnRender(dc);
            if (bitmapBuffer != null)
            {
                dc.DrawImage(bitmapBuffer.Bitmap, new Rect(new Point(), ActualSize), new Rect(0, 0, bitmapBuffer.Bitmap.Width, bitmapBuffer.Bitmap.Height));
                if (gameState == GameState.Paused)
                {
                    var size = ActualSize;
                    dc.DrawImage(FilterPause, new Rect(new Point(), ActualSize), new Rect(0, 0, FilterPause.Width, FilterPause.Height));
                    var textSize = dc.DrawingFactory.MeasureString("暫停", new Font(FontFamily, 36, FontStyles.Italic));

                    var outlineThickness = size.Height / 120;
                    textSize.Width += outlineThickness;
                    textSize.Height += outlineThickness;

                    float textX = (size.Width - textSize.Width) / 2;
                    float textY = (size.Height - textSize.Height) / 2;

                    using (PathGeometry graphicsPath = new PathGeometry(new Font(this.FontFamily, 36, FontStyles.Italic), "暫停"))
                    using (SolidColorBrush outlinePen = new SolidColorBrush(Color.Black))
                    using (LinearGradientBrush linearGradientBrush = new LinearGradientBrush(new GradientStop[] { new GradientStop(Color.White, 0), new GradientStop(Color.LightSkyBlue, 1) }, new Point(), new Point(0, textSize.Height), Matrix.Identity))
                    {
                        var m = Matrix.Identity;
                        m.Translate(textX, textY);
                        graphicsPath.Transform(m);
                        dc.DrawPath(outlinePen, new Stroke(outlineThickness) { LineJoin = LineJoins.Round }, graphicsPath);
                        dc.FillPath(linearGradientBrush, graphicsPath);
                    }
                }
            }
        }

 

 鍵盤操作

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.Key == Keys.Tab)
            {
                e.Handled = true;
            }
            keyboardState[e.Key] = true;
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            keyboardState[e.Key] = false;
        }

 

聲音播放採用SDL2,支持跨平台播放聲音,在 ApuAudioProvider 類里實現

源碼裡帶sdl2的64位dll,如果發佈到其他平台需要對應平台安裝SDL2才行。

 

默認支持直接打開zip文件讀取nes,不過Net4版的不能讀取zip

 

 

 

 

 感興趣的可以下載研究,需要VS2019。需要cpf設計器的話,請看 CPF入門教程

 源碼下載