Ejemplo n.º 1
0
        public static void Init()
        {
            //run conversions
            if (!Directory.Exists(Paths.CachedGFXPath))
            {
                Directory.CreateDirectory(Paths.CachedGFXPath);
            }

            Conversion.CompileAllSprites(Paths.SpritesPath, Paths.CachedGFXPath + "Sprite");
            Conversion.CompileAllTiles(Paths.TilesPath, Paths.CachedGFXPath + "Tile");
            Conversion.CompileAllPortraits(Paths.PortraitsPath, Paths.CachedGFXPath + "Portrait");

            Game.UpdateLoadMsg("Loading Base Textures");
            //load error texture
            ErrorTexture = new AnimSheet();
            ErrorTexture.LoadPixelsFromFile32(Paths.BaseGFXPath + "Error.png");

            //load blank texture
            BlankTexture = new Texture();
            BlankTexture.LoadPixelsFromFile32(Paths.BaseGFXPath + "UI\\Blank.png");

            //load menu data
            MenuBack = new TileSheet();
            MenuBack.LoadPixelsFromFile32(Paths.BaseGFXPath + "UI\\Back.png");

            Picker = new TileSheet();
            Picker.LoadPixelsFromFile32(Paths.BaseGFXPath + "UI\\Picker.png");


            Game.UpdateLoadMsg("Loading Caches");
            //initialize caches
            spriteCache = new MultiNameLRUCache <string, SpriteSheet>(SPRITE_CACHE_SIZE);
            spriteCache.OnItemRemoved = DisposeCachedObject;
            mugshotCache = new MultiNameLRUCache <string, TileSheet>(MUGSHOT_CACHE_SIZE);
            mugshotCache.OnItemRemoved = DisposeCachedObject;
            spellCache = new LRUCache <string, AnimSheet>(SPELL_CACHE_SIZE);
            spellCache.OnItemRemoved = DisposeCachedObject;
            statusCache = new LRUCache <int, AnimSheet>(STATUS_CACHE_SIZE);
            statusCache.OnItemRemoved = DisposeCachedObject;
            itemCache = new LRUCache <int, AnimSheet>(ITEM_CACHE_SIZE);
            itemCache.OnItemRemoved   = DisposeCachedObject;
            objectCache               = new LRUCache <int, AnimSheet>(OBJECT_CACHE_SIZE);
            objectCache.OnItemRemoved = DisposeCachedObject;
            tileCache = new LRUCache <string, Texture>(TILE_CACHE_SIZE);
            tileCache.OnItemRemoved = DisposeCachedObject;

            //load metadata
            tileData = new TileMetadata[TOTAL_TILE_SHEETS];
        }
Ejemplo n.º 2
0
        private static TileSheet BuildPortraitSurface(List <Bitmap> frameCollection, string spriteRootDirectory)
        {
            int frameHeight = 0;
            int frameWidth  = 0;

            TileSheet animSheet = null;

            int totalSurfaceWidth = 0;
            int lastX             = 0;
            int frameNum          = 0;

            foreach (Bitmap frame in frameCollection)
            {
                if (frameHeight == 0)
                {
                    frameHeight = frame.Height;
                }
                else if (frameHeight != frame.Height)
                {
                    throw new Exception("Frameheight mismatch: " + frameNum + " (" + frameHeight + " vs. " + frame.Height + ")");
                }
                if (frameWidth == 0)
                {
                    frameWidth = frame.Width;
                }
                else if (frameWidth != frame.Width)
                {
                    throw new Exception("Framewidth mismatch: " + frameNum + " (" + frameWidth + " vs. " + frame.Width + ")");
                }
                totalSurfaceWidth += frame.Width;
                frameNum++;
            }

            if (totalSurfaceWidth > 0)
            {
                animSheet = new TileSheet();
                animSheet.CreatePixels32(totalSurfaceWidth, frameHeight);
            }
            foreach (Bitmap frame in frameCollection)
            {
                BitmapData frameData = frame.LockBits(new Rectangle(0, 0, frame.Width, frame.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
                animSheet.Blit(frameData, 0, 0, frame.Width, frame.Height, lastX, 0);
                frame.UnlockBits(frameData);
                lastX += frame.Width;
                frame.Dispose();
            }

            return(animSheet);
        }
Ejemplo n.º 3
0
 public void AddSheet(FrameType type, PMDToolkit.Maps.Direction8 dir, TileSheet surface)
 {
     if (!animations.ContainsKey(type))
     {
         animations.Add(type, new Dictionary <PMDToolkit.Maps.Direction8, TileSheet>());
     }
     if (animations[type].ContainsKey(dir) == false)
     {
         animations[type].Add(dir, surface);
     }
     else
     {
         animations[type][dir] = surface;
     }
 }
Ejemplo n.º 4
0
        private static void CompileSpriteToFile(string spriteDir, Dictionary <string, byte[]> spriteData, int num, int form, int shiny, int gender)
        {
            //check to see if files exist
            string[] pngs = Directory.GetFiles(spriteDir, "*.png", SearchOption.TopDirectoryOnly);
            if (pngs.Length < 1)
            {
                return;
            }

            string outForm = "r";

            if (form >= 0)
            {
                outForm += "-" + form;
                if (shiny >= 0)
                {
                    outForm += "-" + shiny;
                    if (gender >= 0)
                    {
                        outForm += "-" + gender;
                    }
                }
            }

            try
            {
                SpriteSheet sprite = CompileSpriteInternal(spriteDir);

                using (MemoryStream spriteStream = new MemoryStream())
                {
                    using (BinaryWriter writer = new BinaryWriter(spriteStream))
                    {
                        //Go through each frame type, get metadata, sprite data
                        foreach (FrameType frameType in Enum.GetValues(typeof(FrameType)))
                        {
                            if (!SpriteSheet.IsFrameTypeDirectionless(frameType))
                            {
                                for (int j = 0; j < 8; j++)
                                {
                                    Maps.Direction8 dir            = (Maps.Direction8)j;
                                    int             frameCountSize = sprite.FrameData.GetFrameCount(frameType, dir);

                                    TileSheet anim = sprite.GetSheet(frameType, dir);

                                    // Add frame count size
                                    writer.Write(frameCountSize);

                                    int    sheetSize      = 0;
                                    byte[] memStreamArray = new byte[0];
                                    if (anim != null)
                                    {
                                        using (MemoryStream memoryStream = new MemoryStream())
                                        {
                                            anim.Save(memoryStream, ImageFormat.Png);
                                            memStreamArray = memoryStream.ToArray();
                                            sheetSize      = memStreamArray.Length;
                                        }
                                        anim.Dispose();
                                    }
                                    // Add the animation size
                                    writer.Write(sheetSize);
                                    //add the animation itself
                                    if (sheetSize > 0)
                                    {
                                        spriteStream.Write(memStreamArray, 0, memStreamArray.Length);
                                    }
                                }
                            }
                            else
                            {
                                int frameCountSize = sprite.FrameData.GetFrameCount(frameType, Maps.Direction8.Down);

                                TileSheet anim = sprite.GetSheet(frameType, Maps.Direction8.Down);

                                // Add frame count size
                                writer.Write(frameCountSize);

                                int    sheetSize      = 0;
                                byte[] memStreamArray = new byte[0];
                                if (anim != null)
                                {
                                    using (MemoryStream memoryStream = new MemoryStream())
                                    {
                                        anim.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
                                        memStreamArray = memoryStream.ToArray();
                                        sheetSize      = memStreamArray.Length;
                                    }
                                    anim.Dispose();
                                }
                                // Add the animation size
                                writer.Write(sheetSize);
                                //add the animation itself
                                if (sheetSize > 0)
                                {
                                    spriteStream.Write(memStreamArray, 0, memStreamArray.Length);
                                }
                            }
                        }
                    }

                    byte[] writingBytes = spriteStream.ToArray();
                    spriteData.Add(outForm, writingBytes);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error compiling sprite" + spriteDir + "\n", ex);
            }
        }
Ejemplo n.º 5
0
        private static void CompilePortraitToFile(string spriteDir, Dictionary <string, byte[]> spriteData, int num, int form, int shiny, int gender)
        {
            //check to see if files exist
            string[] pngs = Directory.GetFiles(spriteDir, "*.png", SearchOption.TopDirectoryOnly);
            if (pngs.Length < 1)
            {
                return;
            }

            String outForm = "r";

            if (form >= 0)
            {
                outForm += "-" + form;
                if (shiny >= 0)
                {
                    outForm += "-" + shiny;
                    if (gender >= 0)
                    {
                        outForm += "-" + gender;
                    }
                }
            }

            try
            {
                TileSheet sprite = CompilePortraitInternal(spriteDir, out int totalFrames);

                using (MemoryStream spriteStream = new MemoryStream())
                {
                    using (BinaryWriter writer = new BinaryWriter(spriteStream))
                    {
                        writer.Write(totalFrames);

                        int    sheetSize      = 0;
                        byte[] memStreamArray = new byte[0];
                        if (sprite != null)
                        {
                            using (MemoryStream memoryStream = new MemoryStream())
                            {
                                sprite.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
                                memStreamArray = memoryStream.ToArray();
                                sheetSize      = memStreamArray.Length;
                            }
                            sprite.Dispose();
                        }

                        // Add the animation size
                        writer.Write(sheetSize);
                        //add the animation itself
                        if (sheetSize > 0)
                        {
                            spriteStream.Write(memStreamArray, 0, memStreamArray.Length);
                        }
                    }

                    byte[] writingBytes = spriteStream.ToArray();
                    spriteData.Add(outForm, writingBytes);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error compiling portait" + spriteDir + "\n", ex);
            }
        }
Ejemplo n.º 6
0
        private static SpriteSheet BuildSpriteSurface(Dictionary <FrameType, Dictionary <Maps.Direction8, List <Bitmap> > > frameCollection, Dictionary <FrameType, List <Bitmap> > miscFrameCollection, string spriteRootDirectory)
        {
            int frameHeight = 0;
            int frameWidth  = 0;

            SpriteSheet sprite = new SpriteSheet();

            //set frame count and frame size
            for (int i = 0; i < frameCollection.Keys.Count; i++)
            {
                Dictionary <Maps.Direction8, List <Bitmap> > frameDirectionCollection = frameCollection.Values.ElementAt(i);
                for (int j = 0; j < 8; j++)
                {
                    Maps.Direction8 direction         = (Maps.Direction8)j;
                    int             totalSurfaceWidth = 0;
                    int             lastX             = 0;

                    List <Bitmap> frameList = frameDirectionCollection[direction];
                    sprite.FrameData.SetFrameCount(frameCollection.Keys.ElementAt(i), direction, frameList.Count);

                    foreach (Bitmap frame in frameList)
                    {
                        if (frameHeight == 0)
                        {
                            frameHeight = frame.Height;
                        }
                        else if (frameHeight != frame.Height)
                        {
                            throw new Exception("Frameheight mismatch: " + frameCollection.Keys.ElementAt(i).ToString() + " " + direction.ToString() + " (" + frameHeight + " vs. " + frame.Height + ")");
                        }
                        if (frameWidth == 0)
                        {
                            frameWidth = frame.Width;
                        }
                        else if (frameWidth != frame.Width)
                        {
                            throw new Exception("Framewidth mismatch: " + frameCollection.Keys.ElementAt(i).ToString() + " " + direction.ToString() + " (" + frameWidth + " vs. " + frame.Width + ")");
                        }
                        totalSurfaceWidth += frame.Width;
                    }

                    TileSheet animSheet = null;
                    if (totalSurfaceWidth > 0)
                    {
                        animSheet = new TileSheet();
                        animSheet.CreatePixels32(totalSurfaceWidth, frameHeight);
                    }
                    foreach (Bitmap frame in frameList)
                    {
                        BitmapData frameData = frame.LockBits(new Rectangle(0, 0, frame.Width, frame.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
                        animSheet.Blit(frameData, 0, 0, frame.Width, frame.Height, lastX, 0);
                        frame.UnlockBits(frameData);
                        lastX += frame.Width;
                        frame.Dispose();
                    }

                    sprite.AddSheet(frameCollection.Keys.ElementAt(i), direction, animSheet);
                }
            }

            //set frame count and frame size
            for (int i = 0; i < miscFrameCollection.Count; i++)
            {
                int totalSurfaceWidth = 0;
                int lastX             = 0;

                List <Bitmap> frameList = miscFrameCollection.Values.ElementAt(i);
                sprite.FrameData.SetFrameCount(miscFrameCollection.Keys.ElementAt(i), Maps.Direction8.Down, frameList.Count);

                foreach (Bitmap frame in frameList)
                {
                    if (frameHeight == 0)
                    {
                        frameHeight = frame.Height;
                    }
                    else if (frameHeight != frame.Height)
                    {
                        throw new Exception("Frameheight mismatch: " + frameCollection.Keys.ElementAt(i).ToString() + " (" + frameHeight + " vs. " + frame.Height + ")");
                    }
                    if (frameWidth == 0)
                    {
                        frameWidth = frame.Width;
                    }
                    else if (frameWidth != frame.Width)
                    {
                        throw new Exception("Framewidth mismatch: " + frameCollection.Keys.ElementAt(i).ToString() + " (" + frameWidth + " vs. " + frame.Width + ")");
                    }
                    totalSurfaceWidth += frame.Width;
                }

                TileSheet animSheet = null;
                if (totalSurfaceWidth > 0)
                {
                    animSheet = new TileSheet();
                    animSheet.CreatePixels32(totalSurfaceWidth, frameHeight);
                }
                foreach (Bitmap frame in frameList)
                {
                    BitmapData frameData = frame.LockBits(new Rectangle(0, 0, frame.Width, frame.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
                    animSheet.Blit(frameData, 0, 0, frame.Width, frame.Height, lastX, 0);
                    frame.UnlockBits(frameData);
                    lastX += frame.Width;
                    frame.Dispose();
                }

                sprite.AddSheet(miscFrameCollection.Keys.ElementAt(i), Maps.Direction8.Down, animSheet);
            }

            return(sprite);
        }
Ejemplo n.º 7
0
        public static TileSheet GetMugshot(int num, int form, Enums.Coloration shiny, Enums.Gender gender)
        {
            string formString = "r";

            if (form >= 0)
            {
                formString += "-" + form;
                if (shiny >= 0)
                {
                    formString += "-" + (int)shiny;
                    if (gender >= 0)
                    {
                        formString += "-" + (int)gender;
                    }
                }
            }

            TileSheet sheet = mugshotCache.Get(num + formString);

            if (sheet != null)
            {
                return(sheet);
            }

            try
            {
                // If we are still here, that means the sprite wasn't in the cache
                if (File.Exists(Paths.CachedGFXPath + "Portrait\\Portrait" + num + ".portrait"))
                {
                    sheet = new TileSheet();
                    string changedFormString = formString;

                    using (FileStream fileStream = File.OpenRead(Paths.CachedGFXPath + "Portrait\\Portrait" + num + ".portrait"))
                    {
                        using (BinaryReader reader = new BinaryReader(fileStream))
                        {
                            int formCount = reader.ReadInt32();
                            Dictionary <string, int[]> formData = new Dictionary <string, int[]>();

                            for (int i = 0; i < formCount; i++)
                            {
                                // Read the form name
                                string formName = reader.ReadString();

                                int[] formIntData = new int[2];

                                // Load form position
                                formIntData[0] = reader.ReadInt32();
                                // Load form size
                                formIntData[1] = reader.ReadInt32();

                                // Add form data to collection
                                formData.Add(formName, formIntData);
                            }

                            while (true)
                            {
                                if (mugshotCache.ContainsKey(num + changedFormString))
                                {//this point will be hit if the first fallback data to be found is already in the cache
                                 //the cache needs to be updated for aliases, but that's it.  No need to load any new data.

                                    sheet = mugshotCache.Get(num + changedFormString);
                                    break;
                                }
                                else if (formData.ContainsKey(changedFormString) || changedFormString == "r")
                                {//we've found a spritesheet in the file, so load it.
                                    int[] formInt = formData[changedFormString];

                                    // Jump to the correct position
                                    fileStream.Seek(formInt[0], SeekOrigin.Current);

                                    try
                                    {
                                        int    frameCount = reader.ReadInt32();
                                        int    size       = reader.ReadInt32();
                                        byte[] imgData    = reader.ReadBytes(size);
                                        using (MemoryStream stream = new MemoryStream(imgData))
                                        {
                                            sheet.LoadPixelsFromBytes(stream);
                                            sheet.LoadTextureFromPixels32();
                                        }

                                        sheet.GenerateDataBuffer(sheet.ImageWidth / frameCount, sheet.ImageHeight);
                                    }
                                    catch (Exception ex)
                                    {
                                        sheet.Dispose();
                                        throw new Exception("Error reading image data.\n", ex);
                                    }

                                    mugshotCache.Add(num + changedFormString, sheet);

                                    break;
                                }

                                // If the form specified wasn't found, continually revert to the backup until only "r" is reached
                                changedFormString = changedFormString.Substring(0, changedFormString.LastIndexOf('-'));
                            }
                        }
                    }

                    //continually add aliases
                    string aliasString = formString;
                    while (aliasString != changedFormString)
                    {
                        //add aliases here
                        mugshotCache.AddAlias(num + aliasString, num + changedFormString);
                        // If the form specified wasn't found, continually revert to the backup until only "r" is reached
                        aliasString = aliasString.Substring(0, aliasString.LastIndexOf('-'));
                    }

                    return(sheet);
                }
            }
            catch (Exception ex)
            {
                Logs.Logger.LogError(new Exception("Error retrieving portrait " + num + " " + formString + "\n", ex));
            }

            //add error sheet
            mugshotCache.Add(num + formString, ErrorTexture);
            return(ErrorTexture);
        }
Ejemplo n.º 8
0
        public static void Init()
        {
            //run conversions
            if (!Directory.Exists(Paths.CachedGFXPath))
                Directory.CreateDirectory(Paths.CachedGFXPath);

            Conversion.CompileAllSprites(Paths.SpritesPath, Paths.CachedGFXPath + "Sprite");
            Conversion.CompileAllTiles(Paths.TilesPath, Paths.CachedGFXPath + "Tile");
            Conversion.CompileAllPortraits(Paths.PortraitsPath, Paths.CachedGFXPath + "Portrait");

            Game.UpdateLoadMsg("Loading Base Textures");
            //load error texture
            ErrorTexture = new AnimSheet();
            ErrorTexture.LoadPixelsFromFile32(Paths.BaseGFXPath+"Error.png");

            //load blank texture
            BlankTexture = new Texture();
            BlankTexture.LoadPixelsFromFile32(Paths.BaseGFXPath + "UI\\Blank.png");

            //load menu data
            MenuBack = new TileSheet();
            MenuBack.LoadPixelsFromFile32(Paths.BaseGFXPath + "UI\\Back.png");

            Picker = new TileSheet();
            Picker.LoadPixelsFromFile32(Paths.BaseGFXPath + "UI\\Picker.png");

            Game.UpdateLoadMsg("Loading Caches");
            //initialize caches
            spriteCache = new MultiNameLRUCache<string, SpriteSheet>(SPRITE_CACHE_SIZE);
            spriteCache.OnItemRemoved = DisposeCachedObject;
            mugshotCache = new MultiNameLRUCache<string, TileSheet>(MUGSHOT_CACHE_SIZE);
            mugshotCache.OnItemRemoved = DisposeCachedObject;
            spellCache = new LRUCache<string, AnimSheet>(SPELL_CACHE_SIZE);
            spellCache.OnItemRemoved = DisposeCachedObject;
            statusCache = new LRUCache<int, AnimSheet>(STATUS_CACHE_SIZE);
            statusCache.OnItemRemoved = DisposeCachedObject;
            itemCache = new LRUCache<int, AnimSheet>(ITEM_CACHE_SIZE);
            itemCache.OnItemRemoved = DisposeCachedObject;
            objectCache = new LRUCache<int, AnimSheet>(OBJECT_CACHE_SIZE);
            objectCache.OnItemRemoved = DisposeCachedObject;
            tileCache = new LRUCache<string, Texture>(TILE_CACHE_SIZE);
            tileCache.OnItemRemoved = DisposeCachedObject;

            //load metadata
            tileData = new TileMetadata[TOTAL_TILE_SHEETS];
        }
Ejemplo n.º 9
0
        public static TileSheet GetMugshot(int num, int form, Enums.Coloration shiny, Enums.Gender gender)
        {
            string formString = "r";

            if (form >= 0)
            {
                formString += "-" + form;
                if (shiny >= 0)
                {
                    formString += "-" + (int)shiny;
                    if (gender >= 0)
                    {
                        formString += "-" + (int)gender;
                    }
                }
            }

            TileSheet sheet = mugshotCache.Get(num + formString);

            if (sheet != null)
                return sheet;

            try
            {
                // If we are still here, that means the sprite wasn't in the cache
                if (System.IO.File.Exists(Paths.CachedGFXPath + "Portrait\\Portrait" + num + ".portrait"))
                {

                    sheet = new TileSheet();
                    string changedFormString = formString;

                    using (FileStream fileStream = File.OpenRead(Paths.CachedGFXPath + "Portrait\\Portrait" + num + ".portrait"))
                    {
                        using (BinaryReader reader = new BinaryReader(fileStream))
                        {
                            int formCount = reader.ReadInt32();
                            Dictionary<string, int[]> formData = new Dictionary<string, int[]>();

                            for (int i = 0; i < formCount; i++)
                            {
                                // Read the form name
                                string formName = reader.ReadString();

                                int[] formIntData = new int[2];

                                // Load form position
                                formIntData[0] = reader.ReadInt32();
                                // Load form size
                                formIntData[1] = reader.ReadInt32();

                                // Add form data to collection
                                formData.Add(formName, formIntData);
                            }

                            while (true)
                            {
                                if (mugshotCache.ContainsKey(num + changedFormString))
                                {//this point will be hit if the first fallback data to be found is already in the cache
                                    //the cache needs to be updated for aliases, but that's it.  No need to load any new data.

                                    sheet = mugshotCache.Get(num + changedFormString);
                                    break;
                                }
                                else if (formData.ContainsKey(changedFormString) || changedFormString == "r")
                                {//we've found a spritesheet in the file, so load it.
                                    int[] formInt = formData[changedFormString];

                                    // Jump to the correct position
                                    fileStream.Seek(formInt[0], SeekOrigin.Current);

                                    try
                                    {
                                        int frameCount = reader.ReadInt32();
                                        int size = reader.ReadInt32();
                                        byte[] imgData = reader.ReadBytes(size);
                                        using (MemoryStream stream = new MemoryStream(imgData))
                                        {
                                            sheet.LoadPixelsFromBytes(stream);
                                            sheet.LoadTextureFromPixels32();
                                        }

                                        sheet.GenerateDataBuffer(sheet.ImageWidth / frameCount, sheet.ImageHeight);
                                    }
                                    catch (Exception ex)
                                    {
                                        sheet.Dispose();
                                        throw new Exception("Error reading image data.\n", ex);
                                    }

                                    mugshotCache.Add(num + changedFormString, sheet);

                                    break;
                                }

                                // If the form specified wasn't found, continually revert to the backup until only "r" is reached
                                changedFormString = changedFormString.Substring(0, changedFormString.LastIndexOf('-'));
                            }
                        }
                    }

                    //continually add aliases
                    string aliasString = formString;
                    while (aliasString != changedFormString)
                    {
                        //add aliases here
                        mugshotCache.AddAlias(num + aliasString, num + changedFormString);
                        // If the form specified wasn't found, continually revert to the backup until only "r" is reached
                        aliasString = aliasString.Substring(0, aliasString.LastIndexOf('-'));
                    }

                    return sheet;
                }
            }
            catch (Exception ex)
            {
                Logs.Logger.LogError(new Exception("Error retrieving portrait " + num + " " + formString + "\n", ex));
            }

            //add error sheet
            mugshotCache.Add(num + formString, ErrorTexture);
            return ErrorTexture;
        }
Ejemplo n.º 10
0
        public void LoadFromData(BinaryReader reader, int totalByteSize)
        {
            foreach (FrameType frameType in Enum.GetValues(typeof(FrameType)))
            {
                if (IsFrameTypeDirectionless(frameType) == false)
                {
                    for (int i = 0; i < 8; i++)
                    {
                        Maps.Direction8 dir        = (Maps.Direction8)i;
                        int             frameCount = reader.ReadInt32();
                        frameData.SetFrameCount(frameType, dir, frameCount);
                        int size = reader.ReadInt32();
                        if (size > 0)
                        {
                            byte[]    imgData      = reader.ReadBytes(size);
                            TileSheet sheetSurface = new TileSheet();
                            using (MemoryStream stream = new MemoryStream(imgData))
                            {
                                try
                                {
                                    sheetSurface.LoadPixelsFromBytes(stream);

                                    sheetSurface.LoadTextureFromPixels32();
                                    sheetSurface.GenerateDataBuffer(sheetSurface.ImageWidth / frameCount, sheetSurface.ImageHeight);
                                }
                                catch (Exception ex)
                                {
                                    sheetSurface.Dispose();
                                    throw new Exception("Error reading image data for " + frameType.ToString() + " " + dir.ToString() + "\n", ex);
                                }
                            }
                            AddSheet(frameType, dir, sheetSurface);

                            frameData.SetFrameSize(sheetSurface.ImageWidth, sheetSurface.ImageHeight, frameCount);
                        }
                    }
                }
                else
                {
                    int frameCount = reader.ReadInt32();
                    frameData.SetFrameCount(frameType, PMDToolkit.Maps.Direction8.Down, frameCount);
                    int size = reader.ReadInt32();
                    if (size > 0)
                    {
                        byte[]    imgData      = reader.ReadBytes(size);
                        TileSheet sheetSurface = new TileSheet();
                        using (MemoryStream stream = new MemoryStream(imgData))
                        {
                            try
                            {
                                sheetSurface.LoadPixelsFromBytes(stream);
                                sheetSurface.LoadTextureFromPixels32();
                            }
                            catch (Exception ex)
                            {
                                sheetSurface.Dispose();
                                throw new Exception("Error reading image data for " + frameType.ToString() + "\n", ex);
                            }
                        }
                        sheetSurface.GenerateDataBuffer(sheetSurface.ImageWidth / frameCount, sheetSurface.ImageHeight);
                        AddSheet(frameType, PMDToolkit.Maps.Direction8.Down, sheetSurface);

                        frameData.SetFrameSize(sheetSurface.ImageWidth, sheetSurface.ImageHeight, frameCount);
                    }
                }
            }


            this.sizeInBytes = totalByteSize;
        }
Ejemplo n.º 11
0
        public void LoadFromData(BinaryReader reader, int totalByteSize)
        {
            foreach (FrameType frameType in Enum.GetValues(typeof(FrameType)))
            {
                if (IsFrameTypeDirectionless(frameType) == false)
                {
                    for (int i = 0; i < 8; i++)
                    {
                        Maps.Direction8 dir = (Maps.Direction8)i;
                        int frameCount = reader.ReadInt32();
                        frameData.SetFrameCount(frameType, dir, frameCount);
                        int size = reader.ReadInt32();
                        if (size > 0)
                        {
                            byte[] imgData = reader.ReadBytes(size);
                            TileSheet sheetSurface = new TileSheet();
                            using (MemoryStream stream = new MemoryStream(imgData))
                            {
                                try
                                {
                                    sheetSurface.LoadPixelsFromBytes(stream);

                                    sheetSurface.LoadTextureFromPixels32();
                                    sheetSurface.GenerateDataBuffer(sheetSurface.ImageWidth / frameCount, sheetSurface.ImageHeight);
                                }
                                catch (Exception ex)
                                {
                                    sheetSurface.Dispose();
                                    throw new Exception("Error reading image data for " + frameType.ToString() + " " + dir.ToString() + "\n", ex);
                                }
                            }
                            AddSheet(frameType, dir, sheetSurface);

                            frameData.SetFrameSize(sheetSurface.ImageWidth, sheetSurface.ImageHeight, frameCount);
                        }
                    }
                }
                else
                {
                    int frameCount = reader.ReadInt32();
                    frameData.SetFrameCount(frameType, PMDToolkit.Maps.Direction8.Down, frameCount);
                    int size = reader.ReadInt32();
                    if (size > 0)
                    {
                        byte[] imgData = reader.ReadBytes(size);
                        TileSheet sheetSurface = new TileSheet();
                        using (MemoryStream stream = new MemoryStream(imgData))
                        {
                            try
                            {
                                sheetSurface.LoadPixelsFromBytes(stream);
                                sheetSurface.LoadTextureFromPixels32();
                            }
                            catch (Exception ex)
                            {
                                sheetSurface.Dispose();
                                throw new Exception("Error reading image data for " + frameType.ToString() + "\n", ex);
                            }
                        }
                        sheetSurface.GenerateDataBuffer(sheetSurface.ImageWidth / frameCount, sheetSurface.ImageHeight);
                        AddSheet(frameType, PMDToolkit.Maps.Direction8.Down, sheetSurface);

                        frameData.SetFrameSize(sheetSurface.ImageWidth, sheetSurface.ImageHeight, frameCount);
                    }
                }
            }

            this.sizeInBytes = totalByteSize;
        }
Ejemplo n.º 12
0
 public void AddSheet(FrameType type, PMDToolkit.Maps.Direction8 dir, TileSheet surface)
 {
     if (!animations.ContainsKey(type)) {
         animations.Add(type, new Dictionary<PMDToolkit.Maps.Direction8, TileSheet>());
     }
     if (animations[type].ContainsKey(dir) == false) {
         animations[type].Add(dir, surface);
     } else {
         animations[type][dir] = surface;
     }
 }
Ejemplo n.º 13
0
        private static SpriteSheet BuildSpriteSurface(Dictionary<FrameType, Dictionary<Maps.Direction8, List<Bitmap>>> frameCollection, Dictionary<FrameType, List<Bitmap>> miscFrameCollection, string spriteRootDirectory)
        {
            int frameHeight = 0;
            int frameWidth = 0;

            SpriteSheet sprite = new SpriteSheet();

            //set frame count and frame size
            for (int i = 0; i < frameCollection.Keys.Count; i++) {
                Dictionary<Maps.Direction8, List<Bitmap>> frameDirectionCollection = frameCollection.Values.ElementAt(i);
                for (int j = 0; j < 8; j++) {
                    Maps.Direction8 direction = (Maps.Direction8)j;
                    int totalSurfaceWidth = 0;
                    int lastX = 0;

                    List<Bitmap> frameList = frameDirectionCollection[direction];
                    sprite.FrameData.SetFrameCount(frameCollection.Keys.ElementAt(i), direction, frameList.Count);

                    foreach (Bitmap frame in frameList) {
                        if (frameHeight == 0) {
                            frameHeight = frame.Height;
                        } else if (frameHeight != frame.Height) {
                            throw new Exception("Frameheight mismatch: " + frameCollection.Keys.ElementAt(i).ToString() + " " + direction.ToString() + " (" + frameHeight + " vs. " + frame.Height + ")" );
                        }
                        if (frameWidth == 0) {
                            frameWidth = frame.Width;
                        } else if (frameWidth != frame.Width) {
                            throw new Exception("Framewidth mismatch: " + frameCollection.Keys.ElementAt(i).ToString() + " " + direction.ToString() + " (" + frameWidth + " vs. " + frame.Width + ")");
                        }
                        totalSurfaceWidth += frame.Width;
                    }

                    TileSheet animSheet = null;
                    if (totalSurfaceWidth > 0) {
                        animSheet = new TileSheet();
                        animSheet.CreatePixels32(totalSurfaceWidth, frameHeight);
                    }
                    foreach (Bitmap frame in frameList) {
                        BitmapData frameData = frame.LockBits(new Rectangle(0, 0, frame.Width, frame.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
                        animSheet.Blit(frameData, 0, 0, frame.Width, frame.Height, lastX, 0);
                        frame.UnlockBits(frameData);
                        lastX += frame.Width;
                        frame.Dispose();
                    }

                    sprite.AddSheet(frameCollection.Keys.ElementAt(i), direction, animSheet);
                }
            }

            //set frame count and frame size
            for (int i = 0; i < miscFrameCollection.Count; i++) {
                int totalSurfaceWidth = 0;
                int lastX = 0;

                List<Bitmap> frameList = miscFrameCollection.Values.ElementAt(i);
                sprite.FrameData.SetFrameCount(miscFrameCollection.Keys.ElementAt(i), Maps.Direction8.Down, frameList.Count);

                foreach (Bitmap frame in frameList) {
                    if (frameHeight == 0) {
                        frameHeight = frame.Height;
                    } else if (frameHeight != frame.Height) {
                        throw new Exception("Frameheight mismatch: " + frameCollection.Keys.ElementAt(i).ToString() + " (" + frameHeight + " vs. " + frame.Height + ")");
                    }
                    if (frameWidth == 0) {
                        frameWidth = frame.Width;
                    } else if (frameWidth != frame.Width) {
                        throw new Exception("Framewidth mismatch: " + frameCollection.Keys.ElementAt(i).ToString() + " (" + frameWidth + " vs. " + frame.Width + ")");
                    }
                    totalSurfaceWidth += frame.Width;
                }

                TileSheet animSheet = null;
                if (totalSurfaceWidth > 0) {
                    animSheet = new TileSheet();
                    animSheet.CreatePixels32(totalSurfaceWidth, frameHeight);
                }
                foreach (Bitmap frame in frameList) {
                    BitmapData frameData = frame.LockBits(new Rectangle(0, 0, frame.Width, frame.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
                    animSheet.Blit(frameData, 0, 0, frame.Width, frame.Height, lastX, 0);
                    frame.UnlockBits(frameData);
                    lastX += frame.Width;
                    frame.Dispose();
                }

                sprite.AddSheet(miscFrameCollection.Keys.ElementAt(i), Maps.Direction8.Down, animSheet);
            }

            return sprite;
        }
Ejemplo n.º 14
0
        private static TileSheet BuildPortraitSurface(List<Bitmap> frameCollection, string spriteRootDirectory)
        {
            int frameHeight = 0;
            int frameWidth = 0;

            TileSheet animSheet = null;

            int totalSurfaceWidth = 0;
            int lastX = 0;
            int frameNum = 0;

            foreach (Bitmap frame in frameCollection)
            {
                if (frameHeight == 0)
                    frameHeight = frame.Height;
                else if (frameHeight != frame.Height)
                {
                    throw new Exception("Frameheight mismatch: " + frameNum + " (" + frameHeight + " vs. " + frame.Height + ")");
                }
                if (frameWidth == 0)
                {
                    frameWidth = frame.Width;
                }
                else if (frameWidth != frame.Width)
                {
                    throw new Exception("Framewidth mismatch: " + frameNum + " (" + frameWidth + " vs. " + frame.Width + ")");
                }
                totalSurfaceWidth += frame.Width;
                frameNum++;
            }

            if (totalSurfaceWidth > 0)
            {
                animSheet = new TileSheet();
                animSheet.CreatePixels32(totalSurfaceWidth, frameHeight);
            }
            foreach (Bitmap frame in frameCollection)
            {
                BitmapData frameData = frame.LockBits(new Rectangle(0, 0, frame.Width, frame.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
                animSheet.Blit(frameData, 0, 0, frame.Width, frame.Height, lastX, 0);
                frame.UnlockBits(frameData);
                lastX += frame.Width;
                frame.Dispose();
            }

            return animSheet;
        }