日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SDL教程4——在VS2010中设置SDL扩展库

發布時間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SDL教程4——在VS2010中设置SDL扩展库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前幾節我們了解到,SDL基本庫只能加載普通的BMP圖像,如果我們還想加載其它格式的圖片,我們就需要用到SDL的擴展庫,它可以幫助我們加載BMP, PNM, XPM, LBM, PCX, GIF, JPEG, TGA and PNG等格式圖片。要下載SDL擴展幫助文檔,點擊此處!

使用擴展庫之前,我們需要先從網上下載適合VS2010的庫文件,點擊此處,進入下載!如下圖所示,下載該版本文件!

下載下來以后,同原來設置SDL基本庫一樣,我們設置好該擴展庫的使用環境,復制dll文件至exe同目錄下。

接著,我們修改上一節所寫的load_image子函數,代碼如下:

SDL_Surface *load_image(std::string filename) { //The image that's loaded SDL_Surface *loadedImage = NULL; ? //The optimized image that will be used SDL_Surface *optimizedImage = NULL; //Loaded the image using SDL_image loadedImage = IMG_Load(filename.c_str()); ? //If the image loaded if(loadedImage != NULL) { //Create an optimized image optimizedImage = SDL_DisplayFormat(loadedImage); ? //Free the old image SDL_FreeSurface(loadedImage); } ? //Return the optimized image return optimizedImage; ? }

然后我們就可以加載包括BMP之內其它格式圖片。在此,我準備了兩幅圖片,一幅test_png.png的png格式圖片:

還有一幅background.bmp的bmp格式背景圖片:

完整代碼如下所示:

#include "SDL.h" #include "SDL_image.h" #include <string> ? //The attributes of the screen const int SCREEN_WIDTH = 600; const int SCREEN_HEIGHT = 450; const int SCREEN_BPP = 32; ? //The surfaces that will be usede SDL_Surface *message = NULL; SDL_Surface *background = NULL; SDL_Surface *screen = NULL; ? SDL_Surface *load_image(std::string filename) { //The image that's loaded SDL_Surface *loadedImage = NULL; ? //The optimized image that will be used SDL_Surface *optimizedImage = NULL; //Loaded the image using SDL_image // loadedImage = SDL_LoadBMP(filename.c_str()); loadedImage = IMG_Load(filename.c_str()); ? //If the image loaded if(loadedImage != NULL) { //Create an optimized image optimizedImage = SDL_DisplayFormat(loadedImage); ? //Free the old image SDL_FreeSurface(loadedImage); } ? //Return the optimized image return optimizedImage; ? } ? void apply_surface(int x,int y,SDL_Surface* source,SDL_Surface *destination) { //Make a temporary rectangle to hold the offsets SDL_Rect offset; ? //Give the offset to the rectangle offset.x = x; offset.y = y; ? //Blit the surface SDL_BlitSurface(source,NULL,destination,&offset); } ? int main(int argc,char *argv[]) { //Initialize all SDL_subsystems if(SDL_Init(SDL_INIT_EVERYTHING) == -1) { return 1; } //Set up Screen screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE); if(screen == NULL) { return 1; } ? //Set the window caption SDL_WM_SetCaption("Hello World",NULL); ? //Load Image message = load_image("test_png.png"); background = load_image("background.bmp"); //Apply the background to the screen apply_surface(0,0,background,screen); ? ? //apply the message to the screen apply_surface(105,78,message,screen); //Update Screen if(SDL_Flip(screen) == -1) { return 1; } //Wait 2 seconds SDL_Delay(20000); ? //Free the loaded image SDL_FreeSurface(message); SDL_FreeSurface(background); ? //Quit SDL SDL_Quit(); return 0; }

最后效果如下:

轉載于:https://www.cnblogs.com/MrTan/archive/2013/02/23/2923346.html

總結

以上是生活随笔為你收集整理的SDL教程4——在VS2010中设置SDL扩展库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。