博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lazy的SDL教程 翻译----Lesson 22 Timing
阅读量:7249 次
发布时间:2019-06-29

本文共 3807 字,大约阅读时间需要 12 分钟。

原文: 

Timing

计时

preview.png

 

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 
#include
#include
#include
#include
#include

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 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。

转载于:https://www.cnblogs.com/Dream-Chaser/p/4638263.html

你可能感兴趣的文章
移植最新版libmemcached到VC++的艰苦历程和经验总结(上)
查看>>
诡异的bug: tcsh陷入死循环
查看>>
java-第一章-上机练习-04
查看>>
Active Directory 基础 (1)
查看>>
xml地图生成网址
查看>>
Python 练习1
查看>>
TCExam文件代码注释分析(后台首页admin/code/index.php)
查看>>
Finereport在企业级BI分析中的应用
查看>>
linux内核参数注释与优化
查看>>
linux 2.6x内核升级
查看>>
pxe
查看>>
NFS网络文件系统安装
查看>>
网页嵌入自动生成当前网页二维码图片代码
查看>>
Linux时间同步服务
查看>>
Python基础-----列表、元组、集合(2)
查看>>
iptables详解
查看>>
Redisson官方文档 - 12. 独立节点模式
查看>>
AD域笔记
查看>>
HTTP协议详解
查看>>
apache实现多端囗多域名配置
查看>>