linux - GLUT 键盘 react 迟钝

标签 linux haskell glut pong

我正在测试在这里找到的简单乒乓球游戏:https://github.com/shangaslammi/frp-pong

问题是键盘控件的工作非常糟糕 - 按键 react 非常迟钝,并且经常有几秒钟的延迟。我假设作者为 Windows 编写了代码,因为他包含了一个 .bat 文件,所以这是一个特定于 Linux 的问题。

为什么会这样?

我不确定问题出在哪里,但这是文件 Keyboard.hs:

import Data.Set (Set)
import qualified Data.Set as Set
import Graphics.UI.GLUT (Key(..), KeyState(..))

-- | Set of all keys that are currently held down
newtype Keyboard = Keyboard (Set Key)

-- | Create a new Keyboard
initKeyboard :: Keyboard
initKeyboard = Keyboard Set.empty

-- | Record a key state change in the given Keyboard
handleKeyEvent :: Key -> KeyState -> Keyboard -> Keyboard
handleKeyEvent k Down = addKey k
handleKeyEvent k Up   = removeKey k

addKey :: Key -> Keyboard -> Keyboard
addKey k (Keyboard s) = Keyboard $ Set.insert k s

removeKey :: Key -> Keyboard -> Keyboard
removeKey k (Keyboard s) = Keyboard $ Set.delete k s

-- | Test if a key is currently held down in the given Keyboard
isKeyDown :: Keyboard -> Key -> Bool
isKeyDown (Keyboard s) k = Set.member k s

并设置回调:

type KeyboardRef = IORef Keyboard
type TimeRef     = IORef POSIXTime
type AccumRef    = TimeRef
type PrevTimeRef = TimeRef
type GameRef     = IORef (Rects, GameLogic)

type CallbackRefs = (AccumRef, PrevTimeRef, KeyboardRef, GameRef)

initCallbackRefs :: IO CallbackRefs
initCallbackRefs = do
    accum <- newIORef secPerTick
    prev  <- getPOSIXTime >>= newIORef
    keyb  <- newIORef initKeyboard
    cont  <- newIORef ([],game)
    return (accum, prev, keyb, cont)

-- | Update the Keyboard state according to the event
handleKeyboard :: CallbackRefs -> KeyboardMouseCallback
handleKeyboard (_, _, kb, _) k ks _ _ = modifyIORef kb (handleKeyEvent k ks)

最佳答案

问题似乎是缺少 GLUT 计时器。

这是 Rian Hunter 的正确工作版本:

https://github.com/rianhunter/frp-pong

关于linux - GLUT 键盘 react 迟钝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10595223/

相关文章:

haskell - 大列表上的单体折叠中的堆栈溢出

text - 如何使用 ansi-wl-pprint 库在 Haskell 中设置 'page width'?

c++ - Qt - Glut32 库

Java: 无法找到或加载主类 Main

python - 为什么 pip freeze >requirements.txt 返回权限被拒绝

linux - linux中的多进程

haskell - 计算整数的程序的长时间运行

c++ - 堆损坏和 F12 问题

opengl - 过多的弹出菜单

linux - Bash 免费命令停止工作