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
下载地址: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
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
#include
#include
#include
#include
typedef enum
{
false,
true
} bool;
int main(int argc,char *argv[])
{
if(SDL_Init(SDL_INIT_VIDEO))/*初始化SDL*/
{
printf("初始化失败ERR=%srn",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,worldn");
quit=false;
printf("quit=%dn",quit);
}
else if(event.type==SDL_MOUSEBUTTONDOWN)/*鼠标点击事件*/
{
if(event.button.button==SDL_BUTTON_LEFT)/*左键*/
{
printf("left clickedn");
SDL_BlitSurface(bmp_surface,NULL,surface,NULL);
}
if(event.button.button==SDL_BUTTON_RIGHT)/*右键*/
{
printf("right clickedn");
SDL_BlitSurface(pngsurface,NULL,surface,NULL);
}
}
else if(event.type==SDL_KEYDOWN)//键盘事件
{
printf("key=%dn",event.key.keysym.sym);
if(event.key.keysym.sym==SDLK_UP)
{
rect.y-=10;
printf("upn");
}
else if(event.key.keysym.sym==SDLK_DOWN)
{
rect.y+=10;
printf("downn");
}
else if(event.key.keysym.sym==SDLK_LEFT)
{
rect.x-=10;
printf("leftn");
}
else if(event.key.keysym.sym==SDLK_RIGHT)
{
rect.x+=10;
printf("rightn");
}
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;
}
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)
全部0条评论
快来发表一下你的评论吧 !