Exemple #1
0
        public void RenderDX()
        {
            while (DoRender)
            {
                if (ziggyWin.zx.needsPaint && !isRendering)
                {
                    lock (ziggyWin.zx.lockThis) {
                        ziggyWin.zx.needsPaint = false;
                        isRendering            = true;
                        this.Invalidate();
                    }
                    frameTime = PrecisionTimer.TimeInMilliseconds() - lastTime;
                    frameCount++;
                    totalFrameTime += frameTime;

                    if (totalFrameTime > 1000.0f)
                    {
                        averageFPS     = (int)(1000 * frameCount / totalFrameTime);
                        frameCount     = 0;
                        totalFrameTime = 0;
                    }
                    lastTime = PrecisionTimer.TimeInMilliseconds();
                }
                System.Threading.Thread.Sleep(1);
            }
        }
Exemple #2
0
 private void Start()
 {
     renderThread          = new System.Threading.Thread(new System.Threading.ThreadStart(RenderDX));
     renderThread.Name     = "Render Thread";
     renderThread.Priority = System.Threading.ThreadPriority.Lowest;
     DoRender    = true;
     isRendering = false;
     isSuspended = false;
     renderThread.Start();
     ledBlinkTimer = PrecisionTimer.TimeInSeconds();
 }
        void PaintMap()
        {
            while (run)
            {
                double currentTime = PrecisionTimer.TimeInSeconds();

                if (currentTime - lastTime < 1.0f)
                {
                    continue;
                }

                lastTime = currentTime;
                //panel1.Invalidate();
                //continue;
                Rectangle rect = new Rectangle(0, 0, bmpOut.Width, bmpOut.Height);
                System.Drawing.Imaging.BitmapData bmpData =
                    bmpOut.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                                    bmpOut.PixelFormat);
                IntPtr ptr = bmpData.Scan0;

                unsafe
                {
                    int *p = (int *)ptr.ToPointer();

                    for (int f = 0; f < 65536; f++)
                    {
                        int colorIndex = (int)(heatMap[f] % 7);

                        if (colorIndex > 7)
                        {
                            colorIndex = 7;
                        }

                        *(p)                 = heatColors[colorIndex].ToArgb();
                        *(p + 1)             = heatColors[colorIndex].ToArgb();
                        *(p + MAP_WIDTH)     = heatColors[colorIndex].ToArgb();
                        *(p + MAP_WIDTH + 1) = heatColors[colorIndex].ToArgb();
                        p++; p++;
                        //*(p++) = heatColors[colorIndex].ToArgb();
                        if ((f + 1) % 256 == 0)
                        {
                            p = p + MAP_WIDTH;
                        }
                        heatMap[f] = 0;
                    }
                }

                bmpOut.UnlockBits(bmpData);
                pictureBox1.Image = bmpOut;
                //pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }
        public CodeProfiler(Form1 zw)
        {
            InitializeComponent();
            ziggyWin    = zw;
            panel1.Size = new Size(MAP_WIDTH, MAP_HEIGHT);
            lastTime    = PrecisionTimer.TimeInSeconds();
            ziggyWin.zx.MemoryWriteEvent += MemoryWriteEventHandler;
            ziggyWin.zx.FrameEndEvent    += FrameEndEventHandler;

            this.Invalidate();
            paintThread          = new Thread(new ThreadStart(PaintMap));
            paintThread.Name     = "Heatmap Thread";
            paintThread.Priority = System.Threading.ThreadPriority.Normal;
            paintThread.Start();
        }
        void PaintMap()
        {
            while (run)
            {
                double currentTime = PrecisionTimer.TimeInSeconds();

                if (currentTime - lastTime < 1.0f)
                {
                    continue;
                }

                lastTime = currentTime;

                Rectangle rect = new Rectangle(0, 0, bmpOut.Width, bmpOut.Height);
                System.Drawing.Imaging.BitmapData bmpData =
                    bmpOut.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                                    bmpOut.PixelFormat);
                IntPtr ptr = bmpData.Scan0;

                unsafe
                {
                    int *p = (int *)ptr.ToPointer();

                    for (int f = 0; f < 65536; f++)
                    {
                        int colorIndex = (int)(heatMap[f] % 7);

                        if (colorIndex > 7)
                        {
                            colorIndex = 7;
                        }

                        *(p++) = heatColors[colorIndex].ToArgb();
                    }
                }

                bmpOut.UnlockBits(bmpData);
                pictureBox1.Image = bmpOut;
            }
        }
Exemple #6
0
        public bool InitDirectX(int width, int height, bool is16bit = false)
        {
            isSuspended = true;
            DestroyDX();

            displayRect   = new Rectangle(0, 0, width, height);
            displayWidth  = width;
            displayHeight = height;

            AdapterInformation adapterInfo = Manager.Adapters.Default;

            ziggyWin.logger.Log("Setting up render parameters...");
            currentParams = new PresentParameters();
            currentParams.BackBufferCount      = 2;
            currentParams.BackBufferWidth      = width;
            currentParams.BackBufferHeight     = height;
            currentParams.SwapEffect           = SwapEffect.Discard;
            currentParams.PresentFlag          = PresentFlag.None;
            currentParams.PresentationInterval = (enableVsync ? PresentInterval.One : PresentInterval.Immediate);
            currentParams.Windowed             = !fullScreenMode;// true;

            Format currentFormat = Manager.Adapters[0].CurrentDisplayMode.Format;
            bool   formatCheck   = Manager.CheckDeviceType(0,
                                                           DeviceType.Hardware,
                                                           currentFormat,
                                                           currentFormat,
                                                           false);

            if (!formatCheck)
            {
                MessageBox.Show("Invalid format", "dx error", MessageBoxButtons.OK);
            }

            if (fullScreenMode)
            {
                currentParams.DeviceWindow     = this.Parent;
                currentParams.BackBufferFormat = currentFormat;//(is16bit ? Format.R5G6B5 : Format.X8B8G8R8);
            }
            else
            {
                currentParams.DeviceWindow     = this;
                currentParams.BackBufferFormat = adapterInfo.CurrentDisplayMode.Format;
            }

            try {
                ziggyWin.logger.Log("Initializing directX device...");
                dxDevice = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, currentParams);
            } catch (Microsoft.DirectX.DirectXException dx) {
                MessageBox.Show(dx.ErrorString, "DX error", MessageBoxButtons.OK);
                try {
                    dxDevice = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, currentParams);
                } catch (Microsoft.DirectX.DirectXException dx2) {
                    MessageBox.Show(dx2.ErrorString, "DX error", MessageBoxButtons.OK);
                    directXAvailable = false;
                    return(false);
                }
            }

            //sprite = new Sprite(dxDevice);
            //interlaceSprite = new Sprite(dxDevice);
            SetSpeccyScreenSize(ziggyWin.zx.GetTotalScreenWidth(), ziggyWin.zx.GetTotalScreenHeight());

            float scaleX = ((float)displayWidth / (float)(ScreenWidth));
            float scaleY = ((float)displayHeight / (float)(ScreenHeight));

            //Maintain 4:3 aspect ration when full screen
            if (EnableFullScreen && ziggyWin.config.MaintainAspectRatioInFullScreen)
            {
                if (displayHeight < displayWidth)
                {
                    float aspectXScale = 0.75f; // (displayHeight * 4.0f) / (displayWidth * 3.0f);
                    scaleX = (scaleX * aspectXScale);
                    int newWidth = (int)(displayWidth * aspectXScale);
                    //displayRect = new Rectangle(0, 0, newWidth, displayHeight);
                    spritePos = new Vector3((((displayWidth - newWidth)) / (scaleX * 2.0f)), 0, 0);
                }
                else //Not tested!!!
                {
                    float aspectYScale = 1.33f;// (displayWidth * 3.0f) / (displayHeight * 4.0f);
                    scaleY = (scaleY * aspectYScale);
                    int newHeight = (int)(displayHeight * aspectYScale);
                    //displayRect = new Rectangle(0, 0, displayWidth, newHeight);
                }
            }
            else
            {
                spritePos = Vector3.Empty;
            }

            if (scaleX < 1.0f)
            {
                scaleX = 1.0f;
            }

            if (scaleY < 1.0f)
            {
                scaleY = 1.0f;
            }

            Matrix scaling = Matrix.Scaling(scaleX, scaleY, 1.0f);

            //sprite.Transform = scaling;
            System.Console.WriteLine("scaleX " + scaleX + "     scaleY " + scaleY);
            System.Console.WriteLine("pos " + spritePos);
            Texture displayTexture = new Texture(dxDevice, ScreenWidth, ScreenHeight, 1, Usage.None, currentParams.BackBufferFormat, Pool.Managed);

            displaySprite.Init(dxDevice, displayTexture, new Rectangle((int)spritePos.X, (int)spritePos.Y, ScreenWidth, ScreenHeight), scaling);

            using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) {
                ZeroWin.Properties.Resources.scanlines2.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                stream.Position = 0;
                Texture interlaceTexture = Texture.FromStream(dxDevice, stream, Usage.None, Pool.Managed);
                Surface interlaceSurface = interlaceTexture.GetSurfaceLevel(0);

                //Why 1.5f? Because it works very well.
                //Trying to use displayHeight/texture_width (which would seem logical) leads to strange banding on the screen.
                scanlineSprite.Init(dxDevice, interlaceTexture, new Rectangle((int)spritePos.X, (int)spritePos.Y, ScreenWidth, ScreenHeight), scaling, 1.0f, displayHeight / 1.5f);
            }

            System.Drawing.Font systemfont = new System.Drawing.Font(System.Drawing.SystemFonts.MessageBoxFont.FontFamily, 10f, FontStyle.Regular);
            isSuspended      = false;
            lastTime         = PrecisionTimer.TimeInMilliseconds();
            directXAvailable = true;
            return(true);
        }