Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Running time in c++  (Read 1495 times)

HavingPhun

  • Bay Watcher
    • View Profile
Running time in c++
« on: February 07, 2013, 03:33:38 pm »

I am going to make a game. But I am wondering how I can have a fake time in game. I know I could have variables: Hour, Minute, Second, Etc. But how can I have the program add one or more minutes to the time every second of real time. I know I would have a loop and I would go min++ or min + 30. But how can I tell it to do something every second real time? I don't want to pause the program while it does it though. All I found online is a function. I think was sleep(1000) <-- That would sleep for one second I'm pretty sure. But I don't want to pause the program.

EDIT: I know that boost has something useful for this. I will also get it for multithreading. But I have to wait for now to get it(I think, unless my internet will let me download it.). But I want to know a way to do it without boost for now.
« Last Edit: February 07, 2013, 03:39:25 pm by HavingPhun »
Logged

cerapa

  • Bay Watcher
  • It wont bite....unless you are the sun.
    • View Profile
Re: Running time in c++
« Reply #1 on: February 07, 2013, 03:51:45 pm »

Some simple googling reveals this.
Logged

Tick, tick, tick the time goes by,
tick, tick, tick the clock blows up.

HavingPhun

  • Bay Watcher
    • View Profile
Re: Running time in c++
« Reply #2 on: February 07, 2013, 04:17:45 pm »

Some simple googling reveals this.
I searched around and found that before posting. But I can't see how I could use those to have the program to do something every 1 second. Or I actually just had any idea. Wouldn't it make more sense to just add time every cycle of the loop as long as the game is running? Since everyone could be running the game at different speeds. Doesn't a DF fortress mode year progress quicker in realtime if you were running at 1 million fps vs. 60 fps. I know those are far - fetched but isn't that how I should do it do to different computing speeds?
Logged

cerapa

  • Bay Watcher
  • It wont bite....unless you are the sun.
    • View Profile
Re: Running time in c++
« Reply #3 on: February 07, 2013, 04:32:52 pm »

If you want everything to work as fast as possible, regardless of other factors, do your stuff every loop.

If the connection to real time is a important, do things every loop after a certain time. Eg check how much time has passed every loop, if over 1000(or anything else) then execute the function.

And there is a third one, good for platformers and such, which is that you calculate your FPS and use that as a reverse multiplier for movement and animations, so everything moves at the same realtime speed regardless of loops.

If you really want to have something be excecuted every second precisely, then you need to have a seperate thread monitoring the time. But thats sorta needless, unless your FPS regularly dips below 5.
« Last Edit: February 07, 2013, 04:34:49 pm by cerapa »
Logged

Tick, tick, tick the time goes by,
tick, tick, tick the clock blows up.

Normandy

  • Bay Watcher
    • View Profile
Re: Running time in c++
« Reply #4 on: February 07, 2013, 04:45:37 pm »

If you're going to make a game, any game library worth its salt will have timing functions available to you to precisely time your loops. And any game tutorial out there worth its salt will also explain how to use these timing functions to fun your game at a fixed FPS. If you're looking at C++ (and for various reasons, you probably shouldn't), look into SFML (http://www.sfml-dev.org/) as a general purpose graphics/windowing library. However, instead, I recommend you begin with a more reasonable choice like python or lua and use pygame (http://www.pygame.org/news.html) or love (https://love2d.org/). If you want something more specific, you'll have to google it yourself.
Logged

HavingPhun

  • Bay Watcher
    • View Profile
Re: Running time in c++
« Reply #5 on: February 07, 2013, 05:01:36 pm »

If you're going to make a game, any game library worth its salt will have timing functions available to you to precisely time your loops. And any game tutorial out there worth its salt will also explain how to use these timing functions to fun your game at a fixed FPS. If you're looking at C++ (and for various reasons, you probably shouldn't), look into SFML (http://www.sfml-dev.org/) as a general purpose graphics/windowing library. However, instead, I recommend you begin with a more reasonable choice like python or lua and use pygame (http://www.pygame.org/news.html) or love (https://love2d.org/). If you want something more specific, you'll have to google it yourself.
   I'm going to use libtcod which has sdl. I would choose sfml since it is more friendly with c++ than sdl. I still might if I can disable sdl in libtcod. But anyways. I already learned c++ fairly decently and want to continue to learn it. I don't care how hard it is and I would rather use it now instead of learning a new language and switching back later. Can't start working on the actual game until around the 17th so for now I'm just screwing around with a few things.

   I'm not wanting to start a programming langauge war or a what is better thread but I was just wondering why you think I shouldn't choose c++? Since I already have gotten pretty far in my c++ learning and this is supposed to help teach me even more about programming ill use c++. Then if it turns out I used bad ways of programming i'll go back and clean up my code. Ill have my book that taught it to me out during the whole process of making the game.

Thanks cerapa and Normandy for the help(and anyone else who posts too.)
Logged

Normandy

  • Bay Watcher
    • View Profile
Re: Running time in c++
« Reply #6 on: February 07, 2013, 06:09:09 pm »

If you're set on using libtcod, you're looking for these functions for timing (straight from the API docs): http://doryen.eptalys.net/data/libtcod/doc/1.5.2/html2/system_time.html?c=true&cpp=true&cs=true&py=true&lua=true

So personally I just think using C++ can get quite frustrating at times, and that's all I'll say on the subject. A lot of other threads discuss language choice (i.e. see the Roguelike Development Megathread (http://www.bay12forums.com/smf/index.php?topic=30113.0))and I just wanted to make sure you're learning languages with an open mind.
Logged

HavingPhun

  • Bay Watcher
    • View Profile
Re: Running time in c++
« Reply #7 on: February 07, 2013, 07:25:40 pm »

If you're set on using libtcod, you're looking for these functions for timing (straight from the API docs): http://doryen.eptalys.net/data/libtcod/doc/1.5.2/html2/system_time.html?c=true&cpp=true&cs=true&py=true&lua=true

So personally I just think using C++ can get quite frustrating at times, and that's all I'll say on the subject. A lot of other threads discuss language choice (i.e. see the Roguelike Development Megathread (http://www.bay12forums.com/smf/index.php?topic=30113.0))and I just wanted to make sure you're learning languages with an open mind.
Ok thank you.
Logged