forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[code] Global callbacks (cool!)

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
Durante



Joined: 02 Oct 2005
Posts: 65
Location: Austria

PostPosted: Mon Oct 31, 2005 5:01 am    Post subject: [code] Global callbacks (cool!) Reply with quote

I wanted to be able to make screenshots and do some debugging stuff from anywhere in my program without cluttering up my UI/game loop code, so I came up with this:

globalCallbacks.lua
Code:
---- An object implementing global key functions ----
-------------------------------------------------------------------------------

GlobalCallbacks = {
   funcs = {},
   ctrlread = Controls.read
}

function GlobalCallbacks:register(f)
   table.insert(self.funcs, f)
end

function GlobalCallbacks:remove(f)
   for i, v in pairs(self.funcs) do
      if v == f then
         table.remove(self.funcs, i)
      end
   end
end

function GlobalCallbacks:read()
   local ctrl = self.ctrlread()
   for _, v in pairs(self.funcs) do
      v(ctrl)
   end
   return ctrl
end

Controls.read = function()
   return GlobalCallbacks:read()
end


You use it like this:
Code:
dofile("src/globalcallbacks.lua")

GlobalCallbacks:register(function(ctrl)
   if ctrl:select() then
      screen:save("screenshot.png")
   end
end)


Simple, but effective. (i.e. with the above code you can now make a screenshot by pressing select, EVERYWHERE controls are read)

This is why I love dynamic languages.
Back to top
View user's profile Send private message
Durante



Joined: 02 Oct 2005
Posts: 65
Location: Austria

PostPosted: Mon Oct 31, 2005 10:04 am    Post subject: Reply with quote

Here's an improved version that allows you to use Controls.read normally in the handlers without causing a stack overflow ;)

Code:

---- An object implementing global key functions ----
-------------------------------------------------------------------------------

GlobalCallbacks = {
   funcs = {},
   ctrlread = Controls.read,
   fakeread = nil
}

function GlobalCallbacks:register(f)
   table.insert(self.funcs, f)
end

function GlobalCallbacks:remove(f)
   for i, v in pairs(self.funcs) do
      if v == f then
         table.remove(self.funcs, i)
      end
   end
end

function GlobalCallbacks:clear()
   self.funcs = {}
end

function GlobalCallbacks:read()
   local ctrl = self.ctrlread()
   Controls.read = self.ctrlread
   for _, v in pairs(self.funcs) do
      v(ctrl)
   end
   Controls.read = self.fakeread
   return ctrl
end

function GlobalCallbacks:cleanup()
   Controls.read = self.ctrlread
end

GlobalCallbacks.fakeread = function()
   return GlobalCallbacks:read()
end

Controls.read = GlobalCallbacks.fakeread


You should call GlobalCallbacks:cleanup() before exiting if you want your program to play nicely with other Lua stuff launched in the same session.
Back to top
View user's profile Send private message
chaos



Joined: 10 Apr 2005
Posts: 135

PostPosted: Mon Oct 31, 2005 2:40 pm    Post subject: Reply with quote

very nice idea..!
_________________
Chaosmachine Studios: High Quality Homebrew.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group