1. 程式人生 > >mac 中 用 qt配置 SDL 2

mac 中 用 qt配置 SDL 2

qt sdl

1.安裝SDL 2
2.同樣也是各種動態與靜態問題。也可以直接用官網上的。
主要是他默認只能顯示bmp格式的圖片。同時要加上(SDL_Delay(2000000000000);//延時2000毫秒,2s後自動關閉)不然看不到

#-------------------------------------------------
#
# Project created by QtCreator 2018-03-09T13:30:28
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = testsdl
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += main.cpp        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui
INCLUDEPATH+=/usr/local/include
#INCLUDEPATH+=/usr/local/Cellar/sdl2/2.0.8/include
LIBS += -L/usr/local/Cellar/sdl2/2.0.8/lib -lSDL2_test -lSDL2 -lSDL2main
#include "mainwindow.h"
#include <QApplication>
#include "SDL2/SDL.h"
#include <iostream>
#undef main
using namespace std;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
//    w.show();
    //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("/Users/allenboy/Desktop/allen.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(2000000000000);//延時2000毫秒,2s後自動關閉

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

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

        SDL_Quit();//退出SDL

        return 0;

    return a.exec();
}

mac 中 用 qt配置 SDL 2