1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | char Line1[] = "HELLO\0"; const int endofLine1 = sizeof(Line1); char delayedText[endofLine1] = {}; int currentLetter = 0; int delay = 10; int currTime = delay while(...) { if(currentLetter <= endofLine1) { char nextChar = Line1[currentLetter]; if(currTime == delay) { strcat(delayedText, &nextChar); currentLetter++; currTime = 0; } else { currTime++; } } } |
1 | DelayTxt(Line1, endofLine1, ¤tLetter, &currTime, delay, delayedText); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | void DelayTxt(char line[], int lastLetter, int *currentLetter, int *currTime, int delay, char delayedText[]) { if(*currentLetter <= lastLetter) { char nextChar = Line1[*currentLetter]; if(*currTime == delay) { strcat(delayedText, &nextChar); *currentLetter++; *currTime = 0; } else { *currTime++; } } } |
1 | char nextChar = Line1[*currentLetter]; |
1 | char nextChar = line[*currentLetter]; |
1 | strcat(delayedText, &nextChar); |
1 | *currTime++; |
mmozeiko
Probably not related to your issue, but this:
1 strcat(delayedText, &nextChar);
is a very incorrect code.
strcat will append string from second argument to string in first argument.
You are not passing string in second argument. You are passing pointer to one character. You need to pass pointer to array of characters, where last one in 0. Currently you are lucky that program does not simply crash. Because strcat will start reading characters from memory after nextChar - basically it will get some garbage values.
But this:
1 *currTime++;
will not do what you probably want. This code will increment currTime pointer, not the value it points to. Same with *currentLetter++. Use a debugger to verify this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | DelayedTXT dt1 = {}; dt1.init("HELLO YOU SON OF A MONKEY", 3); while(running) { switch(gameState) { case MainMenu: { dt1.currentLetter = 0; memset(dt1.textDest, 0, sizeof(dt1.textDest)); memset(dt1.resultString, 0, sizeof(dt1.resultString)); }break; case showtext: { DT(&dt1) }break; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | struct DelayedTXT { char *textSrc; char *textDest; char *resultString; int stringLength = 0; int currentLetter = 0; int delay = 0; int currentTime = 0; void init(char *in_text, int in_delay) { textSrc = (char*)calloc(256, sizeof(char)); textDest = (char*)calloc(256, sizeof(char)); resultString = (char*)calloc(256, sizeof(char)); strcpy(textSrc, in_text); stringLength = strlen(in_text); delay = in_delay; currentTime = in_delay; } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | void DT(DelayedTXT *dt) { if((dt->currentLetter) < dt->stringLength) { if((dt->currentTime) == dt->delay) { dt->textDest[dt->currentLetter] = dt->textSrc[dt->currentLetter]; strcpy(dt->resultString, dt->textDest); (dt->currentLetter) += 1; (dt->currentTime) = 0; } else { (dt->currentTime) += 1; } } } |