Example #1
0
        //Sprite that refernces another sprite's texture
        public SpriteAccess(SpriteAccess NewParentSprite, float NewX, float NewY, float NewZ)
        {
            this.ParentSprite = (SpriteAccess)NewParentSprite;
            this.Image        = null;
            this.ParentDevice = this.ParentSprite.ParentDevice;
            this.MaskColor    = this.ParentSprite.MaskColor;

            this.textures       = this.ParentSprite.textures;
            this.CollisionRects = this.ParentSprite.CollisionRects;

            this.Initialize(NewX, NewY, NewZ, ParentSprite.Width, ParentSprite.Height, ParentSprite.SheetWidth, ParentSprite.SheetHeight);
        }
Example #2
0
        //Loads the texture into a sprite
        private void SetTextureToScreen()
        {
            // Load texture from other sprite's texture
            if (this.ParentSprite == null)
            {
                uint f                  = 0; //Frame of animation
                uint x                  = 0; //Frame x reltive to frame
                uint y                  = 0; //Frame y reletive to frame
                uint SourceXStart       = 0; //Frame's starting x reletive to sprite sheet
                uint SourceYStart       = 0; //Frame's starting y reletive to sprite sheet
                uint DestinationY       = 0;
                uint DestinationX       = 0;
                uint AnimsPerSheetWidth = (uint)(this.SheetWidth / this.Width);

                //Load the whole texture till we can chop it up
                Direct3D.Texture SpriteSheet = Direct3D.TextureLoader.FromFile
                                               (
                    this.ParentDevice, this.Image, 0, 0, 0, Direct3D.Usage.None,
                    Direct3D.Format.A8B8G8R8, Direct3D.Pool.Managed,
                    Direct3D.Filter.Linear, Direct3D.Filter.Linear, this.MaskColor.ToArgb()
                                               );

                //Chop this sprite sheet up into frames
                //Create a blank bitmap and use it as a template for creating a texture
                System.Drawing.Bitmap SingleFrameBackground = new System.Drawing.Bitmap(this.Width, this.Height);

                uint[,] DestinationPixels;
                uint[,] SourcePixels = (uint[, ])SpriteSheet.LockRectangle(typeof(uint), 0, new Rectangle(0, 0, this.SheetHeight, this.SheetWidth), Direct3D.LockFlags.None, this.SheetHeight, this.SheetWidth);
                this.textures        = new Direct3D.Texture[this.AnimCount];

                //For each frame of animation
                for (f = 0; f < this.AnimCount; f++)
                {
                    //Get this frame's starting y
                    if (f >= AnimsPerSheetWidth)
                    {
                        SourceYStart = (uint)(f / AnimsPerSheetWidth);
                    }

                    //Get this frame's starting x
                    SourceXStart = (uint)(f - (SourceYStart * AnimsPerSheetWidth));

                    SourceYStart *= (uint)this.Height;
                    SourceXStart *= (uint)this.Width;

                    DestinationY = 0;
                    DestinationX = 0;

                    //Create the texture's background
                    this.textures[f]  = new Direct3D.Texture(this.ParentDevice, SingleFrameBackground, Direct3D.Usage.None, Direct3D.Pool.Managed);
                    DestinationPixels = (uint[, ]) this.textures[f].LockRectangle(typeof(uint), 0, new Rectangle(0, 0, this.Height, this.Width), Direct3D.LockFlags.None, this.Height, this.Width);

                    //For this frame's x
                    for (x = SourceXStart; x < SourceXStart + this.Width; x++)
                    {
                        //For this frame's y
                        for (y = SourceYStart; y < SourceYStart + this.Height; y++)
                        {
                            DestinationPixels[DestinationY, DestinationX] = SourcePixels[y, x];
                            DestinationY++;
                        }
                        DestinationX++;
                        DestinationY = 0;
                    }

                    this.textures[f].UnlockRectangle(0);
                }

                SpriteSheet.UnlockRectangle(0);
                SpriteSheet.Dispose();

                //Create masks for each frame of animation
                this.CollisionRects = new CollisionRectAccess(this.ParentDevice, this.textures, this.Verticies);
            }
        }