For a school project I need to (re)create a fully functional multi-player version of R-Type without the use of the following external libraries:
- Boost
- SFML/SDL
- Qt
- Use of C++11 not allowed
Moreover, this game must be fully portable between Fedora(Linux) and Windows. I am in charge of the server so the use of any graphic libraries is strictly prohibited.
In order to create a correct game loop I need a correct Timer class, similar as those found in the SDL which implements GetTicks() or GetElapsedTime() methods. But I asked myself what would be the best way to create such a Class, so far this is how I would start:
- Creating a threaded-class using pthread(which is portable)
- Using the functions time() and difftime() in a loop to determine how much time was elapsed since the last tick.
Knowing that this class will be used by dozens of instances playing at the same time, should I use the Singleton Design Pattern? Will this methods be accurate?
EDIT: Changed the explanation of my question to fit more my needs and to be more accurate on what I am allowed to use or not.
There’s not an easy way to do what you’re thinking. Luckily, there are easy ways to do what you want.
First:
Using the functions time() and difftime() in a loop to determine how much time was elapsedThat’s a terrible idea. That will use 100% of one of your CPUs and thus slow your program to a crawl. If you want to wait a specific amount of time (a “tick” of 1/60 of a second, or 1/10 of a second), then just wait. Don’t spin a thread.header:
cpp:
None of this is perfect (especially since I don’t code for linux), but this is the general concept whenever you have to deal with the OS (since it isn’t in the standard and you cant use libraries). The Windows and GCC implementations can be in separate files if you like