FROM:http://lazyfoo.net/tutorials/SDL/05_optimized_surface_loading_and_soft_stretching/index.php

Optimized Surface Loading and Soft Stretching

Last Updated 6/11/19

Up until now we've been blitting our images raw. Since we were only showing one image, it didn't matter. When you're making a game, blitting images raw causes needless slow down. We'll be converting them to an optimized format to speed them up.

SDL 2 also has a new feature for SDL surfaces called soft stretching, which allows you to blit an image scaled to a different size. In this tutorial we'll take an image half the size of the screen and stretch it to the full size.

SDL_Surface* loadSurface( std::string path )
{
//The final optimized image
SDL_Surface* optimizedSurface = NULL; //Load image at specified path
SDL_Surface* loadedSurface = SDL_LoadBMP( path.c_str() );
if( loadedSurface == NULL )
{
printf( "Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
}
Back in our image loading function, we're going to make some modifications so the surface is converted on load. At the top of the function we pretty much load images like we did in previous tutorials, but we also declare a pointer to the final optimized image.
    else
{
//Convert surface to screen format
optimizedSurface = SDL_ConvertSurface( loadedSurface, gScreenSurface->format, 0 );
if( optimizedSurface == NULL )
{
printf( "Unable to optimize image %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
} //Get rid of old loaded surface
SDL_FreeSurface( loadedSurface );
} return optimizedSurface;
}
If the image loaded successfully in the previous lines of code, we optimize the surface we loaded.

See when you load a bitmap, it's typically loaded in a 24bit format since most bitmaps are 24bit. Most, if not all, modern displays are not 24bit by default. If we blit an image that's 24bit onto a 32bit image, SDL will convert it every single time the image is blitted.

So what we're going to do when an image is loaded is convert it to the same format as the screen so no conversion needs to be done on blit. This can be done easily with SDL_ConvertSurface. All we have to do is pass in the surface we want to convert with the format of the screen.

It's important to note that SDL_ConvertSurface returns a copy of the original in a new format. The original loaded image is still in memory after this call. This means we have to free the original loaded surface or we'll have two copies of the same image in memory.

After the image is loaded and converted, we return the final optimized image.
                //Apply the image stretched
SDL_Rect stretchRect;
stretchRect.x = 0;
stretchRect.y = 0;
stretchRect.w = SCREEN_WIDTH;
stretchRect.h = SCREEN_HEIGHT;
SDL_BlitScaled( gStretchedSurface, NULL, gScreenSurface, &stretchRect );
SDL 2 has a new dedicated function to blit images to a different size: SDL_BlitScaled. Like blitting images before, SDL_BlitScaled takes in a source surface to blit onto the destination surface. It also takes in a destination SDL_Rect which defines the position and size of the image you are blitting.

So if we want to take an image that's smaller than the screen and make it the size of the screen, you make the destination width/height to be the width/height of the screen.
Download the media and source code for tutorial here.

Back to SDL Tutorials

【转】Optimized Surface Loading and Soft Stretching的更多相关文章

  1. 【转】Beginning Game Programming v2.0

    Beginning Game Programming v2.0 Last Updated 8/19/18 Greetings everyone, welcome to the ground up re ...

  2. postgresql大批量数据导入方法

    一直没有好好关注这个功能,昨天看了一下,数据库插入有瓶颈,今天研究了一下: 主要有以下方案: 1.使用copy从文件导入: copy table_001(a, b, "f", d, ...

  3. iOS 动态 Framework 对App启动时间影响实测

    最近看到的Slow App Startup Times里提到: The dynamic loader finds and reads the dependent dynamic libraries ( ...

  4. 一个灵活的AssetBundle打包工具

      尼尔:机械纪元 上周介绍了Unity项目中的资源配置,今天和大家分享一个AssetBundle打包工具.相信从事Unity开发或多或少都了解过AssetBundle,但简单的接口以及众多的细碎问题 ...

  5. 实测iOS Dynamic Framework 对 App 启动时间的影响效果

    最近看到的Slow App Startup Times里提到: The dynamic loader finds and reads the dependent dynamic libraries ( ...

  6. OpenCASCADE7.3.0 is available for download

    OpenCASCADE7.3.0 is available for download OPEN CASCADE is pleased to announce a new public release ...

  7. 【转】Loading PNGs with SDL_image

    FROM:http://lazyfoo.net/tutorials/SDL/06_extension_libraries_and_loading_other_image_formats/index2. ...

  8. How to Configure Nginx for Optimized Performance

    Features Pricing Add-ons Resources | Log in Sign up   Guides & Tutorials Web Server Guides Nginx ...

  9. ggsci: error while loading shared libraries: libnnz11.so

    [oracle@localhost goldengate]$ ./ggsci ./ggsci: error while loading shared libraries: libnnz11.so: c ...

随机推荐

  1. 爬虫日志监控 -- Elastc Stack(ELK)部署

    傻瓜式部署,只需替换IP与用户 导读: 现ELK四大组件分别为:Elasticsearch(核心).logstash(处理).filebeat(采集).kibana(可视化) 在elastic官网下载 ...

  2. Linux Shell脚本简单语法汇总(Deepin下运行)

    整理自: https://www.runoob.com/?s=shell Shell 脚本(shell script),是一种为 shell 编写的脚本程序. 业界所说的 shell 通常都是指 sh ...

  3. 自定义带边框TextView--边框粗细不一的问题

    自定义带边框TextView 给textview加边框 最low的做法.textview外层套一层布局,然后给布局加边框样式(这么弱的做法,不能这么干) 自定义控件 canvas.drawLines ...

  4. vant实现下拉多选组件

    1.最近需要做一个移动端多选的功能,发现vant上没有多选的下拉组件,于是决定写一个,样式如下 调用部分传入值 propsselect-data-opts 传入list数据, disabled 下拉是 ...

  5. opencv的imread函数相对路径问题和 main 参数问题

    参考: https://blog.csdn.net/u013404374/article/details/80178822 https://blog.csdn.net/fujilove/article ...

  6. Apache HttpClient 4.5 在Springboot中使用

    ConnectionRequestTimeout httpclient使用连接池来管理连接,这个时间就是从连接池获取连接的超时时间,可以想象下数据库连接池 ConnectTimeout 连接建立时间, ...

  7. 学习Jmeter,这一篇就够了

    Jmeter在软件测试中的常用功能,看完你应该就会了 1.Jmeter的下载安装 1.1  Jmeter的官方网址是:http://jmeter.apache.org/:下载最新的包到本地进行解压,路 ...

  8. ActiveMQ详细入门教程系列(一)

    一.什么是消息中间件 两个系统或两个客户端之间进行消息传送,利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排队模型,它可以在分布式环境下 ...

  9. Python装饰器实现带参数和不带参数

    1 def log(text=None): 2 3 if isinstance(text, str): 4 def decorator(func): 5 @functools.wraps(func) ...

  10. Visual C# 制作DLL文件

    一.制作.dll1.首先创建一个新类库工程文件  文件->新建->项目->Visual C#->类库.填入工程文件名称,并且选择文件要存放的目录. 2.工程文件 将Class1 ...