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); } }
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); }