Re: HUMP - yet another set of helpers
Posted: Sat Oct 30, 2010 7:35 pm
So... why is there no mousepressed callback in the Gamestate thing? I tried implementing it myself and it just doesn't work. I don't understand...?
Araqiel wrote:So... why is there no mousepressed callback in the Gamestate thing? I tried implementing it myself and it just doesn't work. I don't understand...?
Code: Select all
--[[Copyright (c) 2010 Matthias Richter
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
Except as contained in this notice, the name(s) of the above copyright
holders shall not be used in advertising or otherwise to promote the
sale, use or other dealings in this Software without prior written
authorization.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]]--
Gamestate = {}
local function __NULL__() end
function Gamestate.new()
return {
enter = __NULL__,
leave = __NULL__,
update = __NULL__,
draw = __NULL__,
keyreleased = __NULL__,
keypressed = __NULL__,
mousereleased = __NULL__,
mousepressed = __NULL__,
}
end
function Gamestate.switch(to, ...)
if not to then return end
if Gamestate.current then
Gamestate.current:leave()
end
local pre = Gamestate.current
Gamestate.current = to
Gamestate.current:enter(pre, ...)
end
local _update
function Gamestate.update(dt)
if _update then _update(dt) end
Gamestate.current:update(dt)
end
local _keypressed
function Gamestate.keypressed(key, unicode)
if _keypressed then _keyreleased(key) end
Gamestate.current:keypressed(key, unicode)
end
local _keyreleased
function Gamestate.keyreleased(key)
if _keyreleased then _keyreleased(key) end
Gamestate.current:keyreleased(key)
end
local _mousereleased
function Gamestate.mousereleased(x,y,btn)
if _mousereleased then _mousereleased(x,y,btn) end
Gamestate.current:mousereleased(x,y,btn)
end
local _mousepressed
function Gamestate.mousepressed(x,y,btn)
if _mousepressed then _mousepressed(x,y,btn) end
Gamestate.current:mousepressed(x,y,btn)
end
local _draw
function Gamestate.draw()
if _draw then _draw() end
Gamestate.current:draw()
end
function Gamestate.registerEvents()
_update = love.update
love.update = Gamestate.update
_keypressed = love.keypressed
love.keypressed = Gamestate.keypressed
_keyreleased = love.keyreleased
love.keyreleased = Gamestate.keyreleased
_mousereleased = love.mousereleased
love.mousereleased = Gamestate.mousereleased
_mousepressed = love.mousepressed
love.mousepressed = Gamestate.mousepressed
_draw = love.draw
love.draw = Gamestate.draw
end
Err... That's very odd. I'm doing exactly that in one of my games, and it's working just fine, as long as you remember to run Gamestate.registerEvents() in your love.load().Araqiel wrote:@nevon I did exactly that... no luck. I ended up just calling Gamestate.current:mousepressed(...) in my own love.mousepressed(...).
Can you give some (comprehensive) code example that does not work?Araqiel wrote:I tried implementing it myself and it just doesn't work.
I am not sure about that. state:leave() should usually only contain cleanup code, such as stopping audio sources.bartoleo wrote:passing "to" gamestate when calling "Gamestate.current:leave()"
Code: Select all
Gamestate.current:leave(to)
Code: Select all
-- physics
state.world = love.physics.newWorld(0,0,love.graphics.getWidth( ),love.graphics.getHeight()+state.trans_y+100,0, pLevel*100+200,true)
state.world:setMeter(30)
state.world:setCallbacks(addCollision, persistCollision, remCollision, resultCollision)
Code: Select all
function addCollision(a, b, coll)
Code: Select all
function state:addCollision(a, b, coll)
Code: Select all
state.world:setCallbacks(state:addCollision, state:persistCollision, state:remCollision, state:resultCollision)
Code: Select all
function arguments expected neat ','
That's Lua:bartoleo wrote:and I use setCallbacks:why it gives me this error?Code: Select all
state.world:setCallbacks(state:addCollision, state:persistCollision, state:remCollision, state:resultCollision)
Code: Select all
function arguments expected neat ','
Code: Select all
a = {}
function a:b() end
--equivalent to:
function a.b(self) end
a:b()
--equivalent to:
a.b(a)
Code: Select all
Pause = {}
function interrupt(interruption)
interruption.old_draw = love.draw
interruption.old_update = love.update
interruption.old_keyreleased = love.keyreleased
(...)
love.draw = function(...) interruption:draw(...) end
love.update = function(...) interruption:update(...) end
love.keyreleased = function(...) interruption:keyreleased(...) end
end
function continue(interruption)
love.draw = interruption.old_draw
love.update = interruption.old_update
love.keyreleased = interruption.old_keyreleased
end
function Pause.draw()
-- draw old screen content, but darkened
self.old_draw()
love.graphics.setColor(0,0,0, 128)
love.graphics.rectangle('fill', 0,0, love.graphics.getWidth(), love.graphics.getHeight())
-- draw pause stuff
love.graphics.setColor(255,255,255)
love.graphics.print('PAUSE', 25, 25)
draw_more_pause_stuff()
end
function Pause.keyreleased(key)
if key == 'p' then continue(Pause) end
end
What Robin said. In addition to his proposal, you could also use closures:bartoleo wrote:why it gives me this error?
Code: Select all
state.world:setCallbacks(
function() state:addCollision end,
function() state:persistCollision end,
function() state:remCollision end,
function() state:resultCollision end)