zoukankan      html  css  js  c++  java
  • C#搞个跨平台的桌面NES游戏模拟器

    支持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入门教程

     源码下载

    签名:<-CPF C# 跨平台桌面UI框架,支持Windows,Mac,Linux,包括XP,国产麒麟Linux等等->
  • 相关阅读:
    js相关禁止
    单例模式 俗称单例3步曲+1曲
    轮廓线重建:二维平行轮廓线重建理论和方法
    一种面向三维地质剖面的形体表面重构算法
    在不使用gluSphere()的情况下在OpenGL中绘制Sphere
    Balabolka
    jQuery学习笔记之可见性过滤选择器
    Flask学习之四 数据库
    Flask学习之三 web表单
    Flask学习之二 模板
  • 原文地址:https://www.cnblogs.com/dskin/p/14692031.html
Copyright © 2011-2022 走看看