void LoadResources()
        {
            // Get background image and store base palette
            ImgFile img = new ImgFile(Path.Combine(DaggerfallUnity.Arena2Path, backgroundFile), FileUsage.UseMemory, true);

            backgroundBitmap = img.GetDFBitmap(0, 0);
            basePalette      = backgroundBitmap.Palette;

            // Get scroll images and set palette
            GfxFile scroll0 = new GfxFile(Path.Combine(DaggerfallUnity.Arena2Path, scroll0File), FileUsage.UseMemory, true);
            GfxFile scroll1 = new GfxFile(Path.Combine(DaggerfallUnity.Arena2Path, scroll1File), FileUsage.UseMemory, true);

            scroll0.Palette = basePalette;
            scroll1.Palette = basePalette;

            // Build color buffers for all scroll frames ahead of time
            scrollFrames = new ScrollFrame[scrollFrameCount];
            for (int frame = 0; frame < scrollFrameCount; frame++)
            {
                if (frame < 8)
                {
                    scrollFrames[frame].colors = scroll0.GetColor32(0, frame);
                }
                else
                {
                    scrollFrames[frame].colors = scroll1.GetColor32(0, frame - 8);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Loads IMG file to texture using a subrect of source image.
        /// Origin of source image (0,0) is bottom-left corner.
        /// </summary>
        public static Texture2D GetTextureFromImg(string name, Rect subRect, TextureFormat format = TextureFormat.ARGB32, bool readOnly = true)
        {
            ImgFile imgFile = new ImgFile(Path.Combine(DaggerfallUnity.Instance.Arena2Path, name), FileUsage.UseMemory, readOnly);

            imgFile.LoadPalette(Path.Combine(DaggerfallUnity.Instance.Arena2Path, imgFile.PaletteName));

            DFBitmap bitmap = imgFile.GetDFBitmap();

            Color32[] colors = imgFile.GetColor32(bitmap, 0);

            // Invert Y as Unity textures have origin 0,0 at bottom-left and UI expects top-left
            subRect.y = bitmap.Height - subRect.height;

            Color32[] newColors = new Color32[(int)subRect.width * (int)subRect.height];
            ImageProcessing.CopyColors(
                ref colors,
                ref newColors,
                new DFSize(bitmap.Width, bitmap.Height),
                new DFSize((int)subRect.width, (int)subRect.height),
                new DFPosition((int)subRect.x, (int)subRect.y),
                new DFPosition(0, 0),
                new DFSize((int)subRect.width, (int)subRect.height));

            Texture2D texture = new Texture2D((int)subRect.width, (int)subRect.height, format, false);

            texture.SetPixels32(newColors, 0);
            texture.Apply(false, true);
            texture.filterMode = DaggerfallUI.Instance.GlobalFilterMode;

            return(texture);
        }
예제 #3
0
        Texture2D GetTextureFromImg(ImgFile img)
        {
            DFBitmap  bitmap  = img.GetDFBitmap();
            Texture2D texture = new Texture2D(bitmap.Width, bitmap.Height, TextureFormat.ARGB32, false);

            texture.SetPixels32(img.GetColor32(bitmap, 0));
            texture.Apply(false, true);

            return(texture);
        }
예제 #4
0
        public static Texture2D GetTextureFromImg(ImgFile img, TextureFormat format = TextureFormat.ARGB32, bool readOnly = true)
        {
            DFBitmap  bitmap  = img.GetDFBitmap();
            Texture2D texture = new Texture2D(bitmap.Width, bitmap.Height, format, false);

            texture.SetPixels32(img.GetColor32(bitmap, 0));
            texture.Apply(false, readOnly);
            texture.filterMode = DaggerfallUI.Instance.GlobalFilterMode;

            return(texture);
        }
예제 #5
0
        public static DFBitmap GetImgBitmap(string name)
        {
            DaggerfallUnity dfUnity = DaggerfallUnity.Instance;

            if (!dfUnity.IsReady)
            {
                return(null);
            }

            ImgFile imgFile = new ImgFile(Path.Combine(dfUnity.Arena2Path, name), FileUsage.UseMemory, true);

            return(imgFile.GetDFBitmap());
        }
예제 #6
0
        private void LoadNightSky()
        {
            const int width  = 512;
            const int height = 219;

            // Get night sky matching sky index
            int nightSky;

            if (SkyIndex >= 0 && SkyIndex <= 7)
            {
                nightSky = 3;
            }
            else if (SkyIndex >= 8 && SkyIndex <= 15)
            {
                nightSky = 1;
            }
            else if (SkyIndex >= 16 && SkyIndex <= 23)
            {
                nightSky = 2;
            }
            else
            {
                nightSky = 0;
            }

            string filename = string.Format("NITE{0:00}I0.IMG", nightSky);

            imgFile = new ImgFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
            imgFile.Palette.Load(Path.Combine(dfUnity.Arena2Path, imgFile.PaletteName));

            // Get sky bitmap
            DFBitmap dfBitmap = imgFile.GetDFBitmap(0, 0);

            // Draw stars
            if (ShowStars)
            {
                for (int i = 0; i < dfBitmap.Data.Length; i++)
                {
                    // Stars should only be drawn over clear sky indices
                    int index = dfBitmap.Data[i];
                    if (index > 16 && index < 32)
                    {
                        if (random.NextDouble() < starChance)
                        {
                            dfBitmap.Data[i] = starColorIndices[random.Next(0, starColorIndices.Length)];
                        }
                    }
                }
            }

            // Get sky colour array
            Color32[] colors = imgFile.GetColor32(dfBitmap);

            // Fix seam on right side of night skies
            for (int y = 0; y < height; y++)
            {
                int pos = y * width + width - 2;
                colors[pos + 1] = colors[pos];
            }

            skyColors.west       = colors;
            skyColors.east       = colors;
            skyColors.clearColor = skyColors.west[0];
        }
예제 #7
0
        protected override void Setup()
        {
            if (IsSetup)
            {
                return;
            }

            // Set background
            backgroundImg    = new ImgFile(Path.Combine(DaggerfallUnity.Arena2Path, nativeImgName), FileUsage.UseMemory, true);
            backgroundBitmap = backgroundImg.GetDFBitmap(0, 0);
            nativeTexture    = new Texture2D(backgroundBitmap.Width, backgroundBitmap.Height, TextureFormat.ARGB32, false);
            if (!nativeTexture)
            {
                throw new Exception("CreateCharClassQuestions: Could not load native texture.");
            }
            nativeTexture.SetPixels32(backgroundImg.GetColor32(backgroundBitmap, 0));
            nativeTexture.Apply(false, true);
            nativeTexture.filterMode      = DaggerfallUI.Instance.GlobalFilterMode;
            NativePanel.BackgroundTexture = nativeTexture;

            // Load both scroll images as one contiguous list of textures
            scrollFile0         = new GfxFile(Path.Combine(DaggerfallUnity.Instance.Arena2Path, scroll0FileName), FileUsage.UseMemory, true);
            scrollFile1         = new GfxFile(Path.Combine(DaggerfallUnity.Instance.Arena2Path, scroll1FileName), FileUsage.UseMemory, true);
            scrollFile0.Palette = backgroundBitmap.Palette;
            scrollFile1.Palette = backgroundBitmap.Palette;
            scrollTextures      = new List <Texture2D>();
            for (int i = 0; i < scrollFile0.frames.Length; i++)
            {
                scrollTextures.Add(TextureReader.CreateFromAPIImage(scrollFile0, 0, i, 0));
                scrollTextures.Last().filterMode = DaggerfallUI.Instance.GlobalFilterMode;
            }
            for (int i = scrollFile0.frames.Length; i < scrollFile0.frames.Length + scrollFile1.frames.Length; i++)
            {
                scrollTextures.Add(TextureReader.CreateFromAPIImage(scrollFile1, 0, i - scrollFile0.frames.Length, 0));
                scrollTextures.Last().filterMode = DaggerfallUI.Instance.GlobalFilterMode;
            }

            // Position scroll image on screen
            questionScroll.Position          = new Vector2(0, 120f);
            questionScroll.Size              = new Vector2(scrollTextures[0].width, scrollTextures[0].height);
            questionScroll.BackgroundTexture = scrollTextures[0];
            questionScroll.Parent            = NativePanel;
            textArea.Position = new Vector2(leftTextOffset, 120f + topTextOffset);
            textArea.Size     = new Vector2(scrollTextures[0].width, scrollTextures[0].height - topTextOffset * 2f);
            textArea.Parent   = NativePanel;
            NativePanel.Components.Add(textArea);
            NativePanel.Components.Add(questionScroll);

            // Setup question label
            questionIndices = GetQuestions();
            DisplayQuestion(questionIndices[questionsAnswered]);

            // Handle scrolling
            NativePanel.OnMouseScrollDown += NativePanel_OnMouseScrollDown;
            NativePanel.OnMouseScrollUp   += NativePanel_OnMouseScrollUp;
            questionScroll.OnMouseDown    += QuestionScroll_OnMouseDown;
            questionScroll.OnMouseUp      += QuestionScroll_OnMouseUp;

            // Setup animations
            rogueAnim.SetTransparentColor(0, 0, 10);
            rogueAnim.TransparencyEnabled = true;
            rogueAnim.Load("ROGUE.CEL");
            rogueAnim.Size            = new Vector2(rogueAnim.FLCFile.Header.Width, rogueAnim.FLCFile.Header.Height);
            rogueAnim.Position        = new Vector2(1f, 1f);
            rogueAnim.BackgroundColor = Color.clear;
            rogueAnim.OnAnimEnd      += CEL_OnAnimEnd;
            mageAnim.SetTransparentColor(0, 0, 10);
            mageAnim.TransparencyEnabled = true;
            mageAnim.Load("MAGE.CEL");
            mageAnim.Size            = new Vector2(mageAnim.FLCFile.Header.Width, mageAnim.FLCFile.Header.Height);
            mageAnim.Position        = new Vector2(79f, 1f);
            mageAnim.BackgroundColor = Color.clear;
            mageAnim.OnAnimEnd      += CEL_OnAnimEnd;
            warriorAnim.SetTransparentColor(0, 0, 10);
            warriorAnim.TransparencyEnabled = true;
            warriorAnim.Load("WARRIOR.CEL");
            warriorAnim.Size            = new Vector2(warriorAnim.FLCFile.Header.Width, warriorAnim.FLCFile.Header.Height);
            warriorAnim.Position        = new Vector2(110f, 1f);
            warriorAnim.BackgroundColor = Color.clear;
            warriorAnim.OnAnimEnd      += CEL_OnAnimEnd;
            rogueAnim.Loop = mageAnim.Loop = warriorAnim.Loop = false;
            NativePanel.Components.Add(rogueAnim);
            NativePanel.Components.Add(mageAnim);
            NativePanel.Components.Add(warriorAnim);

            IsSetup = true;
        }
        /// <summary>
        /// Reads any Daggerfall image file to ImageData package.
        /// </summary>
        /// <param name="filename">Name of standalone file as it appears in arena2 folder.</param>
        /// <param name="record">Which image record to read for multi-image files.</param>
        /// <param name="frame">Which frame to read for multi-frame images.</param>
        /// <param name="hasAlpha">Enable this for image cutouts.</param>
        /// <param name="createTexture">Create a Texture2D.</param>
        /// <param name="createAllFrameTextures">Creates a Texture2D for every frame in a TEXTURE file (if greater than 1 frames).</param>
        /// <param name="alphaIndex">Set palette index for alpha checks (default is 0).</param>
        /// <returns>ImageData. If result.type == ImageTypes.None then read failed.</returns>
        public static ImageData GetImageData(string filename, int record = 0, int frame = 0, bool hasAlpha = false, bool createTexture = true, bool createAllFrameTextures = false, int alphaIndex = 0)
        {
            // Check API ready
            DaggerfallUnity dfUnity = DaggerfallUnity.Instance;

            if (!dfUnity.IsReady)
            {
                return(new ImageData());
            }

            // Parse image file type
            ImageTypes fileType;

            try
            {
                fileType = ParseFileType(filename);
            }
            catch
            {
                return(new ImageData());
            }

            // Create base image data
            ImageData imageData = new ImageData();

            imageData.type       = fileType;
            imageData.filename   = filename;
            imageData.record     = record;
            imageData.frame      = frame;
            imageData.hasAlpha   = hasAlpha;
            imageData.alphaIndex = alphaIndex;

            // Read supported image files
            DFBitmap dfBitmap = null;

            DFBitmap[] dfBitmapAllFrames = null;
            switch (fileType)
            {
            case ImageTypes.TEXTURE:
                TextureFile textureFile = new TextureFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
                textureFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, textureFile.PaletteName));
                dfBitmap = textureFile.GetDFBitmap(record, frame);
                int frameCount = textureFile.GetFrameCount(record);
                if (createAllFrameTextures && frameCount > 1)
                {
                    dfBitmapAllFrames = new DFBitmap[frameCount];
                    for (int i = 0; i < frameCount; i++)
                    {
                        dfBitmapAllFrames[i] = textureFile.GetDFBitmap(record, i);
                    }
                }
                imageData.offset = textureFile.GetOffset(record);
                imageData.scale  = textureFile.GetScale(record);
                imageData.size   = textureFile.GetSize(record);

                // Texture pack support
                int archive = AssetInjection.TextureReplacement.FileNameToArchive(filename);
                if (createTexture && AssetInjection.TextureReplacement.TryImportTexture(archive, record, frame, out imageData.texture))
                {
                    createTexture = false;
                }
                if (createAllFrameTextures && frameCount > 1 && AssetInjection.TextureReplacement.TryImportTexture(archive, record, out imageData.animatedTextures))
                {
                    createAllFrameTextures = false;
                }

                break;

            case ImageTypes.IMG:
                ImgFile imgFile = new ImgFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
                imgFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, imgFile.PaletteName));
                dfBitmap         = imgFile.GetDFBitmap();
                imageData.offset = imgFile.ImageOffset;
                imageData.scale  = new DFSize();
                imageData.size   = imgFile.GetSize(0);

                // Texture pack support
                if (createTexture && AssetInjection.TextureReplacement.TryImportImage(filename, false, out imageData.texture))
                {
                    createTexture = false;
                }

                break;

            case ImageTypes.CIF:
            case ImageTypes.RCI:
                CifRciFile cifFile = new CifRciFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
                cifFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, cifFile.PaletteName));
                dfBitmap         = cifFile.GetDFBitmap(record, frame);
                imageData.offset = cifFile.GetOffset(record);
                imageData.scale  = new DFSize();
                imageData.size   = cifFile.GetSize(record);

                // Texture pack support
                if (createTexture && AssetInjection.TextureReplacement.TryImportCifRci(filename, record, frame, false, out imageData.texture))
                {
                    createTexture = false;
                }

                break;

            case ImageTypes.CFA:
                CfaFile cfaFile = new CfaFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
                cfaFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, cfaFile.PaletteName));

                dfBitmap         = cfaFile.GetDFBitmap(record, frame);
                imageData.offset = new DFPosition(0, 0);
                imageData.scale  = new DFSize();
                imageData.size   = cfaFile.GetSize(record);

                // Texture pack support
                if (createTexture && AssetInjection.TextureReplacement.TryImportCifRci(filename, record, frame, false, out imageData.texture))
                {
                    createTexture = false;
                }

                break;

            case ImageTypes.BSS:
                BssFile bssFile = new BssFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
                bssFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, bssFile.PaletteName));

                dfBitmap         = bssFile.GetDFBitmap(record, frame);
                imageData.offset = new DFPosition(0, 0);
                imageData.scale  = new DFSize();
                imageData.size   = bssFile.GetSize(record);

                // Texture pack support
                if (createTexture && AssetInjection.TextureReplacement.TryImportCifRci(filename, record, frame, false, out imageData.texture))
                {
                    createTexture = false;
                }

                break;

            case ImageTypes.GFX:
                GfxFile gfxFile = new GfxFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
                gfxFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, gfxFile.PaletteName));

                dfBitmap         = gfxFile.GetDFBitmap(record, frame);
                imageData.offset = new DFPosition(0, 0);
                imageData.scale  = new DFSize();
                imageData.size   = gfxFile.GetSize(record);

                // Texture pack support
                if (createTexture && AssetInjection.TextureReplacement.TryImportCifRci(filename, record, frame, false, out imageData.texture))
                {
                    createTexture = false;
                }

                break;

            default:
                return(new ImageData());
            }

            // Store bitmap
            imageData.dfBitmap = dfBitmap;
            imageData.width    = dfBitmap.Width;
            imageData.height   = dfBitmap.Height;

            // Create Texture2D
            if (createTexture)
            {
                // Get colors array
                Color32[] colors = GetColors(imageData);
                if (colors == null)
                {
                    return(new ImageData());
                }

                // Create new Texture2D
                imageData.texture = GetTexture(colors, imageData.width, imageData.height);
            }

            // Create animated Texture2D frames
            if (createAllFrameTextures && dfBitmapAllFrames != null)
            {
                imageData.animatedTextures = new Texture2D[dfBitmapAllFrames.Length];
                for (int i = 0; i < dfBitmapAllFrames.Length; i++)
                {
                    ImageData curFrame = imageData;
                    curFrame.dfBitmap = dfBitmapAllFrames[i];
                    Color32[] colors = GetColors(curFrame);
                    imageData.animatedTextures[i] = GetTexture(colors, imageData.width, imageData.height);
                }
            }

            return(imageData);
        }
예제 #9
0
        /// <summary>
        /// Reads any Daggerfall image file to ImageData package.
        /// </summary>
        /// <param name="filename">Name of standalone file as it appears in arena2 folder.</param>
        /// <param name="record">Which image record to read for multi-image files.</param>
        /// <param name="frame">Which frame to read for multi-frame images.</param>
        /// <param name="hasAlpha">Enable this for image cutouts.</param>
        /// <param name="createTexture">Create a Texture2D.</param>
        /// <returns>ImageData. If result.type == ImageTypes.None then read failed.</returns>
        public static ImageData GetImageData(string filename, int record = 0, int frame = 0, bool hasAlpha = false, bool createTexture = true)
        {
            // Check API ready
            DaggerfallUnity dfUnity = DaggerfallUnity.Instance;

            if (!dfUnity.IsReady)
            {
                return(new ImageData());
            }

            // Parse image file type
            ImageTypes fileType;

            try
            {
                fileType = ParseFileType(filename);
            }
            catch
            {
                return(new ImageData());
            }

            // Create base image data
            ImageData imageData = new ImageData();

            imageData.type     = fileType;
            imageData.filename = filename;
            imageData.record   = record;
            imageData.frame    = frame;
            imageData.hasAlpha = hasAlpha;

            // Read supported image files
            DFBitmap dfBitmap = null;

            switch (fileType)
            {
            case ImageTypes.TEXTURE:
                TextureFile textureFile = new TextureFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
                textureFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, textureFile.PaletteName));
                dfBitmap         = textureFile.GetDFBitmap(record, frame);
                imageData.offset = textureFile.GetOffset(record);
                imageData.scale  = textureFile.GetScale(record);
                imageData.size   = textureFile.GetSize(record);
                break;

            case ImageTypes.IMG:
                ImgFile imgFile = new ImgFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
                imgFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, imgFile.PaletteName));
                dfBitmap         = imgFile.GetDFBitmap();
                imageData.offset = imgFile.ImageOffset;
                imageData.scale  = new DFSize();
                imageData.size   = imgFile.GetSize(0);
                break;

            case ImageTypes.CIF:
            case ImageTypes.RCI:
                CifRciFile cifFile = new CifRciFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true);
                cifFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, cifFile.PaletteName));
                dfBitmap         = cifFile.GetDFBitmap(record, frame);
                imageData.offset = cifFile.GetOffset(record);
                imageData.scale  = new DFSize();
                imageData.size   = cifFile.GetSize(record);
                break;

            default:
                return(new ImageData());
            }

            // Store bitmap
            imageData.dfBitmap = dfBitmap;
            imageData.width    = dfBitmap.Width;
            imageData.height   = dfBitmap.Height;

            // Create Texture2D
            if (createTexture)
            {
                // Get colors array
                Color32[] colors = GetColors(imageData);
                if (colors == null)
                {
                    return(new ImageData());
                }

                // Create new Texture2D
                imageData.texture = GetTexture(colors, imageData.width, imageData.height);
            }

            return(imageData);
        }