Advertisement
Guest User

Engine/TimeDemo.uc

a guest
Feb 2nd, 2016
75
0
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.Font = C.MedFont;
  168.     C.StrLen("T", XL, YL);
  169.     YL += FMax(1, YL / 3);
  170.  
  171.     C.SetPos(1, 48);
  172.     DrawTextLineExt(C, "Average:", YL, MaxWidth);
  173.     DrawTextLineExt(C, "RMS:", YL, MaxWidth);
  174.     DrawTextLineExt(C, "Last Second:", YL, MaxWidth);
  175.     DrawTextLineExt(C, "Lowest:", YL, MaxWidth);
  176.     DrawTextLineExt(C, "Highest:", YL, MaxWidth);
  177.     DrawTextLineExt(C, CycleMessage, YL, MaxWidth);
  178.  
  179.     C.SetPos(MaxWidth + XL + 1, 48);
  180.     DrawTextLineExt(C, Avg $ " FPS.", YL);
  181.     DrawTextLineExt(C, RMS $ " FPS.", YL);
  182.     DrawTextLineExt(C, LastSec $ " FPS.", YL);
  183.     DrawTextLineExt(C, MinFPS $ " FPS.", YL);
  184.     DrawTextLineExt(C, MaxFPS $ " FPS.", YL);
  185.     DrawTextLineExt(C, CycleResult, YL);
  186. }
  187.  
  188. function DrawTextLineExt(canvas C, string S, float DY, optional out float MaxWidth)
  189. {
  190.     local float X, Y, XL, YL;
  191.     C.StrLen(S, XL, YL);
  192.     X = C.CurX;
  193.     Y = C.CurY;
  194.     C.DrawText(S);
  195.     C.SetPos(X, Y + DY);
  196.     if (MaxWidth < XL)
  197.         MaxWidth = XL;
  198. }
  199.  
  200. function TickTimeDemo(float Delta) // Delta is measured in real seconds
  201. {
  202.     TimePassed = TimePassed + Delta;
  203. }
  204.  
  205. function StartCycle()
  206. {
  207.     local string Temp;
  208.  
  209.     if (LastCycleTime == 0)
  210.     {
  211.         CycleMessage = "Cycle #1:";
  212.         CycleResult = "Timing...";
  213.     }
  214.     else
  215.     {
  216.         CycleMessage = "Cycle #" $ CycleCount $ ":";
  217.         CycleResult = FrameLastCycle / (TimePassed - LastCycleTime)
  218.                       $ " FPS (" $ FrameLastCycle $ " frames, " $
  219.                       (TimePassed - LastCycleTime) $ " seconds)";
  220.  
  221.         Log("Cycle #" $ CycleCount $ ": "
  222.             $ FrameLastCycle / (TimePassed - LastCycleTime)
  223.             $ " FPS (" $ FrameLastCycle $ " frames, " $
  224.             (TimePassed - LastCycleTime) $ " seconds)");
  225.  
  226.         if (bSaveToFile)
  227.         {
  228.             OpenFile();
  229.             Temp = string(int(100 * FrameLastCycle / (TimePassed - LastCycleTime)));
  230.             WriteToFile( Left(Temp, Len(Temp) - 2)$"."$Right(Temp, 2) $ " Unreal "$Console.Viewport.Actor.Level.EngineVersion);
  231.             Temp = string(int(100 * MinFPS));
  232.             WriteToFile(Left(Temp, Len(Temp) - 2)$"."$Right(Temp, 2) $ " Min");
  233.             Temp = string(int(100 * MaxFPS));
  234.             WriteToFile(Left(Temp, Len(Temp) - 2)$"."$Right(Temp, 2) $ " Max");
  235.             CloseFile();
  236.         }
  237.         if ( CycleCount == QuitAfterCycles )
  238.             Console.Viewport.Actor.ConsoleCommand("exit");
  239.     }
  240.     LastCycleTime = TimePassed;
  241.     FrameLastCycle = 0;
  242.     CycleCount++;
  243. }
  244.  
  245. defaultproperties
  246. {
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement