Why is a “main” game loop necessary for developing a game?

前端 未结 11 1821
自闭症患者
自闭症患者 2020-12-23 13:52

I find that most game development requires a main game loop, but I don\'t know why it\'s necessary. Couldn\'t we implement an event listener and respond to every user action

相关标签:
11条回答
  • 2020-12-23 14:21

    In practical terms, as other people have indicated, a loop is needed.

    However, your idea is theoretically sound. You don't need a loop. You need event-based operations.

    At a simple level, you can conceptualize the CPU to have a several timers;

    • one fires on the rising edge of 60Hz and calls the blitting code.
    • Another might be ticking at 60kHz and be rendering the latest updates of the objects in the game world to the blitter buffer.
    • Another might be ticking at 10kHz and be taking input from the user. (pretty high resolution, lol)
    • Another might be the game 'heartbeat' and ticks at 60MHz; AI and physics might operate at heartbeat time.

    Of course these timers can be tuned.

    Practically, what would be happening is your would be (somewhat elided) like this:

    void int_handler1();
    //...
    int main() 
    { 
      //install interrupt handlers
      //configure settings
      while(1);
    }
    
    0 讨论(0)
  • 2020-12-23 14:22

    An event listener is also dependent on some invocation loop whether you see it or not. Who else is going to call the listener?

    Building an explicit game loop gives you absolute control on what's going on so you won't be dependent on whatever some toolkit/event handling library does in its event loop.

    0 讨论(0)
  • 2020-12-23 14:22

    The main loop calls the event listener. If you are lucky enough to have an event-driven operating system or window manager, the event loop resides there. Otherwise, you write a main loop to mediate the "impedance mismatch" between an system-call interfaces that is based on I/O, poll, or select, and a traditional event-driven application.

    P.S. Since you tagged your question with functional-programming, you might want to check out Functional Reactive Programming, which does a great job connecting high-level abstractions to low-level, event-based implementations.

    0 讨论(0)
  • 2020-12-23 14:28

    Any program that can just sit there indefinitely and respond to user's input needs some kind of loop. Otherwise it will just reach the end of program and will exit.

    0 讨论(0)
  • 2020-12-23 14:29

    A game loop (highly simplified is as follows)

    initialise
    do
         input
         update
         render
    loop
    clean up
    

    This will happen every frame the game is drawn. So for games that run at 60fps the above is performed sixty times every second.

    This means the game runs smoothly, the game stays in sync and the updates/draws per cycle happen frequently enough. Animation is simply a trick of the eye, objects move between locations but when played quickly enough they appear to be travelling between these locations.

    If you were to only update on user input, the game would only react when the user was providing input. Other game components such as A.I game objects would not react on their own. A loop is therefore the easiest and best way of updating a game.

    0 讨论(0)
提交回复
热议问题