SDL_Rectの共通部分を求める

SDLの話。
要は二つの矩形の共通部分だけを描画したいと思った訳なのですが、これがSDL_SetClipRectを使うとうまくいかないのです。Clipされた領域の左上座標がその後で描画するときの(0,0)になっていると思いきや、そうならないときもあります(後で描画する領域の左上の座標が関係あるっぽい)。SDLのバグなのか僕がものすごいポカをしているのか分からないのですが、深く考えるのも面倒だったので、二つの矩形の共通部分を求めて、そこだけを描画することで解決しました。

SDL_Rect intersection(SDL_Rect* src, SDL_Rect* dst)
{
	SDL_Rect newrect = { 0, 0, 0, 0 };
	if (src == NULL || dst == NULL || src->x+src->w < dst->x || dst->x+dst->w < src->x || src->y+src->h < dst->y || dst->y+dst->h < src->y)
		return newrect;
	if (src->x < dst->x) {
		newrect.x = dst->x;
		newrect.w = src->x+src->w < dst->x+dst->w ? src->x+src->w-dst->x : dst->w;
	} else {
		newrect.x = src->x;
		newrect.w = dst->x+dst->w < src->x+src->w ? dst->x+dst->w-src->x : src->w;
	}
	if (src->y < dst->y) {
		newrect.y = dst->y;
		newrect.h = src->y+src->h < dst->y+dst->h ? src->y+src->h-dst->y : dst->h;
	} else {
		newrect.y = src->y;
		newrect.h = dst->y+dst->h < src->y+src->h ? dst->y+dst->h-src->y : src->h;
	}
	
	return newrect;
}

JavaのRectやCocoaのNSRectには共通部分(論理積)を求める関数(メソッド)が既にあるってのにSDLにはないっぽいようなので、自分で書きました。