Click here to Skip to main content
15,881,172 members
Articles / Multimedia / DirectX

Endogine sprite engine

Rate me:
Please Sign up or sign in to vote.
4.84/5 (53 votes)
17 Jul 200615 min read 714.5K   22.1K   216  
Sprite engine for D3D and GDI+ (with several game examples).
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Endogine.Serialization
{
    public class RleCodec
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="destinationBuffer"></param>
        /// <param name="bufferOffset"></param>
        /// <returns>the number of uncompressed bytes written to the destination buffer</returns>
        public static int DecodeChunk(Stream reader, byte[] destinationBuffer, int bufferOffset)
        {
            int numUncompressedWritten = 0;

            int len = (int)reader.ReadByte();

            if (len < 128)
            {
                //less than 128: uncompressed pixels
                int numUncompressedToRead = len + 1;
                numUncompressedWritten = numUncompressedToRead;

                while (numUncompressedToRead != 0)
                {
                    destinationBuffer[bufferOffset] = (byte)reader.ReadByte();
                    bufferOffset++;
                    numUncompressedToRead--;
                }
            }
            else if (len > 128)
            {
                //more than 128: RLE-compressed pixels
                int numCompressedToRead = (len ^ 0xff) + 2;

                numUncompressedWritten = numCompressedToRead;

                // Next -len+1 bytes in the dest are replicated from next source byte.
                // (Interpret len as a negative 8-bit int.)
                //									len ^= 0x0FF;
                //									len += 2;
                byte byteValue = (byte)reader.ReadByte();

                while (numCompressedToRead != 0)
                {
                    destinationBuffer[bufferOffset] = byteValue;
                    bufferOffset++;
                    numCompressedToRead--;
                }
            }
            else if (len == 128)
            {
                // Do nothing
            }

            return numUncompressedWritten;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions