支持Windows,Mac,Linux NES模拟器内核源码来自 https://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入门教程