现在的位置: 首页 > 综合 > 正文

linux下C语言编程8-SDL图形入门

2012年12月04日 ⁄ 综合 ⁄ 共 1726字 ⁄ 字号 评论关闭

SDL简介

     SDL 是 Simple DirectMedia Layer(简易直控媒体层)的缩写。它是一个跨平台的多媒
体库,以用于直接控制底层的多媒体硬件的接口。这些多媒体功能包括了音频、键盘和鼠标
(事件)    、游戏摇杆等。当然,最为重要的是提供了 2D 图形帧缓冲(framebuffer)的接口,
以及为 OpenGL 与各种操作系统之间提供了统一的标准接口以实现 3D 图形。从这些属性我
们可以看出,SDL 基本上可以认为是为以电脑游戏为核心开发的多媒体库。
     SDL 支持主流的操作系统,包括 Windows 和 Linux。在官方的介绍中,我们可以找到
它所支持的其他平台。 (SDL supports Linux, Windows, Windows CE, BeOS, MacOS, Mac OS X,
FreeBSD, NetBSD, OpenBSD, BSD/OS, Solaris, IRIX, and QNX. ) 。SDL 本身从 C 语言开发,
并且能很好的在 C++等高级语言中使用。在官方可以看到 SDL 所支持的语言很多,包括
Ada, C#, Eiffel, Erlang, Euphoria, Guile, Haskell, Java, Lisp, Lua, ML, Objective C, Pascal, Perl,
PHP, Pike, Pliant, Python, Ruby, Smalltalk, and Tcl.

 

SDL官网

http://www.libsdl.org/

SDL手册http://www.libsdl.org/docs.php,手册不错,可下载。

 

安装SDL

我的系统为ubuntu,安装方法为:

sudo apt-get install libsdl1.2-dev
顺便把下面几个补充包装上:
sudo apt-get install libsdl-image1.2-dev
sudo apt-get install libsdl-mixer1.2-dev
sudo apt-get install libsdl-ttf2.0-dev

安装完成后,头文件(*.h)会放在目录/usr/include/SDL/下.

 

include

(1)#include "SDL.h"

(2)#include <SDL.h>

(3)#include "SDL/SDL.h"

(4)#include <SDL/SDL.h>

这4个是一样的,SDL必须全为大写。

 

编译格式

gcc -o test test.c `sdl-config --cflags --libs`

g++同gcc

 

例子

#include <SDL.h>   /* All SDL App's need this */
#include <stdio.h>

// gcc -o test test.c `sdl-config --cflags --libs`
int main() {
   
    printf("Initializing SDL./n");
   
    /* Initialize defaults, Video and Audio */
    if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) {
        printf("Could not initialize SDL: %s./n", SDL_GetError());
        exit(-1);
    }

    printf("SDL initialized./n");

    // 创建一个窗口,并加载一张图片
    SDL_Surface* screen = NULL;
    screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );

    SDL_Surface* img = SDL_LoadBMP( "hello.bmp" );

    SDL_BlitSurface( img, NULL, screen, NULL );
    //Update Screen
    SDL_Flip( screen );
    SDL_Delay( 5000 );
   
    printf("Quiting SDL./n");
   
    /* Shutdown all subsystems */
    SDL_Quit();
   
    printf("Quiting..../n");

    exit(0);
}

抱歉!评论已关闭.