home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / rec / games / programm / 5228 < prev    next >
Encoding:
Internet Message Format  |  1992-12-31  |  3.3 KB

  1. Xref: sparky rec.games.programmer:5228 comp.sys.amiga.programmer:17981
  2. Path: sparky!uunet!gatech!emory!wupost!usc!cs.utexas.edu!sun-barr!news2me.EBay.Sun.COM!exodus.Eng.Sun.COM!pepper.Eng.Sun.COM!cmcmanis
  3. From: cmcmanis@pepper.Eng.Sun.COM (Chuck McManis)
  4. Newsgroups: rec.games.programmer,comp.sys.amiga.programmer
  5. Subject: Re: Game timing
  6. Date: 31 Dec 1992 07:34:01 GMT
  7. Organization: Sun
  8. Lines: 88
  9. Message-ID: <lk58j9INN3k1@exodus.Eng.Sun.COM>
  10. References: <1992Dec17.144044.18199@westminster.ac.uk>
  11. NNTP-Posting-Host: pepper
  12.  
  13. In article <1992Dec17.144044.18199@westminster.ac.uk> (Liaket Ali) writes:
  14. >A question (hypothetical) related to Game timing...
  15. >
  16. > For example, consider a flight simulator which, say, updates 
  17. > 10 pixels in 1 frame in order to achieve a frame rate of 10fps.
  18. > I would like to be able to then update 5 pixels in 1 frame to achive 20fps.
  19. >
  20. > Is this possible? And how would I go about programming it.
  21.  
  22. It is relatively simple mentally, somewhat less easy programatically.
  23.  
  24. First you set up four processes/tasks.
  25.     Task #1 sets up the timer.device to send it ticks on a regular
  26.         basis (say every 10 ms or so)
  27.     Task #2 moves the items in your game around based on their
  28.         "velocity" (which is of course speed/time)
  29.     Task #3 renders the current screen to the "hidden" buffer
  30.          and then flips the viewed buffer.
  31.     Task #4 is the "main" task and runs the game and provides
  32.          the user interface.
  33.  
  34. So in task 1, your doing something like :
  35.     do {
  36.         wait(tick);
  37.         ticks_received++;    /* global variable */
  38.     } while (1); /* forever */
  39.  
  40. In task 2 your doing something like :
  41.     static    last_ticks = 0, cur_ticks = 0;
  42.  
  43.     do {
  44.         Wait(MovePort);
  45.         cur_ticks = ticks_received; /* that global again */
  46.         delta = cur_ticks - last_ticks;
  47.         move_pieces(delta); /* does the work */
  48.         /* check to see if scores should be made */
  49.         if (collisions) {
  50.             PutMsg(Mainport); /* tell game what happened */
  51.         }
  52.         PutMsg(RenderPort); /* tell the renderer to "go" */
  53.     } while (1);
  54.  
  55. In task 3, your renderer, you do something like :
  56.     static    CurView, OtherView, TmpView;
  57.     do {
  58.         Wait(RenderPort);
  59.         /*
  60.          * Draw all of the pieces into their new
  61.          * positions in the "hidden" bitmap.
  62.          */
  63.         ...
  64.         LoadView(OtherView);
  65.         TmpView = CurView;    /* Swap views */
  66.         CurView = OtherView;
  67.         OtherView = TmpView;
  68.         PutMsg(MovePort);
  69.     } while (1);
  70.  
  71. In task 4, your "game process" you do something like:
  72.     do {
  73.         Wait(GameIO | MainPort);
  74.         /* process user input, fire buttons etc */
  75.         if (Fire1) {
  76.             ...
  77.         }
  78.         if (Collision) {
  79.             do_explosion();
  80.             delete_items(exploded_items);
  81.             score++;
  82.         }
  83.         ...
  84.     } while (1);
  85.  
  86.  
  87. The effect of a design like this is that it runs correctly on *all*
  88. machines, from slow to fast. The only difference is the frame rate
  89. goes up if Task 3 completes sooner or if Task 2 completes sooner.
  90. Further you can do things like run in "2x" time mode simply by
  91. telling Task 1 to report that its seen twice as many ticks as it
  92. really has. Finally, once you've got the basic "engine" built you
  93. will notice that tasks 1, 2, and 3 are reusable in any game, and
  94. you only need to redo task 1 and the artwork for different games.
  95.  
  96.  
  97. --
  98. --Chuck McManis                Mr. NIS+                Sunsoft
  99. uucp: {anywhere}!sun!cmcmanis   BIX: <none>   Internet: cmcmanis@Eng.Sun.COM
  100. These opinions are my own and no one elses, but you knew that didn't you.
  101.