HUMP - yet another set of helpers

Showcase your libraries, tools and other projects that help your fellow love users.
Araqiel
Citizen
Posts: 54
Joined: Sat Oct 30, 2010 7:33 pm

Re: HUMP - yet another set of helpers

Post by Araqiel »

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...?
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: HUMP - yet another set of helpers

Post by nevon »

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
Araqiel
Citizen
Posts: 54
Joined: Sat Oct 30, 2010 7:33 pm

Re: HUMP - yet another set of helpers

Post by Araqiel »

@nevon I did exactly that... no luck. I ended up just calling Gamestate.current:mousepressed(...) in my own love.mousepressed(...).

EDIT: Oops, I used a . where I needed a :. It's been too long since I've used Lua.
Last edited by Araqiel on Sun Oct 31, 2010 6:22 pm, edited 1 time in total.
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: HUMP - yet another set of helpers

Post by nevon »

Araqiel wrote:@nevon I did exactly that... no luck. I ended up just calling Gamestate.current:mousepressed(...) in my own love.mousepressed(...).
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().
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: HUMP - yet another set of helpers

Post by vrld »

Araqiel wrote:I tried implementing it myself and it just doesn't work.
Can you give some (comprehensive) code example that does not work?

Edit: added some missing callbacks (mousereleased, joystick*) and removed a bug (Gamestate.keypressed called love.keyreleased when using registerEvents). I also made the gamestates ignorant to the arguments passed to the callbacks, so you could use it in your custom love.run loops.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
bartoleo
Party member
Posts: 118
Joined: Wed Jul 14, 2010 10:57 am
Location: Savigliano

Re: HUMP - yet another set of helpers

Post by bartoleo »

may I ask a simple change in Gamestate?

passing "to" gamestate when calling "Gamestate.current:leave()"

thank you...
Bartoleo
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: HUMP - yet another set of helpers

Post by vrld »

bartoleo wrote:passing "to" gamestate when calling "Gamestate.current:leave()"
I am not sure about that. state:leave() should usually only contain cleanup code, such as stopping audio sources.
The only use I see is to do different cleanup tasks depending on the following gamestate. But that would result in a big an cluttered leave() depending on how many follwing states you have.
If you just want to carry stuff over to the following gamestate, it is better to use the pre-parameter in state:enter() instead.

That said, you can easily do this modification yourself by changing line 62 of gamestate.lua to:

Code: Select all

Gamestate.current:leave(to)
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
bartoleo
Party member
Posts: 118
Joined: Wed Jul 14, 2010 10:57 am
Location: Savigliano

Re: HUMP - yet another set of helpers

Post by bartoleo »

yes...
but I was using 'stacked states'
state1->state2->return to original state1 (game->pause->game or game->options->game or game->inventory->game...)

it' s working storing pre in enter 'event' plus with another optional parameter

but if I want to use leave callback
in this 'scenario' it shouldn't clean "all" but only sounds or similar...

another question:
I have a game gamestate
in this gamestate I create a 'physics world'

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)
addCollision,persistCollision,remCollision,resultCollision
are defined as 'global' 'function' :

Code: Select all

function addCollision(a, b, coll)
if I define it as 'class' function:

Code: Select all

function state:addCollision(a, b, coll)
and I use setCallbacks:

Code: Select all

  state.world:setCallbacks(state:addCollision, state:persistCollision, state:remCollision, state:resultCollision)
why it gives me this error?

Code: Select all

function arguments expected neat ',' 
Bartoleo
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: HUMP - yet another set of helpers

Post by Robin »

bartoleo wrote:and I use setCallbacks:

Code: Select all

  state.world:setCallbacks(state:addCollision, state:persistCollision, state:remCollision, state:resultCollision)
why it gives me this error?

Code: Select all

function arguments expected neat ',' 
That's Lua:

Code: Select all

a = {}
function a:b() end
--equivalent to:
function a.b(self) end

a:b()
--equivalent to:
a.b(a)
a:b has no meaning: it can't add an extra argument, because it's not a function call.
So use state.addCollision, etc.
Help us help you: attach a .love.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: HUMP - yet another set of helpers

Post by vrld »

Instead of "stacked" states (which I find not to be a good concept, because states encapsulate - well - state and stacking them stores implicit state in the order of stacking), you could use what would be best described as interruption.
An interruption hijacks all callbacks until it is finished, much like a modal dialog in GUIs. You can achieve this like so:

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
Im guessing if something like this would actually make a good addition to hump. What do you think?
bartoleo wrote:why it gives me this error?
What Robin said. In addition to his proposal, you could also use closures:

Code: Select all

state.world:setCallbacks(
            function() state:addCollision end,
            function() state:persistCollision end,
            function() state:remCollision end,
            function() state:resultCollision end)
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests