/// <summary>
        ///  <para>Loads Image from given Zip-File and Entry.</para>
        /// </summary>
        private Image LoadImageFrom(ZipFile zipFile, ZipEntry imageEntry)
        {
            string ResourceName = Path.GetFileNameWithoutExtension(imageEntry.Name).ToLowerInvariant();

            if (ImageCache.Images.Contains(ResourceName))
            {
                return(null);
            }

            var byteBuffer = new byte[zipBufferSize];

            Stream zipStream = zipFile.GetInputStream(imageEntry);
            //Will throw exception is missing or wrong password. Handle this.

            var memStream = new MemoryStream();

            StreamUtils.Copy(zipStream, memStream, byteBuffer);
            memStream.Position = 0;

            Image loadedImg = Image.FromStream(ResourceName, memStream, (int)memStream.Length);

            memStream.Close();
            zipStream.Close();
            memStream.Dispose();
            zipStream.Dispose();

            return(loadedImg);
        }
示例#2
0
 public void ResolveShadows(Image shadowCastersTexture, RenderImage result, Vector2D lightPosition,
                            bool attenuateShadows, Image mask, Vector4D maskProps, Vector4D diffuseColor)
 {
     resolveShadowsEffect.Parameters["AttenuateShadows"].SetValue(attenuateShadows ? 0 : 1);
     resolveShadowsEffect.Parameters["MaskProps"].SetValue(maskProps);
     resolveShadowsEffect.Parameters["DiffuseColor"].SetValue(diffuseColor);
     //Gorgon.CurrentRenderTarget.BlendingMode = BlendingModes.None;
     ExecuteTechnique(shadowCastersTexture, distancesRT, "ComputeDistances");
     ExecuteTechnique(distancesRT.Image, distortRT, "Distort");
     ApplyHorizontalReduction(distortRT, shadowMap);
     ExecuteTechnique(mask, result, "DrawShadows", shadowMap);
     //ExecuteTechnique(shadowsRT.Image, processedShadowsRT, "BlurHorizontally");
     //ExecuteTechnique(processedShadowsRT.Image, result, "BlurVerticallyAndAttenuate");
     Gorgon.CurrentShader = null;
 }
示例#3
0
 public void ResolveShadows(Image shadowCastersTexture, RenderImage result, Vector2D lightPosition,
                            bool attenuateShadows, Image mask, Vector4D maskProps, Vector4D diffuseColor)
 {
     resolveShadowsEffect.Parameters["AttenuateShadows"].SetValue(attenuateShadows ? 0 : 1);
     resolveShadowsEffect.Parameters["MaskProps"].SetValue(maskProps);
     resolveShadowsEffect.Parameters["DiffuseColor"].SetValue(diffuseColor);
     //Gorgon.CurrentRenderTarget.BlendingMode = BlendingModes.None;
     ExecuteTechnique(shadowCastersTexture, distancesRT, "ComputeDistances");
     ExecuteTechnique(distancesRT.Image, distortRT, "Distort");
     ApplyHorizontalReduction(distortRT, shadowMap);
     ExecuteTechnique(mask, result, "DrawShadows", shadowMap);
     //ExecuteTechnique(shadowsRT.Image, processedShadowsRT, "BlurHorizontally");
     //ExecuteTechnique(processedShadowsRT.Image, result, "BlurVerticallyAndAttenuate");
     Gorgon.CurrentShader = null;
 }
        /// <summary>
        ///  <para>Loads the embedded base files.</para>
        /// </summary>
        public void LoadBaseResources()
        {
            Assembly _assembly = Assembly.GetExecutingAssembly();;
            Stream   _stream;

            _stream = _assembly.GetManifestResourceStream("SS14.Client.Services._EmbeddedBaseResources.bluehigh.ttf");
            if (_stream != null)
            {
                _fonts.Add("base_font", Font.FromStream("base_font", _stream, (int)_stream.Length, 10));
            }
            _stream = null;

            _stream = _assembly.GetManifestResourceStream("SS14.Client.Services._EmbeddedBaseResources.noSprite.png");
            if (_stream != null)
            {
                Image nospriteimage = Image.FromStream("nospriteimage", _stream, (int)_stream.Length);
                _images.Add("nosprite", nospriteimage);
                _sprites.Add("nosprite", new Sprite("nosprite", nospriteimage));
            }
            _stream = null;
        }
示例#5
0
        private void ExecuteTechnique(Image source, RenderImage destination, string techniqueName, RenderImage shadowMap)
        {
            Vector2D renderTargetSize;

            renderTargetSize           = new Vector2D(baseSize, baseSize);
            Gorgon.CurrentRenderTarget = destination;
            Gorgon.CurrentRenderTarget.Clear(Color.White);

            Gorgon.CurrentShader = resolveShadowsEffect.Techniques[techniqueName];
            resolveShadowsEffect.Parameters["renderTargetSize"].SetValue(renderTargetSize);
            if (source != null)
            {
                resolveShadowsEffect.Parameters["InputTexture"].SetValue(source);
            }
            if (shadowMap != null)
            {
                resolveShadowsEffect.Parameters["ShadowMapTexture"].SetValue(shadowMap);
            }

            quadRender.Render(new Vector2D(1, 1) * -1, new Vector2D(1, 1));

            Gorgon.CurrentRenderTarget = null;
        }
示例#6
0
        public bool LoadSprite(XElement spriteValues, string graphicDirectory, out string reason)
        {
            string filename = null;

            Name = null;
            foreach (var attribute in spriteValues.Attributes())
            {
                //Get the graphic file
                switch (attribute.Name.ToString().ToLower())
                {
                case "file":
                {
                    filename = attribute.Value;
                    break;
                }

                case "name":
                {
                    Name = attribute.Value;
                    break;
                }

                case "stretchmode":
                {
                    if (attribute.Value == "repeat")
                    {
                        stretchMode = StretchMode.REPEAT;
                    }
                    break;
                }
                }
            }
            if (string.IsNullOrEmpty(Name) || string.IsNullOrEmpty(filename))
            {
                reason = "No sprite name or file location.";
                return(false);
            }
            Sprite graphicFile;

            if (ImageCache.Images.Contains(Name))
            {
                graphicFile = new Sprite(Name, ImageCache.Images[Name]);
            }
            else
            {
                string path = Path.Combine(graphicDirectory, filename);
                if (!File.Exists(path))
                {
                    reason = "Graphic File doesn't exist: " + path;
                    return(false);
                }
                graphicFile = new Sprite(Name, Image.FromFile(Path.Combine(graphicDirectory, filename)));
            }

            int frameCount = 0;

            foreach (var element in spriteValues.Elements())
            {
                int    x           = 0;
                int    y           = 0;
                int    width       = 0;
                int    height      = 0;
                int    axisX       = 0;
                int    axisY       = 0;
                string frameLength = null;
                //Process each frame
                foreach (var attribute in element.Attributes())
                {
                    switch (attribute.Name.ToString().ToLower())
                    {
                    case "x":
                    {
                        x = int.Parse(attribute.Value);
                        break;
                    }

                    case "y":
                    {
                        y = int.Parse(attribute.Value);
                        break;
                    }

                    case "width":
                    {
                        width = int.Parse(attribute.Value);
                        break;
                    }

                    case "height":
                    {
                        height = int.Parse(attribute.Value);
                        break;
                    }

                    case "framelength":
                    {
                        frameLength = attribute.Value;
                        break;
                    }

                    case "axisx":
                    {
                        axisX = int.Parse(attribute.Value);
                        break;
                    }

                    case "axisy":
                    {
                        axisY = int.Parse(attribute.Value);
                        break;
                    }
                    }
                }
                var frame = new Sprite(Name + frameCount, graphicFile.Image, x, y, width, height, axisX,
                                       axisY);
                if (stretchMode == StretchMode.REPEAT)
                {
                    frame.WrapMode = ImageAddressing.Wrapping;
                }
                Frames.Add(frame);
                FrameLength.Add(frameLength);
                frameCount++;
            }
            reason = null;
            return(true);
        }
示例#7
0
        private void ExecuteTechnique(Image source, RenderImage destination, string techniqueName, RenderImage shadowMap)
        {
            Vector2D renderTargetSize;
            renderTargetSize = new Vector2D(baseSize, baseSize);
            Gorgon.CurrentRenderTarget = destination;
            Gorgon.CurrentRenderTarget.Clear(Color.White);

            Gorgon.CurrentShader = resolveShadowsEffect.Techniques[techniqueName];
            resolveShadowsEffect.Parameters["renderTargetSize"].SetValue(renderTargetSize);
            if (source != null)
                resolveShadowsEffect.Parameters["InputTexture"].SetValue(source);
            if (shadowMap != null)
                resolveShadowsEffect.Parameters["ShadowMapTexture"].SetValue(shadowMap);

            quadRender.Render(new Vector2D(1, 1)*-1, new Vector2D(1, 1));

            Gorgon.CurrentRenderTarget = null;
        }
示例#8
0
 private void ExecuteTechnique(Image source, RenderImage destination, string techniqueName)
 {
     ExecuteTechnique(source, destination, techniqueName, null);
 }
        /// <summary>
        ///  <para>Loads all Resources from given Zip into the respective Resource Lists and Caches</para>
        /// </summary>
        public void LoadResourceZip(string path = null, string pw = null)
        {
            string zipPath  = path ?? _configurationManager.GetResourcePath();
            string password = pw ?? _configurationManager.GetResourcePassword();


            if (!File.Exists(zipPath))
            {
                throw new FileNotFoundException("Specified Zip does not exist: " + zipPath);
            }

            FileStream zipFileStream = File.OpenRead(zipPath);
            var        zipFile       = new ZipFile(zipFileStream);

            if (!string.IsNullOrWhiteSpace(password))
            {
                zipFile.Password = password;
            }

            var directories = from ZipEntry a in zipFile
                              where a.IsDirectory
                              orderby a.Name.ToLowerInvariant() == "textures" descending
                              select a;

            Dictionary <string, List <ZipEntry> > sorted = new Dictionary <string, List <ZipEntry> >();

            foreach (ZipEntry dir in directories)
            {
                if (sorted.ContainsKey(dir.Name.ToLowerInvariant()))
                {
                    continue;                                                  //Duplicate folder? shouldnt happen.
                }
                List <ZipEntry> folderContents = (from ZipEntry entry in zipFile
                                                  where entry.Name.ToLowerInvariant().Contains(dir.Name.ToLowerInvariant())
                                                  where entry.IsFile
                                                  select entry).ToList();

                sorted.Add(dir.Name.ToLowerInvariant(), folderContents);
            }

            sorted = sorted.OrderByDescending(x => x.Key == "textures/").ToDictionary(x => x.Key, x => x.Value); //Textures first.

            foreach (KeyValuePair <string, List <ZipEntry> > current in sorted)
            {
                switch (current.Key)
                {
                case ("textures/"):
                    foreach (ZipEntry texture in current.Value)
                    {
                        if (supportedImageExtensions.Contains(Path.GetExtension(texture.Name).ToLowerInvariant()))
                        {
                            Image loadedImg = LoadImageFrom(zipFile, texture);
                            if (loadedImg == null)
                            {
                                continue;
                            }
                            else
                            {
                                _images.Add(loadedImg.Name, loadedImg);
                            }
                        }
                    }
                    break;

                case ("tai/"):
                    foreach (ZipEntry tai in current.Value)
                    {
                        if (Path.GetExtension(tai.Name).ToLowerInvariant() == ".tai")
                        {
                            IEnumerable <Sprite> loadedSprites = LoadSpritesFrom(zipFile, tai);
                            foreach (Sprite currentSprite in loadedSprites.Where(currentSprite => !_sprites.ContainsKey(currentSprite.Name)))
                            {
                                _sprites.Add(currentSprite.Name, currentSprite);
                            }
                        }
                    }
                    break;

                case ("fonts/"):
                    foreach (ZipEntry font in current.Value)
                    {
                        if (Path.GetExtension(font.Name).ToLowerInvariant() == ".ttf")
                        {
                            Font loadedFont = LoadFontFrom(zipFile, font);
                            if (loadedFont == null)
                            {
                                continue;
                            }
                            else
                            {
                                _fonts.Add(loadedFont.Name, loadedFont);
                            }
                        }
                    }
                    break;

                case ("particlesystems/"):
                    foreach (ZipEntry particles in current.Value)
                    {
                        if (Path.GetExtension(particles.Name).ToLowerInvariant() == ".xml")
                        {
                            ParticleSettings particleSettings = LoadParticlesFrom(zipFile, particles);
                            if (particleSettings == null)
                            {
                                continue;
                            }
                            else
                            {
                                _particles.Add(Path.GetFileNameWithoutExtension(particles.Name), particleSettings);
                            }
                        }
                    }
                    break;

                case ("shaders/"):
                    foreach (ZipEntry shader in current.Value)
                    {
                        if (Path.GetExtension(shader.Name).ToLowerInvariant() == ".fx")
                        {
                            FXShader loadedShader = LoadShaderFrom(zipFile, shader);
                            if (loadedShader == null)
                            {
                                continue;
                            }
                            else
                            {
                                _shaders.Add(loadedShader.Name, loadedShader);
                            }
                        }
                    }
                    break;

                case ("animations/"):
                    foreach (ZipEntry animation in current.Value)
                    {
                        if (Path.GetExtension(animation.Name).ToLowerInvariant() == ".xml")
                        {
                            AnimationCollection animationCollection = LoadAnimationCollectionFrom(zipFile, animation);
                            if (animationCollection == null)
                            {
                                continue;
                            }
                            else
                            {
                                _animationCollections.Add(animationCollection.Name, animationCollection);
                            }
                        }
                    }
                    break;
                }
            }

            sorted = null;

            zipFile.Close();
            zipFileStream.Close();
            zipFileStream.Dispose();

            GC.Collect();
        }
        /// <summary>
        ///  <para>Loads TAI from given Zip-File and Entry and creates & loads Sprites from it.</para>
        /// </summary>
        private IEnumerable <Sprite> LoadSpritesFrom(ZipFile zipFile, ZipEntry taiEntry)
        {
            string ResourceName = Path.GetFileNameWithoutExtension(taiEntry.Name).ToLowerInvariant();

            var loadedSprites = new List <Sprite>();

            var byteBuffer = new byte[zipBufferSize];

            Stream zipStream = zipFile.GetInputStream(taiEntry);
            //Will throw exception is missing or wrong password. Handle this.

            var memStream = new MemoryStream();

            StreamUtils.Copy(zipStream, memStream, byteBuffer);
            memStream.Position = 0;

            var    taiReader = new StreamReader(memStream, true);
            string loadedTAI = taiReader.ReadToEnd();

            memStream.Close();
            zipStream.Close();
            taiReader.Close();
            memStream.Dispose();
            zipStream.Dispose();
            taiReader.Dispose();

            string[] splitContents = Regex.Split(loadedTAI, "\r\n"); //Split by newlines.

            foreach (string line in splitContents)
            {
                if (String.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                string[] splitLine = line.Split(',');
                string[] fullPath  = Regex.Split(splitLine[0], "\t");

                string originalName = Path.GetFileNameWithoutExtension(fullPath[0]).ToLowerInvariant();
                //The name of the original picture without extension, before it became part of the atlas.
                //This will be the name we can find this under in our Resource lists.

                string[] splitResourceName = fullPath[2].Split('.');

                string imageName = splitResourceName[0].ToLowerInvariant();

                if (!ImageCache.Images.Contains(splitResourceName[0]))
                {
                    continue; //Image for this sprite does not exist. Possibly set to defered later.
                }
                Image atlasTex = ImageCache.Images[splitResourceName[0]];
                //Grab the image for the sprite from the cache.

                var info = new SpriteInfo();
                info.Name = originalName;

                float offsetX = 0;
                float offsetY = 0;
                float sizeX   = 0;
                float sizeY   = 0;

                if (splitLine.Length > 8) //Separated with ','. This causes some problems and happens on some EU PCs.
                {
                    offsetX = float.Parse(splitLine[3] + "." + splitLine[4], CultureInfo.InvariantCulture);
                    offsetY = float.Parse(splitLine[5] + "." + splitLine[6], CultureInfo.InvariantCulture);
                    sizeX   = float.Parse(splitLine[8] + "." + splitLine[9], CultureInfo.InvariantCulture);
                    sizeY   = float.Parse(splitLine[10] + "." + splitLine[11], CultureInfo.InvariantCulture);
                }
                else
                {
                    offsetX = float.Parse(splitLine[3], CultureInfo.InvariantCulture);
                    offsetY = float.Parse(splitLine[4], CultureInfo.InvariantCulture);
                    sizeX   = float.Parse(splitLine[6], CultureInfo.InvariantCulture);
                    sizeY   = float.Parse(splitLine[7], CultureInfo.InvariantCulture);
                }

                info.Offsets = new Vector2D((float)Math.Round(offsetX * atlasTex.Width, 1),
                                            (float)Math.Round(offsetY * atlasTex.Height, 1));
                info.Size = new Vector2D((float)Math.Round(sizeX * atlasTex.Width, 1),
                                         (float)Math.Round(sizeY * atlasTex.Height, 1));

                if (!_spriteInfos.ContainsKey(originalName))
                {
                    _spriteInfos.Add(originalName, info);
                }

                loadedSprites.Add(new Sprite(originalName, atlasTex, info.Offsets, info.Size));
            }

            return(loadedSprites);
        }
示例#11
0
 private void ExecuteTechnique(Image source, RenderImage destination, string techniqueName)
 {
     ExecuteTechnique(source, destination, techniqueName, null);
 }