Example #1
0
        /// <summary>
        /// Reads backgrounds from GM file.
        /// </summary>
        private GMList<GMBackground> ReadBackgrounds()
        {
            // Get version.
            int version = ReadInt();

            // Check version.
            if (version != 400 && version != 800)
                throw new Exception("Unsupported Pre-Background object version.");

            // Create a new list of backgrounds.
            GMList<GMBackground> backgrounds = new GMList<GMBackground>();

            // Amount of background ids.
            int num = ReadInt();

            // Iterate through backgrounds.
            for (int i = 0; i < num; i++)
            {
                // If version is 8.0, start inflate.
                if (version == 800)
                    Decompress();

                // If the background at index does not exists, continue.
                if (ReadBool() == false)
                {
                    backgrounds.LastId++;
                    EndDecompress();
                    continue;
                }

                // Create a new background object.
                GMBackground background = new GMBackground();

                // Set background id
                background.Id = i;

                // Get background data.
                background.Name = ReadString();

                // If version is 8.0, get last changed.
                if (version == 800)
                    background.LastChanged = ReadDouble();

                // Get version.
                version = ReadInt();

                // Check version.
                if (version != 400 && version != 543 && version != 710)
                    throw new Exception("Unsupported Background object version.");

                // If version is less than 7.1.
                if (version < 710)
                {
                    // Background data
                    background.Width = ReadInt();
                    background.Height = ReadInt();
                    background.Transparent = ReadBool();

                    // Check version.
                    if (version > 400)
                    {
                        // Read background data.
                        background.SmoothEdges = ReadBool();
                        background.Preload = ReadBool();
                        background.UseAsTileSet = ReadBool();
                        background.TileWidth = ReadInt();
                        background.TileHeight = ReadInt();
                        background.HorizontalOffset = ReadInt();
                        background.VerticalOffset = ReadInt();
                        background.HorizontalSeperation = ReadInt();
                        background.VerticalSeperation = ReadInt();
                    }
                    else
                    {
                        // Read background data.
                        background.UseVideoMemory = ReadBool();
                        background.LoadOnlyOnUse = ReadBool();
                    }

                    // If image data exists.
                    if (ReadBool())
                    {
                        // If pixel data does not exist.
                        if (ReadInt() == -1)
                            continue;

                        // Create a new image.
                        GMImage image = new GMImage();

                        // Get size of image data.
                        int size = ReadInt();

                        // Get compressed image data.
                        image.Data = ReadBytes(size);

                        // Set background image.
                        background.Image = image;
                    }
                }
                else
                {
                    // Get background data.
                    background.UseAsTileSet = ReadBool();
                    background.TileWidth = ReadInt();
                    background.TileHeight = ReadInt();
                    background.HorizontalOffset = ReadInt();
                    background.VerticalOffset = ReadInt();
                    background.HorizontalSeperation = ReadInt();
                    background.VerticalSeperation = ReadInt();

                    // Get version.
                    version = ReadInt();

                    // Check version.
                    if (version != 800)
                        throw new Exception("Unsupported Background object version.");

                    // Get image data.
                    background.Width = ReadInt();
                    background.Height = ReadInt();

                    // If the sprite size is not zero.
                    if (background.Width != 0 && background.Height != 0)
                    {
                        // Create a new image object.
                        GMImage image = new GMImage();
                        image.Compressed = false;

                        // Set image data.
                        image.Width = background.Width;
                        image.Height = background.Height;

                        // Get size of image data.
                        int size = ReadInt();

                        // Get image data.
                        image.Data = ReadBytes(size);

                        // Insert compressed image data.
                        background.Image = image;
                    }
                }

                // End object inflate.
                EndDecompress();

                // Add background.
                backgrounds.Add(background);
            }

            // Return backgrounds.
            return backgrounds;
        }