9

I've made a simple 2D game engine using C# and DirectX and it's fully functional for the demo I made to test it. I have a Timer object that uses QueryPerformanceCounter and I don't know what's the better choice: use only one timer in the game loop to update everything in the game, or an independent timer in every object that needs one.

My worry is that when I try to implement threads, what will happen with timers? What happens with the sync?

Tim M.
  • 53,671
  • 14
  • 120
  • 163
andresjb
  • 101
  • 4
  • I would lean towards a single timer which dispatched tasks (on different threads if needed)...but I'm no game programmer. – Tim M. Oct 12 '12 at 22:48
  • I have a class to check the status for the keyboard and other for the mouse, also I have "game states" that do all the logic (e.g. intro, menu, game). Each one of that classes have a timer for his own, and the game loop have another. Every class manage his own timer. – andresjb Oct 12 '12 at 22:49
  • 4
    Surprisingly enough, most game programming is not significantly different than any other programming. It requires a solid understand of the business logic, multithreading and synchronization, and algorithms... just like every other non-trivial programming position. – corsiKa Oct 12 '12 at 22:50
  • @corsiKa +1. A strange attack of sanity. Unfortunately, being right does not make you/I popular, but I don't care:) – Martin James Oct 13 '12 at 00:14

1 Answers1

2

You had better to have an Update (user interaction and other calculatings like hittest) and Draw functions for each game object. Your timer should call the topmost Update method and this method will call the update methods of each object. Drawing can be done with another timer or just a while loop until game ends. My choice is having only one timer for update. For the differences of timers; Comparing the Timer Classes

aliassce
  • 1,197
  • 6
  • 19