描画キャッシュ

描画のキャッシュって、いまいちどうやるかわからなかったり。


簡単に考えるのであれば、

class DrawCache{
public:
    struct DrawCacheObject{
        int         type;
        int         priority;
        Image       dst;
        int         dx;
        int         dy;
        int         dw;
        int         dh;
        Image       src;
        uint32      color;
    };
    
    DrawCache() : _count( 0 ){}
    
    void DrawImage( Image dst , int dx , int dy , Image src , int priority ){
        if( _count < OBJ_MAX ){
            _cache[ _count ].type       = 0;
            _cache[ _count ].priority   = priority;
            _cache[ _count ].dst        = dst;
            _cache[ _count ].dx         = dx;
            _cache[ _count ].dy         = dy;
            _cache[ _count ].src        = src;
            _count++;
        }
    }
    
    void FillRect( Image dst , int dx , int dy , int dw , int dh ,
                    uint32 color , int priority ){
        if( _count < OBJ_MAX ){
            _cache[ _count ].type       = 1;
            _cache[ _count ].priority   = priority;
            _cache[ _count ].dst        = dst;
            _cache[ _count ].dx         = dx;
            _cache[ _count ].dy         = dy;
            _cache[ _count ].dw         = dw;
            _cache[ _count ].dh         = dh;
            _cache[ _count ].color      = color;
            _count++;
        }
    }
    
    // プライオリティに基づいてソート
    void sort();
    
    void Flush(){
        sort();
        for( int i = 0 ; i < _count ; i++ ){
            DrawCacheObject* p = &_cache[ i ];
            switch( p->type ){
            case 0:
                Graphics::DrawImage( p->dst , p->dx , p->dy , p->src );
                break;
            case 1:
                Graphics::FillRect( p->dst , p->dx , p->dy , p->dw , p->dh , p->color );
                break;
            }
        }
        _count = 0;
    }
    
private:
    enum{
        OBJ_MAX = 100 ,
    }
    DrawCacheObject     _cache[ OBJ_MAX ];
    
    int                 _count;
};

こうすればいいんだけれども、ライブラリ側にこのクラスを用意している場合、ユーザが独自の描画関数を実装出来なくなる。
あと、構造体に必要な変数を全て詰め込んでいるから、メモリを多く消費してしまう。