Advertisement
Guest User

PerformanceGraph

a guest
Jul 13th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.25 KB | None | 0 0
  1. -- version 0.1
  2. -- This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php
  3. -- (C) 2013 Jakub Trzebiatowski
  4.  
  5. -- PerformanceGraph draws a graph of perfomance in last few seconds.
  6. -- Add this code at the end of your main.lua (or other appropriate place):
  7.  
  8. --[[
  9. local graph = PerformanceGraph.new {
  10.     history_length = <width of the graph in seconds, default: 2>,
  11.     min_frame_time = <min value of deltaTime the graph can show, default: 1/application:getFps()>,
  12.     max_frame_time = <max value of deltaTime the graph can show, default: 3*self.min_frame_time>,
  13.     color = <color of graph, default: 0xFF0000 (red)>,
  14.     alpha = <alpha value of graph, default: 0.7>
  15. }
  16. graph:setY(application:getContentHeight())
  17. stage:addChild(graph)
  18. --]]
  19.  
  20. PerformanceGraph = Core.class(Sprite)
  21.  
  22. function PerformanceGraph:init(config)
  23.     config = config or {}
  24.     self.graph = Sprite.new()
  25.     self:addChild(self.graph)
  26.    
  27.     self.graph:setX(application:getContentWidth())
  28.     self.graph:setScaleY(-1)
  29.    
  30.     self.history_length = config.history_length or 2
  31.     self.min_frame_time = config.min_frame_time or 1/application:getFps()
  32.     self.max_frame_time = config.max_frame_time or 3*self.min_frame_time
  33.    
  34.     self.color = config.color or 0xFF0000
  35.     self.alpha = config.alpha or 0.7
  36.    
  37.     self.x = 0
  38.     self.y = 0
  39.    
  40.     stage:addEventListener("enterFrame", self.onEnterFrame, self)
  41. end
  42.  
  43. function PerformanceGraph:onEnterFrame(event)
  44.     local n = self.history_length * application:getFps()
  45.     local w = application:getContentWidth()
  46.     local h = application:getContentHeight()
  47.     local dx = 1/n * w
  48.    
  49.     local x, y = self.x, self.y
  50.    
  51.     if self.graph:getNumChildren() > n then
  52.         self.graph:removeChildAt(1)
  53.     end
  54.    
  55.     local mesh = Mesh.new()
  56.     self.graph:addChild(mesh)
  57.    
  58.     local c, a = self.color, self.alpha
  59.     mesh:setColorArray(c, a, c, a, c, a, c, a)
  60.     mesh:setIndexArray(1, 2, 3, 1, 3, 4)
  61.     mesh:resizeVertexArray(4)
  62.     mesh:setVertices(1, x, 0, 2, x, y)
  63.    
  64.     local dt  = event.deltaTime - self.min_frame_time
  65.     x = x + dx
  66.     y = dt/self.max_frame_time * h
  67.    
  68.     mesh:setVertices(3, x, y, 4, x, 0)
  69.    
  70.     self.graph:setX(self.graph:getX() - dx)
  71.     self.x, self.y = x, y
  72. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement