Ejemplo n.º 1
0
        public void Update(GraphicsDevice Device)
        {
            if (!CompositeValid)
            {
                CompositeValid = true;
                Layers.Sort((a, b) => a.Layer.Precedence - b.Layer.Precedence);

                // Render the composite texture
                var maxSize = new Point(0, 0);
                foreach (var layer in Layers)
                {
                    maxSize.X = Math.Max(layer.Layer.CachedTexture.Width, maxSize.X);
                    maxSize.Y = Math.Max(layer.Layer.CachedTexture.Height, maxSize.Y);
                }

                if (maxSize.X == 0 || maxSize.Y == 0)
                {
                    return;
                }

                if (MemoryComposite == null || MemoryComposite.Width != maxSize.X || MemoryComposite.Height != maxSize.Y)
                {
                    MemoryComposite = new MemoryTexture(maxSize.X, maxSize.Y);
                }

                TextureTool.ClearMemoryTexture(MemoryComposite);

                foreach (var layer in Layers)
                {
                    TextureTool.Blit(layer.Layer.CachedTexture, layer.Palette.CachedPalette, MemoryComposite);
                }

                Composite = TextureTool.Texture2DFromMemoryTexture(Device, MemoryComposite);
            }
        }
Ejemplo n.º 2
0
        public override void Construct()
        {
            Root.RegisterForUpdate(this);
            base.Construct();

            this.OnUpdate = (sender, time) =>
            {
                if (Hidden || Transparent)
                {
                    return;
                }

                if (IsAnyParentHidden())
                {
                    return;
                }

                if (Sprite == null)
                {
                    return;
                }

                var texture = Sprite.GetCompositeTexture();
                if (texture != null)
                {
                    var sheet = new SpriteSheet(texture, 48, 40);
                    var frame = AnimationPlayer.GetCurrentAnimation().Frames[AnimationPlayer.CurrentFrame];
                    if (DynamicAtlasEntry == null)
                    {
                        var tex = new Texture2D(Root.RenderData.Device, 48, 40);
                        DynamicAtlasEntry = Root.SpriteAtlas.AddDynamicSheet(null,
                                                                             new TileSheetDefinition
                        {
                            TileHeight             = 40,
                            TileWidth              = 48,
                            RepeatWhenUsedAsBorder = false,
                            Type = TileSheetType.TileSheet
                        },
                                                                             tex);
                    }

                    var memTex = TextureTool.MemoryTextureFromTexture2D(texture, new Rectangle(frame.X * 48, frame.Y * 40, 48, 40));
                    DynamicAtlasEntry.ReplaceTexture(TextureTool.Texture2DFromMemoryTexture(Root.RenderData.Device, memTex));
                }

                this.Invalidate();
            };

            this.OnClose = (sender) =>
            {
                if (DynamicAtlasEntry != null)
                {
                    DynamicAtlasEntry.Discard();
                }
            };
        }
Ejemplo n.º 3
0
        public static void ProcessImage(MemoryTexture MemTex, String Filename, GraphicsDevice Device)
        {
            var palette = TextureTool.OptimizedPaletteFromMemoryTexture(MemTex);

            palette.Sort((a, b) => (a.R + a.G + a.B) - (b.R + b.G + b.B));

            for (var r = 0; r < MemTex.Height; ++r)
            {
                for (var c = 0; c < MemTex.Width; ++c)
                {
                    var index        = MemTex.Index(c, r);
                    var color        = MemTex.Data[index];
                    var paletteIndex = palette.IndexOf(color);
                    MemTex.Data[index] = new Color(32 * paletteIndex, 32 * paletteIndex, 32 * paletteIndex);
                }
            }

            for (var c = 0; c < palette.Count; ++c)
            {
                MemTex.Data[MemTex.Index(c, 0)] = palette[c];
            }

            TextureTool.Texture2DFromMemoryTexture(Device, MemTex).SaveAsPng(System.IO.File.OpenWrite(Filename), MemTex.Width, MemTex.Height);
        }