Skip to content

Instantly share code, notes, and snippets.

@jhoff
Created November 7, 2012 07:37
Show Gist options
  • Save jhoff/4030044 to your computer and use it in GitHub Desktop.
Save jhoff/4030044 to your computer and use it in GitHub Desktop.
Debugger Patch for JttCoH
diff --git a/src/debugger.lua b/src/debugger.lua
new file mode 100644
index 0000000..59da4aa
--- /dev/null
+++ b/src/debugger.lua
@@ -0,0 +1,54 @@
+local List = require 'list'
+local window = require 'window'
+
+local Debugger = { on=true }
+Debugger.__index = Debugger
+
+Debugger.graphData = {
+ { name = 'gc', color = { 255, 0, 0, 150 }, list = List.new() },
+ { name = 'fps', color = { 255, 255, 0, 150 }, list = List.new() },
+ { name = 'dt', color = { 0, 255, 0, 150 }, list = List.new() }
+}
+
+function Debugger:getData(name)
+ for k,v in pairs(Debugger.graphData) do
+ if v.name == name then
+ return Debugger.graphData[k]
+ end
+ end
+ return false
+end
+
+function Debugger:listPush( list, val )
+ List.pushleft( list, val )
+ if math.abs(list.first) - math.abs(list.last) > window.screen_width then
+ List.popright( list )
+ end
+end
+
+function Debugger:update( dt )
+ if Debugger.on then
+ Debugger:listPush( Debugger:getData('fps').list, love.timer.getFPS() )
+ Debugger:listPush( Debugger:getData('gc').list, collectgarbage( 'count' ) / 150 )
+ Debugger:listPush( Debugger:getData('dt').list, dt * 1000 )
+ end
+end
+
+function Debugger:draw()
+ for k,v in pairs( Debugger.graphData ) do
+ love.graphics.setColor( v.color )
+ for i=v.list.first, v.list.last do
+ if v.list[i] then
+ love.graphics.line(
+ window.screen_width + v.list.first - i,
+ window.screen_height - v.list[i],
+ window.screen_width + v.list.first - i,
+ window.screen_height
+ )
+ end
+ end
+ end
+ love.graphics.setColor( 255, 255, 255, 255 )
+end
+
+return Debugger
\ No newline at end of file
diff --git a/src/list.lua b/src/list.lua
new file mode 100644
index 0000000..6e50dd8
--- /dev/null
+++ b/src/list.lua
@@ -0,0 +1,36 @@
+List = {}
+function List.new ()
+ return {first = 0, last = -1}
+end
+
+function List.pushleft (list, value)
+ local first = list.first - 1
+ list.first = first
+ list[first] = value
+end
+
+function List.pushright (list, value)
+ local last = list.last + 1
+ list.last = last
+ list[last] = value
+end
+
+function List.popleft (list)
+ local first = list.first
+ if first > list.last then error("list is empty") end
+ local value = list[first]
+ list[first] = nil -- to allow garbage collection
+ list.first = first + 1
+ return value
+end
+
+function List.popright (list)
+ local last = list.last
+ if list.first > last then error("list is empty") end
+ local value = list[last]
+ list[last] = nil -- to allow garbage collection
+ list.last = last - 1
+ return value
+end
+
+return List
\ No newline at end of file
diff --git a/src/main.lua b/src/main.lua
index a5ab92b..5333ea7 100644
--- a/src/main.lua
+++ b/src/main.lua
@@ -1,6 +1,7 @@
local correctVersion = require 'correctversion'
if correctVersion then
-
+
+local debugger = require 'debugger'
local Gamestate = require 'vendor/gamestate'
local Level = require 'level'
local camera = require 'camera'
@@ -60,12 +61,14 @@ end
function love.update(dt)
if paused then return end
+ if debugger.on then debugger:update(dt) end
dt = math.min(0.033333333, dt)
Gamestate.update(dt)
sound.cleanup()
end
function love.keyreleased(key)
+ if key == 'f5' then debugger.on = not debugger.on end
local button = controls.getButton(key)
if button then Gamestate.keyreleased(button) end
end
@@ -99,6 +102,7 @@ function love.draw()
love.graphics.setColor(255, 255, 255, 255)
end
+ if debugger.on then debugger:draw() end
end
-- Override the default screenshot functionality so we can disable the fps before taking it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment