Getting current time in milliseconds

Posted by user90293423 on Game Development See other posts from Game Development or by user90293423
Published on 2013-10-18T17:55:59Z Indexed on 2013/10/18 22:19 UTC
Read the original article Hit count: 132

Filed under:
|

How to get the current time in milliseconds?

I'm working on a hacking simulation game and when ever someone connects to another computer/NPC, a login screen popups with a button on the side called BruteForce.

When BruteForce is clicked, what i want the program to do is, calculate how many seconds cracking the password is going to take based on the player's CPU speed but that's the easy part.

The hard part is i want to enter a character in the password's box every X milliseconds based on a TimeToCrack divided by PasswordLength formula.

But since i don't know how to find how many milliseconds have elapsed since the second has passed, the program waits until the CurrentTime is higher than the TimeBeforeTheLoopStarted + HowLongItTakesToTypeaCharacter which is always going to be a second.

How would you handle my problems?

I've commented the game breaking part.

    std::vector<QString> hardware = user.getHardware();
    QString CPU = hardware[0];

    unsigned short Speed = 0;

    if(CPU == "OMG SingleCore 1.8GHZ"){
        Speed = 2;
    }

    const short passwordLength = password.length(); /* It's equal to 16 */

    int Time = passwordLength / Speed; 
    double TypeSpeed = Time / passwordLength; 

    time_t t = time(0);
    struct tm * now = localtime(&t);

    unsigned short EndTime = (now->tm_sec + Time) % 60;
    unsigned short CurrentTime = 0;
    short i = passwordLength - 1;
    do{
        t = time(0);
        now = localtime(&t);
        CurrentTime = now->tm_sec;
        do{
            t = time(0);
            now = localtime(&t);
        }while(now->tm_sec < CurrentTime + TypeSpeed); /* Highly flawed */ 
        /* Do this while your integer value is under this double value */

        QString tempPass = password;
        tempPass.chop(i);
        ui->lineEdit_2->setText(tempPass);
        i--;

    }while(CurrentTime != EndTime);

© Game Development or respective owner

Related posts about c++

Related posts about qt