在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








  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值