SDL加载和缩放图片小例子

来源:互联网 发布:关系数据库的发展动向 编辑:程序博客网 时间:2024/04/30 05:25

 借助于SDL开发包,写的关于SDL加载和缩放图片的小例子;

#include<SDL/SDL.h>
#include<SDL/SDL_image.h>
#include <SDL/SDL_rotozoom.h>

int main(int argc,char *argv[])
{
 
 SDL_Rect  rect = {0,0,640,480};
 SDL_Rect  myrect={0,0,640,480};
 SDL_Rect bmprect;
 SDL_Surface *image;
 SDL_Surface *bmp;
 
 SDL_Event event;
 bmprect.x = rect.x ;
 bmprect.y = rect.y;
 bmprect.w = rect.w-200 ;
 bmprect.h = rect.h-200 ;
 SDL_Init(SDL_INIT_VIDEO);
 
 SDL_Surface *screen=SDL_SetVideoMode(640,480,0,SDL_INIT_NOPARACHUTE);
 if(screen==NULL) {
  printf("setVideoMode Failure");
  exit(1);
 }
 image=SDL_LoadBMP("sample.bmp");
 if(image==NULL){
   printf("load Failure\n");
   exit(1);
  }
 int sw = image->w;
 int sh = image->h;

 double xz = (double)bmprect.w/(double)sw;
 double yz = (double)bmprect.h/(double)sh;
 
 while(1){
  
  SDL_FillRect(screen,&myrect,0); //清屏函数,也可以对某一块区域进行设置颜色可以达到清屏的效果
        bmp = rotozoomSurfaceXY(image,0,xz,yz,1);  //缩放
  SDL_BlitSurface(bmp,NULL,screen,&bmprect); .//将图片添加到screen
  SDL_UpdateRects(screen,1,&rect);  //刷新
  SDL_FreeSurface(bmp); 
  
  SDL_WaitEvent(&event);   //事件了
  switch(event.type)
  {
  
  case SDL_KEYDOWN:if(event.key.keysym.scancode == 9) 
    {
     SDL_FreeSurface(image);
     SDL_Quit();  exit(0);
    }

   break;
  case SDL_MOUSEMOTION:

   break;
  case SDL_KEYUP:
    printf("scancode=%d\n",event.key.keysym.scancode);
     if(event.key.keysym.scancode == 98)
    {
     bmprect.y-=10;
     
    }
    else if(event.key.keysym.scancode == 102)
    {
     bmprect.x+=10;
    
    }
    else if(event.key.keysym.scancode == 100)
    {
     bmprect.x-=10;
     
    }
    else if(event.key.keysym.scancode == 104)
    {
     bmprect.y+=10;
     
    }
    else if(event.key.keysym.scancode == 21)
    {
     bmprect.w+=10;
     bmprect.h+=10;
    }
    else if(event.key.keysym.scancode == 20)
    {
     bmprect.w-=10;
     bmprect.h-=10;
    }
    break; 
  case SDL_MOUSEBUTTONDOWN: break;
  case SDL_MOUSEBUTTONUP: 
   break;
  case SDL_QUIT:
   exit(1);
  
  }
  xz = (double)bmprect.w/(double)sw;  //缩放的尺寸

  yz = (double)bmprect.h/(double)sh;
 }
  
 SDL_FreeSurface(image);
 sleep(3);
 SDL_Quit(); 

}

1 0
原创粉丝点击