Mimicking behavior of love.keypressed

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
agargara
Prole
Posts: 5
Joined: Fri Aug 18, 2017 9:44 am

Mimicking behavior of love.keypressed

Post 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
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Mimicking behavior of love.keypressed

Post 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.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
agargara
Prole
Posts: 5
Joined: Fri Aug 18, 2017 9:44 am

Re: Mimicking behavior of love.keypressed

Post 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?
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Mimicking behavior of love.keypressed

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests