public void EndDrawGUI() { if (State != DrawingManagerState.DrawingGUI) { throw new InvalidOperationException("This method can only be called after BeginDrawGUI."); } try { _state = DrawingManagerState.Idle; // Ensure the RenderWindow is available if (!IsRenderWindowAvailable()) { if (log.IsInfoEnabled) { log.Info("Skipping EndDrawGUI() call - the RenderWindow is not available."); } return; } // Ensure the buffer is available if (_buffer == null || _buffer.IsDisposed) { const string errmsg = "Skipping EndDrawWorld() call - the _buffer is not available."; if (log.IsWarnEnabled) { log.WarnFormat(errmsg); } return; } SafeEndSpriteBatch(_sb); // Copy the GUI to the screen _buffer.Display(); DrawBufferToScreen(_buffer.Texture, BlendMode.Alpha); } catch (AccessViolationException ex) { // More frequently and less concerning exception const string errmsg = "EndDrawGUI failed on `{0}`. Device was probably lost. The GUI will have to skip being drawn this frame. Exception: {1}"; if (log.IsInfoEnabled) { log.InfoFormat(errmsg, this, ex); } } catch (Exception ex) { // Unexpected exception const string errmsg = "EndDrawGUI failed on `{0}` due to unexpected exception. The GUI will have to skip being drawn this frame. Exception: {1}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, this, ex); } } }
public void EndDrawWorld() { if (State != DrawingManagerState.DrawingWorld) throw new InvalidOperationException("This method can only be called after BeginDrawWorld."); try { _state = DrawingManagerState.Idle; // Ensure the RenderWindow is available if (!IsRenderWindowAvailable()) { if (log.IsInfoEnabled) log.Info("Skipping EndDrawWorld() call - the RenderWindow is not available."); return; } // Ensure the buffer is available if (_buffer == null || _buffer.IsDisposed) { const string errmsg = "Skipping EndDrawWorld() call - the _buffer is not available."; if (log.IsWarnEnabled) log.WarnFormat(errmsg); return; } SafeEndSpriteBatch(_sb); // Draw the lights try { if (LightManager.IsEnabled) { // Copy the lights onto the buffer LightManager.DrawToTarget(_worldCamera, _buffer); _buffer.Display(); } else { // Don't have to take any alternate drawing route since lights are just drawn on top of the screen buffer } } catch (Exception ex) { // Do not catch AccessViolationException - let that be handled by the outer block if (ex is AccessViolationException) throw; const string errmsg = "Error on `{0}` while trying to draw the LightManager `{1}`." + " Lights will have to be skipped this frame. Exception: {2}"; if (log.IsErrorEnabled) log.ErrorFormat(errmsg, this, LightManager, ex); } // Have to display the buffer since we will start referencing the texture for it _buffer.Display(); // Draw the refractions try { if (RefractionManager.IsEnabled) { // Pass the buffer to the refraction manager to draw to the screen RefractionManager.DrawToTarget(_worldCamera, _rw, _buffer.Texture); } else { // Since the RefractionManager won't be handling copying to the screen for us, we will have to draw // to the screen manually DrawBufferToScreen(_buffer.Texture, BlendMode.None); } } catch (Exception ex) { // Do not catch AccessViolationException - let that be handled by the outer block if (ex is AccessViolationException) throw; const string errmsg = "Error on `{0}` while trying to draw the RefractionManager `{1}`." + " Refractions will have to be skipped this frame. Exception: {2}"; if (log.IsErrorEnabled) log.ErrorFormat(errmsg, this, RefractionManager, ex); } _view.Reset(new FloatRect(0, 0, _rw.Size.X, _rw.Size.Y)); _rw.SetView(_view); } catch (AccessViolationException ex) { // More frequently and less concerning exception const string errmsg = "EndDrawWorld failed on `{0}`. Device was probably lost. The world will have to skip being drawn this frame. Exception: {1}"; if (log.IsInfoEnabled) log.InfoFormat(errmsg, this, ex); } catch (Exception ex) { // Unexpected exception const string errmsg = "EndDrawWorld failed on `{0}` due to unexpected exception. The world will have to skip being drawn this frame. Exception: {1}"; if (log.IsErrorEnabled) log.ErrorFormat(errmsg, this, ex); } }
public void EndDrawGUI() { if (State != DrawingManagerState.DrawingGUI) throw new InvalidOperationException("This method can only be called after BeginDrawGUI."); try { _state = DrawingManagerState.Idle; // Ensure the RenderWindow is available if (!IsRenderWindowAvailable()) { if (log.IsInfoEnabled) log.Info("Skipping EndDrawGUI() call - the RenderWindow is not available."); return; } // Ensure the buffer is available if (_buffer == null || _buffer.IsDisposed) { const string errmsg = "Skipping EndDrawWorld() call - the _buffer is not available."; if (log.IsWarnEnabled) log.WarnFormat(errmsg); return; } SafeEndSpriteBatch(_sb); // Copy the GUI to the screen _buffer.Display(); DrawBufferToScreen(_buffer.Texture, BlendMode.Alpha); } catch (AccessViolationException ex) { // More frequently and less concerning exception const string errmsg = "EndDrawGUI failed on `{0}`. Device was probably lost. The GUI will have to skip being drawn this frame. Exception: {1}"; if (log.IsInfoEnabled) log.InfoFormat(errmsg, this, ex); } catch (Exception ex) { // Unexpected exception const string errmsg = "EndDrawGUI failed on `{0}` due to unexpected exception. The GUI will have to skip being drawn this frame. Exception: {1}"; if (log.IsErrorEnabled) log.ErrorFormat(errmsg, this, ex); } }
/// <summary> /// Begins drawing of the world. /// </summary> /// <param name="camera">The camera describing the the current view of the world.</param> /// <returns> /// The <see cref="ISpriteBatch"/> to use to draw the world objects, or null if an unexpected /// error was encountered when preparing the <see cref="ISpriteBatch"/>. When null, all drawing /// should be aborted completely instead of trying to draw with a different <see cref="ISpriteBatch"/> /// or manually recovering the error. /// </returns> /// <exception cref="InvalidOperationException"><see cref="IDrawingManager.State"/> is not equal to /// <see cref="DrawingManagerState.Idle"/>.</exception> public ISpriteBatch BeginDrawWorld(ICamera2D camera) { if (State != DrawingManagerState.Idle) throw new InvalidOperationException("This method cannot be called while already busy drawing."); try { // Ensure the RenderWindow is available if (!IsRenderWindowAvailable()) { if (log.IsInfoEnabled) log.Info("Skipping BeginDrawWorld() call - the RenderWindow is not available."); _state = DrawingManagerState.Idle; return null; } _worldCamera = camera; // No matter what the last draw was, we clear the screen when drawing the world since the world drawing // always comes first or not at all (makes no sense to draw the GUI then the world) _rw.Clear(BackgroundColor); _lastDrawWasToWorld = true; // Ensure the buffer is set up _buffer = _rw.CreateBufferRenderTexture(_buffer); _sb.RenderTarget = _buffer; if (_buffer == null) return null; _buffer.Clear(BackgroundColor); // Start up the SpriteBatch _sb.Begin(BlendMode.Alpha, camera); // Change the state _state = DrawingManagerState.DrawingWorld; } catch (AccessViolationException ex) { // More frequent and less concerning exception const string errmsg = "Failed to start drawing world on `{0}`. Device was probably lost. Exception: {1}"; if (log.IsInfoEnabled) log.InfoFormat(errmsg, this, ex); _state = DrawingManagerState.Idle; SafeEndSpriteBatch(_sb); return null; } catch (Exception ex) { // Unexpected exception const string errmsg = "Failed to start drawing world on `{0}` due to unexpected exception. Exception: {1}"; if (log.IsErrorEnabled) log.ErrorFormat(errmsg, this, ex); Debug.Fail(string.Format(errmsg, this, ex)); _state = DrawingManagerState.Idle; SafeEndSpriteBatch(_sb); return null; } return _sb; }
/// <summary> /// Begins drawing the graphical user interface, which is not affected by the camera. /// </summary> /// <param name="clearBuffer">When true, the buffer will be cleared before drawing. When false, the contents of the previous /// frame will remain in the buffer, only if the last draw was also to the GUI. When the last draw call was to the /// world, then this will have no affect. Useful for when you want to draw multiple GUI screens on top of one another.</param> /// <returns> /// The <see cref="ISpriteBatch"/> to use to draw the GUI, or null if an unexpected /// error was encountered when preparing the <see cref="ISpriteBatch"/>. When null, all drawing /// should be aborted completely instead of trying to draw with a different <see cref="ISpriteBatch"/> /// or manually recovering the error. /// </returns> /// <exception cref="InvalidOperationException"><see cref="IDrawingManager.State"/> is not equal to /// <see cref="DrawingManagerState.Idle"/>.</exception> public ISpriteBatch BeginDrawGUI(bool clearBuffer = true) { if (State != DrawingManagerState.Idle) throw new InvalidOperationException("This method cannot be called while already busy drawing."); try { // Ensure the RenderWindow is available if (!IsRenderWindowAvailable()) { if (log.IsInfoEnabled) log.Info("Skipping BeginDrawGUI() call - the RenderWindow is not available."); _state = DrawingManagerState.Idle; return null; } if (clearBuffer) { // If the last draw was also to the GUI, clear the screen if (!_lastDrawWasToWorld) _rw.Clear(BackgroundColor); } _lastDrawWasToWorld = false; // Ensure the buffer is set up _buffer = _rw.CreateBufferRenderTexture(_buffer); _sb.RenderTarget = _buffer; if (_buffer == null) return null; // Always clear the GUI with alpha = 0 since we will be copying it over the screen _buffer.Clear(_clearGUIBufferColor); // Start up the SpriteBatch _sb.Begin(BlendMode.Alpha); // Change the state _state = DrawingManagerState.DrawingGUI; } catch (AccessViolationException ex) { // More frequent and less concerning exception const string errmsg = "Failed to start drawing GUI on `{0}`. Device was probably lost. Exception: {1}"; if (log.IsInfoEnabled) log.InfoFormat(errmsg, this, ex); _state = DrawingManagerState.Idle; SafeEndSpriteBatch(_sb); return null; } catch (Exception ex) { // Unexpected exception const string errmsg = "Failed to start drawing GUI on `{0}` due to unexpected exception. Exception: {1}"; if (log.IsErrorEnabled) log.ErrorFormat(errmsg, this, ex); Debug.Fail(string.Format(errmsg, this, ex)); _state = DrawingManagerState.Idle; SafeEndSpriteBatch(_sb); return null; } return _sb; }
public void EndDrawWorld() { if (State != DrawingManagerState.DrawingWorld) { throw new InvalidOperationException("This method can only be called after BeginDrawWorld."); } try { _state = DrawingManagerState.Idle; // Ensure the RenderWindow is available if (!IsRenderWindowAvailable()) { if (log.IsInfoEnabled) { log.Info("Skipping EndDrawWorld() call - the RenderWindow is not available."); } return; } // Ensure the buffer is available if (_buffer == null || _buffer.IsDisposed) { const string errmsg = "Skipping EndDrawWorld() call - the _buffer is not available."; if (log.IsWarnEnabled) { log.WarnFormat(errmsg); } return; } SafeEndSpriteBatch(_sb); // Draw the lights try { if (LightManager.IsEnabled) { // Copy the lights onto the buffer LightManager.DrawToTarget(_worldCamera, _buffer); _buffer.Display(); } else { // Don't have to take any alternate drawing route since lights are just drawn on top of the screen buffer } } catch (Exception ex) { // Do not catch AccessViolationException - let that be handled by the outer block if (ex is AccessViolationException) { throw; } const string errmsg = "Error on `{0}` while trying to draw the LightManager `{1}`." + " Lights will have to be skipped this frame. Exception: {2}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, this, LightManager, ex); } } // Have to display the buffer since we will start referencing the texture for it _buffer.Display(); // Draw the refractions try { if (RefractionManager.IsEnabled) { // Pass the buffer to the refraction manager to draw to the screen RefractionManager.DrawToTarget(_worldCamera, _rw, _buffer.Texture); } else { // Since the RefractionManager won't be handling copying to the screen for us, we will have to draw // to the screen manually DrawBufferToScreen(_buffer.Texture, BlendMode.None); } } catch (Exception ex) { // Do not catch AccessViolationException - let that be handled by the outer block if (ex is AccessViolationException) { throw; } const string errmsg = "Error on `{0}` while trying to draw the RefractionManager `{1}`." + " Refractions will have to be skipped this frame. Exception: {2}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, this, RefractionManager, ex); } } _view.Reset(new FloatRect(0, 0, _rw.Size.X, _rw.Size.Y)); _rw.SetView(_view); } catch (AccessViolationException ex) { // More frequently and less concerning exception const string errmsg = "EndDrawWorld failed on `{0}`. Device was probably lost. The world will have to skip being drawn this frame. Exception: {1}"; if (log.IsInfoEnabled) { log.InfoFormat(errmsg, this, ex); } } catch (Exception ex) { // Unexpected exception const string errmsg = "EndDrawWorld failed on `{0}` due to unexpected exception. The world will have to skip being drawn this frame. Exception: {1}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, this, ex); } } }
/// <summary> /// Begins drawing of the world. /// </summary> /// <param name="camera">The camera describing the the current view of the world.</param> /// <returns> /// The <see cref="ISpriteBatch"/> to use to draw the world objects, or null if an unexpected /// error was encountered when preparing the <see cref="ISpriteBatch"/>. When null, all drawing /// should be aborted completely instead of trying to draw with a different <see cref="ISpriteBatch"/> /// or manually recovering the error. /// </returns> /// <exception cref="InvalidOperationException"><see cref="IDrawingManager.State"/> is not equal to /// <see cref="DrawingManagerState.Idle"/>.</exception> public ISpriteBatch BeginDrawWorld(ICamera2D camera) { if (State != DrawingManagerState.Idle) { throw new InvalidOperationException("This method cannot be called while already busy drawing."); } try { // Ensure the RenderWindow is available if (!IsRenderWindowAvailable()) { if (log.IsInfoEnabled) { log.Info("Skipping BeginDrawWorld() call - the RenderWindow is not available."); } _state = DrawingManagerState.Idle; return(null); } _worldCamera = camera; // No matter what the last draw was, we clear the screen when drawing the world since the world drawing // always comes first or not at all (makes no sense to draw the GUI then the world) _rw.Clear(BackgroundColor); _lastDrawWasToWorld = true; // Ensure the buffer is set up _buffer = _rw.CreateBufferRenderTexture(_buffer); _sb.RenderTarget = _buffer; if (_buffer == null) { return(null); } _buffer.Clear(BackgroundColor); // Start up the SpriteBatch _sb.Begin(BlendMode.Alpha, camera); // Change the state _state = DrawingManagerState.DrawingWorld; } catch (AccessViolationException ex) { // More frequent and less concerning exception const string errmsg = "Failed to start drawing world on `{0}`. Device was probably lost. Exception: {1}"; if (log.IsInfoEnabled) { log.InfoFormat(errmsg, this, ex); } _state = DrawingManagerState.Idle; SafeEndSpriteBatch(_sb); return(null); } catch (Exception ex) { // Unexpected exception const string errmsg = "Failed to start drawing world on `{0}` due to unexpected exception. Exception: {1}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, this, ex); } Debug.Fail(string.Format(errmsg, this, ex)); _state = DrawingManagerState.Idle; SafeEndSpriteBatch(_sb); return(null); } return(_sb); }
/// <summary> /// Begins drawing the graphical user interface, which is not affected by the camera. /// </summary> /// <param name="clearBuffer">When true, the buffer will be cleared before drawing. When false, the contents of the previous /// frame will remain in the buffer, only if the last draw was also to the GUI. When the last draw call was to the /// world, then this will have no affect. Useful for when you want to draw multiple GUI screens on top of one another.</param> /// <returns> /// The <see cref="ISpriteBatch"/> to use to draw the GUI, or null if an unexpected /// error was encountered when preparing the <see cref="ISpriteBatch"/>. When null, all drawing /// should be aborted completely instead of trying to draw with a different <see cref="ISpriteBatch"/> /// or manually recovering the error. /// </returns> /// <exception cref="InvalidOperationException"><see cref="IDrawingManager.State"/> is not equal to /// <see cref="DrawingManagerState.Idle"/>.</exception> public ISpriteBatch BeginDrawGUI(bool clearBuffer = true) { if (State != DrawingManagerState.Idle) { throw new InvalidOperationException("This method cannot be called while already busy drawing."); } try { // Ensure the RenderWindow is available if (!IsRenderWindowAvailable()) { if (log.IsInfoEnabled) { log.Info("Skipping BeginDrawGUI() call - the RenderWindow is not available."); } _state = DrawingManagerState.Idle; return(null); } if (clearBuffer) { // If the last draw was also to the GUI, clear the screen if (!_lastDrawWasToWorld) { _rw.Clear(BackgroundColor); } } _lastDrawWasToWorld = false; // Ensure the buffer is set up _buffer = _rw.CreateBufferRenderTexture(_buffer); _sb.RenderTarget = _buffer; if (_buffer == null) { return(null); } // Always clear the GUI with alpha = 0 since we will be copying it over the screen _buffer.Clear(_clearGUIBufferColor); // Start up the SpriteBatch _sb.Begin(BlendMode.Alpha); // Change the state _state = DrawingManagerState.DrawingGUI; } catch (AccessViolationException ex) { // More frequent and less concerning exception const string errmsg = "Failed to start drawing GUI on `{0}`. Device was probably lost. Exception: {1}"; if (log.IsInfoEnabled) { log.InfoFormat(errmsg, this, ex); } _state = DrawingManagerState.Idle; SafeEndSpriteBatch(_sb); return(null); } catch (Exception ex) { // Unexpected exception const string errmsg = "Failed to start drawing GUI on `{0}` due to unexpected exception. Exception: {1}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, this, ex); } Debug.Fail(string.Format(errmsg, this, ex)); _state = DrawingManagerState.Idle; SafeEndSpriteBatch(_sb); return(null); } return(_sb); }