gpt4 book ai didi

linux - GLUT 键盘 react 迟钝

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:08:04 27 4
gpt4 key购买 nike

我正在测试在这里找到的简单乒乓球游戏: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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com