SDL庫并沒有提供直接畫點的函數。不過在開發文檔中提供了一個在任意視頻模式下畫出一個像素點的例子。我們可以寫自己的畫點函數。 有了畫點函數意義非凡,我們可以使用它加上數學中美麗的方程繪出各種各樣驚艷的圖案。比如蝴蝶曲線啥的。這里簡單的繪制一個點,主要用于說明使用。
#include <stdio.h>#include <stdlib.h>#include <SDL.h>#define Uint32 unsigned int#define Uint16 unsigned short/* custom draw_Point */void DrawPixel(SDL_Surface *screen, int x, int y, Uint32 color){ Uint16 *bufp; if(SDL_MUSTLOCK(screen)){ if(SDL_LockSurface(screen) < 0){ return ; } } bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x; *bufp = color; if(SDL_MUSTLOCK(screen)){ SDL_UnlockSurface(screen); } SDL_UpdateRect(screen,x,y,1,1);}int main(){ SDL_Surface *screen; Uint32 color; if(SDL_Init(SDL_INIT_VIDEO) < 0){ f結果:在屏幕上繪制一個白點。 不過,在SDL_draw庫中倒是存在著基本的繪畫函數,我們包含相應的頭文件,然后調用庫函數即可,連接時加上選項-lSDL_draw 添加代碼:#include <SDL_draw.h>...Draw_Pixel(screen,320,240,color);SDL_UpdateRect(screen,0,0,0,0);這種現象很有意思,為什么自定義畫點函數不需要更新,但是SDL_draw庫的Draw_Pixel函數需要更新呢?兩者的內容不一樣? 話說回來,怎么查看void Draw_Pixel(SDL_Surface *super, Sint16 x, Sint16 y, Uint32 color);的具體實現啊。
簡單應用SDL_draw庫即可,它提供了函數void Draw_Line(SDL_Surface *super, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color);,我們可以在SDL_draw-1.2.13/./doc/index.html 查看相關的說明。當然我們也可以書寫自己的函數。這里直接用庫函數簡單體驗。

對于面而言,圖形很多啊,矩形,圓形,不規則多邊形等等。
void Draw_Circle(SDL_Surface *super, Sint16 x0, Sint16 y0, Uint16 r,Uint32 color);void Draw_FillCircle(SDL_Surface *super, Sint16 x0, Sint16 y0, Uint16 r, Uint32 color);void Draw_HLine(SDL_Surface *super,Sint16 x0,Sint16 y0, Sint16 x1, Uint32 color);void Draw_VLine(SDL_Surface *super, Sint16 x0,Sint16 y0, Sint16 y1, Uint32 color);void Draw_Rect(SDL_Surface *super, Sint16 x,Sint16 y, Uint16 w,Uint16 h, Uint32 color);void Draw_FillRect(SDL_Surface *super, Sint16 x,Sint16 y, Uint16 w,Uint16 h, Uint32 color);void Draw_Ellipse(SDL_Surface *super, Sint16 x0, Sint16 y0, Uint16 Xradius, Uint16 Yradius, Uint32 color);void Draw_FillEllipse(SDL_Surface *super, Sint16 x0, Sint16 y0, Uint16 Xradius, Uint16 Yradius, Uint32 color);void Draw_Round(SDL_Surface *super, Sint16 x0,Sint16 y0, Uint16 w,Uint16 h, Uint16 corner, Uint32 color);void Draw_Round(SDL_Surface *super, Sint16 x0,Sint16 y0, Uint16 w,Uint16 h, Uint16 corner, Uint32 color);在SDL_draw庫文件夾中,有一個sdldrawtest.c的文件,我們編譯連接,可以發現多邊形的效果。
root@Ubuntu1:/home/edemon/SDL_draw-1.2.13# gcc sdldrawtest.c -o sdlTest -lSDL -lSDL_drawroot@ubuntu1:/home/edemon/SDL_draw-1.2.13# ./sdlTest
在博文linux圖形編程之SDL中,我們使用SDL庫函數SDL_LoadBMP(const char *)加載過圖片,但是這局限于BMP圖片,有其他的方法來處理更多格式的圖片嗎? SDL有許多的開發庫,比如:
| 庫 | 作用 |
|---|---|
| SDL | 基本庫 |
| SDL_image | 圖像庫 |
| SDL_mixer | 混音庫 |
| SDL_net | 網路庫 |
| SDL_ttf | TrueType字體庫 |
我們想要的方案就在圖象庫中。 SDL_image支持的圖像格式:BMP, GIF, JPEG, LBM, PCX, PNG, PNM, TGA, TIFF, WEBP, XCF, XPM, XV。 下載SDL_image-1.2.2.tar.gz,網址:http://www.libsdl.org/projects/SDL_image/release/ 然后解壓,進入文件夾后,安裝三部曲,./configure; make; make install 安裝的時候Linux最好聯網,我在不聯網的情況下安裝時,jpglib庫就沒能正常安裝。 安裝過程中有這樣的信息:
為以后的方便編譯連接,直接將/usr/local/lib下的image的相關文件復制到/usr/lib下。
shell> cd /usr/local/libshell> sudo cp *image* /usr/lib/頭文件復制到/usr/include下:edemon@ubuntu1:~/SDL_image-1.2.2$ sudo cp SDL_image.h /usr/include/ 我們可以看到他和/usr/local/include/SDL下的SDL_image.h是沒有區別的。 edemon@ubuntu1:/usr/local/include/SDL$ diff SDL_image.h /home/edemon/SDL_image-1.2.2/SDL_image.h 在同樣的操作步驟后,我不明白,為什么SDL基本庫可以通過man來查閱函數,但是SDL_image庫中的函數就不能。 接下來,我們使用函數IMG_LOAD()來加載一張png圖片。 cat LoadPic.c
在我的ubuntu上始終不支持JPG(JPEG)啊,坑了我一晚上。這是為什么,我的代碼有問題? 自己將LoadPic.c中加載png圖片改成加載jpg圖片。
在SDL_image文件夾中查看源碼IMG_jpg.c,我們發現函數SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)在#ifdef LOAD_JPG代碼區間內,于是預先定義LOAD_JPG,在主函數外添加#define LOAD_JPG,還是一樣的效果。 那么我就使用安裝生成的showimage進行測試,發現這樣仍然有這樣的提示信息:Unsupported image format
先記著吧。
新聞熱點
疑難解答