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

相关文章
相关标签/搜索