/// <summary> /// Render one of the subblocks (number given by rectangleToDraw) /// </summary> /// <param name="backgroundColor">The background color to initialize the texture</param> /// <param name="drawRoutine">The actual drawing routine delegate</param> private void RenderASingleBlockTexture(Color backgroundColor, DrawingDelegate drawRoutine) { // we wrap this in a try, because we have seen that in some conditions this might crash otherwise // Crashes seem to be related to the interaction between XNA and our program: our routines might be // called at wrong time for some reason. Possibly XNA simply interrupts a long 'Draw'-call to do an // update in between. As a result, spritebatch state might be wrong, and we crash. // Very difficult to track and see what is really going on. Therefore: simple try and catch. try { //drawing area depends on rectangle we want to draw shadowDrawArea.Scale = shadowScale; shadowDrawArea.OffsetX = shadowOffsetX + xOrder[nextRectangleToDraw] * blockW / shadowScale; shadowDrawArea.OffsetZ = shadowOffsetZ + zOrder[nextRectangleToDraw] * blockH / shadowScale; shadowDrawArea.Update(); // Rendering the tracks to a single texture graphicsDevice.SetRenderTarget(shadowRenderTargetSingle[nextRectangleToDraw]); graphicsDevice.Clear(backgroundColor); spriteBatch.Begin(); drawRoutine(shadowDrawArea); //shadowDrawArea.DrawBorder(Color.Black); //debug spriteBatch.End(); graphicsDevice.SetRenderTarget(null); shadowMapsSingle[nextRectangleToDraw] = shadowRenderTargetSingle[nextRectangleToDraw]; needToDrawRectangle[nextRectangleToDraw] = false; } catch { graphicsDevice.SetRenderTarget(null); // return control to main render target } graphicsDevice.SetRenderTarget(null); }
/// <summary> /// Call the supplied routine to draw on one of the subblocks. /// The assumption is that this routine will be called regularly (e.g. during Draw or Update) to make sure /// the various subblocks will indeed be drawn. /// This method will determine itself when there is a need for redrawing /// </summary> /// <param name="drawRoutine">The routine that is used to draw to the drawing area</param> /// <param name="backgroundColor">Background color to use. Using transparent is possible but will not look well with oversampling.</param> public void DrawShadowTextures(DrawingDelegate drawRoutine, Color backgroundColor) { DetermineRedrawingNeeds(); // did we shift out of bounds, did we zoom, ... if (needToRedraw) { SetNativeScales(); AddAllToRedrawList(); needToRedraw = false; } // if we have redrawn the inner part, we might need to redraw it (before we draw the non-visible rings) if ((nextRectangleToDraw >= Ninner * Ninner) && needToRedrawLater) { SetNativeScales(); AddAllToRedrawList(); needToRedrawLater = false; needToRedraw = false; } // if we have redrawn all, we are ready if (nextRectangleToDraw >= Nsubblocks) { return; } // Actual rendering of a single block and then all blocks to the combined texture RenderASingleBlockTexture(backgroundColor, drawRoutine); RenderCombinedTexture(backgroundColor); SetNextRectangleToDraw(false); // in the next (game) loop, we will draw the next one. }
public void DrawCustomOLD(int startIndex, int length, DrawingParameters parameters, DrawingDelegate drawCallback) { int endIndex = startIndex + length; int startLineIndex = GetLineFromCharIndex(startIndex); int endLineIndex = GetLineFromCharIndex(endIndex); if (startLineIndex == endLineIndex)//spans one line { parameters.StartPoint = GetPositionFromCharIndex(startIndex); parameters.EndPoint = GetPositionFromCharIndex(endIndex); drawCallback(parameters); //DrawBorder(parameters); } //compensate for first line being zero so calculations won't break int endLineForComparison = endLineIndex + 1; int startLineForComparison = startLineIndex + 1; if (endLineForComparison - startLineForComparison == 1)//spans 2 lines { parameters.StartPoint = GetPositionFromCharIndex(startIndex); parameters.EndPoint = getEndPointForLine(startLineIndex); drawCallback(parameters); DrawCustomOnEndLine(endIndex, endLineIndex, parameters, drawCallback); } if ((endLineForComparison - startLineForComparison) > 1)//spans >2 lines { int scopeLines = endLineIndex - startLineIndex; parameters.StartPoint = GetPositionFromCharIndex(startIndex); parameters.EndPoint = getEndPointForLine(startLineIndex); drawCallback(parameters); for (int i = startLineIndex + 1; i < scopeLines; i++) { int startLineCharPos = GetFirstCharIndexFromLine(i); parameters.StartPoint = GetPositionFromCharIndex(startLineCharPos); parameters.EndPoint = getEndPointForLine(i); drawCallback(parameters); } DrawCustomOnEndLine(endIndex, endLineIndex, parameters, drawCallback); } }
private void DrawCustomOnEndLine(int endCharIndex, int lineIndex, DrawingParameters parameters, DrawingDelegate draw) { //Scope ends on the next line //outline the final lineToTheEnd int charPos = GetFirstCharIndexFromLine(lineIndex); parameters.StartPoint = GetPositionFromCharIndex(charPos); parameters.EndPoint = GetPositionFromCharIndex(endCharIndex); draw(parameters); }
public void DrawCustom(int startIndex, int length, DrawingParameters parameters, DrawingDelegate drawCallback) { List <Rectangle> rects; rects = GetSurroundingRects(startIndex, length); foreach (Rectangle rect in rects) { parameters.Rect = rect; drawCallback(parameters); } }