tags:

views:

638

answers:

1

I've been trying to get input with IUP to make a small pong game. I wanted to try some input, and tried some of the code that comes with the IUPGL examples, but the input doesn't seem to be working at all. Here's the code as it stands so far:

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()

It's my understanding that canvas:k_any() gets called when a key is pressed, but it's not responding, even to the quit command. Any ideas?

+2  A: 

Here is at least part of the problem for the sample you've quoted. Before the IUP keyname constants are actually defined in your iup table, you need to call a function. The idea is that the memory footprint is lower if you don't need all of the key names, which many applications can do entirely without.

Try adding the following lines sometime shortly after require "iuplua" and before you use the names in any code:

iup.key_open()      -- load key names

This is documented near the top of the topic Keyboard Codes as well as in a variety of places related to the keyboard.

Without the call to iup.key_open(), iup.K_q evaluates to nil (as does any other key name) and thus silently never matches any keycode passed to canvas:k_any().

Edit: I just ran your sample, and indeed calling iup.key_open() causes the q and w keys to be recognized. You have a further glitch you will notice next that just updating the paddle's position doesn't ask for the canvas to redraw. I'll leave that as an exercise as you go forward.

Edit2: Adding the line iup.Update(self) to the function canvas:k_any() just after you modify the state of the paddle appears to do what you want. Doing it that way rather than manually calling the action() method has the advantage of interacting with IUP's message loop handling in the expected way. In the general case, calling iup.UpdateChildren(window) instead might be preferred. That would keep any other IUP controls that are displayed in the dialog updated as well as the canvas, which can be useful if you have controls that don't do all their own updating internally.

Alternatively, you could move the drawing routines into their own function called from both the k_any() and action() methods of the canvas. I'm not sure I'd recommend that in general as it doesn't scale as well when you want to support multiple viewports or related controls.

You might want to rearrange the logic in your k_any() handler so that you are less likely to accidentally forget the call to iup.Update() as you add cases for new keys.

RBerteig
I suspected that the keys were `nil`, thanks for confirming that! I'm currently looking through the documentation, trying to figure out how to force a redraw, without much luck. I've tried `iup.Update` and `iup.UpdateChildren`, but it's complaining that they are `nil` values.
Cristián Romo
Well, I've managed to make it work, but it seems like it's kinda klunky. At the end of canvas:k_any(), I'm directly calling canvas:action(). This gets the job done, but is there a better way to force a redraw?
Cristián Romo
I edited in some explanation about where and how to call iup.Update(self) in the canvas:k_any(key) method.
RBerteig