在Lua中使用IUP获取输入 [英] Getting input with IUP in Lua

查看:202
本文介绍了在Lua中使用IUP获取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试与IUP取得联系,以制作一个小型的乒乓球游戏.我想尝试一些输入,并尝试了IUPGL示例随附的一些代码,但是输入似乎根本不起作用.这是到目前为止的代码:

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

据我了解,按下某个键时会调用canvas:k_any(),但是它甚至没有响应quit命令.有什么想法吗?

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?

推荐答案

这里至少是您引用的示例问题的一部分.在iup表中实际定义IUP密钥名常量之前,您需要调用一个函数.这个想法是,如果您不需要所有键名,那么内存占用量就会减少,许多应用程序完全可以不需要这些键名.

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.

尝试在require "iuplua"之后不久并且在任何代码中使用名称之前,添加以下几行:

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.

在没有调用iup.key_open()的情况下,iup.K_q的计算结果为nil(其他任何键名也是如此),因此,在任何情况下都不会匹配传递给canvas:k_any()的任何键码.

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

编辑:我只是运行了您的示例,实际上调用iup.key_open()会使q和w键被识别.您还有一个小故障,接下来您会注意到,仅更新桨叶的位置并不会要求画布重新绘制.继续前进时,我将其保留为练习.

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 :刚修改完桨的状态后,将行iup.Update(self)添加到功能canvas:k_any()中似乎可以完成您想做的事情.这样做而不是手动调用action()方法具有以预期的方式与IUP的消息循环处理进行交互的优点.在一般情况下,调用iup.UpdateChildren(window)可能是更可取的.这样可以使对话框中显示的所有其他IUP控件以及画布都保持更新,如果您的控件内部未进行所有自己的更新,这将很有用.

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.

或者,您可以将绘图例程移到它们自己的函数中,该函数从画布的k_any()action()方法中调用.我不确定是否会建议一般使用,因为当您要支持多个视口或相关控件时,它并不能很好地缩放.

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.

您可能想重新排列k_any()处理程序中的逻辑,以使您在添加新键的大小写时不太可能意外忘记对iup.Update()的调用.

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.

这篇关于在Lua中使用IUP获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆