基于SDL的图片浏览

space键返回到主界面

点击鼠标会出现两个很丑陋的button,一个是向左浏览一个向右浏览

-/+是对图片的缩放

有点丑陋,代码更加丑陋,后续还需继续优化代码,功能还需继续添加

望高人斧正,代码如下:

#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;
}
截图如下:

转载于:https://my.oschina.net/mjRao/blog/52963

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Ubuntu上使用SDL显示图片,你需要进行以下步骤: 1. 安装SDL库:首先,确保你已经安装了SDL库。可以使用以下命令在Ubuntu上安装: ``` sudo apt-get install libsdl2-dev ``` 2. 初始化SDL:在你的代码中,首先需要初始化SDL。你可以在程序的开始处调用`SDL_Init`函数来初始化SDL。例如: ```c if (SDL_Init(SDL_INIT_VIDEO) != 0) { printf("SDL initialization failed: %s\n", SDL_GetError()); return 1; } ``` 3. 创建窗口和渲染器:在初始化SDL后,你需要创建一个窗口和一个渲染器来显示图片。你可以使用`SDL_CreateWindow`函数创建一个窗口,并使用`SDL_CreateRenderer`函数创建一个渲染器。例如: ```c SDL_Window* window = SDL_CreateWindow("Image Display", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN); if (window == NULL) { printf("Window creation failed: %s\n", SDL_GetError()); return 1; } SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); if (renderer == NULL) { printf("Renderer creation failed: %s\n", SDL_GetError()); return 1; } ``` 4. 加载图片并创建纹理:使用SDL_image库加载图片,并将其转换为纹理以供渲染。你需要安装SDL_image库,并在代码中包含相应的头文件。例如,加载一张名为"image.png"的图片并创建纹理: ```c #include <SDL2/SDL_image.h> SDL_Surface* surface = IMG_Load("image.png"); if (surface == NULL) { printf("Image loading failed: %s\n", IMG_GetError()); return 1; } SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); SDL_FreeSurface(surface); if (texture == NULL) { printf("Texture creation failed: %s\n", SDL_GetError()); return 1; } ``` 5. 渲染纹理:最后,你可以在主循环中使用`SDL_RenderCopy`函数将纹理渲染到窗口上。例如,将纹理渲染到整个窗口: ```c SDL_RenderClear(renderer); SDL_RenderCopy(renderer, texture, NULL, NULL); SDL_RenderPresent(renderer); ``` 6. 释放资源:在程序结束时,记得释放已分配的资源。例如,释放纹理、渲染器和窗口: ```c SDL_DestroyTexture(texture); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); ``` 这就是在Ubuntu上使用SDL显示图片的基本步骤。你可以根据自己的需求进行更多的操作和调整。如果你有其他问题,请提供更多的细节,我将尽力帮助你解决问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值