示例#1
0
        public void RenderToTarget(GraphicsDevice device)
        {
            if (!HasRendered && CurrentFrames.Count > 0)
            {
                device.SetRenderTarget(Target);
                device.Clear(ClearOptions.Target, Color.Transparent, 1.0f, 0);
                DwarfGame.SafeSpriteBatchBegin(SpriteSortMode.Immediate, BlendState.NonPremultiplied, SamplerState.PointClamp,
                                               DepthStencilState.None, RasterizerState.CullNone, null, Matrix.Identity);
                foreach (KeyValuePair <CompositeFrame, Point> framePair in CurrentFrames)
                {
                    CompositeFrame         frame         = framePair.Key;
                    Point                  currentOffset = framePair.Value;
                    List <NamedImageFrame> images        = frame.GetFrames();

                    for (int i = 0; i < images.Count; i++)
                    {
                        int y = FrameSize.Y - images[i].SourceRect.Height;
                        int x = (FrameSize.X / 2) - images[i].SourceRect.Width / 2;
                        DwarfGame.SpriteBatch.Draw(images[i].Image,
                                                   new Rectangle(currentOffset.X * FrameSize.X + x, currentOffset.Y * FrameSize.Y + y,
                                                                 images[i].SourceRect.Width, images[i].SourceRect.Height), images[i].SourceRect,
                                                   frame.Cells[i].Tint);
                    }
                }
                DwarfGame.SpriteBatch.End();
                device.SetRenderTarget(null);
                HasRendered = true;
            }
        }
        private static List <CompositeFrame> CreateFrames(
            List <SpriteSheet> Layers,
            List <Color> Tints,
            List <List <int> > Frames)
        {
            var frameList = new List <CompositeFrame>();

            foreach (var frame in Frames)
            {
                var currentFrame = new CompositeFrame();

                //[0, 1, 2, 3]
                //Values 0 and 1 are the tile to draw.
                //2 and beyond are the layers to draw.

                for (int j = 2; j < frame.Count; j++)
                {
                    var cell = new CompositeCell
                    {
                        Tile  = new Point(frame[0], frame[1]),
                        Sheet = Layers[frame[j]],
                        Tint  = Tints[Math.Min(Math.Max(frame[j], 0), Tints.Count - 1)]
                    };

                    currentFrame.Cells.Add(cell);
                }

                frameList.Add(currentFrame);
            }

            return(frameList);
        }
示例#3
0
 protected bool Equals(CompositeFrame otherFrame)
 {
     if (Cells.Count != otherFrame.Cells.Count)
     {
         return(false);
     }
     for (var i = 0; i < Cells.Count; ++i)
     {
         if (Cells[i] != otherFrame.Cells[i])
         {
             return(false);
         }
     }
     return(true);
 }
示例#4
0
            public void RenderToTarget(GraphicsDevice device, IEnumerable <KeyValuePair <CompositeFrame, FrameID> > currentFrames)
            {
                if (HasChanged)
                {
                    if (Target == null || Target.IsDisposed || Target.GraphicsDevice.IsDisposed)
                    {
                        return;
                    }
                    device.SetRenderTarget(Target);
                    device.Clear(ClearOptions.Target, Color.Transparent, 1.0f, 0);
                    try
                    {
                        DwarfGame.SafeSpriteBatchBegin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp,
                                                       DepthStencilState.None, RasterizerState.CullNone, null, Matrix.Identity);
                        foreach (KeyValuePair <CompositeFrame, FrameID> framePair in currentFrames)
                        {
                            CompositeFrame frame         = framePair.Key;
                            FrameID        currentOffset = framePair.Value;

                            List <NamedImageFrame> images = frame.GetFrames();

                            for (int i = 0; i < images.Count; i++)
                            {
                                int y = FrameSize.Y - images[i].SourceRect.Height;
                                int x = (FrameSize.X / 2) - images[i].SourceRect.Width / 2;
                                DwarfGame.SpriteBatch.Draw(images[i].Image,
                                                           new Rectangle(currentOffset.Offset.X * FrameSize.X + x,
                                                                         currentOffset.Offset.Y * FrameSize.Y + y,
                                                                         images[i].SourceRect.Width,
                                                                         images[i].SourceRect.Height),
                                                           images[i].SourceRect,
                                                           frame.Cells[i].Tint);
                            }
                        }
                    }
                    finally
                    {
                        DwarfGame.SpriteBatch.End();
                    }
                    device.SetRenderTarget(null);
                    HasRendered = true;
                    HasChanged  = false;
                }
            }
示例#5
0
        public Point PushFrame(CompositeFrame frame)
        {
            if (!CurrentFrames.ContainsKey(frame))
            {
                foreach (var layer in frame.Cells)
                {
                    if (layer.Sheet.FrameWidth > FrameSize.X || layer.Sheet.FrameHeight > FrameSize.Y)
                    {
                        FrameSize = new Point(Math.Max(layer.Sheet.FrameWidth, FrameSize.X), Math.Max(layer.Sheet.FrameHeight, FrameSize.Y));
                    }
                }

                Point toReturn = CurrentOffset;
                CurrentOffset.X += 1;
                if (CurrentOffset.X >= TargetSizeFrames.X)
                {
                    CurrentOffset.X  = 0;
                    CurrentOffset.Y += 1;
                }

                if (CurrentOffset.Y >= TargetSizeFrames.Y)
                {
                    TargetSizeFrames = new Point(TargetSizeFrames.X * 2, TargetSizeFrames.Y * 2);
                    Initialize();
                    return(PushFrame(frame));
                }

                CurrentFrames[frame] = toReturn;

                return(toReturn);
            }
            else
            {
                return(CurrentFrames[frame]);
            }
        }
示例#6
0
        public FrameID PushFrame(CompositeFrame frame)
        {
            foreach (var page in Pages.Where(p => p.Target.IsContentLost))
            {
                page.Initialize();
            }

            if (!CurrentFrames.ContainsKey(frame))
            {
                Point nextFreeCell = new Point(-1, -1);
                Page  page         = null;
                int   k            = -1;
                foreach (var pages in Pages)
                {
                    k++;
                    var result = pages.GetNextFreeCell(out nextFreeCell);
                    if (result != Page.AddCellResult.PageFull)
                    {
                        page = pages;
                        if (result == Page.AddCellResult.PageGrown)
                        {
                            EraseFrames(k);
                        }
                        break;
                    }
                }


                Point newFrameSize = new Point(1, 1);
                foreach (var layer in frame.Cells)
                {
                    newFrameSize = new Point(Math.Max(layer.Sheet.FrameWidth, newFrameSize.X),
                                             Math.Max(layer.Sheet.FrameHeight, newFrameSize.Y));
                }

                if (page == null)
                {
                    page = new Page()
                    {
                        TargetSizeFrames = new Point(4, 4),
                        FrameSize        = newFrameSize
                    };
                    page.Initialize();
                    Pages.Add(page);
                    page.GetNextFreeCell(out nextFreeCell);
                    k = Pages.Count - 1;
                }

                if (page.FrameSize.X < newFrameSize.X || page.FrameSize.Y < newFrameSize.Y)
                {
                    EraseFrames(k);

                    if (page.ValidateFrameSizeChange(newFrameSize))
                    {
                        page.FrameSize = newFrameSize;
                        page.Initialize();
                        page.HasChanged = true;
                    }
                    else
                    {
                        page.FrameSize = newFrameSize;
                        if (!page.Grow())
                        {
                            var newPage = new Page()
                            {
                                TargetSizeFrames = new Point(4, 4),
                                FrameSize        = newFrameSize
                            };
                            newPage.Initialize();
                            Pages.Add(newPage);
                            page = newPage;
                            newPage.GetNextFreeCell(out nextFreeCell);
                            k++;
                        }
                        else
                        {
                            page.GetNextFreeCell(out nextFreeCell);
                            EraseFrames(k);
                        }
                    }
                }

                FrameID toReturn = new FrameID(k, nextFreeCell.X, nextFreeCell.Y);
                CurrentFrames[frame] = toReturn;
                page.HasChanged      = true;
                return(toReturn);
            }
            else
            {
                return(CurrentFrames[frame]);
            }
        }