예제 #1
0
        /// <summary>
        /// Create a pre-rotated sprite sheet from a simple sprite.
        /// This can make a huge difference in graphical performance!
        /// </summary>
        /// <param name="graphic">The image you want to rotate and stamp.</param>
        /// <param name="rotations">The number of rotation frames the final sprite should have.  For small sprites this can be quite a large number (360 even) without any problems.</param>
        /// <param name="frame">If the Graphic has a single row of square animation frames on it, you can specify which of the frames you want to use here.  Default is -1, or "use whole graphic."</param>
        /// <param name="antiAliasing">Whether to use high quality rotations when creating the graphic.  Default is false.</param>
        /// <param name="autoBuffer">Whether to automatically increase the image size to accomodate rotated corners.  Default is false.  Will create frames that are 150% larger on each axis than the original frame or graphic.</param>
        /// <returns></returns>
        public FlxSprite loadRotatedGraphic(string graphicFile, uint rotations = 16, int frame = 1, bool antiAliasing = false, bool autoBuffer = false)
        {
            Texture2D graphic = FlxS.ContentManager.Load <Texture2D> (graphicFile);

            _bakedRotation = 0;
            _pixels        = FlxG.addBitmap(graphic);
            if (Frame >= 0)
            {
                Width = FrameWidth = _pixels.Height;                //  GetRegionHeight();
                int rx = (int)(Frame * Width);
                int ry = 0;
                int fw = _pixels.Width;                //getRegionWidth();
                if (rx >= fw)
                {
                    ry  = (int)((rx / fw) * Width);
                    rx %= fw;
                }
                //_pixels.setRegion (rx + _pixels.getRegionX (), ry + _pixels.getRegionY (), (int)Width, (int)Width);
            }
            else
            {
                Width = FrameWidth = _pixels.Width;           //.getRegionWidth();
            }
            Height = FrameHeight = _pixels.Height;            //getRegionHeight();
            resetHelpers();

            return(this);

            //Create the brush and canvas

            /*
             * int rows = Math.sqrt(Rotations);
             * BitmapData brush = FlxG.addBitmap(Graphic);
             * if(Frame >= 0)
             * {
             *      //Using just a segment of the graphic - find the right bit here
             *      BitmapData full = brush;
             *      brush = new BitmapData(full.Height,full.Height);
             *      uint rx = Frame*brush.Width;
             *      uint ry = 0;
             *      uint fw = full.Width;
             *      if(rx >= fw)
             *      {
             *              ry = (uint)(rx/fw)*brush.Height;
             *              rx %= fw;
             *      }
             *      _flashRect.X = rx;
             *      _flashRect.Y = ry;
             *      _flashRect.Width = brush.Width;
             *      _flashRect.Height = brush.Height;
             *      brush.copyPixels(full,_flashRect,_flashPointZero);
             * }
             *
             * uint max = brush.Width;
             * if(brush.Height > max)
             *      max = brush.Height;
             * if(AutoBuffer)
             *      max *= 1.5;
             * uint columns = FlxU.ceil(Rotations/rows);
             * Width = max*columns;
             * Height = max*rows;
             * string key = String(graphic) + ":" + Frame + ":" + Width + "x" + Height;
             * bool skipGen = FlxG.checkBitmapCache(key);
             * _pixels = FlxG.createBitmap(Width, Height, 0, true, key);
             * Width = FrameWidth = _pixels.Width;
             * Height = FrameHeight = _pixels.Height;
             * _bakedRotation = 360/Rotations;
             *
             * //Generate a new sheet if necessary, then fix up the width and height
             * if(!skipGen)
             * {
             *      int row = 0;
             *      int column;
             *      int bakedAngle = 0;
             *      int halfBrushWidth = brush.Width*0.5;
             *      int halfBrushHeight = brush.Height*0.5;
             *      int midpointX = max*0.5;
             *      int midpointY = max*0.5;
             *      while(row < rows)
             *      {
             *              column = 0;
             *              while(column < columns)
             *              {
             *                      _matrix.identity();
             *                      _matrix.translate(-halfBrushWidth,-halfBrushHeight);
             *                      _matrix.rotate(bakedAngle*0.017453293);
             *                      _matrix.translate(max*column+midpointX, midpointY);
             *                      bakedAngle += _bakedRotation;
             *                      _pixels.draw(brush,_matrix,null,null,null,AntiAliasing);
             *                      column++;
             *              }
             *              midpointY += max;
             *              row++;
             *      }
             * }
             * FrameWidth = FrameHeight = Width = Height = max;
             * resetHelpers();
             * if(AutoBuffer)
             * {
             *      Width = brush.Width;
             *      Height = brush.Height;
             *      centerOffsets();
             * }
             * return this;
             */
        }