0

Allegro グラフィック ライブラリからこの関数を複製しようとしています。

void stretch_blit(BITMAP *source, BITMAP *dest, int source_x, 
                  int source_y, int source_width, int source_height, 
                  int dest_x, dest_y, dest_width, dest_height); 

http://www.allegro.cc/manual/api/blitting-and-sprites/stretch_blit

これは、AS3 の BitmapData.draw() メソッドの Rectangle 引数と Matrix 引数の間で機能が重複しているため、Flash ではやや困難です。

これは私がこれまでに持っているものです。ほとんどの場合しか機能せず、信じられないほど非効率的です (ピクセルを 2 回サンプリングする必要があるため)。

function stretch_blit(src:BitmapData, dest:BitmapData, source_x:int, source_y:int, 
   source_width:int, source_height:int, dest_x:int, dest_y:int, 
   dest_width:int, dest_height:int):void {
     var tmp:BitmapData = new BitmapData(dest_width, dest_height, true);
     var scalex:Number = dest_width/source_width;
     var scaley:Number = dest_height/source_height;
     var matrix:Matrix = new Matrix();
     var matrix2:Matrix = new Matrix();
     matrix.scale(scalex, scaley);
     matrix.translate(source_x, -source_y);
     tmp.draw(src, matrix, null, null, new Rectangle(0, 0, dest_width, dest_height));
     matrix2.translate(dest_x-source_x, dest_y-source_y);
     dest.draw(tmp, matrix2);
}

これを行うより良い方法はありますか?

4

3 に答える 3

1

描画する 5 番目のパラメーターがソース rect だと思ったので、source_width と source_height のことですか?

とにかく、これを試してください:

function stretch_blit(src:BitmapData, dest:BitmapData, source_x:int, source_y:int,
source_width:int, source_height:int, dest_x:int, dest_y:int, dest_width:int, dest_height:int):void {
     var scalex:Number = dest_width/source_width;
     var scaley:Number = dest_height/source_height;
     var matrix:Matrix = new Matrix();
     matrix.scale(scalex, scaley);
     matrix.translate(dest_x, dest_y);
     dest.draw(src, matrix, null, null, new Rectangle(source_x, -source_y, source_width, source_height));
}

また、反転した y 軸 (フラッシュ スタイルの y 軸) を使用している場合は、-source_y から - を削除します。

于 2010-04-12T20:33:49.697 に答える
0

ありがとうございますが、使ってみると何も描かれていません。これは、clipRectのxとyの位置がMatrixの位置によってオフセットされていることと関係があると思います。代わりにこれを使用する場合:

    function stretch_blit(src:BitmapData, dest:BitmapData, source_x:int, source_y:int,
    source_width:int, source_height:int, dest_x:int, dest_y:int, dest_width:int, dest_height:int):void {
         var scalex:Number = dest_width/source_width;
         var scaley:Number = dest_height/source_height;
         var matrix:Matrix = new Matrix();
         matrix.scale(scalex, scaley);
         matrix.translate(dest_x, dest_y);
         dest.draw(src, matrix, null, null, new Rectangle(source_x+dest_x, source_y+dest_y, source_width, source_height));
    }

次に、ソースビットマップの正しいクリップが「dest」の正しいX値に描画されますが、間違ったY値に描画されます。

于 2010-04-12T20:54:41.910 に答える
0

この質問はやや古いですが、実際の解決策を投稿すると思いました。これは、2 つの汎用 BitmapData オブジェクト用です。sx、sy、sw、および sh は、それぞれソースの x、y、幅、および高さを表します。

function drawImage(src:BitmapData, dest:BitmapData, sx:int, sy:int, sw:int, sh:int, dx:int, dy:int, dw:int, dh:int):void
{
    var scaleX:Number = dw / sw, scaleY:Number = dh / sh;
    var m:Matrix = new Matrix();
    m.scale(scaleX, scaleY);
    m.translate(dx, dy);
    var temp:BitmapData = new BitmapData(sw, sh, src.transparent, 0);
    temp.copyPixels(src, new Rectangle(sx, sy, sw, sh), new Point());
    dest.draw(temp, m);
}
于 2012-07-21T19:04:17.867 に答える