1

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

问题是键盘控制工作非常糟糕 - 按键非常无响应并且通常有几秒钟的延迟。我假设作者为 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)
4

1 回答 1

1

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

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

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

于 2012-05-15T06:44:25.487 回答