C# 搞个跨平台的桌面NES游戏模拟器

dotNET全栈开发

共 7119字,需浏览 15分钟

 ·

2021-05-11 16:33


支持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(00, bitmapBuffer.Bitmap.Width, bitmapBuffer.Bitmap.Height));
                if (gameState == GameState.Paused)
                {
                    var size = ActualSize;
                    dc.DrawImage(FilterPause, new Rect(new Point(), ActualSize), new Rect(00, 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入门教程:https://www.cnblogs.com/dskin/p/13490921.html

源码下载:https://files.cnblogs.com/files/dskin/NESPlayer.zip

转自:DSkin

链接:cnblogs.com/dskin/p/14692031.html







回复 【关闭】广
回复 【实战】获取20套实战源码
回复 【被删】
回复 【访客】访
回复 【小程序】学获取15套【入门+实战+赚钱】小程序源码
回复 【python】学微获取全套0基础Python知识手册
回复 【2019】获取2019 .NET 开发者峰会资料PPT
回复 【加群】加入dotnet微信交流群


又来一个神奇的网站!


人人影视字幕组凉了,这款美剧APP不能错过!



浏览 43
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报