DieHard Wolfers Forum Index DieHard Wolfers
A Wolfenstein 3d Fan Community


  Hosted by: MCS & Areyep.com - Designed by: BrotherTank

Original Yahoo Forum - Die Hard Archives

AReyeP HomepageAreyep Homepage DieHard Wolfenstein BunkerDieHard Wolfenstein Bunker Log inLog in RegisterRegister Banlist FAQFAQ Search ForumsSearch

  Username:    Password:      Remember me       

[Tutorial]Fixed gamelogic rate (with lag simulator)
Page 1 of 1
DieHard Wolfers Forum Index -> SDL Code Crackers View Previous TopicRefresh this PageAdd Topic to your Browser FavoritesSearch ForumsPrint this TopicE-mail TopicGoto Page BottomView Next Topic
Post new topicReply to topic
Author Message
Codetech84
Code Master
Code Master


Joined: 12 Mar 2003
Last Visit: 01 Apr 2018

Topics: 22
Posts: 1283
Location: Rauma - Finland
finland.gif

PostPosted: Sat Oct 29, 2011 3:12 am
   Subject: [Tutorial]Fixed gamelogic rate (with lag simulator)
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Next PostGoto Bottom of Posts

Here's the code for "fixed gamelogic rate" with lag simulator. This could be something to be added to the next Wolf4SDL revision. It should be compatible with any mod and it's also compatible with original demos.

Enjoy! Mr Green

Apply the changes marked in blue.

In wl_def.h add the code in blue:
::: CODE :::
int DebugKeys (void);


#ifdef FIXEDLOGICRATE
#ifdef LAGSIMULATOR
extern boolean lagging;
#endif
#endif

In wl_play.cpp, make the following changes:
::: CODE :::
int32_t funnyticount;

#ifdef FIXEDLOGICRATE
double accumulator, frametime_spent = 0;
double dt = 1.0f / 70.0f; // 70 FPS
uint32_t oldtime = 0;


void ClockGameLogic (void)
{
    if (demorecord || demoplayback)
    {
        accumulator = dt;
        return;
    }

    uint32_t curtime = SDL_GetTicks ();
    uint32_t deltatime = curtime-oldtime;
    if (oldtime == 0)
        deltatime = 0;
    oldtime = curtime;
    double time_to_pass = (double)(deltatime / 1000.0f);

    // Choking, do not overload the timestep
    // to avoid grinding to a halt
    if (time_to_pass > dt*10)
    {
        time_to_pass = dt*10;
    }

    // Add some frametime to spend
    accumulator += time_to_pass;
}

#ifdef LAGSIMULATOR
uint32_t next_lag_spike = -1;


void LagSimulator (void)
{
    if (demorecord || demoplayback)
        return;

    if (lagging)
    {
        if (next_lag_spike == -1 || SDL_GetTicks () >= next_lag_spike)
        {
            SDL_Delay (80+(rand()%200));
            next_lag_spike = SDL_GetTicks () + 200+rand()%350;
        }
    }
}
#endif
#endif

Make the following changes to PlayLoop
::: CODE :::

void PlayLoop (void)
{
#if defined(USE_FEATUREFLAGS) && defined(USE_CLOUDSKY)
    if(GetFeatureFlags() & FF_CLOUDSKY)
        InitSky();
#endif

#ifdef USE_SHADING
    InitLevelShadeTable();
#endif

    playstate = ex_stillplaying;
    lasttimecount = GetTimeCount();
    frameon = 0;
    anglefrac = 0;
    facecount = 0;
    funnyticount = 0;
    memset (buttonstate, 0, sizeof (buttonstate));
    ClearPaletteShifts ();

    if (MousePresent && IN_IsInputGrabbed())
        IN_CenterMouse();         // Clear accumulated mouse movement

    if (demoplayback)
        IN_StartAck ();

#ifndef FIXEDLOGICRATE
    do
    {
        PollControls ();

//
// actor thinking
//
        madenoise = false;

        MoveDoors ();
        MovePWalls ();

        for (obj = player; obj; obj = obj->next)
            DoActor (obj);

        UpdatePaletteShifts ();

        ThreeDRefresh ();

        //
        // MAKE FUNNY FACE IF BJ DOESN'T MOVE FOR AWHILE
        //
#ifdef SPEAR
        funnyticount += tics;
        if (funnyticount > 30l * 70)
        {
            funnyticount = 0;
            if(viewsize != 21)
                StatusDrawFace(BJWAITING1PIC + (US_RndT () & 1));
            facecount = 0;
        }
#endif

        gamestate.TimeCount += tics;

        UpdateSoundLoc ();      // JAB
        if (screenfaded)
            VW_FadeIn ();

        CheckKeys ();

//
// debug aids
//
        if (singlestep)
        {
            VW_WaitVBL (singlestep);
            lasttimecount = GetTimeCount();
        }
        if (extravbls)
            VW_WaitVBL (extravbls);

        if (demoplayback)
        {
            if (IN_CheckAck ())
            {
                IN_ClearKeysDown ();
                playstate = ex_abort;
            }
        }
    }
#else
    do
    {
        // Break from the playloop if starting game
        if (startgame || loadedgame)
            break;

#ifdef LAGSIMULATOR
        // Do some lagging
        LagSimulator ();
#endif
        // Clock the logictime that has stacked up
        ClockGameLogic ();

        // Loop while there is time left to simulate
        while (accumulator >= dt)
        {
    //
    // start a logic frame
    //
            PollControls ();

    //
    // actor thinking
    //
            madenoise = false;

            MoveDoors ();
            MovePWalls ();

            for (obj = player; obj; obj = obj->next)
                DoActor (obj);

            UpdatePaletteShifts ();

    //
    // MAKE FUNNY FACE IF BJ DOESN'T MOVE FOR AWHILE
    //
#ifdef SPEAR
            funnyticount += tics;
            if (funnyticount > 30l * 70)
            {
                funnyticount = 0;
                if(viewsize != 21)
                    StatusDrawFace(BJWAITING1PIC + (US_RndT () & 1));
                facecount = 0;
            }
#endif
            // Abort demo?
            if (demoplayback)
            {
                if (IN_CheckAck ())
                {
                    IN_ClearKeysDown ();
                    playstate = ex_abort;
                }
            }

            // Advance timer
            gamestate.TimeCount += tics;

            // Debug keys
            CheckKeys ();

            // End of one frame
            frametime_spent += dt;
            accumulator -= dt;
            frameon += tics;
        }

        // Only refresh screen once per frame, instead of once per logic frame
        ThreeDRefresh ();

        UpdateSoundLoc ();      // JAB
        if (screenfaded)
            VW_FadeIn ();

        // Do single stepping outside of the game logic loop
        if (singlestep)
        {
            VW_WaitVBL (singlestep);
            lasttimecount = GetTimeCount();
        }

        // Extra vbls left outside of game logic
        if (extravbls)
            VW_WaitVBL (extravbls);
    }
#endif


    while (!playstate && !startgame);

    if (playstate != ex_died)
        FinishPaletteShifts ();
}

In wl_draw.cpp, make the following changes to CalcTics:
::: CODE :::
void CalcTics (void)
{
//
// calculate tics since last refresh for adaptive timing
//
    if (lasttimecount > (int32_t) GetTimeCount())
        lasttimecount = GetTimeCount();    // if the game was paused a LONG time

#ifndef FIXEDLOGICRATE
    uint32_t curtime = SDL_GetTicks();
    tics = (curtime * 7) / 100 - lasttimecount;
    if(!tics)
    {
        // wait until end of current tic
        SDL_Delay(((lasttimecount + 1) * 100) / 7 - curtime);
        tics = 1;
    }

    lasttimecount += tics;

    if (tics>MAXTICS)
        tics = MAXTICS;
#else
    // The logic rate is always fixed
    tics = FIXEDLOGICRATE;
    lasttimecount += tics;
#endif

}

Still in wl_draw.cpp, patch DarkOne's fps counter and animated sprite code.
::: CODE :::
#ifndef REMDEBUG
    if (fpscounter)
    {
        fps_frames++;
#ifndef FIXEDLOGICRATE
        fps_time+=tics;

        if(fps_time>35)
        {
            fps_time-=35;
            fps=fps_frames<<1;
            fps_frames=0;
        }
#else
        if (SDL_GetTicks() - fps_time > 500)
        {
            fps_time = SDL_GetTicks ();
            fps = fps_frames<<1;
            fps_frames=0;
        }
#endif

    }
#endif

#ifndef FIXEDLOGICRATE
    // When using fixed game logic, this must be elsewhere
    frameon += tics;
#endif

In wl_debug.cpp enable the lag simulator cheat:
::: CODE :::
int DebugKeys (void);

#ifdef FIXEDLOGICRATE
#ifdef LAGSIMULATOR
boolean lagging = false;
#endif
#endif

Add the cheat to wl_debug.cpp:
::: CODE :::
#ifdef FIXEDLOGICRATE
#ifdef LAGSIMULATOR
    else if (Keyboard[sc_M])        // M = lag simulator
    {
        lagging^=1;
        CenterWindow (18,3);
        if (lagging)
            US_PrintCentered ("Lag simulator ON");
        else
            US_PrintCentered ("Lag simulator OFF");
        VW_UpdateScreen();
        IN_Ack ();
        return 1;
    }
#endif
#endif


    return 0;

And finally, enable this feature from version.h:
::: CODE :::
//#define FIXRAINSNOWLEAKS    // Enables leaking ceilings fix (by Adam Biser, only needed if maps with rain/snow and ceilings exist)
#define FIXEDLOGICRATE 1      // Enables framerate independent game logic, aka uncapped refreshrate. (by Codetech84)
#define LAGSIMULATOR          // Enables lag simulator debug option, depends on fixed logic rate (by Codetech84)

_________________
Click here to visit KFH Games website!
*UPDATED* Spear of Destiny Reloaded
KFH Games on Facebook
Codetech84
Code Master
Code Master


Joined: 12 Mar 2003
Last Visit: 01 Apr 2018

Topics: 22
Posts: 1283
Location: Rauma - Finland
finland.gif

PostPosted: Sun Oct 30, 2011 2:06 am
   Subject: [Tutorial]Fixed gamelogic rate (with lag simulator)
   [ IP : Logged ]
Reply with quote
Goto Top of PostsGoto Previous PostGoto Bottom of Posts

Here's a precompiled binary to try out. Mr Green

_________________
Click here to visit KFH Games website!
*UPDATED* Spear of Destiny Reloaded
KFH Games on Facebook
Display posts from previous:   
Post new topicReply to topic Time synchronized with the forum server time
DieHard Wolfers Forum Index -> SDL Code Crackers View Previous TopicRefresh this PageAdd Topic to your Browser FavoritesSearch ForumsPrint this TopicE-mail TopicGoto Page TopView Next Topic
Page 1 of 1
Jump to:  

Related topics
 Topics   Replies   Views   Last Post 
No new posts Announcement: Wolf4SDL released
Author: Ripper
522 195621 Mon May 22, 2017 2:04 pm
Tricob View latest post
No new posts [help] Adding teleporters in wolf4sdl
Author: Mortimer
35 9988 Fri May 26, 2017 8:19 am
Military View latest post
No new posts [WOLF4SDL Help] Blake Stone Style doors
Author: Haasboy
3 2836 Sun Jul 20, 2008 7:56 am
Haasboy View latest post
No new posts [Help] Problem with Checkweaponchange function (wolf4sdl)
Author: Mortimer
4 3026 Sat May 17, 2008 11:26 am
Mortimer View latest post
No new posts [SDL] Apply 2nd ScaleFactor to selected (hi-res) vga gfx
Author: Andy_Nonymous
9 4247 Sun Apr 27, 2008 2:30 am
AlumiuN View latest post
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
   You cannot delete your posts in this forum
You cannot vote in polls in this forum


Copyright ©2003-2008 DieHard Wolfers
A Modified subBunker Theme by BrotherTank
Powered by phpBB © 2001, 2005 phpBB Group