1

そこで、
C++でDIRECTXAPIを試してみてRECT、UIウィンドウや別のスプライトなど、親内でクリッピングを行うスプライトインターフェイスを設計しています。

これは、後でUIウィンドウのスクロール機能を作成するためです。

私が達成しようとしていることを示すには、画像が適切だと思います。
クリッピング

クリッピング計算を行うには、少なくとも4つの変数/構造体が必要です(私が識別できるものから)。

  • RECT:「クリッピング」境界
  • D3DXIMAGE_INFO:幅や高さなどの画像情報が含まれています
  • スプライトの位置
  • Draw RECTLPD3DXSPRITE->Draw()関数用。基本的に、画像のどの部分を描画する必要があります。ここで「クリッピング」が行われます。

さて、インターフェース全体とその内部の仕組み、そしてそれが変数をどのように処理するかを示したら、混乱するだろうと思います。

したがって、代わりに、
このコードは、現在クリッピングを計算している方法を示しています。

void ClippingCalculation(){
    RECT parent_bounds, draw_rect;
    D3DXVECTOR2 sprite_position; //Relative to parent rect
    D3DXIMAGE_INFO img_info;

    //Fill image info
    D3DXGetImageInfoFromFile("image.png", &img_info); //Image is 100x100px

    //Define the rects
    SetRect(&parent_bounds, 0, 0, 200, 200);
    SetRect(&draw_rect, 0, 0, img_info.Width, img_info.Height); //Draw the entire image by default

    //Give it a position that makes a portion go out of parent bounds
    sprite_position = D3DXVECTOR2(150, 150); // 3/4 of the entire image is out of bounds

    //Check if completely within bounds
    bool min_x = (sprite_position.x > parent_bounds.left),
         min_y = (sprite_position.y > parent_bounds.top),
         max_x = (sprite_position.x+img_info.Width < parent_bounds.right),
         max_y = (sprite_position.y+img_info.Height < parent_bounds.bottom);

    if(min_x && min_y && max_x && max_y) 
         return; //No clipping needs to be done

    float delta_top = parent_bounds.top - sprite_position.y,
          delta_left = parent_bounds.left - sprite_position.x,    
          delta_bottom = parent_bounds.bottom - sprite_position.y,
          delta_right = parent_bounds.right - sprite_position.x;

    //Check if completely outside bounds
    bool out_left = (delta_left >= img_info.Width),
         out_top = (delta_top >= img_info.Height),
         out_bottom = (delta_bottom >= img_info.Height),
         out_right = (delta_right >= img_info.Width);

    if(out_left || out_top || out_bottom || out_right){ 
         //No drawing needs to be done, it's not even visible
         return; //No clipping
    }

    if(!min_x) draw_rect.left = delta_left;

    if(!min_x) draw_rect.top = delta_top;

    if(!max_x) draw_rect.right = delta_right;

    if(!max_y) draw_rect.bottom = delta_bottom;

    return;
}

私の質問は次のとおりです。

  1. コードにすぐに何か問題がありますか?
  2. それは効率的ですか、それとも非効率的ですか?
    クリッピングは、スプライトが再配置、ロードされるたび、または親が追加されるたびに実行されます。
  3. 数学はしっかりしていますか?別の方法でアプローチする必要がありますか?
  4. それはより良い方法で行われることができますか、そしてあなたは例を提供できますか?
4

1 に答える 1

2

RECTのクリップは、これよりもはるかに簡単です。

clipped_rect.left= MAX(rect_a.left, rect_b.left);
clipped_rect.right= MIN(rect_a.right, rect_b.right);
clipped_rect.top= MIN(rect_a.top, rect_b.top);
clipped_rect.bottom= MAX(rect_a.bottom, rect_b.bottom);

bool completely_clipped= 
    clipped_rect.left>=clipped_rect.right || 
    clipped_rect.bottom>=clipped_rect.top

これは、上昇するにつれてYが増加することを前提としています。MINのプラットフォーム固有のバージョンを見つけるのは非常に簡単でMAX、パフォーマンスが高いはずです。

于 2012-03-04T04:00:43.240 に答える