How do you implement a wait time?

I've been wondering about how everyone else does this. Is it possible to print messages from a vector every second while continuously loading more messages into the vector?

I admit I don't really understand how doing something like time(&start); works - I pretty much just modified what I found on the difftime C++ Reference page.

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
void Game::PrintMessages()
{
    time_t start, end;

    vector<string>::iterator iter;
    iter = EventLog.begin();

    while (true)
    {
        time(&start);
        while (true)
        {
            time(&end);
            double dif = difftime(end, start);
            if (dif > waitTime) // if enough time has elapsed
                break;
        }
        if (iter != EventLog.end())
        {
            cout << *iter << endl;  // print a message
            EventLog.erase(iter);
        }
        else
            break;
    }
}


As you can see, this requires that I've already stored all the events into the vector - I can't load anything while doing it this way. I don't really need to, but I'm curious.
This is for a game?

http://www.cplusplus.com/forum/articles/28558/


Anyway, for games what I would do is have a deque/list/whatever of strings to display, then during drawing only draw the one in front()

Then every second simply pop_front() and the drawing code will automatically draw the next in line.
It's not really a game - it's part of one of the beginner exercises I found on this site.

★ Modify the program to run in real time, with each turn lasting 2 seconds, and a one second pause between each announcement.


What I'm trying to figure out there was a way to make the messages (dependant on stuff happening in the program) update and display in timed intervals - assuming that there is a message.

I do have SFML installed for if I want to work with graphics, fancier UI, etc, but I'm still more used to the console at this point.

(Btw, is there anything you'd recommend for getting started with programming simplistic games? I get confused with concepts like game states, framerates, etc.. I've read through SFML tutorials, but I don't really understand that well.)
sleep() is the easy way to do this:

http://www.delorie.com/gnu/docs/glibc/libc_445.html

for finer than 1 second resolution, try nanosleep():

http://cc.byexamples.com/2007/05/25/nanosleep-is-better-than-sleep-and-usleep/

however, to use these functions effectively, you may have to use something like POSIX threads so your machine can do work (like adding to that vector - this may require a mutex) while it's sleeping.
Last edited on
Topic archived. No new replies allowed.