Page 1 of 1

Mimicking behavior of love.keypressed

Posted: Fri Aug 18, 2017 9:56 am
by agargara
I'm using SiENcE's fabulous midi library to get input from a midi controller. That's working great, but when I put my midi listener in love.update, it seems to interfere with the drawing thread.

For example, here's code that changes the background image when I press the midi note '48':

Code: Select all

function love.update(dt)
  -- do some calculations
  a,b,c,d = midi.getMessage(midi_controller_port)
  if a == 144 and b == 48 then
    next_scene()
  end
end
When I press the note, the screen flashes white for a split second before changing the background image.
It's very brief (I think only one frame) but noticeable enough to be jarring.
I have almost identical code in love.keypressed:

Code: Select all

function love.keypressed(key, u)
  if key == "w" then
    next_scene()
  end
end
When I press 'w' and it calls next_scene, there's no white flash.
Why does the keypressed function not interfere with the graphics?
Is the keypressed listener running on a separate thread than the main drawing thread or something?
How can I mimic this behavior with a "midi listener"? I tried to figure out how to use a thread to do this but haven't been able to figure threads out from the wiki/forums. Some concrete examples would be most appreciated! :D

Re: Mimicking behavior of love.keypressed

Posted: Fri Aug 18, 2017 10:09 am
by Nixola
Maybe it's due to the fact that you're calling next_scene in the middle of the update. Try, for debugging purposes, to remove next_scene() from keypressed and instead add this in love.update:

Code: Select all

if love.keyboard.isDown("w") then
  next_scene()
end
To see if it causes the same issue. If so, you'll have to be more careful when you call the function.

Re: Mimicking behavior of love.keypressed

Posted: Fri Aug 18, 2017 11:55 am
by agargara
Nixola wrote: Fri Aug 18, 2017 10:09 am Maybe it's due to the fact that you're calling next_scene in the middle of the update. Try, for debugging purposes, to remove next_scene() from keypressed and instead add this in love.update:

Code: Select all

if love.keyboard.isDown("w") then
  next_scene()
end
To see if it causes the same issue. If so, you'll have to be more careful when you call the function.
You were completely right...I feel so silly now!! Thank you!
Indeed, the problem was that I was updating the background before doing midi listening...I switched the order to listen for midi and then update the background. Problem solved! :awesome:
EDIT: I'm still a little curious about how love.keypressed works internally. Is it running on a separate thread or is it always called before love.update?

Re: Mimicking behavior of love.keypressed

Posted: Fri Aug 18, 2017 1:14 pm
by zorg
agargara wrote: Fri Aug 18, 2017 11:55 am I'm still a little curious about how love.keypressed works internally. Is it running on a separate thread or is it always called before love.update?
The wiki shows you how love.run is coded, from which you can see that löve goes through all events it received, then calls update, then draw, sleeps, and these steps are then repeated.

In short, yes; all callbacks, like love.keypressed for example, are called before love.update.