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

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

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;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值