/// <summary> /// Attempts to begin drawing the control. Returns an error message string /// if this was not possible, which can happen if the graphics device is /// lost, or if we are running inside the Form designer. /// </summary> void BeginDraw(RenderingError error) { // If we have no graphics device, we must be running in the designer. if (graphicsDeviceService == null) { error.Message = Text + "\n\n" + GetType(); } if (error.HasErrors == false || error.GraphicsDeviceNeedsReset) { TryHandleDeviceReset(error); } if (!error.HasErrors) { // Many GraphicsDeviceControl instances can be sharing the same // GraphicsDevice. The device backbuffer will be resized to fit the // largest of these controls. But what if we are currently drawing // a smaller control? To avoid unwanted stretching, we set the // viewport to only use the top left portion of the full backbuffer. Viewport viewport = new Viewport(); viewport.X = 0; viewport.Y = 0; viewport.Width = ClientSize.Width; viewport.Height = ClientSize.Height; viewport.MinDepth = 0; viewport.MaxDepth = 1; GraphicsDevice.Viewport = viewport; } }
/// <summary> /// Helper used by BeginDraw. This checks the graphics device status, /// making sure it is big enough for drawing the current control, and /// that the device is not lost. Returns an error string if the device /// could not be reset. /// </summary> void TryHandleDeviceReset(RenderingError error) { switch (GraphicsDevice.GraphicsDeviceStatus) { case GraphicsDeviceStatus.Lost: // If the graphics device is lost, but we can try to reset it error.Message = "Graphics device lost"; error.GraphicsDeviceLost = true; break; case GraphicsDeviceStatus.NotReset: // If device is in the not-reset state, we should try to reset it. error.GraphicsDeviceNeedsReset = true; error.Message = "Graphics device needs reset"; break; default: // If the device state is ok, check whether it is big enough. PresentationParameters pp = GraphicsDevice.PresentationParameters; bool deviceNeedsReset = (ClientSize.Width > pp.BackBufferWidth) || (ClientSize.Height > pp.BackBufferHeight); if (deviceNeedsReset) { error.Message = "Resolution has changed, needs reset"; error.GraphicsDeviceNeedsReset = true; } break; } // Do we need to reset the device? if (error.GraphicsDeviceNeedsReset) { try { graphicsDeviceService.ResetDevice(ClientSize.Width, ClientSize.Height); error.Message = null; error.GraphicsDeviceNeedsReset = false; } catch (Exception e) { error.GraphicsDeviceNeedsReset = false; error.Message = "Graphics device reset failed\n\n" + e; } } }
/// <summary> /// Helper used by BeginDraw. This checks the graphics device status, /// making sure it is big enough for drawing the current control, and /// that the device is not lost. Returns an error string if the device /// could not be reset. /// </summary> void TryHandleDeviceReset(RenderingError error) { switch (GraphicsDevice.GraphicsDeviceStatus) { case GraphicsDeviceStatus.Lost: // If the graphics device is lost, but we can try to reset it error.Message = "Graphics device lost"; error.GraphicsDeviceLost = true; break; case GraphicsDeviceStatus.NotReset: // If device is in the not-reset state, we should try to reset it. error.GraphicsDeviceNeedsReset = true; error.Message = "Graphics device needs reset"; break; default: // If the device state is ok, check whether it is big enough. PresentationParameters pp = GraphicsDevice.PresentationParameters; bool deviceNeedsReset = (ClientSize.Width > pp.BackBufferWidth) || (ClientSize.Height > pp.BackBufferHeight); if(deviceNeedsReset) { error.Message = "Resolution has changed, needs reset"; error.GraphicsDeviceNeedsReset = true; } break; } // Do we need to reset the device? if (error.GraphicsDeviceNeedsReset) { try { graphicsDeviceService.ResetDevice(ClientSize.Width, ClientSize.Height); error.Message = null; error.GraphicsDeviceNeedsReset = false; } catch (Exception e) { error.GraphicsDeviceNeedsReset = false; error.Message = "Graphics device reset failed\n\n" + e; } } }