Side-scrolling?

Started by
14 comments, last by CannoN346 22 years, 9 months ago
Sleep() is only really good for DOS programming, in Windows, since its a multitasking environment you should update all your sprites and backgrounds using floating point variables to update the positions, at every frame work out the difference between the current and previous frame and add that float value to your sprite positions. (use timegettime to calculate the milliseconds).

Here is the code u need to set up a precise update system.
  // Global variablesfloat    g_fCalcDelta;__int64  PerfTimerRate;__int64  PerfTimerDivider = 0;// Call this on game initbool InitPerfTimer(void){  __int64 tempInt64;  // Aquire the timer frequency  if(!QueryPerformanceFrequency((LARGE_INTEGER*)&PerfTimerRate))  {      return FALSE; // System doesnt have a Performance timer   }  // Check the rate (usually 1193180)  if(PerfTimerRate==0)  {      return FALSE;  }   // Calculate millisecond divider  PerfTimerDivider = (__int64)( (__int64)PerfTimerRate / 1000);  if(!QueryPerformanceCounter((LARGE_INTEGER*)&tempInt64))  {      return FALSE; // win errors  }  return TRUE; // Success}WORD FTime(void){    __int64 tempInt64;    QueryPerformanceCounter((LARGE_INTEGER*)&tempInt64);    return (WORD)((_int64)tempInt64 / (__int64)PerfTimerDivider);}// Call this every frame to get the frame diff.void UpdateTimeDeltas(){    static WORD timerDelta = 0;    static WORD oldftime = 0;    static WORD newftime = 0;    static WORD oldnDiff = (WORD)FTime();    // Calculate the frame delta.    newftime = FTime();    if( oldftime > newftime ) oldftime = newftime;    timerDelta = newftime - oldftime;    oldftime   = newftime;    float fCalcDelta = float(timerDelta / 1000.0f);    // Set the global fCalcDelta and counter.    g_fCalcDelta = fCalcDelta;}// Call every frame to update the sprite positions.void UpdateSprites(bool bLeft, bool bRight){    // Player velocity    vel = 100.0f;    if(bLeft)  Player.xpos -= g_fCalcDelta * vel;    if(bRight) Player.xpos += g_fCalcDelta * vel;        // Note ''Player.xpos'' is a float variable.}  


As for the background scrolling, give your background and main sprite a global position, and whenever you update the position of the main sprite, at the same time update the position of the background minus the sprite offset of the main sprite.

For example (640x480):

  void SetBgndPos(){    // Center the player.    Level.xpos = WORD(Player.xpos) - 320;    Level.ypos = WORD(Player.ypos) - 240;}void UpdateFrame(){    // Set the Bgnd pos in relation to the player pos.    UpdateTimeDeltas();    UpdateSprites(..DInput status..);    SetBgndPos();    // Blit the background tiles.    Blit(Level.lpDDSurface, Level.xpos-640, Level.ypos);    Blit(Level.lpDDSurface, Level.xpos, Level.ypos);    Blit(Level.lpDDSurface, Level.xpos+640, Level.ypos);    // Blit the sprite    Blit(Player.lpDDSurface, 320, Player.ypos);    }  


Note; The Blit() function would be your own routine.

Hope this helps


  Downloads:  ZeroOne Realm

  Downloads:  ZeroOne Realm

Advertisement
I would personally NOT run as fast as possible, as it introduces problems in the long term. To keep things simple use the performace counters to sleep the number of millisecond necessary to get an exact framerate. The nice thing about having an exact framerate is that your game is much easier to program if you know that for each tick of the drawing loop you are going exactly ( or close enough for our work ) a specific number of milliseconds. Your engine only has to know how to deal with one type of update and it will never change.

Another reason to keep a fixed framerate is determinism ( aka reproducability ). The game under identical inputs will produce a slightly diffrent game based on the speed of the system it''s running on. You may not think much of this now, but debugging, multiplayer, and recording of games depend on reproducability.

refrence: gamasutra post moterm X-wing vs Tie Fighter

Good luck.
quote:
Another reason to keep a fixed framerate is determinism ( aka reproducability ). The game under identical inputs will produce a slightly diffrent game based on the speed of the system it's running on. You may not think much of this now, but debugging, multiplayer, and recording of games depend on reproducability.


And what will happen if my computer can´t keep up with the framrate you choose ?
Will your program not "produce slightly different game" then?

If my system is capable of 150 fps why should the framerate be locked at say 70fps ??????????????????????????????

I don´t see any good reasons for this..I don´t think many commercial games do this....





Edited by - granat on July 19, 2001 2:15:43 AM
-------------Ban KalvinB !
If the computer can''t keep up tehn you don''t draw every frame, but you still compute it.

Even if the computer can''t keep up the reptiducability is still there because given the same inputs the game still responds the same way, just a little slower. But as you point out keeping up is not really a problem for most games these days.

How about your senatio... if the computer gets lagged and people just start running through walls because you are not checking at regualr intervals.... how does that make the game more accurate?

150fps... what''s the point you can draw twice as fast as the most monitors can handle, and more than twice most normal people can even consiously percieve. If I were you I''de lock your framerate and use those cycles to improve game interaction ( AI, resource managment, network play, ... ).

Many 2d games work off a set clock, all of the animation is based on specific framerates.

But don''t take my word.. read the recent cover feature from gamsutra, might be an eye opener.
Granat:

Yes, commercial games put limits on FPS. One popular example would be halflife the default limit is 72FPS. However there is an option included to change it...

~~~~
sjunkim - P0et
Liquid Ice Studios
members.home.net/liquidicestudios/
~~~~sjunkimLiquid Ice Studioswww.liquidicestudios.com
If you''re looking for some good tutorials, check out http://www.aeon-software.com/ IronBlayde is one to be respected. Although his tutorials focus mainly on creating a 2d top-down RPG, I''ve personally used many of his techniques in different genres.

DracosX

DracosX:Master of the General Protection Fault

This topic is closed to new replies.

Advertisement