Guest User

Engine/TimeDemo.uc

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