linux图形编程之SDL

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;
}

效果:
linux图形编程之SDL_第1张图片

例子:加载一张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;
}

linux图形编程之SDL_第2张图片

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;
}

linux图形编程之SDL_第3张图片

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

程序执行:
linux图形编程之SDL_第4张图片

你可能感兴趣的:(linux环境编程,linux)