在上一篇文章裏,咱們作了一個「加法」把兩張圖片合成,費了好大功夫才搞定,要是用PS可快了,我說哥們,這沒可比性!如今要作減法,就比如你工做是掙錢,你總得花錢吧!上大學的時候,有這麼一苦逼事,全班男生集體賭博,憑藉着過硬技術和良好的心態,居然橫掃,373張1毛的鈔票進了褲袋,那上面要是毛爺爺該多好!惋惜的是它抵不過半個毛爺爺。回過頭繼續上小學,今天任務就是把圖片支解。 app
。 ide
這樣作的好處是,若是你要用不少圖片時,不停加截圖片是否是很煩,不停進行(文件)IO操做也有風險,因此就集成一張圖片,這就是昨天加法,下面是作減法的時候,就是取出圖片裏的具體圖形。 ui
#include "SDL/SDL.h" #include "SDL/SDL_image.h" #include <string> const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; const int SCREEN_BPP = 32; SDL_Surface *dots = NULL; SDL_Surface *screen = NULL; //The event structure SDL_Event event; //The portions of the sprite map to be blitted 發現SDL_Rect了 SDL_Rect clip[ 4 ]; SDL_Surface *load_image( std::string filename ) { //The image that's loaded SDL_Surface* loadedImage = NULL; //The optimized surface that will be used SDL_Surface* optimizedImage = NULL; //Load the image loadedImage = IMG_Load( filename.c_str() ); //If the image loaded if( loadedImage != NULL ){ //Create an optimized surface optimizedImage = SDL_DisplayFormat( loadedImage ); //Free the old surface SDL_FreeSurface( loadedImage ); //If the surface was optimized if( optimizedImage != NULL ){ //Color key surface 對比上篇的代碼發現有什麼改進沒?? SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) ); } } //Return the optimized surface return optimizedImage; } void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL ) { //Holds offsets SDL_Rect offset; //Get offsets offset.x = x; offset.y = y; //Blit SDL_BlitSurface( source, clip, destination, &offset ); } bool init() { //Initialize all SDL subsystems if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) { return false; } //Set up the screen screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE ); //If there was an error in setting up the screen if( screen == NULL ){ return false; } //Set the window caption SDL_WM_SetCaption( "Split the dots", NULL ); //If everything initialized fine return true; } bool load_files() { //Load the sprite map dots = load_image( "dots.png" ); //If there was an problem loading the sprite map if( dots == NULL ){ return false; } //If eveything loaded fine return true; } void clean_up() { //釋放圖片內存 SDL_FreeSurface( dots ); //退出SDL SDL_Quit(); } int main( int argc, char* args[] ) { //關閉標誌 bool quit = false; //初始化 if( init() == false ) { return 1; } //Load the files if( load_files() == false ) { return 1; } /*取圖像 */ //Clip range for the top left 左上圓 clip[ 0 ].x = 0; clip[ 0 ].y = 0; clip[ 0 ].w = 100; clip[ 0 ].h = 100; //Clip range for the top right 右上圓 clip[ 1 ].x = 100; clip[ 1 ].y = 0; clip[ 1 ].w = 100; clip[ 1 ].h = 100; //Clip range for the bottom left 左下圓 clip[ 2 ].x = 0; clip[ 2 ].y = 100; clip[ 2 ].w = 100; clip[ 2 ].h = 100; //Clip range for the bottom right 右下圓 clip[ 3 ].x = 100; clip[ 3 ].y = 100; clip[ 3 ].w = 100; clip[ 3 ].h = 100; //先把整個窗口設爲白色 SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) ); //Apply the sprites to the screen 塊移圖片,把dots進行了分解塊移 apply_surface( 0, 0, dots, screen, &clip[ 0 ] ); apply_surface( 540, 0, dots, screen, &clip[ 1 ] ); apply_surface( 200, 200, dots, screen, &clip[ 2 ] ); apply_surface( 540, 380, dots, screen, &clip[ 3 ] ); //Update the screen if( SDL_Flip( screen ) == -1 ) { return 1; } //While the user hasn't quit while( quit == false ) { //While there's events to handle while( SDL_PollEvent( &event ) ) { //If the user has Xed out the window if( event.type == SDL_QUIT ) { //Quit the program quit = true; } } } clean_up(); return 0; }
保存爲sdl05.cpp 編譯 設計
g++ -o sdl05 sdl06.cpp -lSDL -lSDL_image ./sdl05
有圖片有真相 code
。 orm
果然如此吧,這個用法很是有用,由於在人物設計時每每是很是用效的。而如從不純色的背景裏取某個景特時,上述方法是不可取的。 由於他用到了透明處理,進行RGB圖像映射。 圖片
並且更應該關注的事,當咱們把其它窗口從上面拖過去的時候已經沒有變成黑色的背景的現象,什麼緣由,留給後面再說吧。文章標題言過其實,就當是搏個眼球,固然人民確定是能支解的,毛爺爺會變成工家兵,不過有機會必定要要試試支解下人民幣。 ip