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

最新文章

  1. Entity Framework 6 Recipes 2nd Edition(13-4)译 -> 有效地创建一个搜索查询
  2. java 事件监听 - 鼠标
  3. 基于ZigBee的家居控制系统的设计与应用
  4. 20145311利用gdb调试汇编代码
  5. OneDrive无法正常登录
  6. html转义函数
  7. Selenium - IDE模式匹配
  8. UI事件监听的击穿
  9. Interleaving String——Leetcode
  10. C#中枚举类型和int类型的转化
  11. 使用 Strace 和 GDB 调试工具的乐趣
  12. Linux新手命令
  13. 第2章 rsync(二):inotify+rsync详细说明和sersync
  14. Mysql基础--表的操作
  15. HDU 2066 最短路floyd算法+优化
  16. 深入理解Spring Redis的使用 (三)、使用RedisTemplate的操作类访问Redis
  17. java10.0.2和java 11.0.1配置环境变量
  18. ES6的字符串和数值的扩展
  19. openwrt添加内核模块
  20. python_flask框架学习之路(1)

热门文章

  1. burp suite 之 Repeater(中继器)
  2. linux循环定时任务
  3. Centos-yum软件包安装-yum
  4. IDEA 使用的配置
  5. vue显示后端传递的图片流
  6. Iptables 下 SNAT、DNAT和MASQUERADE三者之间的区别
  7. 收集的照片信息都是Excel超链接?批量命名很困难?来试试这个自制的下载器吧!
  8. Acticiti流程引擎在已知当前流程定义id的情况下获取当前流程的所有信息(包括:节点和连线)
  9. coder初入职场必备:Eclipse+Tomcat8+MAVEN+SVN 工作环境搭建
  10. day39 Pyhton 并发编程02