예제 #1
0
        public unsafe AnimationFrame(GraphicsDevice graphics, uint[] palette, BinaryFileReader reader)
        {
            int xCenter = reader.ReadShort();
            int yCenter = reader.ReadShort();

            int width  = reader.ReadUShort();
            int height = reader.ReadUShort();

            // Fix for animations with no IO.
            if ((width == 0) || (height == 0))
            {
                m_Texture = null;
                return;
            }

            uint[] data = new uint[width * height];

            int header;

            int xBase = xCenter - 0x200;
            int yBase = (yCenter + height) - 0x200;

            fixed(uint *pData = data)
            {
                uint *dataRef = pData;
                int   delta   = width;

                int dataRead = 0;

                dataRef += xBase;
                dataRef += (yBase * delta);

                while ((header = reader.ReadInt()) != 0x7FFF7FFF)
                {
                    header ^= DoubleXor;

                    uint *cur = dataRef + ((((header >> 12) & 0x3FF) * delta) + ((header >> 22) & 0x3FF));
                    uint *end = cur + (header & 0xFFF);

                    int    filecounter = 0;
                    byte[] filedata    = reader.ReadBytes(header & 0xFFF);

                    while (cur < end)
                    {
                        *cur++ = palette[filedata[filecounter++]];
                    }

                    dataRead += header & 0xFFF;
                }

                Metrics.ReportDataRead(dataRead);
            }

            m_Center = new Microsoft.Xna.Framework.Point(xCenter, yCenter);

            m_Texture = new Texture2D(graphics, width, height);
            m_Texture.SetData <uint>(data);
        }
예제 #2
0
        private static ushort[] readStaticDimensions(int index)
        {
            int  length, extra;
            bool is_patched;

            BinaryFileReader reader = m_Index.Seek(index, out length, out extra, out is_patched);

            reader.ReadInt();

            return(new ushort[] { reader.ReadUShort(), reader.ReadUShort() });
        }
예제 #3
0
        private bool Unserialize(BinaryFileReader reader)
        {
            uint magic = reader.ReadUInt();

            if (magic != MAGIC)
            {
                return(false);
            }

            if (m_Macros == null)
            {
                m_Macros = new List <Action>();
            }
            m_Macros.Clear();

            int version = reader.ReadInt();
            int count   = reader.ReadInt();

            for (int i = 0; i < count; i++)
            {
                Action action = new Action();
                action.Keystroke = (WinKeys)reader.ReadUShort();
                action.Ctrl      = reader.ReadBool();
                action.Alt       = reader.ReadBool();
                action.Shift     = reader.ReadBool();
                reader.ReadBool(); // unused filler byte

                int macroCount = reader.ReadUShort();
                for (int j = 0; j < macroCount; j++)
                {
                    int type = reader.ReadInt();
                    Macro.ValueTypes valueType = (Macro.ValueTypes)reader.ReadByte();
                    if (valueType == Macro.ValueTypes.Integer)
                    {
                        action.Macros.Add(new Macro((MacroType)type, reader.ReadInt()));
                    }
                    else if (valueType == Macro.ValueTypes.String)
                    {
                        action.Macros.Add(new Macro((MacroType)type, reader.ReadString()));
                    }
                    else
                    {
                        action.Macros.Add(new Macro((MacroType)type));
                    }
                }

                m_Macros.Add(action);
            }

            return(true);
        }
예제 #4
0
        private bool Unserialize(BinaryFileReader r)
        {
            var magic = r.ReadUInt();

            if (magic != MAGIC)
            {
                return(false);
            }

            if (_macros == null)
            {
                _macros = new List <Action>();
            }
            _macros.Clear();
            var version = r.ReadInt();
            var count   = r.ReadInt();

            for (var i = 0; i < count; i++)
            {
                var action = new Action();
                action.Keystroke = (WinKeys)r.ReadUShort();
                action.Ctrl      = r.ReadBool();
                action.Alt       = r.ReadBool();
                action.Shift     = r.ReadBool();
                r.ReadBool(); // unused filler byte

                var macroCount = r.ReadUShort();
                for (var j = 0; j < macroCount; j++)
                {
                    var type      = r.ReadInt();
                    var valueType = (Macro.ValueTypes)r.ReadByte();
                    if (valueType == Macro.ValueTypes.Integer)
                    {
                        action.Macros.Add(new Macro((MacroType)type, r.ReadInt()));
                    }
                    else if (valueType == Macro.ValueTypes.String)
                    {
                        action.Macros.Add(new Macro((MacroType)type, r.ReadString()));
                    }
                    else
                    {
                        action.Macros.Add(new Macro((MacroType)type));
                    }
                }
                _macros.Add(action);
            }

            return(true);
        }
예제 #5
0
 private ushort[] GetPalette(BinaryFileReader reader)
 {
     ushort[] pal = new ushort[0x100];
     for (int i = 0; i < 0x100; ++i)
     {
         pal[i] = (ushort)(reader.ReadUShort() | 0x8000);
     }
     return pal;
 }
예제 #6
0
        ushort[] GetPalette(BinaryFileReader reader)
        {
            var pal = new ushort[0x100];

            for (var i = 0; i < 0x100; ++i)
            {
                pal[i] = (ushort)(reader.ReadUShort() | 0x8000);
            }
            return(pal);
        }
예제 #7
0
 private static uint[] getPalette(BinaryFileReader reader)
 {
     uint[] pal = new uint[0x100];
     for (int i = 0; i < 0x100; ++i)
     {
         uint color = reader.ReadUShort();
         pal[i] = 0xFF000000 + (
             ((((color >> 10) & 0x1F) * 0xFF / 0x1F)) |
             ((((color >> 5) & 0x1F) * 0xFF / 0x1F) << 8) |
             (((color & 0x1F) * 0xFF / 0x1F) << 16)
             );
     }
     return(pal);
 }
예제 #8
0
        public unsafe AnimationFrame(int uniqueAnimationIndex, GraphicsDevice graphics, ushort[] palette, BinaryFileReader reader, SittingTransformation sitting)
        {
            m_AnimationIndex = uniqueAnimationIndex;
            int xCenter = reader.ReadShort();
            int yCenter = reader.ReadShort();
            int width   = reader.ReadUShort();
            int height  = reader.ReadUShort();

            // Fix for animations with no pixels.
            if ((width == 0) || (height == 0))
            {
                Texture = null;
                return;
            }
            if (sitting == SittingTransformation.StandSouth)
            {
                xCenter += 8;
                width   += 8;
                height  += 4;
            }
            ushort[] data = new ushort[width * height];
            // for sitting:
            // somewhere around the waist of a typical mobile animation, we take twelve rows of pixels,
            // discard every third, and shift every remaining row (total of eight) one pixel to the left
            // or right (depending on orientation), for a total skew of eight pixels.
            fixed(ushort *pData = data)
            {
                ushort *dataRef = pData;

                int dataRead = 0;

                int header;

                while ((header = reader.ReadInt()) != 0x7FFF7FFF)
                {
                    header ^= DoubleXor;
                    int x = ((header >> 22) & 0x3FF) + xCenter - 0x200;
                    int y = ((header >> 12) & 0x3FF) + yCenter + height - 0x200;
                    if (sitting == SittingTransformation.StandSouth)
                    {
                        const int skew_start = -17;
                        const int skew_end   = skew_start - 16;
                        int       iy         = y - height - yCenter;
                        if (iy > skew_start)
                        {
                            // pixels below the skew
                            x -= 8;
                            y -= 4;
                        }
                        else if (iy > skew_end)
                        {
                            // pixels within the skew
                            if ((iy - skew_end) % 4 == 0)
                            {
                                reader.Position += (header & 0xFFF);
                                continue;
                            }
                            x -= (iy - skew_end) / 2;
                            y -= (iy - skew_end) / 4;
                        }
                    }
                    ushort *cur         = dataRef + y * width + x;
                    ushort *end         = cur + (header & 0xFFF);
                    int     filecounter = 0;
                    byte[]  filedata    = reader.ReadBytes(header & 0xFFF);
                    while (cur < end)
                    {
                        *cur++ = palette[filedata[filecounter++]];
                    }
                    dataRead += header & 0xFFF;
                }
                Metrics.ReportDataRead(dataRead);
            }

            Center  = new Point(xCenter, yCenter);
            Texture = new Texture2D(graphics, width, height, false, SurfaceFormat.Bgra5551);
            Texture.SetData(data);
            m_Picking.Set(m_AnimationIndex, width, height, data);
        }
예제 #9
0
        /// <summary>
        /// Reads a DMT file filtered by a provided filter.
        /// </summary>
        /// <param name="file">The mesh file.</param>
        /// <param name="filter">The provided filtered.</param>
        /// <returns>The DMTModel that obeys to the filter condition.</returns>
        public List <DMTModel> ReadFile(File file, IDMTModelFilter filter)
        {
            var blocksIn     = new List <DMTTriangleBlock>();
            var blocksOut    = new List <DMTTriangleBlock>();
            var binaryReader = new BinaryFileReader(file);

            try
            {
                if (file.Exists == false)
                {
                    throw new DMTFileException(DMTFileError.FileDoesNotExist);
                }

                // Read the file header. It is terminated with a 0 (null) and or has a
                // maximum of 256 bytes
                string header = null;
                header = binaryReader.ReadStringUntil(0, 256);

                // Read the file version number. Should be 1000 as this is the only format currently supported
                var version = binaryReader.ReadUShort();
                if (version != 1000)
                {
                    //Close the reader and return fail
                    binaryReader.Close();
                    throw new DMTFileException(DMTFileError.UnsupportedFileFormat);
                }

                // Read the file flags
                // The bits of this integer are used as flags to detail file specific features.
                //   1.  If this bit is set then the vertex data are stored as floats, else doubles.
                //   2.  If this bit is set then the file contains triangles which are known to have been written in units of MMs.
                //       If the flag is unset then the units used are unknown. This change was introduced by api#25 and dicc34143
                //
                //   New files (Since TVD release 2000111) MUST be written in MM. The code in dmkdmt always sets the flag and
                //   the write_node() API ensures that you know about the requirement.
                uint fileFlags = 0;
                fileFlags = binaryReader.ReadUInteger();
                var pointsAreFloats = (fileFlags & 1) == 1;

                // Read the number of triangle blocks in the file. Should not be zero
                uint noOfBlocks = 0;
                noOfBlocks = binaryReader.ReadUInteger();
                if (noOfBlocks == 0)
                {
                    binaryReader.Close();
                    throw new DMTFileException(DMTFileError.NoTriangleBlocks);
                }

                // Read the number of vertices in the file. Should not be zero
                uint totalTriangleVertices = 0;
                totalTriangleVertices = binaryReader.ReadUInteger();
                if (totalTriangleVertices == 0)
                {
                    binaryReader.Close();
                    throw new DMTFileException(DMTFileError.NoVertices);
                }

                // Read the number of triangles in the file. Should not be zero
                uint totalTriangles = 0;
                totalTriangles = binaryReader.ReadUInteger();
                if (totalTriangles == 0)
                {
                    binaryReader.Close();
                    throw new DMTFileException(DMTFileError.NoTriangles);
                }

                // Read the blocks
                for (var blockNo = 0; blockNo <= noOfBlocks - 1; blockNo++)
                {
                    var blockIn       = new DMTTriangleBlock();
                    var blockOut      = new DMTTriangleBlock();
                    var vertices      = new List <Point>();
                    var vertexNormals = new List <Vector>();
                    var oldIndexToNewIndexMapBlockIn  = new Dictionary <int, int>();
                    var oldIndexToNewIndexMapBlockOut = new Dictionary <int, int>();

                    // Read the block flags
                    uint blockFlags = 0;
                    blockFlags = binaryReader.ReadUInteger();
                    var verticesHaveNormals = false;
                    verticesHaveNormals            = (blockFlags & 1) == 1;
                    blockIn.DoVerticesHaveNormals  = verticesHaveNormals;
                    blockOut.DoVerticesHaveNormals = verticesHaveNormals;

                    uint noOfTriangleVertices = 0;
                    noOfTriangleVertices = binaryReader.ReadUInteger();
                    uint noOfTriangles = 0;
                    noOfTriangles = binaryReader.ReadUInteger();

                    // Read the vertices
                    var x  = default(MM);
                    var y  = default(MM);
                    var z  = default(MM);
                    var nx = default(MM);
                    var ny = default(MM);
                    var nz = default(MM);

                    for (var intNodeNo = 0; intNodeNo <= noOfTriangleVertices - 1; intNodeNo++)
                    {
                        // Read the XYZ values
                        if (pointsAreFloats)
                        {
                            x = binaryReader.ReadSingle();
                            y = binaryReader.ReadSingle();
                            z = binaryReader.ReadSingle();
                        }
                        else
                        {
                            x = binaryReader.ReadDouble();
                            y = binaryReader.ReadDouble();
                            z = binaryReader.ReadDouble();
                        }

                        // Continue reading
                        if (verticesHaveNormals)
                        {
                            if (pointsAreFloats)
                            {
                                nx = binaryReader.ReadSingle();
                                ny = binaryReader.ReadSingle();
                                nz = binaryReader.ReadSingle();
                            }
                            else
                            {
                                nx = binaryReader.ReadDouble();
                                ny = binaryReader.ReadDouble();
                                nz = binaryReader.ReadDouble();
                            }
                        }

                        // Store the vertex
                        vertices.Add(new Point(x, y, z));
                        if (verticesHaveNormals)
                        {
                            vertexNormals.Add(new Vector(nx, ny, nz));
                        }
                    }

                    // What size are the pointers?
                    // They will use 32 bit Unsigned Integers if 16 bit Unsigned Integer is not enough
                    var use32bitPointers = noOfTriangleVertices > ushort.MaxValue;

                    // Read the triangles
                    var vertex1Index = 0;
                    var vertex2Index = 0;
                    var vertex3Index = 0;

                    for (var triangleNo = 0; triangleNo <= noOfTriangles - 1; triangleNo++)
                    {
                        if (use32bitPointers)
                        {
                            vertex1Index = binaryReader.ReadInteger();
                            vertex2Index = binaryReader.ReadInteger();
                            vertex3Index = binaryReader.ReadInteger();
                        }
                        else
                        {
                            vertex1Index = binaryReader.ReadUShort();
                            vertex2Index = binaryReader.ReadUShort();
                            vertex3Index = binaryReader.ReadUShort();
                        }

                        if (filter.CanAddTriangle(vertices[vertex1Index], vertices[vertex2Index], vertices[vertex3Index]))
                        {
                            AddTriangle(oldIndexToNewIndexMapBlockIn,
                                        blockIn,
                                        verticesHaveNormals,
                                        vertices,
                                        vertexNormals,
                                        vertex1Index,
                                        vertex2Index,
                                        vertex3Index);
                        }
                        else
                        {
                            AddTriangle(oldIndexToNewIndexMapBlockOut,
                                        blockOut,
                                        verticesHaveNormals,
                                        vertices,
                                        vertexNormals,
                                        vertex1Index,
                                        vertex2Index,
                                        vertex3Index);
                        }
                    }

                    // Check that the version number is ok
                    if (binaryReader.ReadUShort() != version)
                    {
                        throw new DMTFileException(DMTFileError.BlockVersionDoesNotMatchFileVersion);
                    }

                    blocksIn.Add(blockIn);
                    blocksOut.Add(blockOut);
                }

                var modelWithinFilter  = new DMTModel();
                var modelOutsideFilter = new DMTModel();
                modelWithinFilter.TriangleBlocks.AddRange(blocksIn);
                modelOutsideFilter.TriangleBlocks.AddRange(blocksOut);
                var result = new List <DMTModel>();
                result.Add(modelWithinFilter);
                result.Add(modelOutsideFilter);
                return(result);
            }
            finally
            {
                // Close the binary reader
                if (binaryReader != null)
                {
                    binaryReader.Close();
                }
            }
        }