linux编写图形程序,linux图形编程之SDL

本文介绍如何在Ubuntu 15.04上安装Simple DirectMedia Layer (SDL)及其相关库,包括SDL_ttf和SDL_draw,并提供了示例代码展示如何使用这些库来创建基本的图形应用程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SDL start

SDL是编写跨平台游戏和多媒体应用的支持库,包含了对图形、声音、游戏杆、线程等支持,内容丰富、应用广泛。

ubuntu15.04上安装SDL:

Source Code在 http://www.libsdl.org/download-1.2.php上,下载SDL-1.2.15.tar.gz。解压tar -zxvf SDL-1.2.15.tar.gz,在加压后的文件夹SDL-1.2.15中使用root账户进行安装三部曲./configure; make; make install

安装完成后根据安装过程中出现log我们可以知道Libraries在/usr/local/lib,而编码所需要的文件SDL.h在/usr/local/include/SDL中,这依靠find命令可以查找到。在INSTALL文件中可以发现更多的东西:

Look at the example programs in ./test, and check out the HTML

documentation in ./docs to see how to use the SDL library.

我们可以将/usr/local/lib中的库文件放到/usr/lib中,将/usr/local/include/SDL中的头文件放到/usr/include中,然后gcc就能找到了。

对于写好的程序,如果直接编译gcc -o sdl1 sdl1.c,那会出现错误:

/tmp/ccEE9bhU.o: In function `main':

sdl1.c:(.text+0x17): undefined reference to `SDL_Init'

sdl1.c:(.text+0x23): undefined reference to `SDL_GetError'

sdl1.c:(.text+0x59): undefined reference to `SDL_SetVideoMode'

sdl1.c:(.text+0x6a): undefined reference to `SDL_GetError'

sdl1.c:(.text+0x95): undefined reference to `SDL_Quit'

sdl1.c:(.text+0xc3): undefined reference to `SDL_MapRGB'

sdl1.c:(.text+0xd9): undefined reference to `SDL_FillRect'

sdl1.c:(.text+0xef): undefined reference to `SDL_UpdateRect'

sdl1.c:(.text+0x10c): undefined reference to `SDL_Delay'

collect2: error: ld returned 1 exit status

在命令的最后加上连接选项-lSDL,如:gcc -o sdl1 sdl1.c -lSDL

例子:创建一个红色屏幕,并停留5秒。

#include

#include

#include

int main(){

SDL_Surface *screen;

unsigned int color;

int x;

if(SDL_Init(SDL_INIT_VIDEO)<0){

fprintf(stderr,"SDL_Init error: %s\n",SDL_GetError());

exit(1);

}

screen = SDL_SetVideoMode(640,480,16,SDL_SWSURFACE);

if(screen == NULL){

fprintf(stderr,"couldn't set 640*480*16 bits color model, error is %s\n",SDL_GetError());

exit(1);

}

color = SDL_MapRGB(screen->format,255,0,0);

SDL_FillRect(screen,NULL,color);

SDL_UpdateRect(screen,0,0,0,0);

SDL_Delay(5000);

atexit(SDL_Quit);

return 0;

}

效果:

0818b9ca8b590ca3270a3433284dd417.png

例子:加载一张bmp图片,在窗口显示5秒。

#include

#include

#include

void show(char *png,SDL_Surface *screen,int x,int y){

SDL_Surface *image = SDL_LoadBMP(png);

SDL_Rect rect;

if(image == NULL){

fprintf(stderr,"SDL_LoadBMP error %s\n",SDL_GetError());

exit(1);

}

rect.x = x; rect.y = y;

rect.w = 1026; //image->w;

rect.h = 767; //image->h;

SDL_BlitSurface(image,NULL,screen,&rect);

SDL_UpdateRects(screen,1,&rect);

}

int main(){

SDL_Surface *screen;

int x,y;

if(SDL_Init(SDL_INIT_VIDEO) < 0){

fprintf(stderr,"SDL_INIT_VIDEO error %s\n",SDL_GetError());

exit(1);

}

screen = SDL_SetVideoMode(1026,767,24,SDL_SWSURFACE);

if(screen == NULL){

fprintf(stderr,"SDL_SetVideoMode error %s\n",SDL_GetError());

exit(1);

}

atexit(SDL_Quit);

show("test.bmp",screen,0,0);

SDL_Delay(5000);

return 0;

}

0818b9ca8b590ca3270a3433284dd417.png

SDL_ttf

SDL_ttf是一个支持trueType字体的附加库。

SDL_ttf for SDL 1.2: http://www.libsdl.org/projects/SDL_ttf/release-1.2.html

我选择SDL_ttf-2.0.11.tar.gz,解压、安装后还是将/usr/local/include, /usr/local/lib中的文件复制到/usr/include, /usr/local中,便于编译执行。

简单应用:

#include

#include

int main(){

SDL_Surface *text, *screen;

SDL_Rect rect;

TTF_Font *font;

if(SDL_Init(SDL_INIT_VIDEO) < 0){

fprintf(stderr,"SDL_Init error %s\n",SDL_GetError());

exit(1);

}

screen = SDL_SetVideoMode(440,240,16,SDL_SWSURFACE);

if(screen == NULL){

fprintf(stderr,"SDL_SetVideoMode error %s\n",SDL_GetError());

exit(1);

}

atexit(SDL_Quit);

if(TTF_Init() != 0){

fprintf(stderr,"TTF_Init error %s\n",SDL_GetError());

exit(1);

}

char *fontPath = "/usr/share/fonts/truetype/arphic/ukai.ttc";

int fontsize = 28;

SDL_Color red = {255,0,0,0};

font = TTF_OpenFont(fontPath,fontsize);

TTF_SetFontStyle(font,TTF_STYLE_NORMAL);

text = TTF_RenderUTF8_Blended(font,"hello, SDL_ttf truetype 字体",red);

TTF_CloseFont(font);

TTF_Quit();

rect.x = 30;

rect.y = 30;

rect.w = text->w;

rect.y = text->h;

SDL_BlitSurface(text,NULL,screen,&rect);

SDL_UpdateRect(screen,0,0,0,0);

SDL_FreeSurface(text);

SDL_Delay(5000);

return 0;

}

0818b9ca8b590ca3270a3433284dd417.png

SDL_draw

SDL_draw是新增的库插件。带有基本的绘图函数。

SDL_draw source code下载网址: https://sourceforge.net/projects/sdl-draw

我下载文件到用户目录/home/edemon/下,然后直接解压得到SDL_draw-1.2.13。到解压后的文件夹中, 再./configure; make; make install安装成功。

例子:利用Draw_Pixel画一条正弦曲线。

#include

#include

#include

#include

#include

#include

int main(){

SDL_Surface *screen = SDL_SetVideoMode(640,640,16,SDL_SWSURFACE);

int i;

double y;

if(screen == NULL){

fprintf(stderr,"SDL_SetVideoMode error %s\n",SDL_GetError());

exit(1);

}

if(SDL_Init(SDL_INIT_VIDEO) < 0){

fprintf(stderr,"SDL_INIT_VIDEO error %s\n",SDL_GetError());

exit(1);

}

atexit(SDL_Quit);

for(i=0;i<=640;i++){

y = 320-160*sin(3.14/180*i);

Draw_Pixel(screen,i,y,SDL_MapRGB(screen->format,0,255,0));

}

SDL_UpdateRect(screen,0,0,0,0);

SDL_Delay(6000);

printf("this is a simple sin function drawing.\n");

return 0;

}

编译时,需要设定相关的路径。

export CFLAGS="`sdl-config --cflags` -I/home/edemon/SDL_draw-1.2.13/include"

export LIBS="`sdl-config --libs` /home/edemon/SDL_draw-1.2.13/src/.libs/libSDL_draw.a"

gcc -o sin sin.c -Wall $CFLAGS $LIBS -lm

程序执行:

0818b9ca8b590ca3270a3433284dd417.png

XO_OX 0.0 这些是“XO_OX"的注解。它们会让你全面了解这个游戏,并会说明如何安装它。 什么是“XO_OX"? “XO_OX"又名“五子棋”,五子棋则咸信是流传于古中国的传统棋种之一,至今仍在民间广泛流传,规则相当简单。或许因没有形成一套独立完整的棋种理论及文化内涵,更无制定公平完善的规则来解决黑白平衡问题,一直没有得到发展,所以没有像六博、格五、弹棋等传统棋类流传广泛,导致缺少可考古的棋具或文献,直到流传到外国才规则改革。 不管是哪种五子棋,棋手在先后手的观念、空间的思维及对棋形的理解都十分重要。 游戏规则: * 行棋:一人流轮一著下于棋盘空点处,下后不得移动。 * 胜负:先把五枚或以上己棋相连成任何横纵斜方向为胜。 * 和棋: o 行棋中一方提出和棋,另一方同意则判和棋。 o 棋子落满整张棋盘仍未分出胜负为和棋。 o 一方PASS后另一方下一手也PASS为和棋。 技术规格说明: 1、用C语言调用SDL实现; 2、基于LGPL协议。 3、程序中用到了SDL_image扩展包 如何安装: 1、在终端中运行make 2、在终端中运行make install 如何卸载: make uninstall 历史: 一、2011年8月15日 项目开始,谢红负责图形模块,赵梓辰负责游戏逻辑,吕玉飞负责事件响应,范人豪负责整体架构。 二、2011年8月17日 为了增加游戏的可玩性,项目由圈叉棋升级为五子棋。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值