public static AnimationChain FromGif(string fileName, string contentManagerName)
        {
            if (FileManager.IsRelative(fileName))
            {
                fileName = FileManager.RelativeDirectory + fileName;
            }

            if (FlatRedBallServices.IsLoaded <AnimationChain>(fileName, contentManagerName))
            {
                return(FlatRedBallServices.GetNonDisposable <AnimationChain>(fileName, contentManagerName).Clone());
            }

            ImageDataList imageDataList = GifLoader.GetImageDataList(fileName);

            int numberOfFrames = imageDataList.Count;

            AnimationChain animationChain = new AnimationChain(numberOfFrames);

            for (int i = 0; i < numberOfFrames; i++)
            {
                // We assume GIFs are for 2D games that don't need mipmaps.  Could change this later
                // if needed
                const bool generateMipmaps = false;

                Texture2D texture2D = imageDataList[i].ToTexture2D(generateMipmaps, FlatRedBallServices.GraphicsDevice);
                texture2D.Name =
                    fileName + i.ToString();

                if (i >= imageDataList.FrameTimes.Count)
                {
                    const double defaultFrameTime = .1;

                    animationChain.Add(
                        new AnimationFrame(
                            texture2D, (float)defaultFrameTime));
                }
                else
                {
                    animationChain.Add(
                        new AnimationFrame(
                            texture2D, (float)imageDataList.FrameTimes[i]));
                }
                FlatRedBallServices.AddDisposable(texture2D.Name, texture2D, contentManagerName);
            }

            // Don't dispose the anything because it's part of the
            // content manager.

            animationChain.ParentGifFileName = fileName;
            animationChain.Name = FileManager.RemovePath(fileName);

            return(animationChain);
        }
예제 #2
0
        void UpdatePreRenderedTexture()
        {
#if DEBUG
            if (string.IsNullOrEmpty(ContentManager))
            {
                throw new Exception("The ContentManager property must be set before updating textures");
            }
#endif

            UnloadPreRenderedTexture();

            if (!string.IsNullOrEmpty(mText) && this.Font != null)
            {
                PreRenderedTexture = Font.RenderToTexture2D(this.mText, this.HorizontalAlignment, this.Red, this.Green, this.Blue, this.Alpha);
                FlatRedBallServices.AddDisposable(this.GetHashCode().ToString() + "PreRendered", PreRenderedTexture, ContentManager);
            }
        }
예제 #3
0
        private static Texture2D GenerateRadiusTexture(int maxRange, int minRange)
        {
            // Determine the new layer size
            var newTextureHeight = maxRange * 2;
            var newTextureWidth  = maxRange * 2;

            // This layer holds whatever we want drawn
            var temporaryLayer = new Layer();

            // Define the layer.
            var renderTarget = new RenderTarget2D(
                FlatRedBallServices.GraphicsDevice,
                newTextureWidth,
                newTextureHeight);

            temporaryLayer.RenderTarget = renderTarget;

            var maxRangeSprite = new Sprite
            {
                Texture        = RangeCircleTexture,
                TextureScale   = newTextureHeight / (float)RangeCircleTexture.Height,
                ColorOperation = ColorOperation.ColorTextureAlpha,
                Z     = 0,
                Red   = 0,
                Green = 255,
                Blue  = 0,
            };

            SpriteManager.AddToLayer(maxRangeSprite, temporaryLayer);

            var minRangeSprite = new Sprite
            {
                Texture      = MinRangeCircleTexture,
                TextureScale = minRange * 2 / (float)MinRangeCircleTexture.Height,
                //ColorOperation = ColorOperation.Subtract,
                ColorOperation = ColorOperation.ColorTextureAlpha,
                Z     = 1,
                Red   = 255,
                Green = 0,
                Blue  = 0,
            };

            SpriteManager.AddToLayer(minRangeSprite, temporaryLayer);

            // Rendering requires a camera. We'll create a temporary one (don't add it to the SpriteManager)
            var temporaryCamera = new Camera(null, newTextureWidth, newTextureHeight)
            {
                DrawsWorld = false, Z = 40
            };

            // We only want the camera to draw the layer
            temporaryCamera.UsePixelCoordinates();
            temporaryCamera.AddLayer(temporaryLayer);

            FlatRedBall.Graphics.Renderer.DrawCamera(temporaryCamera, null);

            //using (var fileStream = File.Create("range.png"))
            //{
            //    renderTarget.SaveAsPng(fileStream, newTextureWidth, newTextureHeight);
            //}

            SpriteManager.RemoveSprite(maxRangeSprite);
            SpriteManager.RemoveSprite(minRangeSprite);

            FlatRedBallServices.AddDisposable(
                "Max" + maxRange.ToString() + "Min" + minRange.ToString() + ".png",
                renderTarget,
                "ContentManagerName");

            return(renderTarget);
        }
예제 #4
0
        public void UpdateFromStructure(BaseStructure structure, IEntityFactory factory, int currentSatoshis)
        {
            BuildingFactory = factory;
            RangeTuple      = new Tuple <int, int>((int)structure.RangedRadius, (int)structure.MinimumRangeRadius);
            RangeXOffset    = structure.PivotPoint.RelativeX;
            RangeYOffset    = structure.SpriteInstance.RelativeY;
            BuildingType    = structure.GetType();
            structureInfo   = new StructureInfoRuntime.StructureInfoSaveState(structure);
            UpdateAffordability(currentSatoshis);

            RenderTarget2D renderTarget       = null;
            var            renderName         = BuildingType.FullName + ".png";
            var            contentManagerName = FlatRedBallServices.GlobalContentManager;

            if (FlatRedBallServices
                .GetContentManagerByName(contentManagerName)
                .IsAssetLoadedByName <RenderTarget2D>(renderName))
            {
                renderTarget = FlatRedBallServices
                               .GetContentManagerByName(contentManagerName)
                               .GetDisposable <RenderTarget2D>(renderName);
            }

            var baseHeight = structure.SpriteInstance.Height;
            var baseWidth  = structure.SpriteInstance.Width;

            if (renderTarget == null)
            {
                structure.LightSpriteInstance.Visible         = false;
                structure.LightAimSpriteInstance.Visible      = false;
                structure.AimSpriteInstance.RelativeRotationZ = MathHelper.ToRadians(0);
                structure.Collision.Visible           = false;
                structure.LightSpriteInstance.Visible = false;
                var relativeY = structure.SpriteInstance.RelativeY;
                structure.SpriteInstance.RelativeY = 0;

                relativeY = structure.SpriteInstance.RelativeY - relativeY;

                structure.PivotPoint.RelativeY += relativeY;
                structure.ForceUpdateDependenciesDeep();

                var aimHeight = structure.AimSpriteInstance.Height;
                var aimWidth  = structure.AimSpriteInstance.Width;

                var aimRelativeX = structure.PivotPoint.RelativeX + structure.AimSpriteInstance.RelativeX;
                var aimRelativeY = structure.PivotPoint.RelativeY + structure.AimSpriteInstance.RelativeY;

                var structureTop = Math.Min(-baseHeight / 2,
                                            -aimRelativeY - aimHeight / 2);
                var structureBottom = Math.Max(baseHeight / 2,
                                               -aimRelativeY + aimHeight / 2);

                var structureLeft = Math.Min(-baseWidth / 2,
                                             aimRelativeX - aimWidth / 2);
                var structureRight = Math.Max((baseWidth / 2),
                                              aimRelativeX + aimWidth / 2);

                var structureHeight = (int)Math.Abs(structureTop - structureBottom);
                var structureWidth  = (int)Math.Abs(structureRight - structureLeft);

                // This layer holds whatever we want drawn
                var temporaryLayer = new Layer();

                // Define the layer. This uses the current game's resolution, but it could be any size
                renderTarget = new RenderTarget2D(
                    FlatRedBallServices.GraphicsDevice,
                    structureWidth,
                    structureHeight);
                temporaryLayer.RenderTarget = renderTarget;
                structure.MoveToLayer(temporaryLayer);
                structure.Y = -structureHeight / 2 + structure.SpriteInstance.Height / 2;
                structure.ForceUpdateDependenciesDeep();

                // Rendering requires a camera. We'll create a temporary one (don't add it to the SpriteManager)
                var temporaryCamera = new Camera(null, structureWidth, structureHeight)
                {
                    DrawsWorld = false, Z = 40
                };
                // We only want the camera to draw the layer
                temporaryCamera.UsePixelCoordinates();
                temporaryCamera.AddLayer(temporaryLayer);

                Renderer.DrawCamera(temporaryCamera, null);

                //using (var fileStream = File.Create("test.png"))
                //{
                //    structureTexture.SaveAsPng(fileStream, structureTexture.Width, structureTexture.Height);
                //}

                FlatRedBallServices.AddDisposable(
                    renderName,
                    renderTarget,
                    contentManagerName);
            }

            StructureSprite.SourceFile = renderTarget;

            StructureSprite.TextureHeight = renderTarget.Height;
            StructureSprite.TextureWidth  = renderTarget.Width;
            StructureSprite.TextureLeft   = 0;
            StructureSprite.TextureTop    = 0;

            if (renderTarget.Width > renderTarget.Height)
            {
                StructureSprite.WidthUnits  = DimensionUnitType.Percentage;
                StructureSprite.HeightUnits = DimensionUnitType.PercentageOfOtherDimension;
                StructureSprite.Width       = 100;
                StructureSprite.Height      = baseHeight / baseWidth * 100;
            }
            else
            {
                StructureSprite.HeightUnits = DimensionUnitType.Percentage;
                StructureSprite.WidthUnits  = DimensionUnitType.PercentageOfOtherDimension;
                StructureSprite.Height      = 100;
                StructureSprite.Width       = baseWidth / baseHeight * 100;
            }
        }