前几节我们了解到,SDL基本库只能加载普通的BMP图像,如果我们还想加载其它格式的图片,我们就需要用到SDL的扩展库,它可以帮助我们加载BMP, PNM, XPM, LBM, PCX, GIF, JPEG, TGA and PNG等格式图片。要下载SDL扩展帮助文档,点击此处!

使用扩展库之前,我们需要先从网上下载适合VS2010的库文件,点击此处,进入下载!如下图所示,下载该版本文件!

下载下来以后,同原来设置SDL基本库一样,我们设置好该扩展库的使用环境,复制dll文件至exe同目录下。

接着,我们修改上一节所写的load_image子函数,代码如下:

SDL_Surface *load_image(std::string filename)
{
    //The image that's loaded
    SDL_Surface *loadedImage = NULL;
 
    //The optimized image that will be used
    SDL_Surface *optimizedImage = NULL;
    
    //Loaded the image using SDL_image
    loadedImage = IMG_Load(filename.c_str());
 
    //If the image loaded
    if(loadedImage != NULL)
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat(loadedImage);
 
        //Free the old image
        SDL_FreeSurface(loadedImage);
    }
 
    //Return the optimized image
    return optimizedImage;
 
}

然后我们就可以加载包括BMP之内其它格式图片。在此,我准备了两幅图片,一幅test_png.png的png格式图片:

还有一幅background.bmp的bmp格式背景图片:

完整代码如下所示:

#include "SDL.h"
#include "SDL_image.h"
#include <string>
 
//The  attributes of the screen
const int SCREEN_WIDTH = 600;
const int SCREEN_HEIGHT = 450;
const int SCREEN_BPP = 32;
 
//The surfaces that will be usede
SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;                  
 
SDL_Surface *load_image(std::string filename)
{
    //The image that's loaded
    SDL_Surface *loadedImage = NULL;
 
    //The optimized image that will be used
    SDL_Surface *optimizedImage = NULL;
    
    //Loaded the image using SDL_image
//    loadedImage = SDL_LoadBMP(filename.c_str());
    loadedImage = IMG_Load(filename.c_str());
 
    //If the image loaded
    if(loadedImage != NULL)
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat(loadedImage);
 
        //Free the old image
        SDL_FreeSurface(loadedImage);
    }
 
    //Return the optimized image
    return optimizedImage;
 
}
 
void apply_surface(int x,int y,SDL_Surface* source,SDL_Surface *destination)
{
    //Make a temporary rectangle to hold the offsets
    SDL_Rect offset;
 
    //Give the offset to the rectangle
    offset.x = x;
    offset.y = y;
 
    //Blit the surface
    SDL_BlitSurface(source,NULL,destination,&offset);
}
 
int main(int argc,char *argv[])
{
    //Initialize all SDL_subsystems
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        return 1;
    }
    
    //Set up Screen
    screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);
    if(screen == NULL)
    {
        return 1;
    }
 
    //Set the window caption
    SDL_WM_SetCaption("Hello World",NULL);
 
    //Load Image
    message = load_image("test_png.png");
    background = load_image("background.bmp");
    
    //Apply  the background  to the screen
    apply_surface(0,0,background,screen);
 
 
    //apply the message to the screen
    apply_surface(105,78,message,screen);
    
    //Update Screen
    if(SDL_Flip(screen) == -1)
    {
        return 1;
    }
    
    //Wait 2 seconds
    SDL_Delay(20000);
 
    //Free the loaded image
    SDL_FreeSurface(message);
    SDL_FreeSurface(background);
 
    //Quit SDL
    SDL_Quit();
    
    return 0;
}

最后效果如下:

转载于:https://www.cnblogs.com/MrTan/archive/2013/02/23/2923346.html

SDL教程4——在VS2010中设置SDL扩展库相关推荐

  1. Lua 中写 C 扩展库时用到的一些技巧

    Lua 中写 C 扩展库时用到的一些技巧(转) 通常,C 扩展库中 C 代码会有一些数据要放在 lua 状态机中.Lua 提供的方案是放在它的 注册表 中.如文档所言,因为 Lua 的注册表是全局共享 ...

  2. Spire.Presentation使用教程:在Java中设置PowerPoint图像的透明度

    Spire.Presentation for Java专业的 PowerPoint API,它允许开发人员在 Java 应用程序中创建.读取.写入.转换和保存 PowerPoint 文档,而无需安装 ...

  3. 在 ubuntu 中设置安装 boost 库

    c++ 编程中有时候要用到 Boost 库,这里总结一下安装的过程. 一.平台 ubuntu 16.04 Boost_1_65_1 二.安装 1.下载安装文件. 1).http://www.boost ...

  4. sublime后缀_在sublime text中如何设置某种扩展名文件的默认语法

    下面由sublime教程栏目给大家介绍在sublime text中设置某种扩展名文件的默认语法,希望对需要的朋友有所帮助! 对于正常扩展名(后缀)的文件,sublime text都能识别. 对于其他不 ...

  5. 详细介绍Qt,ffmpeg 和SDl 教程之间的联系

    Qt与 ffmpeg 与 SDl 教程是本文要介绍的内容,从多个角度介绍本文,运用了qmake,先来看内容. 1.  注释 从" #" 开始,到这一行结束. 2.  指定源文件 1 ...

  6. SDL教程零基础入门 简单操作 day1

    1.0 SDL 简介 文章目录 1.0 SDL 简介 1.1 什么是 SDL? 1.2 SDL 可以做什么? 2. 在VS上获取和安装 SDL 2.1 SDL2库下载 2.2 安装SDL2 2.3 在 ...

  7. ffmpeg 和 SDL 教程

    教程1:制作屏幕录像 代码:tutorial01.c 概要 电影文件有很多基本的组成部分.首先,文件本身被称为容器Container,容器的类型决定了信息被存放在文件中的位置.AVI和Quicktim ...

  8. 在QT搭建的播放器外壳中嵌入SDL的窗口

    [cpp] view plaincopy print? <span style="font-family: Arial, Verdana, sans-serif; white-spac ...

  9. ffmpeg和SDL教程 04:创建线程

    2019独角兽企业重金招聘Python工程师标准>>> 概述 前面,我们利用 SDL 的音频处理功能优势增加声音支持.每次我们提供音频支持,需要创建一个函数,供SDL启动线程来回调. ...

最新文章

  1. Ocelot + Consul实践
  2. 八年之痒!除了NLP和CV,人工智能就不能干点别的啥了?
  3. 近期发现的一些-20190519
  4. github使用心得
  5. QT的QDBusContext类的使用
  6. python3的fft_科学网—用Python、Matlab、C实现傅立叶变换FFT() - 康建的博文
  7. cbitmap 从内存中加载jpg_Pytorch数据加载的分析
  8. Flutter高级第3篇:底部 Tab 切换保持页面状态的几种方法
  9. 全球完美打通元宇宙、DeFi、NFT的区块链游戏平台
  10. multisim红绿灯元器件在哪里_实验二Multisim交通灯仿真.ppt
  11. Recorder+人脸识别︱国内人脸识别技术趋势与识别难点、技术实践
  12. 学CNC编程,首先要从哪里开始?
  13. 企业邮箱怎么开通?手机微信怎么绑定公司邮箱?
  14. 4只鸭子在同一个圆圈内游泳,问:4只鸭子出现在同一个半圆内的概率是多少?Python模拟
  15. 4 anbox 树莓派_Anbox让你在Linux上“原生运行”Android应用
  16. PLSQL12.0.7的下载、安装及使用教程
  17. mini2440之--adc程序
  18. WinIo驱动级键盘模拟编程
  19. 数字银行论坛 | 银行数字化转型路径与策略
  20. 运放技术——电压跟随器

热门文章

  1. Python基础之函数
  2. PAT-乙级-1062 最简分数
  3. Jmeter——for循环控制器和if逻辑控制器
  4. mongodb报错一例
  5. PHP扩展开发(3)-config.m4
  6. c++成员变量与构造函数
  7. 添加组合索引时,做相等运算字段应该放在最前面
  8. 基于Docker的Redis集群简单搭建
  9. NotificationManager: notifyAsUser: tag=null, id=6, user=UserHandle{0}
  10. codeblocks如何导入项目_T3如何利用系统工具导入导出复制存货档案