国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

SDL點、線、面及圖像加載

2019-11-08 02:13:03
字體:
來源:轉載
供稿:網友

SDL畫圖系列之點,線,面

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 查看相關的說明。當然我們也可以書寫自己的函數。這里直接用庫函數簡單體驗。

#include <stdio.h>#include <stdlib.h>#include <SDL.h>#include <SDL_draw.h>#define Uint32 unsigned int#define Uint16 unsigned shortint main(){ SDL_Surface *screen; Uint32 color; if(SDL_Init(SDL_INIT_VIDEO) < 0){ fprintf(stderr,"can not init SDL: %s/n",SDL_GetError()); exit(1); } screen = SDL_SetVideoMode(640,480,16,SDL_SWSURFACE); /* 640*480*16 */ if(screen == NULL){ fprintf(stderr,"can't set 640*480*16 video mode: %s/n",SDL_GetError()); exit(1); } atexit(SDL_Quit); color = SDL_MapRGB(screen->format,255,255,255); /* last three parameters are red, green and blue. */ Draw_Line(screen,10,10,300,300,color); SDL_UpdateRect(screen,0,0,0,0); SDL_Delay(3000); return 0;}shell> gcc drawLine.c -lSDL -lSDL_draw -o drawLineshell> ./drawLine

這里寫圖片描述

對于面而言,圖形很多啊,矩形,圓形,不規則多邊形等等。

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_Ell
ipse(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

這里寫圖片描述

SDL之加載圖像

在博文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庫就沒能正常安裝。 安裝過程中有這樣的信息:

chmod 644 /usr/local/lib/libSDL_image.aPATH="$PATH:/sbin" ldconfig -n /usr/local/lib----------------------------------------------------------------------Libraries have been installed in: /usr/local/lib.../bin/sh ./mkinstalldirs /usr/local/bin /bin/sh ./libtool --mode=install /usr/bin/install -c showimage /usr/local/bin/showimage/usr/bin/install -c .libs/showimage /usr/local/bin/showimage/bin/sh ./mkinstalldirs /usr/local/include/SDL /usr/bin/install -c -m 644 SDL_image.h /usr/local/include/SDL/SDL_image.hmake[1]: Leaving directory '/home/edemon/SDL_image-1.2.2'

為以后的方便編譯連接,直接將/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

#include <stdio.h>#include <stdlib.h>#include <SDL.h>#include <SDL_image.h>int main(){//#define LOAD_PNG char *file = "/home/edemon/Pictures/quan.png"; SDL_Surface *screen; SDL_Surface *picture; if(SDL_Init(SDL_INIT_VIDEO) < 0){ fprintf(stderr,"SDL_INIT_VIDEO error %s/n",SDL_GetError()); exit(1); } picture = IMG_Load(file); if(picture == NULL){ fprintf(stderr,"IMG_Load error %s/n",SDL_GetError()); exit(1); } screen = SDL_SetVideoMode(picture->w,picture->h,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); } /* int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect); */ if(SDL_BlitSurface(picture,NULL,screen,NULL) < 0){ fprintf(stderr,"SDL_BlitSurface error %s/n",SDL_GetError()); exit(1); } SDL_UpdateRect(screen, 0, 0, 0, 0); atexit(SDL_Quit); SDL_FreeSurface(picture); SDL_Delay(5000); SDL_FreeSurface(screen); return 0;}shell> gcc LoadPic.c -lSDL -lSDL_imageshell> ./a.out

在我的ubuntu上始終不支持JPG(JPEG)啊,坑了我一晚上。這是為什么,我的代碼有問題? 自己將LoadPic.c中加載png圖片改成加載jpg圖片。

./a.out IMG_Load error Unsupported image format

在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

shell> /usr/local/bin/showimage /home/edemon/Pictures/sum.jpg Couldn't load /home/edemon/Pictures/sum.jpg: Unsupported image format

先記著吧。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南乐县| 辽阳县| 五指山市| 汉寿县| 乐业县| 巴东县| 安丘市| 孟连| 精河县| 丹江口市| 衡阳县| 蒙阴县| 金堂县| 昌黎县| 陇南市| 佛坪县| 汉阴县| 临高县| 桃江县| 怀来县| 犍为县| 开原市| 于都县| 额尔古纳市| 治多县| 神木县| 屯留县| 十堰市| 温泉县| 逊克县| 钟祥市| 宿迁市| 依兰县| 乌兰察布市| 临清市| 新晃| 林甸县| 佳木斯市| 犍为县| 驻马店市| 无棣县|