在QT5中搭建SDL2環境以及顯示bmp圖片

        在VisualStudio 2012中配置SDL可以參考這篇文章:SDL2學習筆記1-環境搭建以及HelloSDL

        將SDL窗口嵌入MFC總可以參考這篇文章:在MFC中使用SDL2.0(SDL窗口嵌入到MFC中)



        本文測試環境:win7 64位 + Qt5 + SDL2 ;

             第一步下載Windows下SDL2的庫文件。在這裏下載,如下圖:

        第二步新建QT工程。此處新建了一個純C++項目(新建QT項目配置基本一樣),如下:

        第三步將下載後的lib文件夾和include文件夾以及一張640*480的bmp格式圖片複製到main.cpp文件的上一層目錄下。如下:


        第四步配置lib庫。打開.pro文件,如下:


        在.pro文件的後面加入如下代碼:

#第一種方法:格式爲:  LIBS += -L+相對路徑+空格+-l+庫文件的名字(不加.lib)
LIBS += -L../lib/x86 -lSDL2
LIBS += -L../lib/x86 -lSDL2main
LIBS += -L../lib/x86 -lSDL2test

#第二種方法:格式爲:  LIBS += -L+絕對路徑+空格+-l+庫文件的名字(不加.lib)
#LIBS += -LE:/CODE/QT/lib/x86 -lSDL2
#LIBS += -LE:/CODE/QT/lib/x86 -lSDL2main
#LIBS += -LE:/CODE/QT/lib/x86 -lSDL2test


#第三種方法:格式爲:  LIBS += -L+相對路徑++庫文件的名字(加.lib)
#LIBS += -L../lib/x86/SDL2.lib
#LIBS += -L../lib/x86/SDL2main.lib
#LIBS += -L../lib/x86/SDL2test.lib

#第四種方法:
#LIBS += -L../lib/x86
#LIBS += SDL2.lib
#LIBS += SDL2main.lib
#LIBS += SDL2test.lib

        第五步爲main.cpp添加SDL程序。程序如下:

#include <iostream>

#include "../include/SDL.h"
#undef main


using namespace std;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main()
{
    //The window we'll be rendering to
    SDL_Window* gWindow = NULL;

    //The surface contained by the window
    SDL_Surface* gScreenSurface = NULL;

    //The image we will load and show on the screen
    SDL_Surface* gHelloWorld = NULL;

    //首先初始化   初始化SD視頻子系統
    if(SDL_Init(SDL_INIT_VIDEO)<0)
    {
        printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
        return false;
    }
    //創建窗口
    gWindow=SDL_CreateWindow("SHOW BMP",//窗口標題
                            SDL_WINDOWPOS_UNDEFINED,//窗口位置設置
                            SDL_WINDOWPOS_UNDEFINED,
                            SCREEN_WIDTH,//窗口的寬度
                            SCREEN_HEIGHT,//窗口的高度
                            SDL_WINDOW_SHOWN//顯示窗口
                            );
    if(gWindow==NULL)
    {
        printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
        return false;
    }
    //Use this function to get the SDL surface associated with the window.
    //獲取窗口對應的surface
    gScreenSurface=SDL_GetWindowSurface(gWindow);

    //加載圖片
    gHelloWorld = SDL_LoadBMP("../Hello_World.bmp");//加載圖片
    if( gHelloWorld == NULL )
    {
        printf( "Unable to load image %s! SDL Error: %s\n", "Hello_World.bmp", SDL_GetError() );
        return false;
    }
    //Use this function to perform a fast surface copy to a destination surface.
    //surface的快速複製
    //下面函數的參數分別爲: SDL_Surface* src ,const SDL_Rect* srcrect , SDL_Surface* dst ,  SDL_Rect* dstrect
    SDL_BlitSurface( gHelloWorld ,     NULL ,                     gScreenSurface ,          NULL);
    SDL_UpdateWindowSurface(gWindow);//更新顯示copy the window surface to the screen
    SDL_Delay(2000);//延時2000毫秒

    //釋放內存
    SDL_FreeSurface( gHelloWorld );//釋放空間
    gHelloWorld = NULL;

    SDL_DestroyWindow(gWindow);//銷燬窗口
    gWindow = NULL ;

    SDL_Quit();//退出SDL

    return 0;
}


        其中需要注意的有:

        1.添加SDL頭文件:#include"../include/SDL.h"

        2.在頭文件的下面要加入這條語句:#undef main,這是因爲SDL中的 SDL_main.h已經定義了main。

 

    第六步編譯運行程序,可以看到運行結果爲:



整個測試工程可以在這裏下載:http://download.csdn.net/detail/hjl240/9063263








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