Lazy的SDL教程 翻译

时间:2022-09-26 12:05:46

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

Timing

计时

Lazy的SDL教程 翻译

 

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。