Beispiel #1
0
        private static void Initialize()
        {
            if (Layers != null && Palettes != null)
            {
                return;
            }

            Layers = FileUtils.LoadJsonListFromMultipleSources <Layer>(ContentPaths.dwarf_layers, null, l => l.Type + "&" + l.Asset);

            foreach (var layer in Layers)
            {
                var rawTexture = AssetManager.GetContentTexture(layer.Asset);
                var memTexture = TextureTool.MemoryTextureFromTexture2D(rawTexture);
                if (memTexture == null)
                {
                    continue;
                }
                layer.CachedTexture = TextureTool.DecomposeTexture(memTexture, BaseDwarfPalette.CachedPalette);
            }

            Palettes = FileUtils.LoadJsonListFromMultipleSources <Palette>(ContentPaths.dwarf_palettes, null, l => l.Asset);

            foreach (var palette in Palettes)
            {
                var rawTexture = AssetManager.GetContentTexture(palette.Asset);
                palette.CachedPalette = TextureTool.RawPaletteFromTexture2D(rawTexture);
            }
        }
Beispiel #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();
                }
            };
        }
Beispiel #3
0
        private static void ResizeLayerLibrary()
        {
            int currentFrameSizeX = 32;
            int currentFrameSizeY = 40;
            int newFrameSizeX     = 48;
            int newFrameSizeY     = 40;

            foreach (var layer in Layers)
            {
                var     rawTexture    = AssetManager.GetContentTexture(layer.Asset);
                var     memTexture    = TextureTool.MemoryTextureFromTexture2D(rawTexture);
                int     num_frames_x  = memTexture.Width / currentFrameSizeX;
                int     num_frames_y  = memTexture.Height / currentFrameSizeY;
                int     currentWidth  = memTexture.Width;
                int     currentHeight = memTexture.Height;
                Color[] newColor      = new Color[(num_frames_x * newFrameSizeX) * (num_frames_y * newFrameSizeY)];
                int     newWidth      = (num_frames_x * newFrameSizeX);
                int     newHeight     = (num_frames_y * newFrameSizeY);
                for (int frameX = 0; frameX < num_frames_x; frameX++)
                {
                    for (int frameY = 0; frameY < num_frames_y; frameY++)
                    {
                        int currentFrameStartX = frameX * currentFrameSizeX;
                        int currentFrameStartY = frameY * currentFrameSizeY;
                        int newFrameStartX     = frameX * newFrameSizeX;
                        int newFrameStartY     = frameY * newFrameSizeY;
                        for (int px = 0; px < currentFrameSizeX; px++)
                        {
                            for (int py = 0; py < currentFrameSizeY; py++)
                            {
                                int newPX = newFrameSizeX / 2 - currentFrameSizeX / 2 + px;
                                int newPY = newFrameSizeY / 2 - currentFrameSizeY / 2 + py;

                                int currentPixelX = px + currentFrameStartX;
                                int currentPixelY = py + currentFrameStartY;
                                int newPixelX     = newPX + newFrameStartX;
                                int newPixelY     = newPY + newFrameStartY;
                                newColor[newPixelX + newPixelY * newWidth] = memTexture.Data[currentPixelX + currentPixelY * currentWidth];
                            }
                        }
                    }
                }

                Texture2D newTexture = new Texture2D(GameStates.GameState.Game.GraphicsDevice, newWidth, newHeight);
                newTexture.SetData(newColor);

                using (System.IO.FileStream fs = new System.IO.FileStream(System.IO.Path.GetFileName(layer.Asset) + "_resized.png", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
                {
                    newTexture.SaveAsPng(fs, newWidth, newHeight);
                }
            }

            System.Environment.Exit(0);
        }
Beispiel #4
0
        private void BlitSheet(TextureAtlas.SpriteAtlasEntry Sheet, Texture2D Into)
        {
            Sheet.NeedsBlitToAtlas = false;

            var memTexture = TextureTool.MemoryTextureFromTexture2D(Sheet.SourceTexture);

            if (Sheet.SourceDefinition.Type == TileSheetType.VariableWidthFont)
            {
                memTexture.Filter(c => (c.R == 0 && c.G == 0 && c.B == 0) ? new Color(0, 0, 0, 0) : c);
            }

            Into.SetData(0, Sheet.AtlasBounds, memTexture.Data, 0, memTexture.Width * memTexture.Height);
        }
Beispiel #5
0
        private static void FillPoliticalRectangle(Rectangle R, Color[] Data, Color Color, int Stride)
        {
            if (StripeTexture == null)
            {
                StripeTexture = TextureTool.MemoryTextureFromTexture2D(AssetManager.GetContentTexture("World\\stripes"));
            }

            for (var x = R.Left; x < R.Right; ++x)
            {
                for (var y = R.Top; y < R.Bottom; ++y)
                {
                    var tX = x % StripeTexture.Width;
                    var tY = y % StripeTexture.Height;
                    if (StripeTexture.Data[(tY * StripeTexture.Width) + tX].R != 0)
                    {
                        Data[(y * Stride) + x] = Blend(Data[(y * Stride) + x], Color, PoliticalOverlayOpacity);
                    }
                }
            }
        }
Beispiel #6
0
        private static bool RuinTemplatesLoaded = false; // Todo: Move this over into the Library

        private static void LoadRuinTemplates()
        {
            if (RuinTemplatesLoaded)
            {
                return;
            }

            RuinTemplatesLoaded = true;
            RuinTemplates       = new List <MemoryTexture>();

            var tex = TextureTool.MemoryTextureFromTexture2D(AssetManager.GetContentTexture("World/ruins"));

            for (var x = 0; x < tex.Width; x += 16) // If our texture isn't the right size this will f**k up.
            {
                for (var y = 0; y < tex.Height; y += 16)
                {
                    var template = new MemoryTexture(16, 16);
                    TextureTool.Blit(tex, new Rectangle(x, y, 16, 16), template, new Point(0, 0));
                    RuinTemplates.Add(template);
                }
            }
        }
        private static void InitializePalettes()
        {
            if (PalettesInitialized)
            {
                return;
            }
            PalettesInitialized = true;

            PaletteTypes = FileUtils.LoadJsonListFromMultipleSources <PaletteType>("Entities/Dwarf/Layers/palette-types.json", null, p => p.Name);
            Palettes     = FileUtils.LoadJsonListFromMultipleSources <Palette>("Entities/Dwarf/Layers/palettes.json", null, p => p.Name);

            foreach (var palette in Palettes)
            {
                var asset = AssetManager.GetContentTexture(palette.Asset);
                palette.CachedPalette = new DwarfCorp.Palette(TextureTool.RawPaletteFromMemoryTextureRow(TextureTool.MemoryTextureFromTexture2D(asset), palette.Row));
            }

            if (Palettes.Count == 0)
            {
                throw new InvalidProgramException("No palettes?");
            }
            _BasePalette = Palettes.FirstOrDefault(p => p.Type == "Base");

            if (_BasePalette == null)
            {
                _BasePalette = Palettes[0];
            }
        }