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. jpeg编解码概述

    本博文为概览性介绍.后面有空了再分几篇博文分别介绍所用到的技术细节. 1.编解码目标 编码和解码是个逆过程.jpeg编码的目的在于图形去冗余,进行数据压缩,解码的目的在于还原图像,使能够进行预览. 2 ...

  2. 微服务实战系列(十)-网关高可用之中间件Keepalived

    1.场景描述 因为要做网关的高可用,用到了keepalived+nginx,来保证nginx的高可用,如下图: 安装了keepavlived,走了一些弯路,记录下吧,nginx的安装就不多说了,博客已 ...

  3. oracle中插入一条记录后,重新登录查找不到数据

    你插入了数据,但是没有提交.其他Session也就是你再次登录后自然就看不到了(但是在当前回话可以看到插入的数据),但是你用SQLPLUS EXIT之后再次登录就可以看到插入的数据了,因为ORACLE ...

  4. Centos-清屏命令-clear

    clear 清理屏幕输出 相关快捷键 ctrl + l

  5. Pots(POJ - 3414)【BFS 寻找最短路+路径输出】

    Pots(POJ - 3414) 题目链接 算法 BFS 1.这道题问的是给你两个体积分别为A和B的容器,你对它们有三种操作,一种是装满其中一个瓶子,另一种是把其中一个瓶子的水都倒掉,还有一种就是把其 ...

  6. vue显示后端传递的图片流

    一.显示部分(组件我使用的vuetify) <template> <v-container fluid> <v-card width="100%" m ...

  7. Windows7 提示“无法访问 xxxx,您没有权限访问,请与网络管理员联系请求访问权限”的解决办法

    Windows7 客户端访问提示"无法访问 xxxx,您没有权限访问,请与网络管理员联系请求访问权限"的解决办法

  8. 配置DVWA漏洞环境

    web萌新,因为在别人的环境上练习总有点不舒服,所以在本地搭建了网站:下面记录一下搭建的步骤 DVWA:是一个漏洞环境包,可以用phpstudy或者wamp解析:所以要想配置这个环境,就必须有这两个软 ...

  9. git-代码分支管理

    1. git代码分支管理     DEV SIT UAT PET PRE PRD PROD常见环境英文缩写含义 英文缩写 英文 中文 DEV development 开发 SIT System Int ...

  10. swoft根据表创建实体

    php bin/swoft entity:gen table= table1,table2,table3,... [root@localhost swoft]# php bin/swoft entit ...