1

ここにある簡単なポンゲームをテストしています:https ://github.com/shangaslammi/frp-pong

問題は、キーボードコントロールが非常にうまく機能しないことです。キーが非常に反応しなくなり、多くの場合、数秒の遅延が発生します。作成者が.batファイルを含めたため、Windows用のコードを作成したと思います。これは、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タイマーの欠如が問題のようでした。

RianHunterによる正しく機能するバージョンは次のとおりです。

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

于 2012-05-15T06:44:25.487 に答える