原文:http://lazyfoo.net/tutorials/SDL/22_timing/index.php

Timing

计时

Last Updated 3/10/14


Another important part of any sort of gaming API is the ability to handle time. In this tutorial we'll make a timer we can restart.

对于任何游戏API,另一个重要的部分是能够处理时间参量。在这个教程中,我们将写一个能够重启的定时器。


//Using SDL, SDL_image, SDL_ttf, standard IO, strings, and string streams
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <stdio.h>
#include <string>
#include <sstream>

For this tutorial we'll be using string streams and have to include the sstream header which should come standard with your C++ compiler.

在这个教程中,将使用到string streams,并且include了stream头文件,你的c++编译器应该有这个头文件。


bool loadMedia()
{
//Loading success flag
bool success = true; //Open the font
gFont = TTF_OpenFont( "22_timing/lazy.ttf", 28 );
if( gFont == NULL )
{
printf( "Failed to load lazy font! SDL_ttf Error: %s\n", TTF_GetError() );
success = false;
}
else
{
//Set text color as black
SDL_Color textColor = { 0, 0, 0, 255 }; //Load prompt texture
if( !gPromptTextTexture.loadFromRenderedText( "Press Enter to Reset Start Time.", textColor ) )
{
printf( "Unable to render prompt texture!\n" );
success = false;
}
} return success;
}

As mentioned in the, you want to minimize the amount of times you render text. We'll have a texture to prompt input and a texture to display the current time in milliseconds. The time texture changes every frame so we have to render that every frame, but the prompt texture doesn't change so we can render it once in the file loading function.
正如font rendering tutorial中所提到的,减少文字渲染所需的时间。我们用一个texture来展示输入信息和一个texture来显示时间(ms)。time texture在每一帧中都变化,所以不得不渲染每一帧。但是prompt texture 并不变,所以只用在加载文件的函数中渲染一次就可以。


 //Main loop flag
bool quit = false; //Event handler
SDL_Event e; //Set text color as black
SDL_Color textColor = { 0, 0, 0, 255 }; //Current time start time
Uint32 startTime = 0; //In memory text stream
std::stringstream timeText;

Before we enter the main loop we want to declare some variables. The two we want to pay attention to is the startTime variable (which is an Unsigned integer that's32bits) and the timeText variable which is a string stream.

在进入主循环之前,我们想声明一些变量。得加以注意的两个变量是startTime--这是一个无符号32为整型和timeText--这是一个string stream。
For those of you who have never used string streams, just know that they function like iostreams only instead of reading or writing to the console, they allow you to read and write to a string in memory. It'll be easier to see when we see them used further on in the program.

如果你以前没用过string stream,那只要知道它们就像iostream一样,除了向终端读写,它们允许你向内存读写string。当我们在接下来的程序中看到时,很容易弄明白这个。


There's a function called SDL_GetTicks which returns the time since the program started in milliseconds. For this demo, we'll be having a timer that restarts every time we press the return key.

SDL_GetTicks 将返回从程序开始至此的时间(ms)。在这个示例中,我们写了一个Timer(计时器),每当我们按下返回键时,计时器将重启。
Remember how we initialized the start time to 0 at the start of the program? This means the timer's time is just the current time since the program started returned by SDL_GetTicks. If we were to restart the timer when SDL_GetTicks was at 5000 milliseconds (5 seconds), then at 10,000 milliseconds the current time - the start time would be 10000 minus 5000 would be 5000 milliseconds. So even though the timer contained by SDL_GetTicks hasn't restarted, we can have a timer keep track of a relative start time and reset its start time.

还记得在程序一开始时我们是怎样初始化start time 为 0 的吗?这说明计时器的值为从SDL_GetTicks被调用的时刻到当前的时刻 这一区间。比如,当SDL_GetTicks 在5000ms时,重启计时器。然后等到10000ms时(当前时刻),那么start time的值将是10000-5000=5000ms。

Lazy的SDL教程 翻译----Lesson 22 Timing的更多相关文章

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

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

  2. Deep Learning 教程翻译

    Deep Learning 教程翻译 非常激动地宣告,Stanford 教授 Andrew Ng 的 Deep Learning 教程,于今日,2013年4月8日,全部翻译成中文.这是中国屌丝军团,从 ...

  3. SDL教程第一和第二个视频的笔记

    观看正月点灯笼的SDL教程,地址http://www.tudou.com/listplay/9eG9tkk91oQ.html #include <stdio.h> #include < ...

  4. lesson 22 by heart

    lesson 22 by heart on end = continuously 连续不断地 know/learn sth by heart 记忆sth falter: speak hesitantl ...

  5. 网页3D引擎“Babylon.JS”入门教程翻译总结

    使用三个月的业余时间把官方教程的入门部分译为中文并上传到github,在下一步编程前做一个总结. 历程: 最早接触游戏编程是在大三下学期,用汇编语言和实验室里的单片机.触摸屏.电机(提供声效)编的打地 ...

  6. 《Entity Framework 6 Recipes》中文翻译系列 (22) -----第五章 加载实体和导航属性之延迟加载

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第五章 加载实体和导航属性 实体框架提供了非常棒的建模环境,它允许开发人员可视化地使 ...

  7. 【RL-TCPnet网络教程】第22章 RL-TCPnet之网络协议IP

    第22章      RL-TCPnet之网络协议IP 本章节为大家讲解IP(Internet Protocol,网络协议),通过前面章节对TCP和UDP的学习,需要大家对IP也有个基础的认识. (本章 ...

  8. Dapper简易教程(翻译自Github上StackExchange/Dapper)

    本文源自:https://github.com/cnxy/Dapper-zh-cn 本博客作者与Github上作者(cnxy)实为同一个作者.由于笔者翻译水平有限,文本中错误难免,欢迎指正! 本文翻译 ...

  9. Entity Framework教程翻译 ---- 系列教程

    Entity Framework教程(第二版) (翻译)Entity Framework技巧系列之十四 - Tip 56 (翻译)Entity Framework技巧系列之十三 - Tip 51 - ...

随机推荐

  1. Leetcode题目75.颜色分类(双指针-中等)

    题目描述: 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白 ...

  2. CISCO实验记录四:备份路由器的IOS

    1.配置好TFTP服务器(假设ip为192.168.2.1) 2.查看当前IOS名称 #show version 输出中有一段:System image file is "bootflash ...

  3. 爬虫实践——数据存储到Excel中

    在进行爬虫实践时,我已经爬取到了我需要的信息,那么最后一个问题就是如何把我所爬到的数据存储到Excel中去,这是我没有学习过的知识. 如何解决这个问题,我选择先百度查找如何解决这个问题. 百度查到的方 ...

  4. 使用 PM2 将 Node.js 的集群变得更加容易

    介绍 众所周知,Node.js运行在Chrome的JavaScript运行时平台上,我们把该平台优雅地称之为V8引擎.不论是V8引擎,还是之后的Node.js,都是以单线程的方式运行的,因此,在多核心 ...

  5. pytorch-Resnet网络

    残差网络:将输入层与输出层进行连接,保证了经过这层网路结构,网络的运算能力不会出现较大的改变 网络解析: 第一层网络: 输入网络经过一个卷积层,再经过一个batch_normalize, 再经过一个r ...

  6. 提高组刷题营 DAY 1 下午

    DFS 深度优先搜索 通过搜索得到一棵树形图 策略:只要能发现没走过的点,就走到它.有多个点可走就随便挑一个,如果无路可走就回退,再看有没有没走过的点可走. 在图上寻找路径[少数可用最短路解决]:最短 ...

  7. git分布式版本管理系统

    Git是分布式版本管理系统Svn是集中式版本管理系统 git速度快,适合大规模协同开发 什么是分布式版本管理系统 假如有10个人,每个人的代码库都是独立的,自己想进行代码提交回滚都可以,无需链接中央服 ...

  8. Java中AWT、Swing与SWT三大GUI技术的原理与效率差异

    Java中AWT.Swing与SWT三大GUI技术的原理与效率差异 转 https://blog.csdn.net/weixin_37703598/article/details/81843810   ...

  9. C++之ListNode

    单链表,弄清楚可stl中list的区别 ListNode的结构 struct ListNode { int val; //当前结点的值 ListNode *next; //指向下一个结点的指针 Lis ...

  10. Java数据结构之TreeMap

    一.源码注释 /** * TreeMap基于NavigableMap 的一个红黑树的实现.TreeMap会根据比较器comparator对键值对的key进行比较进行排序,如果没有比较器就是用key的自 ...