public override void Draw(Squared.Render.Frame frame) { const float LightmapScale = 1f; LightmapMaterials.ViewportScale = new Vector2(1f / LightmapScale); LightmapMaterials.ProjectionMatrix = Matrix.CreateOrthographicOffCenter( 0, Width, Height, 0, 0, 1 ); ClearBatch.AddNew(frame, 0, Game.ScreenMaterials.Clear, clearColor: Color.Black); Renderer.RenderLighting(frame, frame, 1); using (var bg = BatchGroup.New(frame, 2)) { var dc = new BitmapDrawCall(TestImage, new Vector2(0, 550), 0.55f); using (var bb = BitmapBatch.New(bg, 0, Renderer.Materials.ScreenSpaceBitmap)) bb.Add(ref dc); dc.Position.X += 600; dc.Textures = new TextureSet(dc.Texture, RampTexture); using (var bb2 = BitmapBatch.New(bg, 1, Renderer.IlluminantMaterials.ScreenSpaceRampBitmap, samplerState2: SamplerState.LinearClamp)) bb2.Add(ref dc); } if (ShowOutlines) { Renderer.RenderOutlines(frame, 2, true); } }
/// <summary> /// Captures batch of screenshots of area specified by Rectangle. /// </summary> /// <param name="duration">Duration of taking the screenshots (in milliseconds)</param> /// <param name="frequency">Frequency of taking the screenshots (in milliseconds)</param> /// <param name="sector">Definition of area to capture</param> /// <returns></returns> public static BitmapBatch GetScreenshotBatch(int duration, int frequency, Rectangle sector) { BitmapBatch batch = new BitmapBatch(); Stopwatch stopwatch; int durationCounter = 0; while (durationCounter < duration) { stopwatch = Stopwatch.StartNew(); // Caputre screenshot and add to batch batch.Add(GetScreenshot(sector)); // Increment periodCounter durationCounter = durationCounter + frequency; stopwatch.Stop(); // Sleep until next iteration // (we can afford converting long to int because stopwatch.ElapsedMilliseconds will never be more that INT_MAX) Thread.Sleep(frequency - (int)(stopwatch.ElapsedMilliseconds)); } return(batch); }
/// <summary> /// Draw everything in the level from background to foreground. /// </summary> public void Draw(GameTime gameTime, Frame frame, DefaultMaterialSet materials) { BitmapBatch bb; for (int i = 0; i <= EntityLayer; ++i) { using (bb = BitmapBatch.New(frame, i, materials.ScreenSpaceBitmap)) bb.Add(new BitmapDrawCall(layers[i], Vector2.Zero)); } using (bb = BitmapBatch.New(frame, EntityLayer + 1, materials.ScreenSpaceBitmap)) DrawTiles(bb); using (var entityBatch = BitmapBatch.New(frame, EntityLayer + 1, materials.ScreenSpaceBitmap)) { foreach (Gem gem in gems) { gem.Draw(gameTime, entityBatch); } Player.Draw(gameTime, entityBatch); foreach (Enemy enemy in enemies) { enemy.Draw(gameTime, entityBatch); } } for (int i = EntityLayer + 1; i < layers.Length; ++i) { using (bb = BitmapBatch.New(frame, i + 1, materials.ScreenSpaceBitmap)) bb.Add(new BitmapDrawCall(layers[i], Vector2.Zero)); } }
/// <summary> /// Draws a gem in the appropriate color. /// </summary> public void Draw(GameTime gameTime, BitmapBatch batch) { batch.Add(new BitmapDrawCall( texture, Position, new Squared.Game.Bounds(Vector2.Zero, Vector2.One), Color, Vector2.One, origin )); }
/// <summary> /// Draws a gem in the appropriate color. /// </summary> public void Draw(GameTime gameTime, BitmapBatch batch) { batch.Add(new BitmapDrawCall( texture, Position, texture.Bounds(), Color, Vector2.One, origin )); }
public LargeBufferTestGame() { Graphics = new GraphicsDeviceManager(this); Graphics.PreferredBackBufferWidth = 1280; Graphics.PreferredBackBufferHeight = 720; Content.RootDirectory = "Content"; BitmapBatch.AdjustPoolCapacities( null, 1024000, null, 16 ); // UseThreadedDraw = false; }
/// <summary> /// Advances the time position and draws the current frame of the animation. /// </summary> public void Draw(GameTime gameTime, BitmapBatch batch, Vector2 position, SpriteEffects spriteEffects) { if (Animation == null) { throw new NotSupportedException("No animation is currently playing."); } // Process passing time. time += (float)gameTime.ElapsedGameTime.TotalSeconds; while (time > Animation.FrameTime) { time -= Animation.FrameTime; // Advance the frame index; looping or clamping as appropriate. if (Animation.IsLooping) { frameIndex = (frameIndex + 1) % Animation.FrameCount; } else { frameIndex = Math.Min(frameIndex + 1, Animation.FrameCount - 1); } } // Calculate the source rectangle of the current frame. Rectangle source = new Rectangle(FrameIndex * Animation.Texture.Height, 0, Animation.Texture.Height, Animation.Texture.Height); var texScale = new Vector2(1.0f / Animation.Texture.Width, 1.0f / Animation.Texture.Height); // Draw the current frame. var drawCall = new BitmapDrawCall( Animation.Texture, position - Origin, new Squared.Game.Bounds( new Vector2(source.X, source.Y) * texScale, new Vector2(source.Right, source.Bottom) * texScale ), Color.White, Vector2.One ); if ((spriteEffects & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally) { drawCall.Mirror(true, false); } if ((spriteEffects & SpriteEffects.FlipVertically) == SpriteEffects.FlipVertically) { drawCall.Mirror(false, true); } batch.Add(drawCall); // spriteEffects }
/// <summary> /// Draws the animated player. /// </summary> public void Draw(GameTime gameTime, BitmapBatch batch) { // Flip the sprite to face the way we are moving. if (Velocity.X > 0) { flip = SpriteEffects.FlipHorizontally; } else if (Velocity.X < 0) { flip = SpriteEffects.None; } // Draw that sprite. sprite.Draw(gameTime, batch, Position, flip); }
public BitmapBatchComparator(BitmapBatch batch) { bitmapBatch = batch; // Check number of Bitmap objects in BitmapBatch if (bitmapBatch.Count < 2) { throw new Exception("BitmapBatch object needs to contain at least two Bitmap objects"); } // Initialize BitmapDifference holder bitmapDifference = new BitmapDifference(bitmapBatch[0]); Compare(); }
/// <summary> /// Draws each tile in the level. /// </summary> private void DrawTiles(BitmapBatch batch) { // For each tile position for (int y = 0; y < Height; ++y) { for (int x = 0; x < Width; ++x) { // If there is a visible tile in that position Texture2D texture = tiles[x, y].Texture; if (texture != null) { // Draw it in screen space. Vector2 position = new Vector2(x, y) * Tile.Size; batch.Add(new BitmapDrawCall(texture, position)); } } } }
public override void Draw(GameTime gameTime, Frame frame) { ClearBatch.AddNew(frame, -1, Materials.Clear, clearColor: ClearColor); const int width = 1280; const int height = 720; var options = new ParallelOptions { // MaxDegreeOfParallelism = 1 }; int layer = 0; Parallel.For( 0, height, options, // One batch per worker thread () => BitmapBatch.New( frame, // Suppress batch combining Interlocked.Increment(ref layer), Materials.ScreenSpaceBitmap ), (y, loopState, bb) => { var drawCall = new BitmapDrawCall(WhitePixel, new Vector2(0, y)); float fx = 0; var range = bb.ReserveSpace(width); var array = range.Array; var offset = range.Offset; for (int x = 0; x < width; x++, fx++) { drawCall.Texture = ((x % 2) == 0) ? WhitePixel : GrayPixel; drawCall.Position.X = fx; drawCall.MultiplyColor = new Color(255, x % 255, y % 255); array[offset + x] = drawCall; } return(bb); }, (bb) => bb.Dispose() ); }
public BitmapBatch GetBitmapBatch(int?layer, bool?worldSpace, BlendState blendState, SamplerState samplerState) { if (Materials == null) { throw new InvalidOperationException("You cannot use the argumentless ImperativeRenderer constructor."); } var actualLayer = layer.GetValueOrDefault(Layer); var actualWorldSpace = worldSpace.GetValueOrDefault(WorldSpace); var desiredBlendState = blendState ?? BlendState; var desiredSamplerState = samplerState ?? SamplerState; CachedBatch cacheEntry; if (!Cache.TryGet <BitmapBatch>( out cacheEntry, container: Container, layer: actualLayer, worldSpace: actualWorldSpace, rasterizerState: RasterizerState, depthStencilState: DepthStencilState, blendState: desiredBlendState, samplerState: desiredSamplerState, useZBuffer: UseZBuffer )) { var material = Materials.GetBitmapMaterial( actualWorldSpace, RasterizerState, DepthStencilState, desiredBlendState ); cacheEntry.Batch = BitmapBatch.New(Container, actualLayer, material, desiredSamplerState, desiredSamplerState, UseZBuffer); Cache.InsertAtFront(ref cacheEntry, null); } if (AutoIncrementLayer && !layer.HasValue) { Layer += 1; } return((BitmapBatch)cacheEntry.Batch); }
/// <summary> /// Draws the animated enemy. /// </summary> public void Draw(GameTime gameTime, BitmapBatch batch) { // Stop running when the game is paused or before turning around. if (!Level.Player.IsAlive || Level.ReachedExit || Level.TimeRemaining == TimeSpan.Zero || waitTime > 0) { sprite.PlayAnimation(idleAnimation); } else { sprite.PlayAnimation(runAnimation); } // Draw facing the way the enemy is moving. SpriteEffects flip = direction > 0 ? SpriteEffects.FlipHorizontally : SpriteEffects.None; sprite.Draw(gameTime, batch, Position, flip); }
public override void Draw(Squared.Render.Frame frame) { const float LightmapScale = 1f; LightmapMaterials.ViewportScale = new Vector2(1f / LightmapScale); LightmapMaterials.ProjectionMatrix = Matrix.CreateOrthographicOffCenter( 0, Width, Height, 0, 0, 1 ); CreateRenderTargets(); var args = new float[] { MagnitudeScale, MiddleGray, AppliedAverageLuminance, MaximumLuminance }; using (var bg = BatchGroup.ForRenderTarget( frame, -1, Lightmap, (dm, _) => Renderer.IlluminantMaterials.SetGammaCompressionParameters(args[0], args[1], args[2], args[3]) )) { ClearBatch.AddNew(bg, 0, LightmapMaterials.Clear, clearColor: Color.Black, clearZ: 0, clearStencil: 0); Renderer.RenderLighting(frame, bg, 1, intensityScale: 1 / MagnitudeScale); }; ClearBatch.AddNew(frame, 0, Game.ScreenMaterials.Clear, clearColor: Color.Black); using (var bb = BitmapBatch.New( frame, 1, Game.ScreenMaterials.Get(Renderer.IlluminantMaterials.ScreenSpaceGammaCompressedBitmap, blendState: BlendState.Opaque) )) bb.Add(new BitmapDrawCall(Lightmap, Vector2.Zero)); if (ShowOutlines) { Renderer.RenderOutlines(frame, 2, true); } }
/// <summary> /// Advances the time position and draws the current frame of the animation. /// </summary> public void Draw(GameTime gameTime, BitmapBatch batch, Vector2 position, SpriteEffects spriteEffects) { if (Animation == null) throw new NotSupportedException("No animation is currently playing."); // Process passing time. time += (float)gameTime.ElapsedGameTime.TotalSeconds; while (time > Animation.FrameTime) { time -= Animation.FrameTime; // Advance the frame index; looping or clamping as appropriate. if (Animation.IsLooping) { frameIndex = (frameIndex + 1) % Animation.FrameCount; } else { frameIndex = Math.Min(frameIndex + 1, Animation.FrameCount - 1); } } // Calculate the source rectangle of the current frame. Rectangle source = new Rectangle(FrameIndex * Animation.Texture.Height, 0, Animation.Texture.Height, Animation.Texture.Height); var texScale = new Vector2(1.0f / Animation.Texture.Width, 1.0f / Animation.Texture.Height); // Draw the current frame. var drawCall = new BitmapDrawCall( Animation.Texture, position - Origin, new Squared.Game.Bounds( new Vector2(source.X, source.Y) * texScale, new Vector2(source.Right, source.Bottom) * texScale ), Color.White, Vector2.One ); if ((spriteEffects & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally) drawCall.Mirror(true, false); if ((spriteEffects & SpriteEffects.FlipVertically) == SpriteEffects.FlipVertically) drawCall.Mirror(false, true); batch.Add(drawCall); // spriteEffects }
public override void Draw(Squared.Render.Frame frame) { CreateRenderTargets(); LightmapMaterials.ViewportScale = new Vector2(1f / LightmapScale); LightmapMaterials.ProjectionMatrix = Matrix.CreateOrthographicOffCenter( 0, BackgroundLightmap.Width, BackgroundLightmap.Height, 0, 0, 1 ); using (var backgroundGroup = BatchGroup.ForRenderTarget(frame, 0, Background)) { ClearBatch.AddNew(backgroundGroup, 1, Game.ScreenMaterials.Clear, clearColor: Color.Transparent); using (var bb = BitmapBatch.New(backgroundGroup, 2, Game.ScreenMaterials.WorldSpaceBitmap)) { for (var i = 0; i < 1; i++) { var layer = Layers[i]; var dc = new BitmapDrawCall(layer, Vector2.Zero); dc.SortKey = i; bb.Add(dc); } } } using (var foregroundGroup = BatchGroup.ForRenderTarget(frame, 1, Foreground)) { ClearBatch.AddNew(foregroundGroup, 1, Game.ScreenMaterials.Clear, clearColor: Color.Transparent); using (var bb = BitmapBatch.New(foregroundGroup, 2, Game.ScreenMaterials.WorldSpaceBitmap)) { for (var i = 1; i < Layers.Length; i++) { var layer = Layers[i]; var dc = new BitmapDrawCall(layer, Vector2.Zero); dc.SortKey = i; bb.Add(dc); } } } if (ShowBrickSpecular) { using (var bricksLightGroup = BatchGroup.ForRenderTarget(frame, 2, ForegroundLightmap)) { ClearBatch.AddNew(bricksLightGroup, 1, LightmapMaterials.Clear, clearColor: new Color(0, 0, 0, 255), clearZ: 0, clearStencil: 0); ForegroundRenderer.RenderLighting(frame, bricksLightGroup, 2); } } if (ShowAOShadow) { using (var aoShadowFirstPassGroup = BatchGroup.ForRenderTarget(frame, 3, AOShadowScratch)) { ClearBatch.AddNew(aoShadowFirstPassGroup, 1, LightmapMaterials.Clear, clearColor: Color.Transparent); using (var bb = BitmapBatch.New(aoShadowFirstPassGroup, 2, Game.ScreenMaterials.ScreenSpaceHorizontalGaussianBlur5Tap)) { bb.Add(new BitmapDrawCall(Foreground, Vector2.Zero, 1f / LightmapScale)); } } } using (var backgroundLightGroup = BatchGroup.ForRenderTarget(frame, 4, BackgroundLightmap)) { ClearBatch.AddNew(backgroundLightGroup, 1, LightmapMaterials.Clear, clearColor: new Color(40, 40, 40, 255), clearZ: 0, clearStencil: 0); BackgroundRenderer.RenderLighting(frame, backgroundLightGroup, 2); if (ShowBrickSpecular) { using (var foregroundLightBatch = BitmapBatch.New(backgroundLightGroup, 3, MaskedForegroundMaterial)) { var dc = new BitmapDrawCall( ForegroundLightmap, Vector2.Zero ); dc.Textures = new TextureSet(dc.Textures.Texture1, BricksLightMask); foregroundLightBatch.Add(dc); } } else { ForegroundRenderer.RenderLighting(frame, backgroundLightGroup, 3); } if (ShowAOShadow) { using (var aoShadowBatch = BitmapBatch.New(backgroundLightGroup, 4, AOShadowMaterial)) { var dc = new BitmapDrawCall( AOShadowScratch, new Vector2(0, 4) ); dc.MultiplyColor = Color.Black; dc.AddColor = Color.White; aoShadowBatch.Add(dc); } } } using (var foregroundLightGroup = BatchGroup.ForRenderTarget(frame, 5, ForegroundLightmap)) { ClearBatch.AddNew(foregroundLightGroup, 1, LightmapMaterials.Clear, clearColor: new Color(127, 127, 127, 255), clearZ: 0, clearStencil: 0); ForegroundRenderer.RenderLighting(frame, foregroundLightGroup, 2); } SetRenderTargetBatch.AddNew(frame, 49, null); ClearBatch.AddNew(frame, 50, Game.ScreenMaterials.Clear, clearColor: Color.Black, clearZ: 0, clearStencil: 0); if (ShowLightmap) { using (var bb = BitmapBatch.New(frame, 55, Game.ScreenMaterials.WorldSpaceBitmap)) { var dc = new BitmapDrawCall(BackgroundLightmap, Vector2.Zero, LightmapScale); bb.Add(dc); } } else { var dc = new BitmapDrawCall(Background, Vector2.Zero); var material = LightmapMaterials.Get(LightmapMaterials.WorldSpaceLightmappedBitmap, blendState: BlendState.AlphaBlend); using (var bb = BitmapBatch.New(frame, 55, material)) { dc.Textures = new TextureSet(Background, BackgroundLightmap); dc.SortKey = 0; bb.Add(dc); } ParticleRenderer.Draw(frame, 56); using (var bb = BitmapBatch.New(frame, 57, material)) { dc.Textures = new TextureSet(Foreground, ForegroundLightmap); dc.SortKey = 1; bb.Add(dc); } } if (ShowOutlines || (Dragging != null)) { BackgroundRenderer.RenderOutlines(frame, 59, true); } }
public override void Draw(GameTime gameTime, Frame frame) { if (false) { var stats = RenderManager.GetMemoryStatistics(); Console.WriteLine( "managed: {0:0000000}kb vertex: {1:0000000}kb index: {2:0000000}kb", (stats.ManagedIndexBytes + stats.ManagedVertexBytes) / 1024.0, stats.UnmanagedVertexBytes / 1024.0, stats.UnmanagedIndexBytes / 1024.0 ); } ClearBatch.AddNew(frame, -1, Materials.Clear, clearColor: ClearColor); const int width = 1280; const int height = 720; var options = new ParallelOptions { // MaxDegreeOfParallelism = 1 }; int layer = 0; Parallel.For( 0, height, options, // One batch per worker thread () => BitmapBatch.New( frame, // Suppress batch combining Interlocked.Increment(ref layer), Materials.ScreenSpaceBitmap ), (y, loopState, bb) => { var drawCall = new BitmapDrawCall(WhitePixel, new Vector2(0, y)); float fx = 0; var range = bb.ReserveSpace(width); var array = range.Array; var offset = range.Offset; for (int x = 0; x < width; x++, fx++) { drawCall.Texture = ((x % 2) == 0) ? WhitePixel : GrayPixel; drawCall.Position.X = fx; drawCall.MultiplyColor = new Color(255, x % 255, y % 255); array[offset + x] = drawCall; } return(bb); }, (bb) => bb.Dispose() ); var ir = new ImperativeRenderer( frame, Materials, blendState: BlendState.Opaque, depthStencilState: DepthStencilState.None, rasterizerState: RasterizerState.CullNone, worldSpace: false, layer: 9999 ); DrawPerformanceStats(ref ir); }
static void Main(string[] args) { Stopwatch stopwatch = new Stopwatch(); Bitmap a = new Bitmap("./images/A.png"); Bitmap b = new Bitmap("./images/B.png"); Bitmap c = new Bitmap("./images/C.png"); Bitmap d = new Bitmap("./images/D.png"); Bitmap e = new Bitmap("./images/D.png"); // Test cases enabling bool testBitmapEditor = true; bool testBitmapDifference = true; bool testBitmapComparator = true; bool testBitmapBatchComparator = true; bool testIntegration = true; // CLASS BitmapEditor if (testBitmapEditor) { // BitmapEditor.SetPixel() // Bitmap <-> BitmapEditor implicit conversions { Bitmap bitmap = new Bitmap(4, 4, PixelFormat.Format24bppRgb); BitmapEditor bitmapEditor = bitmap; bitmapEditor.SetPixel(0, 0, Color.Cyan); bitmapEditor.SetPixel(1, 1, Color.Cyan); bitmapEditor.SetPixel(2, 2, Color.Cyan); bitmap = bitmapEditor; bitmap.Save("./bitmapEditorConversions1.png"); } // BitmapEditor.SetPixel() // Bitmap <-> BitmapEditor explicit conversions { BitmapEditor bitmapEditor = new BitmapEditor(new Bitmap(a)); bitmapEditor.SetPixel(0, 0, Color.Cyan); bitmapEditor.SetPixel(1, 1, Color.Cyan); bitmapEditor.SetPixel(2, 2, Color.Cyan); bitmapEditor.SetPixel(3, 3, Color.Cyan); bitmapEditor.SetPixel(4, 4, Color.Cyan); bitmapEditor.SetPixel(5, 5, Color.Cyan); bitmapEditor.SetPixel(6, 6, Color.Cyan); bitmapEditor.SetPixel(100, 100, Color.Cyan); bitmapEditor.SetPixel(101, 100, Color.Cyan); bitmapEditor.SetPixel(102, 100, Color.Cyan); bitmapEditor.SetPixel(103, 100, Color.Cyan); ((Bitmap)(bitmapEditor)).Save("./bitmapEditorConversions2.png"); } } // CLASS BitmapDifference TEST if (testBitmapDifference) { { // BitmapDifference from Bitmap // BitmapDifference.SetPixel() - marking pixels as different // BitmapDifference.GetBitmapDifferenceImage BitmapDifference bitmapDifferenceA = new BitmapDifference(a); bitmapDifferenceA.SetPixel(100, 100, true); bitmapDifferenceA.SetPixel(100, 101, true); bitmapDifferenceA.SetPixel(100, 102, true); bitmapDifferenceA.SetPixel(100, 103, true); bitmapDifferenceA.GetBitmapDifferenceImage().Save("./bitmapDifferenceImage.png"); // BitmapDifference from Bitmap // BitmapDifference.AddMask() // BitmapDifference.GetBitmapMaskedImage() BitmapDifference bitmapDifferenceB = new BitmapDifference(a); bitmapDifferenceB.AddMask(bitmapDifferenceA.GetBitmapDifferenceImage()); bitmapDifferenceB.GetBitmapMaskedImage().Save("./bitmapDifferenceMaskedImage.png"); } } // CLASS BitmapComparator TEST if (testBitmapComparator) { BitmapComparator bitmapComparator = new BitmapComparator(a, b); bitmapComparator.GetBitmapDifferenceImage().Save("./bitmapComparatorDiffImg.png"); } // CLASS BitmapBatchComparator TEST if (testBitmapBatchComparator) { BitmapBatch bitmapBatch = new BitmapBatch(); bitmapBatch.Add(a); bitmapBatch.Add(b); bitmapBatch.Add(c); bitmapBatch.Add(d); bitmapBatch.Add(e); BitmapBatchComparator bitmapBatchComparator = new BitmapBatchComparator(bitmapBatch); bitmapBatchComparator.GetBitmapDifferenceImage().Save("./bitmapBatchComparatorDiffImg.png"); } // CLASS integration TEST if (testIntegration) { stopwatch.Restart(); BitmapBatch bbb = new BitmapBatch(); Console.WriteLine("A: " + stopwatch.ElapsedMilliseconds); bbb = Screener.GetScreenshotBatch(3000, 250); Console.WriteLine("B: " + stopwatch.ElapsedMilliseconds); BitmapBatchComparator bbcb = new BitmapBatchComparator(bbb); Console.WriteLine("C: " + stopwatch.ElapsedMilliseconds); bbcb.GetBitmapDifferenceImage().Save("./bitmapBatchDifference.png"); Console.WriteLine("D: " + stopwatch.ElapsedMilliseconds); BitmapComparator ndnjjcd = new BitmapComparator(a, b); Console.WriteLine("E: " + stopwatch.ElapsedMilliseconds); } Console.WriteLine("Done."); Console.ReadKey(); }
public override void Draw(GameTime gameTime, Frame frame) { ClearBatch.AddNew(frame, 4, Materials.Clear, clearColor: new Color(16, 32, 48)); var alphaGeometry = Materials.Get(Materials.ScreenSpaceGeometry, blendState: BlendState.AlphaBlend); using (var gb = GeometryBatch.New(frame, 5, alphaGeometry)) { gb.AddGradientFilledQuad( Vector2.Zero, new Vector2(Graphics.PreferredBackBufferWidth, Graphics.PreferredBackBufferHeight), Color.DarkSlateGray, Color.DarkSlateGray, Color.SlateBlue, Color.SlateBlue ); } using (var gb = GeometryBatch.New(frame, 6, alphaGeometry)) { var alphaBlack = new Color(0, 0, 0, 192); var alphaBlack2 = new Color(0, 0, 0, 64); gb.AddQuadBorder( Playfield.Bounds.TopLeft + new Vector2(32 + 24, 0), Playfield.Bounds.BottomRight + new Vector2(-32 - 24, 0), alphaBlack2, alphaBlack, 24 ); } // Render the contents of the trail buffer to the screen using additive blending using (var bb = BitmapBatch.New(frame, 7, Materials.Trail)) { bb.Add(new BitmapDrawCall( TrailBuffer, Vector2.Zero, (float)TrailScale )); } // Render the paddles and ball to both the framebuffer and the trail buffer (note the different layer values) using (var gb = GeometryBatch.New(frame, 8, alphaGeometry)) using (var gb2 = GeometryBatch.New(frame, 9, alphaGeometry)) using (var trailBatch = GeometryBatch.New(frame, 2, alphaGeometry)) { foreach (var paddle in Paddles) { gb.AddFilledQuad( paddle.Bounds.TopLeft, paddle.Bounds.BottomRight, Color.White ); gb2.AddQuadBorder( paddle.Bounds.TopLeft, paddle.Bounds.BottomRight, Color.Black, Color.Black, 2.25f ); trailBatch.AddFilledQuad( paddle.Bounds.TopLeft, paddle.Bounds.BottomRight, Color.White ); } gb.AddFilledRing(Ball.Position, 0.0f, Ball.Radius, Color.White, Color.White); gb2.AddFilledRing(Ball.Position, Ball.Radius, Ball.Radius + 2.0f, Color.Black, Color.Black); trailBatch.AddFilledRing(Ball.Position, 0.0f, Ball.Radius, Color.White, Color.White); } // Render the score values using a stringbatch (unfortunately this uses spritebatch to render spritefonts :( ) { var ir = new ImperativeRenderer(frame, Materials, 10, blendState: BlendState.AlphaBlend); ir.DrawString( Font, String.Format("Player 1: {0:00}", Scores[0]), new Vector2(16, 16) ); var player2Text = String.Format("Player 2: {0:00}", Scores[1]); ir.DrawString( Font, player2Text, new Vector2(Graphics.PreferredBackBufferWidth - 16 - Font.MeasureString(player2Text).X, 16) ); } // The first stage of our frame involves selecting the trail buffer as our render target (note that it's layer 0) SetRenderTargetBatch.AddNew(frame, 0, TrailBuffer); if (FirstFrame) { // If it's the first time we've rendered, we erase the trail buffer since it could contain anything ClearBatch.AddNew(frame, 1, Materials.Clear, clearColor: Color.Black); FirstFrame = false; } else { // Otherwise, we fade out the contents of the trail buffer using (var gb = GeometryBatch.New(frame, 1, Materials.SubtractiveGeometry)) { gb.AddFilledQuad( new Bounds(Vector2.Zero, new Vector2(Graphics.PreferredBackBufferWidth, Graphics.PreferredBackBufferHeight)), new Color(12, 12, 12, 0) ); } } // After the trail buffer has been updated, we turn it off and begin rendering to the framebuffer. Note layer 3. SetRenderTargetBatch.AddNew(frame, 3, null); }
/// <summary> /// Draws the animated enemy. /// </summary> public void Draw(GameTime gameTime, BitmapBatch batch) { // Stop running when the game is paused or before turning around. if (!Level.Player.IsAlive || Level.ReachedExit || Level.TimeRemaining == TimeSpan.Zero || waitTime > 0) { sprite.PlayAnimation(idleAnimation); } else { sprite.PlayAnimation(runAnimation); } // Draw facing the way the enemy is moving. SpriteEffects flip = direction > 0 ? SpriteEffects.FlipHorizontally : SpriteEffects.None; sprite.Draw(gameTime, batch, Position, flip); }
/// <summary> /// Draws the animated player. /// </summary> public void Draw(GameTime gameTime, BitmapBatch batch) { // Flip the sprite to face the way we are moving. if (Velocity.X > 0) flip = SpriteEffects.FlipHorizontally; else if (Velocity.X < 0) flip = SpriteEffects.None; // Draw that sprite. sprite.Draw(gameTime, batch, Position, flip); }
public override void Draw(Squared.Render.Frame frame) { CreateRenderTargets(); LightmapMaterials.ViewportScale = new Vector2(1f / LightmapScale); LightmapMaterials.ProjectionMatrix = Matrix.CreateOrthographicOffCenter( 0, BackgroundLightmap.Width, BackgroundLightmap.Height, 0, 0, 1 ); using (var backgroundGroup = BatchGroup.ForRenderTarget(frame, 0, Background)) { ClearBatch.AddNew(backgroundGroup, 1, Game.ScreenMaterials.Clear, clearColor: Color.Transparent); using (var bb = BitmapBatch.New(backgroundGroup, 2, Game.ScreenMaterials.WorldSpaceBitmap)) { for (var i = 0; i < 1; i++) { var layer = Layers[i]; var dc = new BitmapDrawCall(layer, Vector2.Zero); dc.SortKey = i; bb.Add(dc); } } } using (var foregroundGroup = BatchGroup.ForRenderTarget(frame, 1, Foreground)) { ClearBatch.AddNew(foregroundGroup, 1, Game.ScreenMaterials.Clear, clearColor: Color.Transparent); using (var bb = BitmapBatch.New(foregroundGroup, 2, Game.ScreenMaterials.WorldSpaceBitmap)) { for (var i = 1; i < Layers.Length; i++) { var layer = Layers[i]; var dc = new BitmapDrawCall(layer, Vector2.Zero); dc.SortKey = i; bb.Add(dc); } } } using (var backgroundLightGroup = BatchGroup.ForRenderTarget(frame, 4, BackgroundLightmap)) { ClearBatch.AddNew(backgroundLightGroup, 1, LightmapMaterials.Clear, clearColor: new Color(32, 32, 32, 255), clearZ: 0, clearStencil: 0); BackgroundRenderer.RenderLighting(frame, backgroundLightGroup, 2); } using (var foregroundLightGroup = BatchGroup.ForRenderTarget(frame, 5, ForegroundLightmap)) { ClearBatch.AddNew(foregroundLightGroup, 1, LightmapMaterials.Clear, clearColor: new Color(96, 96, 96, 255), clearZ: 0, clearStencil: 0); ForegroundRenderer.RenderLighting(frame, foregroundLightGroup, 2); } SetRenderTargetBatch.AddNew(frame, 49, null); ClearBatch.AddNew(frame, 50, Game.ScreenMaterials.Clear, clearColor: Color.Black, clearZ: 0, clearStencil: 0); if (ShowLightmap) { using (var bb = BitmapBatch.New(frame, 55, Game.ScreenMaterials.WorldSpaceBitmap)) { var dc = new BitmapDrawCall(BackgroundLightmap, Vector2.Zero, LightmapScale); bb.Add(dc); } ParticleRenderer.Draw(frame, 56); } else { var dc = new BitmapDrawCall(Background, Vector2.Zero); var material = LightmapMaterials.Get(LightmapMaterials.WorldSpaceLightmappedBitmap, blendState: BlendState.AlphaBlend); using (var bb = BitmapBatch.New(frame, 55, material)) { dc.Textures = new TextureSet(Background, BackgroundLightmap); dc.SortKey = 0; bb.Add(dc); } ParticleRenderer.Draw(frame, 56); using (var bb = BitmapBatch.New(frame, 57, material)) { dc.Textures = new TextureSet(Foreground, ForegroundLightmap); dc.SortKey = 1; bb.Add(dc); } } if (ShowOutlines || (Dragging != null)) { BackgroundRenderer.RenderOutlines(frame, 59, true); } }
/// <summary> /// Draws each tile in the level. /// </summary> private void DrawTiles(BitmapBatch batch) { // For each tile position for (int y = 0; y < Height; ++y) { for (int x = 0; x < Width; ++x) { // If there is a visible tile in that position Texture2D texture = tiles[x, y].Texture; if (texture != null) { // Draw it in screen space. Vector2 position = new Vector2(x, y) * Tile.Size; batch.Add(new BitmapDrawCall(texture, position)); } } } }
public IBitmapBatch GetBitmapBatch(int?layer, bool?worldSpace, BlendState blendState, SamplerState samplerState, Material customMaterial) { if (Materials == null) { throw new InvalidOperationException("You cannot use the argumentless ImperativeRenderer constructor."); } var actualLayer = layer.GetValueOrDefault(Layer); var actualWorldSpace = worldSpace.GetValueOrDefault(WorldSpace); var desiredBlendState = blendState ?? BlendState; var desiredSamplerState = samplerState ?? SamplerState; if (LowPriorityMaterialOrdering) { desiredSamplerState = null; } CachedBatch cacheEntry; if (!Cache.TryGet <IBitmapBatch>( out cacheEntry, LowPriorityMaterialOrdering ? CachedBatchType.MultimaterialBitmap : CachedBatchType.Bitmap, container: Container, layer: actualLayer, worldSpace: actualWorldSpace, rasterizerState: RasterizerState, depthStencilState: DepthStencilState, blendState: desiredBlendState, samplerState: desiredSamplerState, customMaterial: customMaterial, useZBuffer: UseZBuffer )) { Material material; if (customMaterial != null) { material = Materials.Get( customMaterial, RasterizerState, DepthStencilState, desiredBlendState ); } else { material = Materials.GetBitmapMaterial( actualWorldSpace, RasterizerState, DepthStencilState, desiredBlendState ); } IBitmapBatch bb; if (LowPriorityMaterialOrdering) { bb = MultimaterialBitmapBatch.New(Container, actualLayer, Material.Null, UseZBuffer); } else { bb = BitmapBatch.New(Container, actualLayer, material, desiredSamplerState, desiredSamplerState, UseZBuffer); } bb.Sorter = DeclarativeSorter; cacheEntry.Batch = bb; Cache.InsertAtFront(ref cacheEntry, null); } if (AutoIncrementLayer && !layer.HasValue) { Layer += 1; } return((IBitmapBatch)cacheEntry.Batch); }