Linux下的SDL图片浏览

74 篇文章 1 订阅
Linux下的SDL图片浏览
#include "SDL.h"
//#include "SDL_ttf.h"
#include "SDL_rotozoom.h"
#include "SDL_image.h"
#include "math.h"
#include "string.h"

#define MAX  16
#define BMP_NAME "icon.bmp"
#define ICON "sample.bmp"
#define TITLE_NAME "TestSDL"
#define ICON_NAME  "My WinFrame"		//窗体最小化name
//#define PNG_NAME "Bliss.png"			//背景图片

#define TEXT "欢迎光临"				//文字设置
#define TEXT_SIZE 50				//设置字体大小

#define WIDTH  640
#define HEIGTH 480

char *picture[] = {
		  "1.jpg"  ,  "2.jpg"  ,  "3.jpg"  ,  "4.jpg" , 
		  "5.jpg"  ,  "6.jpg"  ,  "7.jpg"  ,  "8.jpg" ,
		  "9.jpg"  ,  "10.jpg" ,  "11.jpg" ,  "12.jpg" ,
		  "13.png" ,  "14.jpg" ,  "15.jpg" ,  "16.jpg"
		  };
static SDL_Surface *screen = NULL;

int Init()						//初始化SDL
{
	if((SDL_Init(SDL_INIT_VIDEO)&&IMG_Init(IMG_INIT_PNG)) == -1)
	{
		fprintf(stderr,"SDL init error:%s",SDL_GetError());
		return -1;
	}
	return 0;
}

SDL_Surface *loadBMP(char *fileName)			//加载bmp图片
{
	SDL_Surface *bmp;
	bmp = SDL_LoadBMP(fileName);
	if(bmp == NULL)
	{
		fprintf(stderr,"Could not load %s: %s",fileName,SDL_GetError());
		exit(1);
	}
	return bmp;
}

void creatScreen(int width , int height , int bpp , Uint32 flags)		//创建一个VideoMode
{
	screen = SDL_SetVideoMode(width , height,  bpp , flags);
	if(screen == NULL)
	{
		fprintf(stderr,"Could not Creat a Screen!:%s",SDL_GetError());
		exit(1);
	}
	return ;
}
int Destory(SDL_Surface *file)					//释放空间
{
	SDL_FreeSurface( file );	return 0;
}
void show_Pic(SDL_Surface *bmp ,SDL_Rect *rect)	//显示bmp图片
{
	SDL_BlitSurface(bmp , NULL , screen , rect);
	SDL_UpdateRect(screen , 0 , 0 , 0 , 0 );
	return ;
}

int getRightPic(int i)
{
	
	i++;
	if(i % 15 == 0)
	{
		i = 0;
	}
	return i; 
}
int	 getLeftPic(int i)
{
	i--;
	if(i < 0)
	{
		i = 15;
	}
	return i; 
}
void zoomBig(double *a)
{	
	*a -= 0.25;
	if(*a <= 1.5)
	{
		*a = 1.5;
	}
	
}
void zoomSmall(double *a)
{

	*a += 0.25;	
	if(*a >= 5)
	{
		*a = 5;
	}	
}

void draw_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_Pic( 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_Pic( screen , &(screen->clip_rect) );	
	}
	return ;
}
void button(char *str)
{
	SDL_Rect rect;
	int x,y;
	if(strcmp(str , "left") == 0)
	{
		boxColor(screen, 160, 420 , 200, 460, 0xf3f5ffff);
		SDL_UpdateRect(screen , 0 , 0 , 0 , 0 );
	}
	if(strcmp(str , "right") == 0)
	{
		boxColor(screen, 438, 420 , 478, 460, 0xf3f5ffff);
		SDL_UpdateRect(screen , 0 , 0 , 0 , 0 );
	}
}
int main(int argc,char **argv)
{	

	const SDL_VideoInfo *info = NULL;
	int flag = 1;
	int i = 0;
	int width = WIDTH;
	int heigth = HEIGTH;
	int bpp = 0;
	double zoom_x,zoom_y;
	SDL_Surface *backpng = NULL;
	Init();
	creatScreen(width , heigth, bpp , SDL_SWSURFACE);

	info = SDL_GetVideoInfo();
	if(info == NULL)
	{
		fprintf( stderr, "Video query failed: %s\n",SDL_GetError( ) );
		exit(1);

	}
	bpp = info->vfmt->BitsPerPixel ;			//得到VideoMode的色度bpp
	
	backpng = IMG_Load(picture[i]);
	if(!backpng)
	{
		fprintf(stderr,"Could not load %s: %s\n",picture[i],SDL_GetError());
		exit(1);	
	}

	zoom_x = (double)((screen->w) / (double)(backpng->w));
	zoom_y = (double)((screen->h) / (double)(backpng->h));
	
	backpng = zoomSurface(backpng , zoom_x , zoom_y , 1);
	show_Pic(backpng ,&(screen->clip_rect));
	SDL_Rect fillRect;
	
	fillRect.x = 107;
	fillRect.y = 80;
	fillRect.w = 426;
	fillRect.h = 320;
	
	
	SDL_WM_SetCaption(TITLE_NAME, ICON_NAME);				//设置窗口标题
	SDL_WM_SetIcon(loadBMP(ICON) , NULL);						//设置图标	
	static double zoomXY = 1.5;
	SDL_Event event;
	while(flag)
	{
		while(SDL_PollEvent(&event))			//将事件加入事件队列队列
		{	
			switch(event.type)
			{
				case SDL_KEYDOWN:
					printf("Key Down......\n");
					if(event.key.keysym.sym == SDLK_ESCAPE)
					{
						flag = 0;
					}
					if(event.key.keysym.sym == SDLK_SPACE)
					{
						backpng = IMG_Load(picture[0]);
						if(!backpng)
						{
							fprintf(stderr,"Could not load %s: %s\n",picture[i],SDL_GetError());
							exit(1);	
						}

						zoom_x = (double)((screen->w) / (double)(backpng->w));
						zoom_y = (double)((screen->h) / (double)(backpng->h));
	
						backpng = zoomSurface(backpng , zoom_x , zoom_y , 1);
						show_Pic(backpng ,&(screen->clip_rect));
						Destory(backpng);
						backpng = NULL;
					}	
					break;
				case SDL_KEYUP:
					/*
					*keyborad test
					*/
					printf("Key up......\n");
					printf("key : %d\n" , event.key.keysym.sym );
					if(event.key.keysym.sym == 269)
					{
						zoomSmall(&zoomXY);
					}
					if(event.key.keysym.sym == 270)
					{
						zoomBig(&zoomXY);
					}
					break;
				case SDL_MOUSEMOTION:
					/*
					*mouse motton test
					*/
					printf("mouse motton : x = %d , y = %d\n" ,event.button.x , event.button.y);
					break;
				case SDL_MOUSEBUTTONDOWN:
					draw_button("left" , 0) ;
					draw_button("right" , 0) ; 
					if(((event.button.x >= 160) && (event.button.x <= 200 )) && ((event.button.y >= 420) && (event.button.y <= 460 )))
					{
						draw_button("left" , 1) ;
						i = getLeftPic(i);	
						backpng = IMG_Load(picture[i]);
						if(!backpng)
						{
							fprintf(stderr,"Could not load %s: %s\n",picture[i],SDL_GetError());
							exit(1);	
						}
						zoom_x = (double)((screen->w) / (double)(backpng->w));
						zoom_y = (double)((screen->h) / (double)(backpng->h));
	
						backpng = zoomSurface(backpng , (zoom_x / zoomXY), (zoom_y / zoomXY) , 1);
						SDL_Rect rect;
						rect.x =  (screen->w - backpng->w ) / 2;
						rect.y =  (screen->h - backpng->h ) / 2;
					
					
						rect.w = backpng->w;
						rect.h = backpng->h;
						printf("%d , %d , %d , %d\n" , rect.x, rect.y, rect.w , rect.h );
						SDL_FillRect(screen ,&fillRect , 0xffffffff);
						SDL_UpdateRect(screen , 0 , 0 , 0 , 0 );
						show_Pic(backpng , &rect);
						Destory(backpng);
						backpng = NULL;
						
					}
					if(  (event.button.x >= 438 && event.button.x <= 478 ) && (event.button.y >= 420 && event.button.y <= 460 ) )
					{
						draw_button("right" , 1) ; 
						i = getRightPic(i);	
						backpng = IMG_Load(picture[i]);
						if(!backpng)
						{
							fprintf(stderr,"Could not load %s: %s\n",picture[i],SDL_GetError());
							exit(1);	
						}
						zoom_x = (double)((screen->w) / (double)(backpng->w));
						zoom_y = (double)((screen->h) / (double)(backpng->h));
	
						backpng = zoomSurface(backpng , (zoom_x / zoomXY), (zoom_y / zoomXY) , 1);
						SDL_Rect rect;
						rect.x =  (screen->w - backpng->w ) / 2;
						rect.y =  (screen->h - backpng->h ) / 2;
					
					
						rect.w = backpng->w;
						rect.h = backpng->h;
						printf("%d , %d , %d , %d\n" , rect.x, rect.y, rect.w , rect.h );
						SDL_FillRect(screen ,&fillRect , 0xffffffff);
						SDL_UpdateRect(screen , 0 , 0 , 0 , 0 );
						show_Pic(backpng , &rect);
						Destory(backpng);
						backpng = NULL;
					}
					break;
				case SDL_MOUSEBUTTONUP:	
					draw_button("left" , 0) ;
					draw_button("right" , 0) ;
					break;
				case SDL_QUIT:
					printf("quit\n");
					flag = 0;
					break;
			}
		}
	}


	Destory(backpng);
	Destory(screen);
	//TTF_Quit();
	SDL_Quit();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值