1. 程式人生 > >Qt開發: Qt使用SDL2開發庫 -- -- 【圖文並茂】

Qt開發: Qt使用SDL2開發庫 -- -- 【圖文並茂】

1 下載SDL2


2 解壓SDL2.0


3 Qt5專案檔案新增

INCLUDEPATH+=E:/FFMPEG/SDL2/include
LIBS+=-LE:/FFMPEG/SDL2/lib/x86-lSDL2
LIBS+=-LE:/FFMPEG/SDL2/lib/x86-lSDL2main
LIBS+=-LE:/FFMPEG/SDL2/lib/x86-lSDL2test

4 測試程式碼

#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毫秒,2s後自動關閉

    //釋放記憶體
    SDL_FreeSurface( gHelloWorld );//釋放空間
    gHelloWorld = NULL;

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

    SDL_Quit();//退出SDL

    return 0;
}


5 遇到的問題


error: undefined reference to `qMain(int, char**)

解決方法:

#ifdef__MINGW32__
#undefmain/*PreventsSDLfromoverridingmain()*/
#endif

6 推薦部落格: