2
\$\begingroup\$

Recently I started working on a game with the spritekit engine. My question is not about spritekit in specific though, but generally about game engines.

When I write a loop and run it (eg while i < 100000) my cpu usage goes to 100% normally, but when I run the test game there is no changes especially on cpu usage, why? (I know game engines run in a loop that includes logic and graphic commands.)

\$\endgroup\$
4
  • \$\begingroup\$ Please do not post questions to multiple stackexchange sites. Relevant: meta.stackexchange.com/questions/64068/… \$\endgroup\$
    – bummzack
    Mar 23, 2015 at 10:26
  • \$\begingroup\$ Ok.sorry.how can i remove it \$\endgroup\$ Mar 23, 2015 at 10:30
  • \$\begingroup\$ Since the question probably fits better on gamedev.SE, you'd have to ask a moderator over at stackoverflow to close the question over there (flag the question for moderator attention). \$\endgroup\$
    – bummzack
    Mar 23, 2015 at 14:38
  • \$\begingroup\$ For introduction to game programming principles I liked the book Game Programming Patterns by Bob Nystrom. Here the link to the chapter about game loops: gameprogrammingpatterns.com/game-loop.html \$\endgroup\$
    – R.K.
    Mar 23, 2015 at 17:21

2 Answers 2

2
\$\begingroup\$

Typically most of the more feature complete game engines actually have 2 game loops. The first is a fixed step loop aimed at iteration around every 16ms for that ideal 60fps zone.

The second is the "as fast as possible" loop.

The idea is that some actions like physics processing need to have some idea of process in order to be calculated correctly and other actions like drawing things on the screen are best served up at the highest possible rate (unless vertical sync is enabled forcing a limit).

Essentially the fixed step is handled by a timer that simply calls a function every x amount of time and the fast as possible loop is just an endless "while(true)" type loop.

For the most part you want to aim to keep your main thread in your game / engine as clean and empty as possible.

With multicore cpu's being the common situation these days even if you maxxed out that thread you should only see around 10% to 30% cpu load so my guess is you are doing a lot of multithreaded cpu intensive work in order to maxx out your cpu.

\$\endgroup\$
2
  • \$\begingroup\$ i want to know more about this,what should i do?i am new to develop games and i want to know the bases. \$\endgroup\$ Mar 23, 2015 at 15:22
  • \$\begingroup\$ Best advice I can give you is to start by building some games in engines that already have the looping handled for you. Once you get your head round the timing issues associated with building stuff "per frame" you can then work up to building your own game engine. It's a massively broad topic. \$\endgroup\$
    – War
    Mar 23, 2015 at 18:34
1
\$\begingroup\$

The game loop is normally called update() that would update everything that happens since the last frame, then draw() that draws the changes, your game loop will work to ensure both update and draw run frequently so that if a large calculation is being run, the game screen doesn't freeze. So your update method will be given a max time if competition, if it doesn't run in that time then update is paused, the draw method is called and so on.

Additionally if the update finished before its max time your method normally would sleep until it is needed next so updates dont run too frequently.

\$\endgroup\$
2
  • \$\begingroup\$ Not all engines work that way, for example in unity there's a difference between update and fixedupdate and draw in terms of timing which my answer covers. You want to draw as many frames as possible but you only want to update certain components based on fixed steps. The whole application does not work on a fixed step timing. \$\endgroup\$
    – War
    Mar 23, 2015 at 18:36
  • \$\begingroup\$ Due noted but it's a great example of a simple game engine that works workout adding additional complications. Other game engines refine depending on the game for speed, networking, architecture, or requirements. \$\endgroup\$ Mar 23, 2015 at 18:40

Not the answer you're looking for? Browse other questions tagged .