//把图片加载到SDL_Texture

SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren){

        SDL_Texture *texture = nullptr;

        SDL_Surface *loadedImage = SDL_LoadBMP(file.c_str());

        texture = SDL_CreateTextureFromSurface(ren, loadedImage);

        SDL_FreeSurface(loadedImage);

        return texture;

}

//渲染纹理(渲染图片)

void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y){

        SDL_Rect dst;

        dst.x = x;

        dst.y = y;

        SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);//获取图像的宽、高

        SDL_RenderCopy(ren, tex, NULL, &dst);//纹理复制给渲染器

}

int main()

{

//初始化SDL、Window、前景、后景

SDL_Init(SDL_INIT_VIDEO)

SDL_CreateWindow("Lesson 2", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);

SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)

SDL_Texture *background = loadTexture(resPath + "background.bmp", renderer);

SDL_Texture *image = loadTexture(resPath + "image.bmp", renderer);

while(1){

SDL_RenderClear(renderer);

SDL_QueryTexture(background, NULL, NULL, &bW, &bH);//获取背景的宽、高

renderTexture(background, renderer, 0, 0);

renderTexture(background, renderer, bW, 0);

renderTexture(background, renderer, 0, bH);

renderTexture(background, renderer, bW, bH);

SDL_QueryTexture(image, NULL, NULL, &iW, &iH);//获取前景的宽、高

renderTexture(image, renderer, x, y);

SDL_RenderPresent(renderer);//渲染

SDL_Delay(1000);

}

}

最后的效果图

完整代码如下

#include <iostream>
#include <SDL.h>
#include "res_path.h"
#include "cleanup.h" /*
* Lesson 2: Don't Put Everything in Main
*/
//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480; /*
* Log an SDL error with some error message to the output stream of our choice
* @param os The output stream to write the message too
* @param msg The error message to write, format will be msg error: SDL_GetError()
os << msg << " error: " << SDL_GetError() << std::endl;
}
/*
* Loads a BMP image into a texture on the rendering device
* @param file The BMP image file to load
*/
SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren){
SDL_Texture *texture = nullptr;
//Load the image
SDL_Surface *loadedImage = SDL_LoadBMP(file.c_str());
//If the loading went ok, convert to texture and return the texture
if (loadedImage != nullptr){
texture = SDL_CreateTextureFromSurface(ren, loadedImage);
SDL_FreeSurface(loadedImage);
//Make sure converting went ok too
if (texture == nullptr){
logSDLError(std::cout, "CreateTextureFromSurface");
}
}
else {
logSDLError(std::cout, "LoadBMP");
}
return texture;
}
/*
* Draw an SDL_Texture to an SDL_Renderer at position x, y, preserving
* the texture's width and height
* @param tex The source texture we want to draw
* @param ren The renderer we want to draw too
* @param x The x coordinate to draw too
* @param y The y coordinate to draw too
*/
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y){
//Setup the destination rectangle to be at the position we want
SDL_Rect dst;
dst.x = x;
dst.y = y;
//Query the texture to get its width and height to use
SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);
SDL_RenderCopy(ren, tex, NULL, &dst);
} int main(int, char**){
//Start up SDL and make sure it went ok
if (SDL_Init(SDL_INIT_VIDEO) != 0){
logSDLError(std::cout, "SDL_Init");
return 1;
} //Setup our window and renderer
if (window == nullptr){
logSDLError(std::cout, "CreateWindow");
SDL_Quit();
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr){
logSDLError(std::cout, "CreateRenderer");
cleanup(window);
SDL_Quit();
return 1;
} //The textures we'll be using
const std::string resPath = getResourcePath("Lesson2");
SDL_Texture *background = loadTexture(resPath + "background.bmp", renderer);
SDL_Texture *image = loadTexture(resPath + "image.bmp", renderer);
//Make sure they both loaded ok
if (background == nullptr || image == nullptr){
cleanup(background, image, renderer, window);
SDL_Quit();
return 1;
} //A sleepy rendering loop, wait for 3 seconds and render and present the screen each time
for (int i = 0; i < 3; ++i){
//Clear the window
SDL_RenderClear(renderer); //Get the width and height from the texture so we know how much to move x,y by
//to tile it correctly
int bW, bH;
SDL_QueryTexture(background, NULL, NULL, &bW, &bH);
//We want to tile our background so draw it 4 times
renderTexture(background, renderer, 0, 0);
renderTexture(background, renderer, bW, 0);
renderTexture(background, renderer, 0, bH);
renderTexture(background, renderer, bW, bH); //Draw our image in the center of the window
//We need the foreground image's width to properly compute the position
//of it's top left corner so that the image will be centered
int iW, iH;
SDL_QueryTexture(image, NULL, NULL, &iW, &iH);
int x = SCREEN_WIDTH / 2 - iW / 2;
int y = SCREEN_HEIGHT / 2 - iH / 2;
renderTexture(image, renderer, x, y); //Update the screen
SDL_RenderPresent(renderer);
//Take a quick break after all that hard work
SDL_Delay(1000);
}

一个SDL2.0程序的分析的更多相关文章

  1. 03-第一个C语言程序的分析

    本文目录 一.代码分析 二.开发和运行C程序的步骤 三.总结 说明:这个C语言专题,是学习iOS开发的前奏.也为了让有面向对象语言开发经验的程序员,能够快速上手C语言.如果你还没有编程经验,或者对C语 ...

  2. SDL2.0教程翻译·目录

    原文地址:SDL 2.0 Tutorial Index Welcome! 下面的教程旨在为你提供一个SDL2.0以及c++中游戏设计和相关概念的介绍.在本教程中,我们假定你对C++有一定程度上的知识, ...

  3. Android:日常学习笔记(2)——分析第一个Android应用程序

    Android:日常学习笔记(2)——分析第一个Android应用程序 Android项目结构 整体目录结构分析 说明: 除了APP目录外,其他目录都是自动生成的.APP目录的下的内容才是我们的工作重 ...

  4. 单片机小白学步系列(十四) 点亮第一个LED的程序分析

    本篇我们将分析上一篇所写的程序代码.未来学习单片机的大部分精力,我们也将放在程序代码的编写上. 可是不用操心.我会很具体的介绍每一个程序的编写思路和各种注意事项等. 之前我们写的程序例如以下: #in ...

  5. 通过汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的

    秦鼎涛  <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验一 通过汇编一个简单的C程序,分析汇编代码 ...

  6. 通过反汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的

    实验一:通过反汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的 学号:20135114 姓名:王朝宪 注: 原创作品转载请注明出处   <Linux内核分析>MOOC课程http: ...

  7. win8.1下golang+sdl2.0环境搭建

    sdl2.0的golang绑定我是使用的这个,但是它的官方介绍里面只有linux以及OSX系统的说明,没有windows的,在我的mbp上弄好以后就考虑在win下也搭建一个开发环境,这样就能比较方便的 ...

  8. [原]如何在Android用FFmpeg+SDL2.0解码图像线程

    关于如何在Android上用FFmpeg+SDL2.0解码显示图像参考[原]如何在Android用FFmpeg+SDL2.0解码显示图像 ,关于如何在Android使用FFmpeg+SDL2.0解码声 ...

  9. JavaScript中的ParseInt("08")和“09”返回0的原因分析及解决办法

    今天在程序中出现一个bugger ,调试了好久,最后才发现,原来是这个问题. 做了一个实验: alert(parseInt("01")),当这个里面的值为01====>07时 ...

随机推荐

  1. treeview所有节点递归解法及注意!!!!!!!!!!!!!!!!!

    好吧 我把所有之前写的都删了,只为这一句话“所有变量切记小心在递归函数内部初始化”,包括:布尔,变量i,等等.至于为什么....递归就是调用自己,你初始化以后的变量,等再次调用的时候又回来了 bool ...

  2. 一、mysql下载与安装

    1.下载 官网:https://www.mysql.com/ 注意,其实我看了好的博文,下载截图五花八门,官网内容是变动的,布局也不一样,所以不要指望标准参照那个博文去找到安装包下载: 不过,你可以不 ...

  3. SQL-W3School-高级:SQL NULL 函数

    ylbtech-SQL-W3School-高级:SQL NULL 函数 1.返回顶部 1. SQL ISNULL().NVL().IFNULL() 和 COALESCE() 函数 请看下面的 &quo ...

  4. 图解 HTTP 笔记(八)——常见 Web 攻击技术

    本章主要讲解 HTTP 通信过程中的一些常见 Web 攻击技术 一.跨站脚本攻击 跨站脚本攻击(Cross-Site Scripting, XSS)是指通过存在安全漏洞的 Web 网站注册用户的浏览器 ...

  5. PAT 甲级 1026 Table Tennis (30 分)(坑点很多,逻辑较复杂,做了1天)

    1026 Table Tennis (30 分)   A table tennis club has N tables available to the public. The tables are ...

  6. 免费申请https

    https://blog.csdn.net/MariaOzawa/article/details/81877756 实测有效

  7. 【error】 for i in range(len(shape)/2): TypeError: 'float' object cannot be interpreted as an integer

    Q: for i in range(len(shape)/2):TypeError: 'float' object cannot be interpreted as an integer A: for ...

  8. 申请 Let's Encrypt 通配符 HTTPS 证书

    目录 一.背景知识 1.1.什么是通配符证书 1.2.什么是 Let's Encrypt 二.证书申请(certbot) 2.1.系统确定 2.2.工具安装 2.3.证书申请 2.4.证书查看 2.5 ...

  9. 集群架构03·MySQL初识,mysql8.0环境安装,mysql多实例

    官方网址 https://dev.mysql.com/downloads/mysql/社区版本分析 MySQL5.5:默认存储引擎改为InnoDB,提高性能和可扩展性,增加半同步复制 MySQL5.6 ...

  10. 关于LuckyE博客的前言

    第一次开始尝试怎么写博客,也不知道写点什么好...... 开始写博客的原因其实很多,主要原因是因为最近考出cisp-pte证书,然后前几天电话面试某比较有名的安全公司(x盟)的渗透测试工程师实习生被撸 ...