SDL教程4——在VS2010中设置SDL扩展库
生活随笔
收集整理的這篇文章主要介紹了
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扩展库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU 2063 过山车【二分图最大匹配
- 下一篇: [转帖][攻防测试工具]系统监控必备工具