开源跨平台多媒体开发库SDL初学编程练习--偷UP主的

//下面分了几个main函数,打开相应的main函数即可使用,使用代码替换自己的工程图片。
//代码实现功能:类似泡泡龙小游戏的功能.
//开发环境:Code::blocks 10.05
//Note:使用前需配置SDL2.0在IDE的使用,细节参考博主的点击打开链接
 
  
#include 
#include 
#include 
#include 
#include 

int main1(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window *window =
    SDL_CreateWindow("sdl",
                     100,100,
                     800,600,
                     SDL_WINDOW_SHOWN);
    SDL_Surface *surface =SDL_GetWindowSurface(window);
//    SDL_Surface *image   =IMG_Load("test2.png");
//    SDL_BlitSurface(image,NULL,surface,NULL);
    SDL_Surface *face1  =SDL_LoadBMP("test.bmp");
    SDL_Surface *face2  =IMG_Load("test2.png");
//    SDL_BlitSurface(face1,NULL,surface,NULL);
    SDL_Rect rect;
    rect.x=10;
    rect.y=10;
    SDL_Event event;
    bool flag;

    while(flag==false){
        while(SDL_PollEvent(&event)){

            if(event.type == SDL_QUIT){
               flag=true;
            }
            else if(event.type == SDL_MOUSEBUTTONDOWN){
                if(event.button.button==SDL_BUTTON_LEFT){
                    SDL_BlitSurface(face1,NULL,surface,&rect);
                    SDL_UpdateWindowSurface(window);
                    printf("left\n");
                }
                else if(event.button.button==SDL_BUTTON_RIGHT)
                {
                    SDL_BlitSurface(face2,NULL,surface,&rect);
                    SDL_UpdateWindowSurface(window);
                    printf("right\n");
                }
                printf("clicked\n");
            }
            else if(event.type == SDL_KEYDOWN){
             if(event.key.keysym.sym==SDLK_UP)
             {
                   rect.y=rect.y-10;
                   printf("SDLK_UP\n");
             }
             else if(event.key.keysym.sym==SDLK_DOWN)
             {
                   rect.y=rect.y+10;
                   printf("SDLK_DOWN\n");
             }
             else if(event.key.keysym.sym==SDLK_LEFT)
             {
                   rect.x=rect.x-10;
                   printf("SDLK_LEFT\n");
             }
             else if(event.key.keysym.sym==SDLK_RIGHT)
             {
                   rect.x=rect.x+10;
                   printf("SDLK_RIGHT\n");
             }
             SDL_BlitSurface(face2,NULL,surface,&rect);
             SDL_UpdateWindowSurface(window);
             SDL_FillRect(surface,NULL,0);
            }

        }
        }
    SDL_UpdateWindowSurface(window);

    SDL_Delay(2000);
    SDL_FreeSurface(surface);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;

}
/*********************************2*************************************************/
int main2(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window *window = SDL_CreateWindow("hello",100,100,
                                          800,710,
                                          SDL_WINDOW_SHOWN
                                          );
    SDL_Surface *surface=SDL_GetWindowSurface(window);
    SDL_Surface *cat = SDL_LoadBMP("test.bmp");
    SDL_Rect rect;
    rect.x=0;
    rect.y=0;
    SDL_Event event;
    bool quit =true;
    while(quit ==true){
        while(SDL_PollEvent(&event)){
        if(event.type==SDL_QUIT)
        {
            quit =false;
        }
        else if(event.type==SDL_MOUSEMOTION)
        {
            rect.x=event.motion.x-(cat->w/2);
            rect.y=event.motion.y-(cat->h/2);
            SDL_FillRect(surface,NULL,0);
            SDL_BlitSurface(cat,NULL,surface,&rect);
            printf("x=%d,y=%d\n",rect.x,rect.y);
            SDL_UpdateWindowSurface(window);
        }
        }
    }
    SDL_UpdateWindowSurface(window);
    SDL_FreeSurface(cat);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

/*****************************************3****************************************************/
SDL_Rect rect;

bool MoveDown=true;
bool MoveLeft =true;

void UpdataPostion()
{
    if(MoveLeft==true)
    {
        rect.x++;
        if(rect.x>=800-100)
        {
            MoveLeft=false;
        }

    }
    else
    {
        rect.x--;
        if(rect.x<=0)
        {
            MoveLeft=true;
        }
    }

    if(MoveDown==true)
    {
        rect.y++;
        if(rect.y+rect.h>=700)
        {
            MoveDown=false;
        }

    }
    else
    {
        rect.y--;
        if(rect.y<=0)
        {
            MoveDown=true;
        }
    }

}


int main3(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window *window = SDL_CreateWindow("hello",100,100,
                                          800,700,
                                          SDL_WINDOW_SHOWN
                                          );
    SDL_Renderer *rend =SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
    SDL_RenderClear(rend);
    SDL_Surface *catSurface = SDL_LoadBMP("test.bmp");
    SDL_Texture *cat =SDL_CreateTextureFromSurface(rend,catSurface);

    SDL_Event event;
    rect.x=0;
    rect.y=0;
    rect.h=100;
    rect.w=100;
    bool quit =true;
    while(quit ==true){
        while(SDL_PollEvent(&event)){
        if(event.type==SDL_QUIT)
        {
            quit =false;
        }
        }
        UpdataPostion();
        SDL_Delay(5);
        SDL_RenderCopy(rend,cat,NULL,&rect);
        SDL_RenderPresent(rend);
    }
    SDL_UpdateWindowSurface(window);
    SDL_FreeSurface(cat);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

/*********************************4************************************************/
int main(int argc,char *argv[])  
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window *window=SDL_CreateWindow("test3",100,100,
                                        800,700,
                                        SDL_WINDOW_SHOWN);
    SDL_Renderer *render =SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
    SDL_Surface  *image  =IMG_Load("move.png");
    SDL_SetColorKey(image,SDL_TRUE,SDL_MapRGB(image->format,255,255,255));
    SDL_Texture *texture =SDL_CreateTextureFromSurface(render,image);
    SDL_Event event;
    SDL_Rect postion;
    postion.x=0;
    postion.y=0;
    postion.w=image->w/5;
    postion.h=image->h/2;
    SDL_Rect clips[8];
    int i,j;
    int ImgWidth  =image->w/5;
    int ImgHeight =image->h/2;
    for(j=0;j<2;j++)
    {
        for(i=0;i<5;i++)
        {
            clips[j*5+i].x=0;
            clips[j*5+i].y=j*5+i*ImgWidth;
            clips[j*5+i].w=ImgWidth;
            clips[j*5+i].h=ImgHeight;
        }
    }

    bool quit =true;
    while(quit ==true){
        while(SDL_PollEvent(&event)){
        if(event.type==SDL_QUIT)
        {
            quit =false;
        }
        }
        SDL_RenderClear(render);
        SDL_RenderCopy(render,texture,&clips[j*5+i],&postion);
        SDL_RenderPresent(render);
        j=(j+1)%2;          /*省事,代替if语句,代码更好看*/
        i=(i+1)%5;
        postion.x++;
        SDL_Delay(500);
    }

    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}




 
  
 
 

你可能感兴趣的:(开源跨平台多媒体开发库SDL初学编程练习--偷UP主的)