Changeset 577


Ignore:
Timestamp:
May 3, 2011, 3:02:52 PM (6 years ago)
Author:
sam
Message:

game: get rid of milli_wait() and rewrite the fixed framerate handling.

Location:
abuse/trunk/src
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • abuse/trunk/src/ant.cpp

    r555 r577  
    481481    wm->font()->put_string(screen,x,y,msg,wm->bright_color());
    482482    wm->flush_screen();
    483     milli_wait(500);
    484   }
    485 
    486 }
     483    Timer now; now.WaitMs(500);
     484  }
     485}
     486
  • abuse/trunk/src/cop.cpp

    r555 r577  
    10821082
    10831083  wm->flush_screen();
    1084   milli_wait(4000);   // wait 4 seconds
     1084  Timer now; now.WaitMs(4000);   // wait 4 seconds
    10851085
    10861086  return NULL;
  • abuse/trunk/src/demo.cpp

    r555 r577  
    203203/*
    204204      fade_in(cache.img(cache.reg("art/help.spe","sell6",SPEC_IMAGE,1)),8);
    205       milli_wait(2000);
     205      Timer now; now.WaitMs(2000);
    206206      fade_out(8);
    207207*/
  • abuse/trunk/src/game.cpp

    r576 r577  
    12441244    delete blank;
    12451245    fade_in(cache.img(cdc_logo), 32);
    1246 
    1247     milli_wait(400);
     1246    Timer tmp; tmp.WaitMs(400);
     1247    fade_out(32);
    12481248
    12491249    void *space_snd = LSymbol::FindOrCreate("SPACE_SND")->GetValue();
    1250 
    1251     fade_out(32);
    1252     milli_wait(100);
    1253 
    12541250    char *str = lstring_value(LSymbol::FindOrCreate("plot_start")->Eval());
    12551251
     
    15111507}
    15121508
    1513 
    1514 
    1515 time_marker *led_last_time = NULL, *fps_mark_start = NULL;
    1516 double avg_fps = 15.0, possible_fps = 15.0;
     1509time_marker *led_last_time = NULL;
     1510static float avg_fps = 15.0f, possible_fps = 15.0f;
    15171511
    15181512void Game::toggle_delay()
    15191513{
    1520   no_delay=!no_delay;
    1521   if(no_delay)
    1522     show_help(symbol_str("delay_off"));
    1523   else show_help(symbol_str("delay_on"));
    1524   avg_fps = possible_fps = 15.0;
     1514    no_delay = !no_delay;
     1515    show_help(symbol_str(no_delay ? "delay_off" : "delay_on"));
     1516    avg_fps = possible_fps = 15.0f;
    15251517}
    15261518
    15271519void Game::show_time()
    15281520{
    1529   if(first_view && fps_on)
    1530   {
    1531     char str[10];
     1521    if (!first_view || !fps_on)
     1522        return;
     1523
     1524    char str[16];
    15321525    sprintf(str, "%ld", (long)(avg_fps * 10.0));
    15331526    console_font->put_string(screen, first_view->cx1, first_view->cy1, str);
     
    15351528    sprintf(str, "%d", total_active);
    15361529    console_font->put_string(screen, first_view->cx1, first_view->cy1 + 10, str);
    1537   }
    15381530}
    15391531
     
    16001592}
    16011593
     1594// FIXME: refactor this to use the Lol Engine main fixed-framerate loop?
    16021595int Game::calc_speed()
    16031596{
     1597    static Timer frame_timer;
     1598    static int first = 1;
     1599
     1600    if (first)
     1601    {
     1602        first = 0;
     1603        return 0;
     1604    }
     1605
     1606    // Find average fps for last 10 frames
     1607    float fps = 1000.0f / Max(1.0f, frame_timer.PollMs());
     1608
     1609    avg_fps = 0.9f * avg_fps + 0.1f * fps;
     1610    possible_fps = 0.9f * possible_fps + 0.1f * fps;
     1611
     1612    if (avg_fps > 14)
     1613        massive_frame_panic = Max(0, Min(20, massive_frame_panic - 1));
     1614
    16041615    int ret = 0;
    1605     if(fps_mark_start)
    1606     {
    1607         time_marker t;
    1608 
    1609         // find average fps for last 10 frames
    1610         double td = t.diff_time(fps_mark_start);
    1611         if(td < 0.001)     // something is rotten in the state of demark
    1612             td = 0.001;
    1613 
    1614         avg_fps = avg_fps * 9.0 / 10.0 + 1.0/(td * 10.0);
    1615         possible_fps = possible_fps * 9.0 / 10.0 + 1.0/(td * 10.0);
    1616 
    1617         if(avg_fps > 14)
     1616
     1617    if (dev & EDIT_MODE)
     1618    {
     1619        // ECS - Added this case and the wait.  It's a cheap hack to ensure
     1620        // that we don't exceed 30FPS in edit mode and hog the CPU.
     1621        frame_timer.WaitMs(33);
     1622    }
     1623    else if (avg_fps > 15 && need_delay)
     1624    {
     1625        frame_panic = 0;
     1626        if (!no_delay)
    16181627        {
    1619             if(massive_frame_panic > 20)
    1620                 massive_frame_panic = 20;
    1621             else if(massive_frame_panic)
    1622                 massive_frame_panic--;
     1628            frame_timer.WaitMs(1000.0f / 15);
     1629            avg_fps -= 0.1f * fps;
     1630            avg_fps += 0.1f * 15.0f;
    16231631        }
    1624 
    1625         if(avg_fps > 15 && ((dev & EDIT_MODE)==0 || need_delay))
    1626         {
    1627             frame_panic = 0;
    1628             int32_t stime=(int32_t)((1 / 15.0 - 1.0 / possible_fps)*1000.0);
    1629             if(stime > 0 && !no_delay)
    1630             {
    1631                 milli_wait(stime);
    1632                 avg_fps -= 1.0/(td * 10.0);   // subtract out old estimate
    1633 
    1634                 time_marker t;
    1635 
    1636                 // find average fps for last 10 frames
    1637                 double td = t.diff_time(fps_mark_start);
    1638                 if(td < 0.00001)     // something is rotten in the state of demark
    1639                     td = 0.00001;
    1640 
    1641                 avg_fps += 1.0/(td * 10.0);       // add in new estimate
    1642             }
    1643         }
    1644         else if(avg_fps < 14)
    1645         {
    1646             if(avg_fps < 10)
    1647                 massive_frame_panic++;
    1648             frame_panic++;
    1649             ret = 1;
    1650         }
    1651         else if(dev & EDIT_MODE)
    1652         {
    1653             // ECS - Added this case and the wait.  It's a cheap hack to assure that
    1654             // we don't exceed 30FPS in edit mode and hog the CPU.
    1655             milli_wait(33);
    1656         }
    1657 
    1658         delete fps_mark_start;
    1659     }
    1660     fps_mark_start = new time_marker;
     1632    }
     1633    else if (avg_fps < 14)
     1634    {
     1635        if(avg_fps < 10)
     1636            massive_frame_panic++;
     1637        frame_panic++;
     1638        // All is lost, don't sleep during this frame
     1639        ret = 1;
     1640    }
     1641
     1642    // Ignore our wait time, we're more interested in the frame time
     1643    frame_timer.GetMs();
    16611644    return ret;
    16621645}
     
    21192102  }
    21202103  free_pframes();
    2121   if(fps_mark_start) delete fps_mark_start; fps_mark_start = NULL;
    21222104  delete pal;
    21232105  free(object_names);
     
    24492431
    24502432    stat_man = new text_status_manager();
    2451     if (!get_option("-no_timer"))
    2452         timer_init();
    24532433
    24542434#if !defined __CELLOS_LV2__
     
    24722452        if (main_net_cfg && !main_net_cfg->notify_reset())
    24732453        {
    2474             if (!get_option("-no_timer"))
    2475                 timer_uninit();
    24762454            sound_uninit();
    24772455            exit(0);
     
    25752553        delete chat;
    25762554
    2577         milli_wait(500);
     2555        Timer tmp; tmp.WaitMs(500);
    25782556
    25792557        delete small_render; small_render = NULL;
     
    26222600    set_save_filename_prefix(NULL);
    26232601
    2624     if (!get_option("-no_timer"))
    2625         timer_uninit();
    2626 
    26272602    sound_uninit();
    26282603
  • abuse/trunk/src/imlib/timing.h

    r555 r577  
    2626};
    2727
    28 void milli_wait(unsigned int wait_time); // sleep for a millisecond
    29 void timer_init();
    30 void timer_uninit();
    31 
    3228#endif
    3329
  • abuse/trunk/src/menu.cpp

    r555 r577  
    257257      wm->flush_screen();
    258258      save->put_image(screen,mx+1,by1);
    259     } else milli_wait(10);
     259    } else { Timer tmp; tmp.WaitMs(10); }
    260260
    261261  } while (!done);
     
    694694        {
    695695            // ECS - Added so that main menu doesn't grab 100% of CPU
    696             milli_wait(30);
     696            Timer tmp; tmp.WaitMs(30);
    697697        }
    698698
  • abuse/trunk/src/netcfg.cpp

    r555 r577  
    512512        else
    513513        {
    514             // No event waiting...  We can't wait for long, because we are pretending to broadcast.
    515             milli_wait(5); // ECS - Added so waiting in dialog doesn't use 100% of CPU
     514            // No event waiting...  We can't wait for long, because we are
     515            // pretending to broadcast.
     516            // ECS - Added so waiting in dialog doesn't use 100% of CPU
     517            Timer tmp; tmp.WaitMs(5);
    516518        }
    517519    }
  • abuse/trunk/src/sdlport/event.cpp

    r555 r577  
    136136
    137137        if (!ewaiting)
    138             milli_wait( 1 );    // Sleep for 1 millisecond if there are no events
     138        {
     139            // Sleep for 1 millisecond if there are no events
     140            Timer tmp; tmp.WaitMs(1);
     141        }
    139142    }
    140143
  • abuse/trunk/src/sdlport/timing.cpp

    r555 r577  
    6565}
    6666
    67 void timer_init()
    68 {
    69     /* Do Nothing */
    70 }
    71 
    72 void timer_uninit()
    73 {
    74     /* Do Nothing */
    75 }
    76 
    77 void milli_wait( unsigned wait_time )
    78 {
    79     struct timespec ts = { 0, wait_time * 1000000 };
    80     nanosleep( &ts, NULL );
    81 }
    82 
Note: See TracChangeset for help on using the changeset viewer.