中を塗りつぶさない矩形、つまり矩形の外枠部分の描画方法。
水平線、垂直線は矩形の幅や高さを1として描画してやればよい。
typedef struct { int left; int top; int right; int bottom; } RECT; SDL_Surface* pSurface; void drawRect(RECT& rect, int r, int g, int b, int a) { SDL_Rect src; src.x = rect.left; src.y = rect.top; src.w = rect.right - rect.left -1; src.h = rect.bottom - rect.top -1; SDL_Rect dst; dst.x = src.x; dst.y = src.y; dst.w = 1; dst.h = src.h; SDL_FillRect( pSurface, &dst, SDL_MapRGBA(pSurface->format,r,g,b,a) ); dst.x = src.x; dst.y = src.y + src.h; dst.w = src.w; dst.h = 1; SDL_FillRect( pSurface, &dst, SDL_MapRGBA(pSurface->format,r,g,b,a) ); dst.x = src.x + src.w; dst.y = src.y; dst.w = 1; dst.h = src.h; SDL_FillRect( pSurface, &dst, SDL_MapRGBA(pSurface->format,r,g,b,a) ); dst.x = src.x; dst.y = src.y; dst.w = src.w; dst.h = 1; SDL_FillRect( pSurface, &dst, SDL_MapRGBA(pSurface->format,r,g,b,a) ); }
自作ライブラリでは矩形を4点の座標で表現しているが、SDLの矩形は左上の点と幅、高さで表現しているので、関数の頭で変換してる。
また描画するときには幅、高さを-1しておかないと、右側と下側の描画位置が矩形の外になってしまうので注意。