Guest User

Engine/TimeDemo.uc

a guest
Feb 9th, 2016
27
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // TimeDemo - calculate and display framerate
  3. // This is a built-in Unreal class and it shouldn't be modified.
  4. //=============================================================================
  5. class TimeDemo extends Info
  6.             native;
  7.  
  8. // Native
  9. var int FileAr; // C++ FArchive*.
  10.  
  11. var float TimePassed;
  12. var float TimeDilation; // unused, preserved for compatibility
  13.  
  14. var float StartTime;
  15. var float LastSecTime;
  16. var float LastCycleTime;
  17. var float LastFrameTime;
  18. var float SquareSum;
  19.  
  20. var int FrameNum;
  21. var int FrameLastSecond;    // Frames in the last second
  22. var int FrameLastCycle;     // Frames in the last cycle
  23. var int CycleCount;
  24. var int QuitAfterCycles;
  25.  
  26. var string CycleMessage;
  27. var string CycleResult;
  28.  
  29. var bool bSaveToFile;
  30. var bool bFirstFrame;
  31.  
  32. var float LastSec;
  33. var float MinFPS;
  34. var float MaxFPS;
  35.  
  36. var InterpolationPoint OldPoint;
  37. var TimeDemoInterpolationPoint NewPoint;
  38.  
  39. var Console Console;
  40.  
  41. native final function OpenFile();
  42. native final function WriteToFile( string Text );
  43. native final function CloseFile();
  44.  
  45. function DoSetup( Console C, optional bool bSave, optional int QuitAfter )
  46. {
  47.     local InterpolationPoint    I;
  48.  
  49.     Console = C;
  50.     bSaveToFile = bSave;
  51.     QuitAfterCycles = QuitAfter;
  52.  
  53.     bFirstFrame = True;
  54.     OldPoint = None;
  55.  
  56.     // Find the first interpolation point, and replace it with one of ours.
  57.     foreach Console.ViewPort.Actor.AllActors( class 'InterpolationPoint', I, 'Path' )
  58.     {
  59.         if (I.Position == 0)
  60.         {
  61.             OldPoint = I;
  62.             break;
  63.         }
  64.     }
  65.  
  66.     if (OldPoint != None)
  67.     {
  68.  
  69.         Log("*************************");
  70.         Console.Viewport.Actor.StartWalk();
  71.         Console.Viewport.Actor.SetLocation(OldPoint.Location);
  72.  
  73.         // We've got a flyby sequence - break into it
  74.         OldPoint.Tag = 'OldPath';
  75.  
  76.         NewPoint = Console.ViewPort.Actor.Spawn(class 'TimeDemoInterpolationPoint', OldPoint.Owner);
  77.         NewPoint.SetLocation(OldPoint.Location);
  78.         NewPoint.SetRotation(OldPoint.Rotation);
  79.         NewPoint.Position = 0;
  80.         NewPoint.RateModifier = OldPoint.RateModifier;
  81.         NewPoint.bEndOfPath = OldPoint.bEndOfPath;
  82.         NewPoint.Tag = 'Path';
  83.         NewPoint.Next = OldPoint.Next;
  84.         NewPoint.Prev = OldPoint.Prev;
  85.         NewPoint.Prev.Next = NewPoint;
  86.         NewPoint.Next.Prev = NewPoint;
  87.         NewPoint.T = Self;
  88.     }
  89. }
  90.  
  91. function DoShutdown()
  92. {
  93.     Local float Avg;
  94.  
  95.     if (OldPoint != None)
  96.     {
  97.         NewPoint.Destroy();
  98.         OldPoint.Tag = 'Path';
  99.         OldPoint.Prev.Next = OldPoint;
  100.         OldPoint.Next.Prev = OldPoint;
  101.         OldPoint = None;
  102.     }
  103.  
  104.     Avg = FrameNum / (TimePassed - StartTime);
  105.     Console.Viewport.Actor.ClientMessage(FrameNum @ "frames rendered in" @ (TimePassed - StartTime) @ "seconds." @ Avg @ "FPS average.");
  106.     Console.TimeDemo = None;
  107.     Destroy();
  108. }
  109.  
  110. function PostRender( canvas C )
  111. {
  112.     local float Avg, RMS;
  113.     local float XL, YL, MaxWidth;
  114.  
  115.     TimeDilation = Console.Viewport.Actor.Level.TimeDilation; // unused, preserved for compatibility
  116.  
  117.     if (bFirstFrame)
  118.     {
  119.         StartTime = TimePassed;
  120.         LastSecTime = TimePassed;
  121.         LastFrameTime = TimePassed;
  122.  
  123.         FrameNum = 0;
  124.         FrameLastSecond = 0;
  125.         FrameLastCycle = 0;
  126.         CycleCount = 0;
  127.  
  128.         LastSec = 0;
  129.         LastCycleTime = 0;
  130.         CycleMessage = "";
  131.         CycleResult = "";
  132.  
  133.         SquareSum = 0;
  134.  
  135.         MinFPS = 0;
  136.         MaxFPS = 0;
  137.  
  138.         bFirstFrame = False;
  139.  
  140.         return;
  141.     }
  142.  
  143.     FrameNum++;
  144.     FrameLastSecond++;
  145.     FrameLastCycle++;
  146.  
  147.     SquareSum = SquareSum + (LastFrameTime - TimePassed) * (LastFrameTime - TimePassed);
  148.     RMS = 1 / sqrt(SquareSum / FrameNum);
  149.  
  150.     LastFrameTime = TimePassed;
  151.  
  152.     Avg = FrameNum / (TimePassed - StartTime);
  153.  
  154.     if (TimePassed - LastSecTime > 1)
  155.     {
  156.         LastSec = FrameLastSecond / (TimePassed - LastSecTime);
  157.         FrameLastSecond = 0;
  158.         LastSecTime = TimePassed;
  159.     }
  160.  
  161.     if (LastSec < MinFPS || MinFPS == 0) MinFPS = LastSec;
  162.     if (LastSec > MaxFPS) MaxFPS = LastSec;
  163.  
  164.  
  165.     if (Console.ViewPort.Actor.bShowMenu) return;
  166.  
  167.     C.Reset();
  168.     C.bNoSmooth = true;
  169.     C.DrawColor = MakeColor(255, 255, 255);
  170.     C.Font = C.MedFont;
  171.     C.StrLen("T", XL, YL);
  172.     YL += FMax(1, YL / 3);
  173.  
  174.     C.SetPos(1, 48);
  175.     DrawTextLineExt(C, "Average:", YL, MaxWidth);
  176.     DrawTextLineExt(C, "RMS:", YL, MaxWidth);
  177.     DrawTextLineExt(C, "Last Second:", YL, MaxWidth);
  178.     DrawTextLineExt(C, "Lowest:", YL, MaxWidth);
  179.     DrawTextLineExt(C, "Highest:", YL, MaxWidth);
  180.     DrawTextLineExt(C, CycleMessage, YL, MaxWidth);
  181.  
  182.     C.SetPos(MaxWidth + XL + 1, 48);
  183.     DrawTextLineExt(C, Avg $ " FPS.", YL);
  184.     DrawTextLineExt(C, RMS $ " FPS.", YL);
  185.     DrawTextLineExt(C, LastSec $ " FPS.", YL);
  186.     DrawTextLineExt(C, MinFPS $ " FPS.", YL);
  187.     DrawTextLineExt(C, MaxFPS $ " FPS.", YL);
  188.     DrawTextLineExt(C, CycleResult, YL);
  189. }
  190.  
  191. function DrawTextLineExt(canvas C, string S, float DY, optional out float MaxWidth)
  192. {
  193.     local float X, Y, XL, YL;
  194.     C.StrLen(S, XL, YL);
  195.     X = C.CurX;
  196.     Y = C.CurY;
  197.     C.DrawText(S);
  198.     C.SetPos(X, Y + DY);
  199.     if (MaxWidth < XL)
  200.         MaxWidth = XL;
  201. }
  202.  
  203. function TickTimeDemo(float Delta) // Delta is measured in real seconds
  204. {
  205.     TimePassed = TimePassed + Delta;
  206. }
  207.  
  208. function StartCycle()
  209. {
  210.     local string Temp;
  211.  
  212.     if (LastCycleTime == 0)
  213.     {
  214.         CycleMessage = "Cycle #1:";
  215.         CycleResult = "Timing...";
  216.     }
  217.     else
  218.     {
  219.         CycleMessage = "Cycle #" $ CycleCount $ ":";
  220.         CycleResult = FrameLastCycle / (TimePassed - LastCycleTime)
  221.                       $ " FPS (" $ FrameLastCycle $ " frames, " $
  222.                       (TimePassed - LastCycleTime) $ " seconds)";
  223.  
  224.         Log("Cycle #" $ CycleCount $ ": "
  225.             $ FrameLastCycle / (TimePassed - LastCycleTime)
  226.             $ " FPS (" $ FrameLastCycle $ " frames, " $
  227.             (TimePassed - LastCycleTime) $ " seconds)");
  228.  
  229.         if (bSaveToFile)
  230.         {
  231.             OpenFile();
  232.             Temp = string(int(100 * FrameLastCycle / (TimePassed - LastCycleTime)));
  233.             WriteToFile( Left(Temp, Len(Temp) - 2)$"."$Right(Temp, 2) $ " Unreal "$Console.Viewport.Actor.Level.EngineVersion);
  234.             Temp = string(int(100 * MinFPS));
  235.             WriteToFile(Left(Temp, Len(Temp) - 2)$"."$Right(Temp, 2) $ " Min");
  236.             Temp = string(int(100 * MaxFPS));
  237.             WriteToFile(Left(Temp, Len(Temp) - 2)$"."$Right(Temp, 2) $ " Max");
  238.             CloseFile();
  239.         }
  240.         if ( CycleCount == QuitAfterCycles )
  241.             Console.Viewport.Actor.ConsoleCommand("exit");
  242.     }
  243.     LastCycleTime = TimePassed;
  244.     FrameLastCycle = 0;
  245.     CycleCount++;
  246. }
  247.  
  248. defaultproperties
  249. {
  250. }
RAW Paste Data