3.1:矩形區(qū)域SDL_Rect。
typedef struct{
Sint16 x, y;
Uint16 w, h;
} SDL_Rect;
因為顯示器通常是矩形的,所以,矩形是計算機圖形學(xué)中最基本的操作區(qū)域單元。這個結(jié)構(gòu)很簡單,x和y是矩形的左上角坐標(biāo)。x從左到右增加;y從上到下增加。左上角的坐標(biāo)就是(0,0)——SDL中就是這樣的。w是矩形的寬,h是矩形的高。Sint16 x, y;
Uint16 w, h;
} SDL_Rect;
3.2:進(jìn)一步了解SDL_BlitSurface()。
int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);
4個參數(shù)都是指針——2個SDL_Surface指針,2個SDL_Rect指針。src是源面,也就是被blit的面;dst是目的面,也就是源面被blit到的面。srcrect是源面上的一個矩形區(qū)域,實際上,正是這個矩形區(qū)域被blit,如果是空指針,則整個源面被blit;dstrect雖然是個矩形區(qū)域指針,但是實際上只用到了這個矩形左上角坐標(biāo)的數(shù)據(jù)。所以,實際上,它是源面被blit到目的面上的坐標(biāo)。如果是空指針,則被blit到目的面的左上角(0,0)。3.3:為surface類增加新的方法。
class DisplaySurface
{
public:
bool blit(int at_x, int at_y) const;
bool blit(int at_x, int at_y, int from_x, int from_y, int w, int h, int delta_x = 2, int delta_y = 2) const;
};
我們重載了blit()方法。{
public:
bool blit(int at_x, int at_y) const;
bool blit(int at_x, int at_y, int from_x, int from_y, int w, int h, int delta_x = 2, int delta_y = 2) const;
};
bool DisplaySurface::blit(int at_x, int at_y) const
{
SDL_Rect offset;
offset.x = at_x;
offset.y = at_y;
if ( SDL_BlitSurface(pSurface, 0, pScreen, &offset) < 0 )
return false;
else return true;
}
這個方法,讓整個源面被blit到目的面的(at_x,at_y)坐標(biāo)上。{
SDL_Rect offset;
offset.x = at_x;
offset.y = at_y;
if ( SDL_BlitSurface(pSurface, 0, pScreen, &offset) < 0 )
return false;
else return true;
}
bool DisplaySurface::blit(int at_x, int at_y,
int from_x, int from_y, int w, int h,
int delta_x, int delta_y) const
{
SDL_Rect offset;
offset.x = at_x - delta_x;
offset.y = at_y - delta_y;
SDL_Rect dest;
dest.x = from_x - delta_x;
dest.y = from_y - delta_y;
dest.w = w + delta_x*2;
dest.h = h + delta_y*2;
if ( SDL_BlitSurface(pSurface, &dest, pScreen, &offset) < 0 )
return false;
else return true;
}
這個函數(shù),把源面上,一個左上角坐標(biāo)是(from_x,from_y),寬為w,高為h的矩形區(qū)域,blit到目的面的(at_x,at_y)坐標(biāo)上。int from_x, int from_y, int w, int h,
int delta_x, int delta_y) const
{
SDL_Rect offset;
offset.x = at_x - delta_x;
offset.y = at_y - delta_y;
SDL_Rect dest;
dest.x = from_x - delta_x;
dest.y = from_y - delta_y;
dest.w = w + delta_x*2;
dest.h = h + delta_y*2;
if ( SDL_BlitSurface(pSurface, &dest, pScreen, &offset) < 0 )
return false;
else return true;
}
要說明delta_x和delta_y的作用,我們先思考一個問題:動畫效果是如何實現(xiàn)的。
我們假設(shè)有個作為背景的surface,名為back,我們要讓一個名為front的surface在back上動起來。顯然,至少有兩種方法:
1) 把front blit到back上,把back blit到screen上,flit screen,顯示出原來的圖像;
把back面“搽干凈”;改變front的坐標(biāo),將改變坐標(biāo)后的front blit到back上,把back blit到screen上,flit screen,顯示出更新后的圖像。
2) 把back blit到screen上,flit screen,首先顯示出back圖像;
先把back面“搽干凈”;把front blit到screen上(back是一直保持在screen上的,而front會被blit到back的上層),flit screen,顯示出更新后的圖像。
因為,第二種方法把所有的surface都直接往screen上blit,思路更為簡單,所以,我們先討論這種方法。
而對于“搽干凈”這個概念,又有兩種思路:
1) 全部back更新;
2) 更新back上“被弄臟”的部分。
實際上,當(dāng)前的電腦速度對在平面上的blit速度問題已經(jīng)不再是問題了。但是,在不久之前,程序員們還在為了計算機圖形的實現(xiàn)速度而絞盡腦汁。blit一部分應(yīng)該是比blit全部圖像要快的。所以,這個重載的blit()方法多用于對于back的blit。delta_x和delta_y是為了保證blit的back部分,比front大那么一點點。不然的話——實際上大家可以把delta_x和delta_y設(shè)置為0看看是什么效果。