Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="b"></param>
        /// <param name="storeInVideoMemory"></param>
        /// <param name="keyColor"></param>
        /// <returns></returns>
        public ISprite LoadSprite(BitmapBuilder b, bool storeInVideoMemory, Color keyColor)
        {
            if (!AllowLoadTextures)
            {
                throw new InvalidOperationException("Loading sprites is only allowed inside the LoadSprites event");
            }
            Format format = Format.A8R8G8B8;

            if (keyColor == System.Drawing.Color.Transparent)
            {
                format = Format.R8G8B8;
            }

            Bitmap bmp = new Bitmap(b.GetStream());
            int    w = (int)(b.Width * XRatio), h = (int)(b.Height * YRatio);

            if (keyColor != Color.Transparent)
            {
                bmp.MakeTransparent(keyColor);
            }
            bmp = new Bitmap(bmp, w, h);
            using (MemoryStream s = new MemoryStream())
            {
                bmp.Save(s, System.Drawing.Imaging.ImageFormat.Bmp);
                bmp.Dispose();
                s.Seek(0, SeekOrigin.Begin);

                return(new SDXSprite(Texture.FromStream(Device, s, w, h, 0, Usage.None, format,
                                                        storeInVideoMemory ? Pool.Managed : Pool.Managed, Filter.None, Filter.None, keyColor.ToArgb())));
            }
            //return new SDXSprite(Texture.FromMemory(Device, b.BitmapData, (int)b.Width, (int)b.Height, 0, Usage.None, format,
            //		storeInVideoMemory ? Pool.Default : Pool.Managed, Filter.None, Filter.None, keyColor.ToArgb()));
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="playerRemapInfo"></param>
        public void Remap(ColorRemapInfo[] playerRemapInfo)
        {
            //System.Diagnostics.Debug.Assert(RemappedCopies != null, "Trying to remap a frame twice!");
            if (RemappedCopies != null)
            {
                return;
            }
            RemappedCopies = new AnimationFrame[playerRemapInfo.Length];

            for (int i = 0; i < playerRemapInfo.Length; ++i)
            {
                //System.Console.WriteLine("Remapping " + FileName + " to " + playerRemapInfo[i] + "(" + i + ")");
                RemappedCopies[i] = ColorRemapper.Remap(this, playerRemapInfo[i]);
            }

            if (Sprite != null)
            {
                System.Diagnostics.Debug.Assert(false);
                Sprite.Dispose();
                Sprite = null;
            }
            // don't dispose of the BB explicitly
            // A remapped frame may use this
            BitmapBuilder = null;
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="b"></param>
        /// <param name="storeInVideoMemory"></param>
        /// <param name="keyColor"></param>
        /// <returns></returns>
        public ISprite LoadSprite(BitmapBuilder b, bool storeInVideoMemory, Color keyColor)
        {
            Bitmap bitmap = new Bitmap(b.GetStream());

            bitmap.MakeTransparent(keyColor);
            return(new Sprite(bitmap));
        }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="device"></param>
        /// <param name="videoMemory"></param>
        /// <returns></returns>
        public ISprite GetSprite(IDevice device, bool videoMemory)
        {
            //System.Diagnostics.Debug.Assert(RemappedCopies == null, "Getting original Sprite for remapped frame " + FileName);
            if (Sprite == null)
            {
                Sprite = device.LoadSprite(BitmapBuilder, videoMemory, KeyColor);
                System.Diagnostics.Debug.Assert(Sprite != null);
                BitmapBuilder.Dispose();
                BitmapBuilder = null;
            }

            return(Sprite);
        }
Example #5
0
        public void IndexForWithCustomKeyReturnCorrectKeys()
        {
            // arrange
            var dto     = TestModels.GetSimpleDto();
            var builder = new BitmapBuilder <DtoWithNestedDto>();

            // act
            builder.IndexFor("testKey", simpleDto => simpleDto.NestedDto.Boolean);

            var builtKeys = builder.Keys();

            // assert
            builtKeys.Should().BeEquivalentTo("testKey");
        }
Example #6
0
        /// <summary>
        /// Create a deep (enough) copy of another frame
        /// </summary>
        /// <param name="old"></param>
        public AnimationFrame(AnimationFrame old)
        {
            // TRYTRY: do we need to copy this? That can probably be optimized:
            // 1) if remap actually occurs, create a new BB in remap
            // 2) if no remap occurs, use the original BB
            // see ColorRemapper.Remap
            BitmapBuilder = null;            // new BitmapBuilder(old.BitmapBuilder);
            Offset        = old.Offset;
            KeyColor      = old.KeyColor;
            RawKeyColor   = old.RawKeyColor;
#if DEBUG
            FileName = old.FileName;
#endif
            // this is null for a remapped copy
            RemappedCopies = null;
        }
Example #7
0
        public void ForDtoWithNestedDtoAlwaysReturnCorrectKeys()
        {
            // arrange
            var dto     = TestModels.GetDtoWithNestedDto();
            var builder = new BitmapBuilder <DtoWithNestedDto>();

            // act
            builder.IndexFor(dto, simpleDto => simpleDto.Integer);
            builder.IndexFor(dto, simpleDto => simpleDto.Double);
            builder.IndexForClass(dto, simpleDto => simpleDto.NestedDto, nestedDto => nestedDto.Boolean);

            var builtKeys = builder.Keys();

            // assert
            builtKeys.Should().BeEquivalentTo(new List <string>
            {
                "Integer.123", "Double.-2,5", "NestedDto.Boolean.False"
            });
        }
Example #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static BitmapBuilder FromFile(string filename)
        {
            Stream        inStream = File.OpenRead(filename);
            BitmapBuilder ret      = null;

            try
            {
                ret = FromStream(inStream);
            }
            catch (FormatException e)
            {
                throw new FormatException(filename + ": " + e.Message);
            }
            finally
            {
                inStream.Close();
            }

            return(ret);
        }
Example #9
0
        public void ForPrimitiveDtoAlwaysReturnCorrectKeys()
        {
            // arrange
            var dto     = TestModels.GetSimpleDto();
            var builder = new BitmapBuilder <SimpleDto>();

            // act
            builder.IndexFor(dto, simpleDto => simpleDto.Integer);
            builder.IndexFor(dto, simpleDto => simpleDto.Str);
            builder.IndexFor(dto, simpleDto => simpleDto.Boolean);
            builder.IndexFor(dto, simpleDto => simpleDto.Float);

            var builtKeys = builder.Keys();

            // assert
            builtKeys.Should().BeEquivalentTo(new List <string>
            {
                "Integer.123", "Str.string", "Boolean.True", "Float.1,5"
            });
        }
Example #10
0
        private void btnRender_Click(object sender, EventArgs e)
        {
            _timer.Interval = 1000;
            _timer.Tick += timer_Tick;
            _timer.Start();
            btnRender.Enabled = false;

            ThreadStart start = delegate {
                var camera = new GeneralCamera3D {
                    Position = new Vector3D(0, 7, -50),
                    FOV = 60 * System.Math.PI / 180
                };

                _canvas.Clear();
                _engine.Render(_scene, camera, _canvas);
                var builder = new BitmapBuilder();
                Bitmap bmp = builder.BuildBitmap(_canvas, 1);
                bmp.Save(string.Format(@"d:\temp\result.png"), ImageFormat.Png);
            };
            start.BeginInvoke(null, null);
        }
Example #11
0
        void timer_Tick(object sender, EventArgs e)
        {
            var builder = new BitmapBuilder();
            Bitmap bmp = builder.BuildBitmap(_canvas, 1);
            pictureBox1.Image = bmp;

            pictureBox1.Refresh();
        }
Example #12
0
        private static BitmapBuilder RawFromStream(Stream s)
        {
            BinaryReader r = new BinaryReader(s);

            //
            // Read the PCX header
            //

            // Manufacturer, must be 10 (ZSoft)
            int manufacturer = r.ReadByte();

            if (manufacturer != 10)
            {
                throw new FormatException("PCX specifies manufacturer " + manufacturer + ", which is invalid");
            }

            // Skip version
            r.ReadByte();

            // Encoding, must be 1 (RLE)
            int encoding = r.ReadByte();

            if (encoding != 1)
            {
                throw new FormatException("PCX specifies encoding " + encoding + ", which is invalid");
            }

            byte bitsPerPixel = r.ReadByte();

            if (bitsPerPixel != 1 && bitsPerPixel != 2 && bitsPerPixel != 4 && bitsPerPixel != 8)
            {
                throw new FormatException("PCX specifies " + bitsPerPixel + " bits per pixel, which is invalid");
            }

            ushort xMin = r.ReadUInt16();
            ushort yMin = r.ReadUInt16();
            ushort xMax = r.ReadUInt16();
            ushort yMax = r.ReadUInt16();

            // Skip resolution
            r.ReadUInt32();

            // Skip short palette if > 4 bpp

            /*if (bitsPerPixel <= 4)
             * {
             *
             * }
             * else*/
            s.Seek(48, SeekOrigin.Current);

            // Skip reserved
            r.ReadByte();

            byte nPlanes      = r.ReadByte();
            int  bytesPerLine = r.ReadUInt16();

            // Go to start of image data
            s.Seek(128, SeekOrigin.Begin);

            //
            // Construct the bitmap
            //
            BitmapBuilder b = new BitmapBuilder((ushort)(nPlanes * bitsPerPixel),
                                                (uint)(xMax - xMin + 1),
                                                (uint)(yMax - yMin + 1));

            // Palette will be inserted later, as it's at the end of the PCX
            long i = b.DataLocation;
            //
            // Run-Length-Decode the PCX data and insert as bitmap data
            //
            byte data = 0;

            // we have either one (palette) or three (rgb) planes in a bitmap
            uint planeFactor = (nPlanes == 1U) ? 1U : 3U;

            for (int y = 0; y < b.Height; ++y)
            {
                int count = 0;

                for (int iPlane = 0; iPlane < nPlanes; ++iPlane)
                {
                    i = b.DataLocation + (b.Height - y - 1) * b.BytesPerLine;
                    for (int x = 0; x < bytesPerLine; ++x)
                    {
                        // we are still "running"
                        if (count > 0)
                        {
                            --count;
                        }
                        // read new data and check if it's a count
                        else if (((data = r.ReadByte()) & 0xC0) == 0xC0)
                        {
                            // one byte will be processed right now, so subtract 1
                            count = (data & 0x3F) - 1;
                            // the data to be repeated is the next byte
                            data = r.ReadByte();
                        }

                        // ignore padding
                        if (x >= b.Width)
                        {
                            continue;
                        }

                        // we can do at most 3 "planes" in a bitmap, ignore the others
                        if (iPlane < 3)
                        {
                            b.BitmapData[i + (planeFactor - iPlane - 1) % planeFactor] = data;
                            i += planeFactor;
                        }
                    }
                }
            }

            // if there's a palette in the bmp, then there must be one in the pcx
            if (b.PaletteSize > 0)
            {
                // HACKHACK: this should actually be done at the start instead of
                //           skipping the palette there, since this destroys the
                //           possibility of a seek-less implementation
                if (b.BitsPerPixel <= 4)
                {
                    s.Seek(16, SeekOrigin.Begin);
                }
                else
                {
                    s.Seek(-769, SeekOrigin.End);
                    if (r.ReadByte() != 12)
                    {
                        throw new FormatException("Something wrong with the palette");
                    }
                }

                // read palette and put into bitmap data
                b.ReadPaletteFromPCX(r);
            }

            return(b);
        }