mac 中 用 qt配置 SDL 2
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mac 中 用 qt配置 SDL 2相关的知识,希望对你有一定的参考价值。
1.安装SDL 22.同样也是各种动态与静态问题。也可以直接用官网上的。
主要是他默认只能显示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的主要内容,如果未能解决你的问题,请参考以下文章