【转】Optimized Surface Loading and Soft Stretching
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() );
}
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;
}
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 );
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.
【转】Optimized Surface Loading and Soft Stretching的更多相关文章
- 【转】Beginning Game Programming v2.0
Beginning Game Programming v2.0 Last Updated 8/19/18 Greetings everyone, welcome to the ground up re ...
- postgresql大批量数据导入方法
一直没有好好关注这个功能,昨天看了一下,数据库插入有瓶颈,今天研究了一下: 主要有以下方案: 1.使用copy从文件导入: copy table_001(a, b, "f", d, ...
- iOS 动态 Framework 对App启动时间影响实测
最近看到的Slow App Startup Times里提到: The dynamic loader finds and reads the dependent dynamic libraries ( ...
- 一个灵活的AssetBundle打包工具
尼尔:机械纪元 上周介绍了Unity项目中的资源配置,今天和大家分享一个AssetBundle打包工具.相信从事Unity开发或多或少都了解过AssetBundle,但简单的接口以及众多的细碎问题 ...
- 实测iOS Dynamic Framework 对 App 启动时间的影响效果
最近看到的Slow App Startup Times里提到: The dynamic loader finds and reads the dependent dynamic libraries ( ...
- OpenCASCADE7.3.0 is available for download
OpenCASCADE7.3.0 is available for download OPEN CASCADE is pleased to announce a new public release ...
- 【转】Loading PNGs with SDL_image
FROM:http://lazyfoo.net/tutorials/SDL/06_extension_libraries_and_loading_other_image_formats/index2. ...
- How to Configure Nginx for Optimized Performance
Features Pricing Add-ons Resources | Log in Sign up Guides & Tutorials Web Server Guides Nginx ...
- ggsci: error while loading shared libraries: libnnz11.so
[oracle@localhost goldengate]$ ./ggsci ./ggsci: error while loading shared libraries: libnnz11.so: c ...
随机推荐
- fastjson 1.2.6以下版本 解析字符串末尾出现/x会陷入死循环 报oom异常
记一次使用阿里fastjson遇到的问题 项目最好将fastjson版本升级到1.2.6或以上版本 1.2.6以下版本存在漏洞,测试如下代码直接报错 public class FastJsonTest ...
- JDK1.8新特性之(三)--函数式接口
在上一篇文章中我们介绍了JDK1.8的新特性有以下几项. 1.Lambda表达式 2.方法引用 3.函数式接口 4.默认方法 5.Stream 6.Optional类 7.Nashorm javasc ...
- python-格式化(%,format,f-string)输出+输入
1-格式化输出: % 1.print('我的姓名是%s,身高%s cm'%(name,height)) 2.%s -str() ; %d–十进制3.传入值的时候一定是个元组,不是列表4.当指定长度时: ...
- javascript之判断数组的几种方法
今天和小伙伴一起出去吃饭,有个小伙伴突然问我,你是前端是吧,问一下现在前端判断数组都有哪些方法,哈哈不知道是不是考我,当时没有说全,吃过饭后看了下自己以前的小笔记这里总结一下目前知道的所有对于数组的判 ...
- CentOS7 执行 service iptables save 报错 The service command supports only basic LSB actions xxxxxx
现象描述 在 CentOS 7.6.1810 下执行 service iptables save 命令,出现如下错误: [root@test ~]# service iptables save The ...
- xshell的下载与使用
昨天刚刚立下每天一篇原创的宏图,今天就停电,到11:05才来电,没办法,学习也学不了了,就只有发一下学过的东西,才能维持得了立下的flag的那个样子,而且,老铁们,今天就不写什么原创博客了,今天转载, ...
- shell-变量的字串应用技术
1. 变量子串的常用操作 常用操作如下表: 依次举例说明: 定义aa变量,内容为"I am scajy" [root@1-241 script]# aa="I am sc ...
- 微信小程序tabbar不显示2019.04.06
app.json中pages的第一项必须在tabBar中,且这一项需要在pages的list中(与顺序无关)否则无法显示tabBar app.json中pages数组中第一项(首页),必须在tabBa ...
- excel 无效引用 所引用的单元格不能位于256列
无效引用 该文件版本所包含的公式中,所引用的单元格不能位于256列(列IW 或更远)或 65536 行以外的区域. 原因及解决方法: 1.版本问题,把两文件都另存为一致的版本,改为后缀为xlsx. 2 ...
- C 和 C++ 打起来了!曾今最亲密的伙伴到现今的不爽?
70年代初,贝尔实验室创建了C语言,它是开发UNIX的副产品.很快C就成为了最受欢迎的编程语言之一.但是对于Bjarne Stroustrup来说,C的表达能力还不够.于是,他在1983年的博士论文中 ...