Time in c++?

I thought of making a race simulation where two (or more races ) start across the galaxy and try to colonize it. I have a dilemma about a timer I was trying to make to resemble the timeline, for example a second in the compiler would be a month of time in game you could say.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

	int y = 1;
	int timeline = 0;
	int currency = 0;
	int stars_clnzd = 0; // stars colonized 
	do {
		timeline++;
            currency += 75;
		std::cout << timeline << " "; 
		Sleep(1000);
		
		if (timeline <= 1)
		{
			Sleep(10000);
			std::cout << "Our currency is " << currency << " ";
		}
	} while (y == 1);

I did some research online and the best thing I could find about time was the sleep function, my problem is, is that if I add an if statement saying if timeline reaches a certain time output this and then Sleep(5000) to add a 5 second delay for the output, but that doesn't work with the sleep function because every time I add some code like Sleep(1000) it doesn't pause output , it pauses the entire application for an extra second which I cannot work with. Sure I could add something like :
1
2
3
4
if (timeline == 3) // as in 3 seconds
		{
			std::cout << // some output
		}

but that's too specific and would waste so much code trying to code every possible output individually. I'm just looking for a good timer in short, any suggestions?
Last edited on
you want threading and sleep_for
http://www.cplusplus.com/reference/thread/this_thread/sleep_for/

also I think if you use windows' libraries, windows has a timer (or did in MFC dats) object you can use. I am sure unix has something too but I can't recall what it is, never needed it.
I prefer to do my own with a thread but its likely there are many other timer objects you can get in 3rd party libraries.

if you do not know threading, it may be a good time to learn, but if its off the table, any solution you come up with is going to become a little screwy. You end up with something like

1
2
3
4
5
6
7
8
for(ever)
{
     deal_with_timeslice; //get the current time at some resolution. 
     if(is_time_slice_for_thing1(1))  //if enough time has passed for this activity
       thing1();
     if(is_time_slice_for_thing2(2))
      thing2(); 
}


and you typically put a SMALL sleep in there because if the time between things is long the loop does nothing 99% of the time, then sleep some of that off, say 100MS or something.
Last edited on
Topic archived. No new replies allowed.