// ===== #region Constructors // Designated constructor public SpriteTexture(Rectangle rectangle, WrappedTexture texture, WrappedShader shader) { // Get ID this.id = nextID++; // Entry logging #if IS_LOGGING_METHODS Log.Write(String.Format("Entering method for {0}", this.Name)); #endif // Set instance variables this.layer = null; this.layerIndex = -1; this.depth = 0; this.needsUpdate = false; this.rectangle = rectangle; this.texture = texture; this.shader = shader; // Initialize vertices this.vertices = new VertexInput[4]; this.vertices[0] = new VertexInput(rectangle.VertexAt(0), Color.White, new Vector2(0, 0)); this.vertices[1] = new VertexInput(rectangle.VertexAt(1), Color.White, new Vector2(1, 0)); this.vertices[2] = new VertexInput(rectangle.VertexAt(2), Color.White, new Vector2(1, 1)); this.vertices[3] = new VertexInput(rectangle.VertexAt(3), Color.White, new Vector2(0, 1)); // Exit logging #if IS_LOGGING_METHODS Log.Write(String.Format("Exiting method for {0}", this.Name)); #endif }
// ===== #region Constructors // Designated constructor public SpriteText(Rectangle rectangle, String text, WrappedFont font, WrappedShader shader) { // Get ID this.id = nextID++; // Entry logging #if IS_LOGGING_METHODS Log.Write(String.Format("Entering method for {0}", this.Name)); #endif // Set instance variables this.layer = null; this.layerIndex = -1; this.depth = 0; this.shader = shader; this.needsUpdate = false; this.rectangle = rectangle; this.text = text; this.font = font; // Exit logging #if IS_LOGGING_METHODS Log.Write(String.Format("Exiting method for {0}", this.Name)); #endif }
// ===== #region Draw // Simple draw public void Draw() { // Entry logging #if IS_LOGGING_METHODS Log.Write(String.Format("Entering method for {0}", this.Name)); #endif // Initialization this.BeginDraw(); // this.ActiveShader = Globals.ShaderManager.DefaultShader; // Loop through layers for (int i = 0; i < Globals.SpriteManager.Layers.Count; i++) { // Get current layer SpriteLayer layer = Globals.SpriteManager.Layers[i]; // Draw layer this.DrawLayer(layer); } // Exit logging #if IS_LOGGING_METHODS Log.Write(String.Format("Exiting method for {0}", this.Name)); #endif }
// Description public override String ToString() { String layer = "null"; if (this.Layer != null) { layer = this.Layer.ID.ToString(); } return(String.Format("{{Tx: {0}, Ly: {1}, Or: {2}, An: {3}}}", this.Text, layer, this.Origin, this.Angle)); }
// ===== #region Methods // Add sprite layer public void AddLayer(SpriteLayer layer) { // Entry logging #if IS_LOGGING_METHODS Log.Write(String.Format("Entering method for {0} with {1}", this.Name, layer.Name)); #endif // Add to primary list this.Layers.Add(layer); // Exit logging #if IS_LOGGING_METHODS Log.Write(String.Format("Exiting method for {0} with {1}", this.Name, layer.Name)); #endif }
// ===== #region Constructors // Designated constructor public SpritePathFill(Geometry geometry, Color[] colors, WrappedShader shader) { // Get ID this.id = nextID++; // Entry logging #if IS_LOGGING_METHODS Log.Write(String.Format("Entering method for {0}", this.Name)); #endif // Get vertex count int n = geometry.Points.Count; // Set instance variables this.layer = null; this.layerIndex = -1; this.depth = this.id; // 'CPU z-sorting' hack... Rendered in order of creation... this.vertices = new VertexInput[n]; this.indices = new int[3 * (n - 2)]; this.shader = shader; this.needsUpdate = false; this.geometry = geometry; // Initialize vertices for (int i = 0; i < n; i++) { this.vertices[i] = new VertexInput(geometry.Points[i], colors[i], Vector2.Zero); } // Initialize indices for (int i = 0; i < n - 2; i++) { this.indices[3 * i + 0] = 0; this.indices[3 * i + 1] = (i + 1); this.indices[3 * i + 2] = (i + 2); } // Exit logging #if IS_LOGGING_METHODS Log.Write(String.Format("Exiting method for {0}", this.Name)); #endif }
// Draw a layer public void DrawLayer(SpriteLayer layer) { // Entry logging #if IS_LOGGING_METHODS Log.Write(String.Format("Entering method for {0}", this.Name)); #endif // If no sprites, skip if (layer.SpriteCount <= 0) { goto exit; } // Activate layer buffers on device this.ActiveVertexBuffer = layer.VertexBuffer; this.ActiveIndexBuffer = layer.IndexBuffer; // Below, we will render our sprites in batches. For performance // reasons, we want these batches to be as large as possible. Recall // that our sprites are ordered by depth and texture. We will iterate // over each sprite and ask, 'Can this sprite be batched with the next?' // If so, we defer its rendering and add it to the current batch. // Otherwise, we immediately render the current batch. // Initialization Sprite sprite = layer.Sprites[0]; WrappedTexture texture = sprite.Texture; WrappedShader parameters = sprite.Shader; Sprite nextSprite = sprite; WrappedTexture nextTexture = texture; WrappedShader nextParameters = parameters; int vertexStart = 0; int vertexCount = sprite.Vertices.Length; int indexStart = 0; int indexCount = sprite.Indices.Length; int primitiveCount = sprite.Vertices.Length - 2; // Loop for (int i = 0; i < layer.SpriteCount; i++) { // Log #if IS_LOGGING_DRAW Log.Write(String.Format("Now entering iteration {0} of {1} for {2}", i + 1, layer.SpriteCount, sprite.Name)); Log.Write(String.Format("vertexStart = {0}", vertexStart)); Log.Write(String.Format("vertexCount = {0}", vertexCount)); Log.Write(String.Format("indexStart = {0}", indexStart)); Log.Write(String.Format("indexCount = {0}", indexCount)); Log.Write(String.Format("primitiveCount = {0}", primitiveCount)); Log.Write(String.Format("drawCalls = {0}", drawCalls)); #endif // Initialize canBatch, a boolean that equals true if this sprite can // be batched with the next, as false bool canBatch = false; // On the last iteration, there is no 'next' sprite if (i + 1 < layer.SpriteCount) { // Get next sprite nextSprite = layer.Sprites[i + 1]; nextTexture = nextSprite.Texture; nextParameters = nextSprite.Shader; // Check if this sprite can be batched with the next if (texture == nextTexture && parameters == nextParameters) { canBatch = true; } } // 'Can this sprite be batched with the next?' if (canBatch) { // If so, add to batch vertexCount += sprite.Vertices.Length; indexCount += sprite.Indices.Length; primitiveCount += sprite.Vertices.Length - 2; // Log #if IS_LOGGING_DRAW Log.Write(String.Format("This sprite can be grouped with the next. Adding to batch...")); #endif } else { // Otherwise, render current batch // Log #if IS_LOGGING_DRAW Log.Write(String.Format("This sprite CANNOT be grouped with the next. Rendering batch!")); #endif // Set the active texture this.ActiveTexture = texture; // Set the active shader this.ActiveShader = sprite.Shader; // Call draw this.Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, vertexStart, vertexCount, indexStart, primitiveCount); /* * this.ActiveShader.Effect.CurrentTechnique.Passes[0].Apply(); * Trace.WriteLine(String.Format("updateCount = {0}, drawCalls = {1}:", Globals.Clock.TotalUpdateCount, this.DrawCalls)); * Trace.WriteLine(String.Format("activeShader = {0}", this.ActiveShader.Name)); * Trace.WriteLine(String.Format("texWidth = {0}", this.ActiveShader.Effect.Parameters["tex"].GetValueTexture2D().Width)); * Trace.WriteLine(String.Format("view = {0}", this.ActiveShader.Effect.Parameters["view"].GetValueMatrix())); * Trace.WriteLine(String.Format("projection = {0}", this.ActiveShader.Effect.Parameters["projection"].GetValueMatrix())); * Trace.WriteLine(String.Format("threshold = {0}", this.ActiveShader.Effect.Parameters["threshold"].GetValueSingle())); * Trace.WriteLine(String.Format("alpha = {0}", this.ActiveShader.Effect.Parameters["alpha"].GetValueSingle())); * Trace.WriteLine("=========="); */ /* * String s = String.Format("updateCount = {0}, drawCalls = {1}:", Globals.Clock.TotalUpdateCount, this.DrawCalls); * s += String.Format("\nactiveRenderTarget = {0}", this.activeRenderTarget); * s += String.Format("\nactiveVertexBuffer = {0}", this.activeVertexBuffer); * s += String.Format("\nactiveIndexBuffer = {0}", this.activeIndexBuffer); * s += String.Format("\nactiveView = {0}", this.activeView); * s += String.Format("\nactiveProjection = {0}", this.activeProjection); * s += String.Format("\nactiveTexture = {0}", this.activeTexture); * s += String.Format("\nactiveShader = {0}", this.activeShader); * s += String.Format("\nactiveBlendState = {0}", this.activeBlendState); * s += String.Format("\nactiveDepthStencilState = {0}", this.activeDepthStencilState); * s += "\n=========="; * Trace.WriteLine(s); */ /* * String s = String.Format("updateCount = {0}, device.vB = {1}", Globals.Clock.TotalUpdateCount, this.Device.GetVertexBuffers()[0].VertexBuffer.VertexCount); * Trace.WriteLine(s); */ // Log #if IS_LOGGING_DRAW Log.Write(String.Format("Just rendered the following buffer data:")); for (int l = vertexStart; l < vertexStart + vertexCount; l++) { Log.Write(String.Format("layer.Vertices[{0}] = {1}", l, layer.Vertices[l])); } for (int l = indexStart; l < indexStart + indexCount; l++) { Log.Write(String.Format("layer.Indices[{0}] = {1}", l, layer.Indices[l])); } #endif // This batch is complete this.drawCalls++; // Now, we create a brand new batch vertexStart += vertexCount; indexStart += indexCount; // So far, it only contains the next sprite vertexCount = nextSprite.Vertices.Length; indexCount = nextSprite.Indices.Length; primitiveCount = nextSprite.Vertices.Length - 2; } // Prepare for next iteration sprite = nextSprite; texture = nextTexture; parameters = nextParameters; } // [*] Exit trap: exit: // Exit logging #if IS_LOGGING_METHODS Log.Write(String.Format("Exiting method for {0}", this.Name)); #endif // Exit return; }
// Complicated blur draw public void Draw1() { // Entry logging #if IS_LOGGING_METHODS Log.Write(String.Format("Entering method for {0}", this.Name)); #endif // REMEMBER BUFFER USAGE WRITE ONLY CHANGE! // VertexPositionColorTexture[] vertexArray = new VertexPositionColorTexture[4]; // this.ActiveVertexBuffer.VertexBuffer.GetData<VertexPositionColorTexture>(vertexArray); // for (int i = 0; i < vertexArray.Length; i++) { Trace.WriteLine(String.Format("drawLayer\t{0}\tvertexArray[{1}] = {2}", Globals.Clock.TotalGameTime, i, vertexArray[i])); } // Initialization this.BeginDraw(); int k = 2; // downsampling int n = 2; // iterations WrappedRenderTarget backBuffer = this.BackBuffer; WrappedRenderTarget target1 = this.RenderTargetManager.Allocate(0); WrappedRenderTarget target2 = this.RenderTargetManager.Allocate(k); WrappedRenderTarget target3 = this.RenderTargetManager.Allocate(k); WrappedShader defaultShader = Globals.ShaderManager.DefaultShader; WrappedShader horizontalBlurShader = WrappedHorizontalBlurShader.Gaussian(9, 2); WrappedShader verticalBlurShader = WrappedVerticalBlurShader.Gaussian(9, 2); this.ActiveRenderTarget = target1; this.Clear(this.BackgroundColor, true, false); // Loop through layers for (int i = 0; i < Globals.SpriteManager.Layers.Count - 1; i++) { // Get current layer SpriteLayer layer = Globals.SpriteManager.Layers[i]; // Draw layer this.DrawLayer(layer); } // Loop through iterations for (int i = 0; i < n; i++) { // Apply horizontal blur this.ActiveRenderTarget = target2; if (i == 0) { this.ActiveTexture = target1; } else { this.ActiveTexture = target3; } this.ActiveShader = horizontalBlurShader; this.DrawBackBufferQuad(); // Apply vertical blur this.ActiveRenderTarget = target3; this.ActiveTexture = target2; this.ActiveShader = verticalBlurShader; this.DrawBackBufferQuad(); } // Draw blurred layers to back buffer this.ActiveRenderTarget = backBuffer; this.ActiveTexture = target3; this.ActiveShader = Globals.ShaderManager.DefaultShader; this.DrawBackBufferQuad(); // Loop through remaining layers for (int i = 1; i < Globals.SpriteManager.Layers.Count; i++) { // Get current layer SpriteLayer layer = Globals.SpriteManager.Layers[i]; // Draw layer this.DrawLayer(layer); } this.RenderTargetManager.Release(target3); this.RenderTargetManager.Release(target2); this.RenderTargetManager.Release(target1); if (Globals.Clock.TotalGameTime == 0 && !Globals.TestBool) { target1.Save(@"C:\Users\Chris\Desktop\GameLog\target1.png"); target2.Save(@"C:\Users\Chris\Desktop\GameLog\target2.png"); target3.Save(@"C:\Users\Chris\Desktop\GameLog\target3.png"); Globals.TestBool = true; } // Exit logging #if IS_LOGGING_METHODS Log.Write(String.Format("Exiting method for {0}", this.Name)); #endif }