一个SDL2.0程序的分析
//把图片加载到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程序的分析的更多相关文章
- 03-第一个C语言程序的分析
本文目录 一.代码分析 二.开发和运行C程序的步骤 三.总结 说明:这个C语言专题,是学习iOS开发的前奏.也为了让有面向对象语言开发经验的程序员,能够快速上手C语言.如果你还没有编程经验,或者对C语 ...
- SDL2.0教程翻译·目录
原文地址:SDL 2.0 Tutorial Index Welcome! 下面的教程旨在为你提供一个SDL2.0以及c++中游戏设计和相关概念的介绍.在本教程中,我们假定你对C++有一定程度上的知识, ...
- Android:日常学习笔记(2)——分析第一个Android应用程序
Android:日常学习笔记(2)——分析第一个Android应用程序 Android项目结构 整体目录结构分析 说明: 除了APP目录外,其他目录都是自动生成的.APP目录的下的内容才是我们的工作重 ...
- 单片机小白学步系列(十四) 点亮第一个LED的程序分析
本篇我们将分析上一篇所写的程序代码.未来学习单片机的大部分精力,我们也将放在程序代码的编写上. 可是不用操心.我会很具体的介绍每一个程序的编写思路和各种注意事项等. 之前我们写的程序例如以下: #in ...
- 通过汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的
秦鼎涛 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验一 通过汇编一个简单的C程序,分析汇编代码 ...
- 通过反汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的
实验一:通过反汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的 学号:20135114 姓名:王朝宪 注: 原创作品转载请注明出处 <Linux内核分析>MOOC课程http: ...
- win8.1下golang+sdl2.0环境搭建
sdl2.0的golang绑定我是使用的这个,但是它的官方介绍里面只有linux以及OSX系统的说明,没有windows的,在我的mbp上弄好以后就考虑在win下也搭建一个开发环境,这样就能比较方便的 ...
- [原]如何在Android用FFmpeg+SDL2.0解码图像线程
关于如何在Android上用FFmpeg+SDL2.0解码显示图像参考[原]如何在Android用FFmpeg+SDL2.0解码显示图像 ,关于如何在Android使用FFmpeg+SDL2.0解码声 ...
- JavaScript中的ParseInt("08")和“09”返回0的原因分析及解决办法
今天在程序中出现一个bugger ,调试了好久,最后才发现,原来是这个问题. 做了一个实验: alert(parseInt("01")),当这个里面的值为01====>07时 ...
随机推荐
- Python 学习 —— 进阶篇(装饰器、类的特殊方法)
Python基础部分学完之后,在进入其OOP部分前,先理解一下其装饰器这种结构,其功能可类比于Java中的面向切面编程,下面参见具体实例: def log(f): def fn(x): print ' ...
- UML期末复习题——2.7:UML Sequence Diagram
第七题:顺序图 重要概念: 1. 顺序图的主要介绍,在之前对第5题“系统顺序图”的解析讲解里已经很详细了,在这里只是对部分不全面的内容进行补充. 第五题:系统顺序图&后置条件 http://w ...
- Flex 布局教程实例
Flex 布局教程实例 一.Flex 布局是什么? Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. 任何一个容器都可以指定为 F ...
- 关于OpenCL中三重循环的执行次序
源自OpenGPU社区的一个帖子的讨论: 一个有意思的openCL问题
- Android中实现日期时间选择器(DatePicker和TimePicker)
利用Android应用框架提供的DatePicker(日期选择器)和TimePicker(时间选择器),实现日期时间选择器. Dialog的Content布局文件(date_time_dialog.x ...
- sqlmap工具介绍
工具介绍 sqlmap是一个自动化的SQL注入工具,其主要功能是扫描,发现并利用给定的URL的SQL注入漏洞,目前支持的数据库是MySQL, Oracle, PostgreSQL, Microsoft ...
- Python学习笔记——esle和with 语句
1. else与while组合 def showMaxFactor(num): count = num // 2 while count > 1: if num % count == 0: pr ...
- windows下安装配置winpcap
winpcap官网:http://www.winpcap.org/ 1.首先下载安装winpcap.exe,http://www.winpcap.org/install/default.htm 目的是 ...
- 【ARM-Linux开发】wubi.exe安装Ubuntu14.04出现为/检查磁盘时发生严重错误的解决方法
原文:http://jingyan.baidu.com/article/0aa22375bbffbe88cc0d6419.html 我写过一篇叫做 如何硬盘装麒麟系统 的经验,介绍了如何不使用优盘的情 ...
- POJ 1840:Eqs
Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53= The coe ...