Receiving input from IUP in Lua
I am trying to access the IUP to make a little pong game. I wanted to try a little input and tried some of the code that comes with the IUPGL examples, but input doesn't seem to work at all. Here's the code that's still standing:
require "iuplua"
require "iupluagl"
require "luagl"
paddle = {x = -0.9, y = 0.2}
function drawPaddle(x, y)
gl.Begin(gl.QUADS)
gl.Color(0.0, 0.5, 0.0)
gl.Vertex(x, y)
gl.Vertex(x + 0.1, y)
gl.Vertex(x + 0.1, y - 0.4)
gl.Vertex(x, y - 0.4)
end
canvas = iup.glcanvas{buffer = "DOUBLE", rastersize = "300x300"}
function canvas:action(x, y)
iup.GLMakeCurrent(self)
gl.ClearColor(0.0, 0.0, 0.0, 0.0)
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.Clear(gl.DEPTH_BUFFER_BIT)
gl.MatrixMode(gl.PROJECTION)
gl.Viewport(0, 0, 300, 300)
gl.LoadIdentity()
drawPaddle(paddle.x, paddle.y)
gl.End()
iup.GLSwapBuffers(self)
end
window = iup.dialog{canvas, title = "Soon to be Pong"}
function canvas:k_any(c)
if c == iup.K_q then
return iup.CLOSE
elseif c == iup.K_w then
paddle.y = paddle.y + 0.02
return iup.CONTINUE
else
return iup.DEFAULT
end
end
window:show()
iup.MainLoop()
I understand what canvas:k_any()
is called when a key is pressed, but not even responding to the quit command. Any ideas?
source to share
Here is at least part of the problem for the sample you provided. Before the IUP key constants are actually defined in your table iup
, you need to call the function. The idea is that the memory footprint is lower if you don't need all the key names, which many applications can do completely without.
Try adding the following lines some time after require "iuplua"
and before using names in any code:
iup.key_open () - load key names
This is documented at the top of the Keyboard Codes topic , as well as various keyboard related locations.
Without a call iup.key_open()
, it iup.K_q
evaluates to nil
(like any other key name) and thus silently never matches a key code passed to canvas:k_any()
.
Edit: I just ran your sample and in fact the call iup.key_open()
causes the q and w keys to be recognized. You have another glitch that you will notice that simply updating the paddle position does not require redrawing the canvas. I'll leave this as an exercise as you move forward.
Edit2: Adding a line iup.Update(self)
to the function canvas:k_any()
right after you change the paddle state seems to do what you want. Executing this method instead of manually calling the method action()
has the advantage that it interacts with the processing of the IUP message loop as expected. In general, calling may be preferred iup.UpdateChildren(window)
. This will keep any other IUP controls showing up in the updated dialog as well as on the canvas, which can be useful if you have controls that don't do all of their own update internally.
Alternatively, you can move the drawing routines into your own function called both with methods k_any()
and action()
on the canvas. I'm not sure if I would recommend this at all, as it doesn't scale when you want to support multiple viewports or related controls.
You might want to rearrange the logic in your handler k_any()
so that you are less likely to accidentally forget the call iup.Update()
when adding cases for new keys.
source to share