SDL图片的旋转与缩放

 

#include

#include "SDL_rotozoom.h"

#include "SDL_image.h"

#include "math.h"

#include "string.h"

#include "SDL_gfxPrimitives.h"

 

 

/*

 

初始化SDL 函数库

SDL——INITEVERTHING 代表所有的 视频音频 图片 ...

 

*/

 

SDL_Surface * screen = NULL;

void Init()

{

 

    if(SDL_Init(SDL_INIT_EVERYTHING)== -1)

    {

        printf("SDL_INIT fault !\n");

        exit(0);

    }

   

}

// 创建屏幕 640*480 0  SDL_SWSURFACE

void Create_Screen(int width , int height , int bpp , Uint32 flags)

{

 

 

    //SDL_Surface *screen = NULL;

    screen = SDL_SetVideoMode(width , height,  bpp , flags);

    if(screen == NULL)

    {

        printf("SDL_SetVideoMode default !\n");

        exit(1);

    }

 

}

// 加载图片 传递文件的 绝对路径

SDL_Surface * Image_Load(char *filename)

{

    SDL_Surface *image = NULL;

    image = IMG_Load(filename);

    if(image == NULL)

    {

        printf("The image load default, maybe can't find %s  !!!\n",filename);

        exit(0);

   

    }

    return image;

}

void Free_Image(SDL_Surface * image)

{

    SDL_FreeSurface(image);

 

}

 

//用来显示 image 图片,rect 可以调节位置

int Show_Image(SDL_Surface * image,SDL_Rect *rect)

{

 

    SDL_BlitSurface(image , NULL , screen , rect);

    SDL_UpdateRect(screen , 0 , 0 , 0 , 0 );

    return 0;

}

 

SDL_Rect Set_Image(SDL_Surface *image ,double zoom)

{

    //double x,y ;

    SDL_Rect rect;

   

    rect.x =  (screen->w - image->w ) / 2;

    rect.y =  (screen->h - image->h ) / 2;

   

    return rect;

   

}

 

void Zoom_Small(double* x,SDL_Surface *image)

{

    SDL_Rect rect ;

    if(*x <= 0)

    return ;

    *x = *x-0.05;

    SDL_Surface *image_1;

    image_1 = zoomSurface(image , *x , *x, 1);

    boxColor(screen, 0, 0, screen->w, screen->h, 0x000000ff);

    rect = Set_Image(image_1 , *x);

    Show_Image(image_1,&rect);

    Free_Image(image_1);

 

   

}

 

void Zoom_Big(double* x,SDL_Surface *image)

{

    *x =  *x + 0.05;

    SDL_Rect rect; 

    SDL_Surface *image_1;

    image_1 = zoomSurface(image , *x , *x, 1);

    boxColor(screen, 0, 0, screen->w, screen->h, 0x000000ff);

    rect = Set_Image(image_1 , *x);

    Show_Image(image_1,&rect);

   

    Free_Image(image_1);

}

SDL_Surface *Change_Angle(SDL_Surface *image ,double *angle,double * x)

{

    SDL_Surface* change = NULL;

    SDL_Rect rect;

   

    *angle = *angle -10.0;

   

   

    boxColor(screen, 0, 0, screen->w, screen->h, 0x000000ff);

    change = rotozoomSurfaceXY(image, *angle, *x, *x, 1);

   

    rect = Set_Image(change , *x);

    Show_Image(change,&rect);

   

    return change;

 

}

void Drav_Button(char *str , int flag)

{

    if(strcmp(str , "left") == 0)

    {

        //boxColor(screen, 160, 420 , 200, 460, 0xf3f5ffff);

        if(flag == 1)

        {

            hlineColor( screen, 160 , 200 ,460 , 0x000000ff);

            vlineColor( screen, 200 , 420 , 460 , 0x000000ff);

        }

   

        Show_Image( screen , &(screen->clip_rect) );   

    }

    if(strcmp(str , "right") == 0)

    {

        boxColor(screen, 438, 420 , 478, 460, 0xf3f5ffff);

        if(flag == 1)

        {

            hlineColor( screen, 438 , 478 ,  460, 0x000000ff);

            vlineColor( screen, 478 , 420 , 460 , 0x000000ff);

        }

        Show_Image( screen , &(screen->clip_rect) );   

    }

    return ;

}

 

 

int main()

{

    SDL_Surface * image = NULL;

    SDL_Surface * change = NULL;

    int flag = 1;

    double zoom_xy = 1,angle = 0;

    Init();

    Create_Screen(640,480,0,SDL_SWSURFACE);

    image = Image_Load("sheep4.jpg");

    Show_Image(image,NULL);

    printf("hello world \n");

   

    SDL_Event event;

    while(flag)

    {

        while(SDL_PollEvent(&event))            //将事件加入事件队列队列

        {  

            switch(event.type)

            {

                case SDL_KEYDOWN:

                    printf("The key is %d\n",event.key.keysym.sym);

                   

                    if(event.key.keysym.sym == SDLK_ESCAPE)

                    {

                        flag = 0;

                    }

                    if(event.key.keysym.sym == SDLK_SPACE)

                    {

                        printf("The space is down ... \n");

                    }

/* 事件中按 s 图片缩小 按 b 放大 ,点击鼠标开始旋转

                    if(event.key.keysym.sym == 115)

                    {

                        printf("The image is become smaller !\n");

                        Zoom_Small(&zoom_xy,image);

                        printf("%lf\n",zoom_xy);

                    }

                    if(event.key.keysym.sym == 98)

                    {

                        printf("The image is become smaller !\n");

                        Zoom_Big(&zoom_xy,image);

                        printf("%lf\n",zoom_xy);

                    }

                case SDL_KEYUP:

                    {

                       

                       

                    }break;

                case SDL_MOUSEBUTTONDOWN:

                        Drav_Button("left" , 0);

                        //circleColor(screen, 320, 240, 240, 0x000000ff);

                        //change = rotozoomSurfaceXY(image, 90.0, 1, 1, 1);

                        //change = shrinkSurface(image,320,240);

                        //Show_Image(change,0);

                        Change_Angle(image,&angle,&zoom_xy);

                        //Drav_Button("right" ,0 );

                        break; 

                case SDL_MOUSEBUTTONUP:    

                       // Drav_Button("left" , 1);

                        //Drav_Button("right" ,1 );

                        break;

                case SDL_QUIT:

                    {

                        flag = 0;

                    }break;

               

            }

        }

   

    }

    Free_Image(image);

    return 0;

}

 

 

                                                                         Write by douyuan888 2012/12/30

你可能感兴趣的:(SDL)