SDL编译安装图片显示

SDL编译安装图片显示

1.SDL简介

  SDL(Simple DirectMediaLayer)是一套开放源代码的跨平台多媒体开发库,使用C语言写成。SDL提供了数种控制图像、声音、输出入的函数,让开发者只要用相同或是相似的代码就可以开发出跨多个平台(Linux、Windows、MacOS X等)的应用软件。现SDL多用于开发游戏、模拟器、媒体播放器等多媒体应用领域。

  在编译SDL库前,先安装以下工具:

sudo apt-get install autoconf
 sudo apt-get install automake
sudo apt-get install libtool
sudo apt-get install make
sudo apt-get install xorg-dev 

2. SDL库编译与安装

 下载地址:SDL2.0
 (1)将下载的SDL2-2.0.14.tar.gz压缩包拷贝至虚拟机解压。

[wbyq@wbyq src_pack]$ tar xvf /mnt/hgfs/ubuntu/software_pack/SDL2-2.0.14.tar.gz

 (2)配置编译安装路径

[wbyq@wbyq SDL2-2.0.14]$ ./autogen.sh 
[wbyq@wbyq SDL2-2.0.14]$ ./configure --prefix=$PWD/_install
[wbyq@wbyq SDL2-2.0.14]$ make && make install 

 (3)编译成功生成文文件

[wbyq@wbyq _install]$ ls
bin  include  lib  share

(4)拷贝.so文件到/usr/lib下

[wbyq@wbyq _install]$ sudo cp ./lib/*.so* /usr/lib -fdv
[wbyq@wbyq _install]$ sudo cp bin/sdl2-config /usr/bin

1.3 SDL2_image库编译与安装

  bmp_surface加载jpg格式图片和png格式图片需要相对应库支持

[wbyq@wbyq src_pack]$ sudo apt-get install libjpeg-dev  #安装jpg库
[wbyq@wbyq src_pack]$ sudo apt-get install libpng-dev  #安装png库

 下载地址:SDL_IMAGE
 (1)将 SDL2_image-2.0.5.tar.gz压缩包拷贝至虚拟机解压。

[wbyq@wbyq src_pack]$ tar xvf /mnt/hgfs/ubuntu/software_pack/SDL2_image-2.0.5.tar.gz

 (2)配置编译安装路径

[wbyq@wbyq SDL2_image-2.0.5]$ ./autogen.sh 
[wbyq@wbyq SDL2_image-2.0.5]$ ./configure --prefix=$PWD/_install
[wbyq@wbyq SDL2_image-2.0.5]$ make && make install

 (3)编译成功生成文文件

[wbyq@wbyq _install]$ tree
.
├── include
│   └── SDL2
│       └── SDL_image.h
└── lib
    ├── libSDL2_image-2.0.so.0 -> libSDL2_image-2.0.so.0.2.3
    ├── libSDL2_image-2.0.so.0.2.3
    ├── libSDL2_image.a
    ├── libSDL2_image.la
    ├── libSDL2_image.so -> libSDL2_image-2.0.so.0.2.3
    └── pkgconfig
        └── SDL2_image.pc

4 directories, 7 files

1.4 示例1:SDL显示图片和获取输入出设备数据

 1.4.1 示例代码1.4.2 Makefile文件

#include 
#include 
#include 
#include 
typedef enum
{
    false,
    true
} bool;
int main(int argc,char *argv[])
{
	if(SDL_Init(SDL_INIT_VIDEO))/*初始化SDL*/
	{
		printf("初始化失败ERR=%s\r\n",SDL_GetError());
		return 0;
	}
	/*创建SDL窗口*/
	SDL_Window *window=SDL_CreateWindow("SDL Test", SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,800,480,SDL_WINDOW_SHOWN);
	SDL_Rect rect;
	rect.x=0;
	rect.y=0;
	SDL_Surface *surface=SDL_GetWindowSurface(window);/*获取窗口*/
	SDL_Surface *pngsurface=IMG_Load("bg6.png");
	SDL_Surface *bmp_surface=IMG_Load("1.bmp");
	SDL_BlitSurface(bmp_surface,NULL,surface,NULL);
	SDL_UpdateWindowSurface(window);/*刷新窗口*/
	bool quit= true;

	SDL_Event event;
	while(quit)
	{
		while(SDL_PollEvent(&event))/*监测事件*/
		{
			if(event.type==SDL_QUIT)//关闭窗口
			{
				printf("hello,world\n");
				quit=false;
				printf("quit=%d\n",quit);
			}
			else if(event.type==SDL_MOUSEBUTTONDOWN)/*鼠标点击事件*/
			{
				if(event.button.button==SDL_BUTTON_LEFT)/*左键*/
				{
					printf("left clicked\n");
					SDL_BlitSurface(bmp_surface,NULL,surface,NULL);
				}
				if(event.button.button==SDL_BUTTON_RIGHT)/*右键*/
				{
					printf("right clicked\n");
					SDL_BlitSurface(pngsurface,NULL,surface,NULL);
				}
			}
			else if(event.type==SDL_KEYDOWN)//键盘事件
			{
				
				printf("key=%d\n",event.key.keysym.sym);
				if(event.key.keysym.sym==SDLK_UP)
				{
					rect.y-=10;
					printf("up\n");
				}
				else if(event.key.keysym.sym==SDLK_DOWN)
				{
					rect.y+=10;
					printf("down\n");
				}
				else if(event.key.keysym.sym==SDLK_LEFT)
				{
					rect.x-=10;
					printf("left\n");
				}
				else if(event.key.keysym.sym==SDLK_RIGHT)
				{
					rect.x+=10;
					printf("right\n");
				}
				SDL_BlitSurface(bmp_surface,NULL,surface,NULL);
				SDL_BlitSurface(pngsurface,NULL,surface,&rect);
			}
			SDL_UpdateWindowSurface(window);/*刷新窗口*/
		}
	}
	SDL_FreeSurface(surface);
	SDL_DestroyWindow(window);/*销毁窗口*/
	SDL_Quit();/*关闭SDL*/
	return 0;
}

1.4.2 Makefile文件

CFLAGS =-I/home/wbyq/src_pack/SDL2-2.0.14/_install/include -I/home/wbyq/src_pack/SDL2-2.0.14/_install/include/SDL2 -L/home/wbyq/src_pack/SDL2-2.0.14/_install/lib
CFLAGS +=-L/home/wbyq/src_pack/SDL2_image-2.0.5/_install/lib -I/home/wbyq/src_pack/SDL2_image-2.0.5/_install/include -I/home/wbyq/src_pack/SDL2_image-2.0.5/_install/include/SDL2
CFLAGS+=-lSDL2 -lpthread -lm -ldl -lSDL2_image
app:
	gcc 01sdl_test.c  $(CFLAGS)

1.4.3 运行效果

SDL编译安装图片显示_第1张图片

你可能感兴趣的:(SDL,linux,sdl,SDL_Image)