4。SDL遊戲開發:作一個「加法「運算

當咱們剛開吚啊吚的時個,父母就告訴咱們,大拇指+食指 = 2,現在,爲了在天朝混口飯吃,別把本身當碼農,爲了和諧社會我們再來2一回,今天的題目很簡單,把兩個圖片合起來。 app

 

背景圖片: ide

獲得以下圖片: 函數

在以前的基礎上把代碼進得改寫
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;

//The surfaces
SDL_Surface *background = NULL;
SDL_Surface *foo = NULL;
SDL_Surface *screen = NULL;

//The event structure
SDL_Event event;

SDL_Surface *load_image( std::string filename )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized image 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 image
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old image
        SDL_FreeSurface( loadedImage );

        //If the image was optimized just fine
        if( optimizedImage != NULL ) {
            //Map the color key
            Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );

            //Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
        }
    }

    //Return the optimized image
    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
    //Temporary rectangle to hold the offsets
    SDL_Rect offset;

    //Get the offsets
    offset.x = x;
    offset.y = y;

    //Blit the surface
    SDL_BlitSurface( source, NULL, destination, &offset );
}

bool init()
{
    //初始化全部子系統
    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( "Foo says \"Hello!\"", NULL );

    //If everything initialized fine
    return true;
}

bool load_files()
{
    //導入背景圖片
    background = load_image( "background.png" );

    if( background == NULL ) {
        return false;
    }

    //Load the stick figure
    foo = load_image( "foo.png" );

    //If the stick figure didn't load
    if( foo == NULL ) {
        return false;
    }

    return true;
}

void clean_up()
{
    //Free the surfaces
    SDL_FreeSurface( background );
    SDL_FreeSurface( foo );

    //Quit SDL
    SDL_Quit();
}

int main( int argc, char* args[] )
{
    //Quit flag
    bool quit = false;

    //初始化
    if( init() == false ) {
        return 1;
    }

    //導入圖片文件
    if( load_files() == false ) {
        return 1;
    }

    //Apply the surfaces to the screen
    apply_surface( 0, 0, background, screen );
    apply_surface( 240, 190, foo, screen );

    //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;
}
保存爲sdl04.cpp 編譯

g++ -o sdl04 sdl04.cpp -lSDL -lSDL_image
對比於sdl03.cpp發現程序的改變並不大,關鍵看這幾行代碼load_image函數裏的 

if( optimizedImage != NULL ) {
            //Map the color key
            Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );

            //Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
        }

裏面的SDL_MapRGB這個函數,(提醒你們一下,如今相關SDL的函數在使用的過程當中,若是不是很清楚能夠經過男人來找,男人不只找樂子歷害,找函數也很拿手哦。 spa


NAME
       SDL_MapRGB - Map a RGB color value to a pixel format.
SYNOPSIS
       #include "SDL.h"
       Uint32 SDL_MapRGB(SDL_PixelFormat *fmt, Uint8 r, Uint8 g, Uint8 b);
就是對某張圖片的指定的RGB像素點進行映射。(剛開始的時候,對這個函數,不是很好的理解,經過RGB(0,0XFF,0XFF)指定的顏色是 對比下小人頭的圖片背景色,原來如此)若是仍是不很理解,接下來看了SDL_SetColorKey()函數就會好多了,它的功能就是把上一個函數的進行映射的像素點除去(透明化),能夠更解爲透明處理,固然還有 啓用或禁用RLE的blit加速功能。可是SDL_SetColorKey()只能處理純色,剛開始仍是多貼點註釋!
NAME
       SDL_SetColorKey - Sets the color key (transparent pixel) in a blittable
       surface and RLE acceleration.
SYNOPSIS
       #include "SDL.h"
       int SDL_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key);


完成昨晚沒有完成的任務,寫點東西真不容易!(求職公寓,10平方,兩人),這個苦就不說了! code

相關文章
相關標籤/搜索