作者:龍飛 2.1:準(zhǔn)備工作。 找一張*.bmp格式的圖片。我在例子中將使用640*480大小的圖片。如果你在windows下面,你可以打開畫圖程序自己簡單的畫一張,或者將其他格式的圖片另存為bmp。然后將圖片名字修改為helloworld.bmp(當(dāng)然,你也可以在程序的相應(yīng)部分修改為你目標(biāo)圖片的名字。),這是我們將要顯示的圖片。 2.2:創(chuàng)建一個SDL的執(zhí)行窗口。 我們討論過,SDL是跨平臺的,它的設(shè)計(jì)者希望使用SDL的源程序不要依賴于具體平臺,甚至具體的GUI和窗口管理器。在前面一節(jié)中,我們已經(jīng)簡單使用了SDL_SetVideoMode(),在這里,我們對它做進(jìn)一步的介紹——使用這個函數(shù)實(shí)際上遇到的問題會比我們預(yù)想中涉及到的問題多,換句話說,這里的介紹仍然是不完整的。我們當(dāng)前的目的,只是為了簡單的顯示一張BMP位圖。 SDL_Surface *SDL_SetVideoMode(int width, int height, int bitsperpixel, Uint32 flags);
在這里,我們使用的flag(s)仍然是SDL_SWSURFACE。它的作用是說明所建立的surface是儲存在系統(tǒng)內(nèi)存中的。實(shí)際上,SDL_SWSURFACE是一個“偽位標(biāo)”,如果你讀出它的值,會發(fā)現(xiàn)其實(shí)是0!這意味著任何其他位標(biāo)(以及|組合)與SDL_SWSURFACE的&結(jié)果都是0。這個事實(shí)的另外一層含義是,surface的數(shù)據(jù)“至少”會被儲存在系統(tǒng)內(nèi)存中——對立面的意思是,這些數(shù)據(jù)有可能儲存在顯存中(指定使用顯存儲存數(shù)據(jù)的位標(biāo)是SDL_HWSURFACE,它的值是1)。這個函數(shù)的返回值是一個SDL_Surface的結(jié)構(gòu)指針。如果返回是空指針(C中習(xí)慣用NULL,而C++標(biāo)準(zhǔn)將空指針表示為0),則表示這個函數(shù)調(diào)用失敗了。我們可以通過SDL_GetError()獲得異常的原因。SDL_Surface結(jié)構(gòu)包含了一個surface的數(shù)據(jù)結(jié)構(gòu),包括寬,高和每個像素點(diǎn)的具體顏色等等,我們也放在后面具體討論。這里,我們還是直接把SDL_Surface看成一個類,這個函數(shù)返回一個SDL_Surface類對象的指針。 width和height是你希望建立的窗口的寬與高。如果值為0,則建立與你當(dāng)前桌面等寬高的窗口。bitsperpixel是這個窗口的顏色位深。當(dāng)前的硬件環(huán)境下,相信你的桌面也是32位色的。如果這個值為0,則所建立的窗口使用你當(dāng)前桌面的位深。 我們試圖建立一個640*480大小的,32位色的窗口。并且讓返回的surface值儲存在系統(tǒng)內(nèi)存里。(后面會介紹使用顯存的方法。)需要注意的是,我們必須記下這個返回的surface的指針,因?yàn)樗械膱D像操作,最后都是通過修改這個surface的數(shù)據(jù)作用在顯示這個surface的窗口上,最終呈現(xiàn)在我們眼前的。 const int SCREEN_WIDTH = 640; // 0 means use current width.
const int SCREEN_HEIGHT = 480; // 0 means use current height. const int SCREEN_BPP = 32; // 0 means use current bpp. const Uint32 SCREEN_FLAGS = SDL_SWSURFACE; // SDL_SWSURFACE == 0,surface in system memory. SDL_Surface* pScreen = 0; pScreen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SCREEN_FLAGS); // Creat a SDL window, and get the window's surface. try { if ( pScreen == 0 ) throw SDL_GetError(); } catch ( const char* s ) { std::cerr << "SDL_SetVideoMode() failed!\n" << s << std::endl; SDL_Quit(); return -1; }
SDL_Surface *SDL_LoadBMP(const char *file);
這個函數(shù)使用C風(fēng)格字符串的形參,這意味著如果我們使用std::string objName傳值的時候,需要使用objName.c_str()(請注意objName.data()沒有'/0'),把std::string類轉(zhuǎn)化為C風(fēng)格字符串。這個函數(shù)把一個BMP位圖轉(zhuǎn)化成為SDL的surface數(shù)據(jù)結(jié)構(gòu)方式(SDL_Surface結(jié)構(gòu)),儲存在系統(tǒng)內(nèi)存中(我沒找到任何信息可以說明能直接儲存到顯存中),并返回這個surface的指針。如果返回的指針為空,說明函數(shù)調(diào)用失敗了。 SDL_Surface* pShownBMP = 0;
pShownBMP = SDL_LoadBMP("helloworld.bmp"); // Load a BMP file, and convert it as a surface. try { if ( pShownBMP == 0 ) throw SDL_GetError(); } catch ( const char* s ) { std::cerr << "SDL_LoadBMP() failed!\n" << s << std::endl; SDL_Quit(); return -1; } 2.4:塊移圖面(blit surface)。 int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);
src指的是要進(jìn)行blit的源surface,dst指的是blit這個surface要去的目的地——另外一個surface。我們這里先忽略SDL_Rect結(jié)構(gòu)的具體意思,僅僅需要了解的是,如果srcrect為空指針,意味著整個源surface將被blit;如果dstrect為空指針,意味著源surface與目的surface的左上角重合(坐標(biāo)(0,0))。 SDL_Rect* pSrcRect = 0; // If pSrcRect is NULL, the entire source surface is copied.
SDL_Rect* pDstRect = 0; // If pDstRect is NULL, then the destination position (upper left corner) is (0, 0). try { if ( SDL_BlitSurface(pShownBMP, pSrcRect, pScreen, pDstRect) != 0 ) // Put the BMP's surface on the SDL window's surface. throw SDL_GetError(); } catch ( const char* s ) { std::cerr << "SDL_BlitSurface() failed!\n" << s << std::endl; SDL_Quit(); return -1; } 2.5:顯示圖片。 int SDL_Flip(SDL_Surface *screen);
源圖面被blit到目的圖面上后,就與目的圖面融為一體了。在我們的例子中,ShownBMP被“畫”在了Screen上(我這里去掉了p,是為了說明這里討論的是surface而不是surface的指針)。換句話說,Screen被修改了(似乎也可以用“染指”-_-!!),ShownBMP則沒有改變。另外一個需要了解的問題是,我們之前對surface的種種操作,實(shí)際上都是在修改surface數(shù)據(jù)結(jié)構(gòu)中的某些數(shù)據(jù),當(dāng)我們最后需要將這些surface顯示到屏幕上(我們打開的SDL操作窗口上),我們需要使用函數(shù)SDL_Flip()。如果函數(shù)調(diào)用成功,則返回0;否則返回-1。 try {
if ( SDL_Flip(pScreen) != 0 ) // Show the SDL window's surface. throw SDL_GetError(); } catch ( const char* s ) { std::cerr << "SDL_Flip() failed!\n" << s << std::endl; SDL_Quit(); return -1; } 2.6:這個例子的完整源代碼。 #include <iostream>
#include "SDL/SDL.h" void pressESCtoQuit(); int main(int argc, char* argv[]) { try { if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) throw SDL_GetError(); } catch ( const char* s ) { std::cerr << "SDL_Init() failed!\n" << s << std::endl; return -1; } const int SCREEN_WIDTH = 640; // 0 means use current width. const int SCREEN_HEIGHT = 480; // 0 means use current height. const int SCREEN_BPP = 32; // 0 means use current bpp. const Uint32 SCREEN_FLAGS = SDL_SWSURFACE; // SDL_SWSURFACE == 0,surface in system memory. SDL_Surface* pScreen = 0; pScreen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SCREEN_FLAGS); // Creat a SDL window, and get the window's surface. try { if ( pScreen == 0 ) throw SDL_GetError(); } catch ( const char* s ) { std::cerr << "SDL_SetVideoMode() failed!\n" << s << std::endl; SDL_Quit(); return -1; } SDL_Surface* pShownBMP = 0; pShownBMP = SDL_LoadBMP("helloworld.bmp"); // Load a BMP file, and convert it as a surface. try { if ( pShownBMP == 0 ) throw SDL_GetError(); } catch ( const char* s ) { std::cerr << "SDL_LoadBMP() failed!\n" << s << std::endl; SDL_Quit(); return -1; } SDL_Rect* pSrcRect = 0; // If pSrcRect is NULL, the entire source surface is copied. SDL_Rect* pDstRect = 0; // If pDstRect is NULL, then the destination position (upper left corner) is (0, 0). try { if ( SDL_BlitSurface(pShownBMP, pSrcRect, pScreen, pDstRect) != 0 ) // Put the BMP's surface on the SDL window's surface. throw SDL_GetError(); } catch ( const char* s ) { std::cerr << "SDL_BlitSurface() failed!\n" << s << std::endl; SDL_Quit(); return -1; } try { if ( SDL_Flip(pScreen) != 0 ) // Show the SDL window's surface. throw SDL_GetError(); } catch ( const char* s ) { std::cerr << "SDL_Flip() failed!\n" << s << std::endl; SDL_Quit(); return -1; } pressESCtoQuit(); SDL_Quit(); return 0; } void pressESCtoQuit() { bool gameOver = false; while( gameOver == false ){ SDL_Event gameEvent; while ( SDL_PollEvent(&gameEvent) != 0 ){ if ( gameEvent.type == SDL_QUIT ){ gameOver = true; } if ( gameEvent.type == SDL_KEYUP ){ if ( gameEvent.key.keysym.sym == SDLK_ESCAPE ){ gameOver = true; } } } } return; }
|
|