linux下C語言編程8-SDL圖形入門

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章