コード例 #1
0
ファイル: ImageBrushImpl.cs プロジェクト: rdterner/Perspex
        public ImageBrushImpl(
            ImageBrush brush,
            SharpDX.Direct2D1.RenderTarget target,
            Size targetSize)
        {
            if (brush.Source == null)
            {
                return;
            }

            var image = ((BitmapImpl)brush.Source.PlatformImpl).GetDirect2DBitmap(target);
            var imageSize = new Size(brush.Source.PixelWidth, brush.Source.PixelHeight);
            var tileMode = brush.TileMode;
            var sourceRect = brush.SourceRect.ToPixels(imageSize);
            var destinationRect = brush.DestinationRect.ToPixels(targetSize);
            var scale = brush.Stretch.CalculateScaling(destinationRect.Size, sourceRect.Size);
            var translate = CalculateTranslate(brush, sourceRect, destinationRect, scale);
            var intermediateSize = CalculateIntermediateSize(tileMode, targetSize, destinationRect.Size);
            var brtOpts = CompatibleRenderTargetOptions.None;

            // TODO: There are times where we don't need to draw an intermediate bitmap. Identify
            // them and directly use 'image' in those cases.
            using (var intermediate = new BitmapRenderTarget(target, brtOpts, intermediateSize))
            {
                Rect drawRect;
                var transform = CalculateIntermediateTransform(
                    tileMode,
                    sourceRect,
                    destinationRect,
                    scale,
                    translate,
                    out drawRect);

                intermediate.BeginDraw();
                intermediate.PushAxisAlignedClip(drawRect.ToDirect2D(), AntialiasMode.Aliased);
                intermediate.Transform = transform.ToDirect2D();
                intermediate.DrawBitmap(image, 1, BitmapInterpolationMode.Linear);
                intermediate.PopAxisAlignedClip();
                intermediate.EndDraw();

                this.PlatformBrush = new BitmapBrush(
                    target, 
                    intermediate.Bitmap,
                    GetBitmapBrushProperties(brush),
                    GetBrushProperties(brush, destinationRect));
            }
        }
コード例 #2
0
ファイル: Wave.cs プロジェクト: chuckj/ConsoleApplication1
        public override void Draw(DrawData dd)
        {
            //	draw wave data
            byte[] wavedata = dd.Song.WaveformData;

            if (wavedata != null)
            {
#if (MARKERS)
                Markers.WriteFlag("Wave");
#endif

                int fnd = -1;
                for (int ndx = dd.LFT / Global.pxpersec; ndx < (dd.RIT + Global.pxpersec - 1) / Global.pxpersec; ndx++)
                {
                    if (resources.Wave_Bmps[ndx] == null)
                    {
                        fnd = ndx;
                        break;
                    }
                }

                if (fnd == -1)
                {
                    for (int ndx = 0; ndx < resources.Wave_Bmps.Length; ndx++)
                    {
                        if (resources.Wave_Bmps[ndx] == null)
                        {
                            fnd = ndx;
                            break;
                        }
                    }
                }

                if (fnd != -1)
                {
                    var ht   = Global.Wave_Height;
                    var half = ht >> 1;

                    using (var renderT = new SharpDX.Direct2D1.BitmapRenderTarget(dd.target, CompatibleRenderTargetOptions.None, new Size2F(Global.pxpersec, ht),
                                                                                  null, new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)))
                    {
                        renderT.BeginDraw();
                        renderT.Clear(SharpDX.Color.Black);

                        using (var path = new PathGeometry(Global.Instance.factory2D1))
                        {
                            var sink = path.Open();
                            sink.BeginFigure(new Vector2(Global.pxpersec, half + 1), FigureBegin.Hollow);
                            sink.AddLine(new Vector2(0, half + 1));
                            for (int ndx = 0, src = fnd * 2 * Global.pxpersec; ndx < Global.pxpersec && src < wavedata.Length; ndx++, src += 2)
                            {
                                sink.AddLine(new Vector2(ndx, half + 1 + wavedata[src]));
                            }
                            sink.EndFigure(FigureEnd.Open);
                            sink.Close();

                            renderT.DrawGeometry(path, resources.Wave_RedBrush);
                        }

                        using (var path = new PathGeometry(Global.Instance.factory2D1))
                        {
                            var sink = path.Open();
                            sink.BeginFigure(new Vector2(Global.pxpersec, half - 1), FigureBegin.Hollow);
                            sink.AddLine(new Vector2(0, half - 1));
                            for (int ndx = 0, src = fnd * 2 * Global.pxpersec + 1; ndx < Global.pxpersec && src < wavedata.Length; ndx++, src += 2)
                            {
                                sink.AddLine(new Vector2(ndx, half - 1 - wavedata[src]));
                            }
                            sink.EndFigure(FigureEnd.Open);
                            sink.Close();

                            renderT.DrawGeometry(path, resources.Wave_GreenBrush);
                        }

                        renderT.EndDraw();

                        resources.Wave_Bmps[fnd] = renderT.Bitmap;
                    }
                }


                for (int ndx = dd.LFT / Global.pxpersec, bgn = ndx * Global.pxpersec; ndx < resources.Wave_Bmps.Length && bgn <= dd.RIT; ndx++, bgn += Global.pxpersec)
                {
                    var bmp = resources.Wave_Bmps[ndx];
                    if (bmp == null)
                    {
                        break;
                    }

                    dd.target.DrawBitmap(bmp, new RectangleF(bgn, Global.Wave_Channels, Global.pxpersec, Global.Wave_Height), 1.0f, BitmapInterpolationMode.NearestNeighbor,
                                         new RectangleF(0, 0, Global.pxpersec, Global.Wave_Height));
                }

#if (MARKERS)
                Markers.WriteFlag("WaveEnd");
#endif
            }
        }
コード例 #3
0
        /// <summary>
        /// Draw the layer.
        /// </summary>
        /// <param name="renderTime">Time since last render.</param>
        /// <remarks>include base.Render(renderTime); in overloads to preserve updating RenderTime field.</remarks>
        public override void Render(RenderTime renderTime)
        {
            base.Render(renderTime);


            if (_lastRender.TotalSeconds > 4)
            {
                _isDirty = true;
            }


            if (_isDirty)
            {
                _target.BeginDraw();
                _target.Clear(new RawColor4(0, 0, 0, 0.95f));
                _target.Transform = Matrix3x2.Translation(-_location);

                // draw borders
                _target.AntialiasMode = AntialiasMode.PerPrimitive;
                _target.FillGeometry(_state.Geometry, Surface.Brushes.GetBrush(new RawColor4(0, 0, 0, .9f)));
                _target.DrawGeometry(_state.Geometry, Surface.Brushes.GetBrush(_state.Color), 1);


                // draw violations
                DrawVisibleViolations();

                // draw crosshair
                DrawCrosshair();

                // Draw lasso
                DrawLasso();

                // draw view rectangle
                DrawViewRectangle();

                // DrawText();

                _target.Transform = Matrix3x2.Identity;
                _target.EndDraw();
                _isDirty    = false;
                _lastRender = TimeSpan.Zero;
            }
            else
            {
                _lastRender += renderTime.ElapsedTime;
            }



            var minimap = _target.Bitmap;

            var tx = Surface.RenderTarget2D.Transform;

            Surface.RenderTarget2D.Transform = Matrix3x2.Identity;
            Surface.RenderTarget2D.DrawBitmap(
                minimap,
                _viewLocation,
                1f,
                BitmapInterpolationMode.Linear);
            Surface.RenderTarget2D.Transform = tx;
        }
コード例 #4
0
 public override void End()
 {
     renderTarget.EndDraw();
 }
コード例 #5
0
ファイル: WorldProcessor.cs プロジェクト: rantingmong/blocm
        public void Start()
        {
            if (ProcessStarted != null)
                ProcessStarted();

            int index           = 0;
            var rendererUtil    = new RendererUtil();

            // obtain file list
            var regionList      = Directory.GetFiles(Path.Combine(worldLocation, "region"), "*.mca").ToList();
            var regionEntries   = new List<RegionEntry>();

            foreach (string region in regionList)
            {
                try
                {
                    // load the region
                    using (RegionFile regionFile = RegionFile.OpenRegion(File.OpenRead(region)))
                    {
                        var renderedChunks = new List<ChunkEntry>();

                        if (RegionLoaded != null)
                            RegionLoaded(regionFile);

                        Debug.WriteLine("Rendering region");

                        DateTime sTime = DateTime.Now;

                        #region Chunk render

                        using (var renderTarget = new BitmapRenderTarget(rendererUtil.D2DDeviceContext, CompatibleRenderTargetOptions.None, new DrawingSizeF(16, 16), new DrawingSize(16, 16), new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied)))
                        {
                            foreach (var anvilChunk in regionFile.Content.Select(chunk => new Anvil(chunk)))
                            {
                                ChunkEntry entry;
                                RenderSegment(anvilChunk, renderTarget, out entry);

                                renderedChunks.Add(entry);
                            }
                        }

                        #endregion

                        #region Region compositor

                        using (var renderTarget = new BitmapRenderTarget(rendererUtil.D2DDeviceContext, CompatibleRenderTargetOptions.None, new DrawingSizeF(512, 512), new DrawingSize(512, 512), new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied)))
                        {
                            renderTarget.BeginDraw();

                            renderTarget.Clear(Color.Transparent);

                            // compose the images
                            foreach (ChunkEntry chunk in renderedChunks)
                            {
                                int cxPos = chunk.XPos % 32;
                                int czPos = chunk.ZPos % 32;

                                if (cxPos < 0)
                                    cxPos = 32 + cxPos;

                                if (czPos < 0)
                                    czPos = 32 + czPos;

                                int xPos = cxPos * 16;
                                int zPos = czPos * 16;

                                renderTarget.Transform = Matrix3x2.Translation(xPos, zPos);
                                renderTarget.DrawBitmap(chunk.RenderedChunk, 1, BitmapInterpolationMode.Linear);
                            }

                            // ReSharper disable PossibleNullReferenceException
                            string[] info = Path.GetFileNameWithoutExtension(region).Split('.');
                            // ReSharper restore PossibleNullReferenceException

                            regionEntries.Add(new RegionEntry
                            {
                                RenderedRegion  = renderTarget.Bitmap,
                                XPos            = Convert.ToInt32(info[1]),
                                ZPos            = Convert.ToInt32(info[2])
                            });

                            renderTarget.EndDraw();
                        }

                        #endregion

                        Debug.WriteLine("Render time is: " + (DateTime.Now - sTime).Seconds + " seconds.");

                        if (RegionRendered != null)
                            RegionRendered();

                        #region Cleanup

                        foreach (ChunkEntry chunk in renderedChunks)
                            chunk.RenderedChunk.Dispose();

                        renderedChunks.Clear();

                        #endregion
                    }
                }
                catch (Exception exception)
                {
                    if (ProcessFailed != null)
                        ProcessFailed(exception.Message + "\nAt:\n" + exception);
                }

                if (ProgressChanged != null)
                    ProgressChanged(++index / (float)regionList.Count);
            }

            #region Extrema processor

            int xMin = 0;
            int zMin = 0;

            int xMax = 0;
            int zMax = 0;

            foreach (RegionEntry entry in regionEntries)
            {
                if (xMin > entry.XPos)
                    xMin = entry.XPos;

                if (xMax < entry.XPos)
                    xMax = entry.XPos;

                if (zMin > entry.ZPos)
                    zMin = entry.ZPos;

                if (zMax < entry.ZPos)
                    zMax = entry.ZPos;
            }

            int wSizeX = (xMax - xMin) * 512 + 512;
            int wSizeZ = (zMax - zMin) * 512 + 512;

            xMin = Math.Abs(xMin);
            zMin = Math.Abs(zMin);

            #endregion

            #region World compositor

            var ResultingBitmap = new Bitmap1(rendererUtil.D2DDeviceContext,
                                              new DrawingSize(wSizeX, wSizeZ),
                                              new BitmapProperties1
                                              {
                                                  BitmapOptions = BitmapOptions.Target,
                                                  PixelFormat   = new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied)
                                              });

            rendererUtil.D2DDeviceContext.Target = ResultingBitmap;

            rendererUtil.D2DDeviceContext.BeginDraw();

            rendererUtil.D2DDeviceContext.Clear(Color.Transparent);

            foreach (RegionEntry entry in regionEntries)
            {
                int xPos = ((xMin + entry.XPos) * 512);
                int zPos = ((zMin + entry.ZPos) * 512);

                rendererUtil.D2DDeviceContext.Transform = Matrix3x2.Translation(xPos, zPos);
                rendererUtil.D2DDeviceContext.DrawBitmap(entry.RenderedRegion, 1, BitmapInterpolationMode.Linear);
            }

            rendererUtil.D2DDeviceContext.EndDraw();

            #endregion

            #region File save

            FileStream file = File.OpenWrite(Path.GetFileName(worldLocation) + ".png");

            var encoder = new PngBitmapEncoder(rendererUtil.ImagingFactory);
            encoder.Initialize(file);

            var frameEncode = new BitmapFrameEncode(encoder);
            frameEncode.Initialize();

            var imageEncoder = new ImageEncoder(rendererUtil.ImagingFactory, rendererUtil.D2DDevice);
            imageEncoder.WriteFrame(ResultingBitmap,
                                    frameEncode,
                                    new ImageParameters(
                                        new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied),
                                        96,
                                        96,
                                        0,
                                        0,
                                        wSizeX,
                                        wSizeZ));

            frameEncode.Commit();
            encoder.Commit();

            #endregion

            #region Cleanup

            file.Close();
            file.Dispose();

            foreach (RegionEntry bitmap in regionEntries)
            {
                bitmap.RenderedRegion.Dispose();
            }

            regionEntries.Clear();

            rendererUtil.Dispose();
            theBlocks.Dispose();

            ResultingBitmap.Dispose();

            #endregion

            if (ProcessComplete != null)
                ProcessComplete();
        }