構建SDL surface類

1:構建普通的surface類。

        在所有的surface裏面,只有screen surface是最特殊的。因爲第一,screen surface只有一個;第二,其他所有的普通surface都必須被blit到screen surface上,通過flip screen surface才能顯示出來。所以,我們可以認爲普通的surface是“依賴”於一個screen surface的。所以,考慮在構建surface的時候,除了需要裝載的bmp文件,還需要指定其所依賴的screen surface。
class DisplaySurface
{
private:
    
string fileName;
    SDL_Surface
* pSurface;
    SDL_Surface
* pScreen;
public:
    DisplaySurface(
string file_name, const ScreenSurface& screen);
    
~DisplaySurface();
    SDL_Surface
* point() const;
    
bool blit() const;
};

2:surface的類方法。
DisplaySurface::DisplaySurface(std::string file_name, const ScreenSurface& screen):
fileName(file_name)
{
    pSurface 
= SDL_LoadBMP(file_name.c_str());
    
if ( pSurface == 0 )
        
throw SDL_GetError();
    pScreen 
= screen.point();
}
構造函數。我們指定一個bmp文件,和一個screen surface對象來構造surface。注意,這裏我們用到了C++的string。我們前面說過,string的方法c_str()用於把C++的string對象轉化爲C風格字符串,而這正是SDL的函數所需要的。
DisplaySurface::~DisplaySurface()
{
    SDL_FreeSurface(pSurface);
}
雖然load的bmp可以在SDL_Quit()的時候自動釋放,不過也許我們也會有需要手動釋放的時候。比如,裝載一張圖片,進行某種處理和轉換後,保留處理後的,就可以把原來用於處理的對象釋放掉了。
SDL_Surface* DisplaySurface::point() const
{
    
return pSurface;
}
返回對象中,bmp的SDL_Surface指針。同樣爲了用於SDL庫的函數。
bool DisplaySurface::blit() const
{
    
if ( SDL_BlitSurface(pSurface, 0, pScreen, 0< 0 )
        
return false;
    
else return true;
}
把surface blit到screen surface上面,左上角重合。到這裏,我們前面用到的對於surface的所有操作,都包含到類方法中了。
發佈了28 篇原創文章 · 獲贊 6 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章