Here is an updated example with elapsed time handling, in this one I didn't need to reduce the frame rate. You can control the speed with the TYPING_TIME constant.
#include "DarkGDK.h"
//
class typingText
{
public:
typingText();
virtual ~typingText() {};
static const int TEXT_LENGTH = 60;
char tText[TEXT_LENGTH];
void typeText(int x, int y, int elapsedTime);
private:
static const int TYPING_TIME = 50; // milliseconds
int elTime;
int tIndex, tCurIndex;
char tTextDisp[TEXT_LENGTH];
};
//
typingText::typingText()
: tIndex(0), tCurIndex(0), elTime(0)
{
tText[0] = 0;
}
//
void typingText::typeText(int x, int y, int elapsedTime)
{
elTime += elapsedTime;
if (elTime >= TYPING_TIME) {
if (tCurIndex < strlen(tText) && tCurIndex < TEXT_LENGTH-1) ++tCurIndex;
elTime -= TYPING_TIME;
}
for (tIndex = 0; tIndex < tCurIndex; ++tIndex)
{
tTextDisp[tIndex] = tText[tIndex];
}
tTextDisp[tIndex] = 0;
dbText(x, y, tTextDisp);
}
//
void DarkGDK(void)
{
dbSyncOn();
dbSyncRate(60);
typingText myTypingText;
strncpy(myTypingText.tText, "this is my text this is my text this is my text this is my text this is my text", typingText::TEXT_LENGTH-1);
int prevTime = dbTimer();
int curTime = prevTime;
while (LoopGDK())
{
curTime = dbTimer();
dbCLS();
myTypingText.typeText(10, 10, curTime - prevTime);
prevTime = curTime;
dbSync();
}
}
If you do include such an effect in a game, I would say make it optional, because not everybody likes it. It's a nice effect in a few cut-scenes, but to watch it throughout the whole game gets annoying and tiring for the eyes in my opinion, especially if the text is long.