为了玩弄Haskell的Gloss库,我写了以下代码:
import Graphics.Gloss
data World = World { worldBugs :: [Picture] }
bug :: Point -> Float -> Picture
bug (x, y) s =
let head = Translate x (y - s) $ Circle (s * 0.8)
body = Translate x (y + s) $ Circle (s * 1.2)
in pictures [head, body]
main = play (InWindow "Animation Test" (400, 400) (100, 100)) white 10
(World . map (\(n,b) -> Translate (n * 20) (n * 20) $ b) $ zip [0..] $ replicate 100 $ bug (0,0) 100)
(\world -> pictures $ worldBugs world)
(\event world -> world)
(\time (World bs) -> World $ map (Rotate (time * 10)) bs)
这段代码展示了一些“bug”(两个圆形组成的头和躯干),随着时间的推移旋转。问题是,运行几秒钟后,它会崩溃并显示以下错误信息:
Gloss / OpenGL Stack Overflow "after drawPicture."
This program uses the Gloss vector graphics library, which tried to
draw a picture using more nested transforms (Translate/Rotate/Scale)
than your OpenGL implementation supports. The OpenGL spec requires
all implementations to have a transform stack depth of at least 32,
and Gloss tries not to push the stack when it doesn't have to, but
that still wasn't enough.
You should complain to your harware vendor that they don't provide
a better way to handle this situation at the OpenGL API level.
To make this program work you'll need to reduce the number of nested
transforms used when defining the Picture given to Gloss. Sorry.
如果我理解正确,这基本上意味着最终将太多的转换放到堆栈中,从而导致堆栈溢出。它指出这可能是硬件限制(我使用的是Surface 2 Pro),那么我该怎么办?使用animate
时不会出现这种情况,但这可能是因为它在每个刻度不会传递状态。
如果我要制作游戏,我必须使用play
将状态传递到下一个刻度;我不能把所有东西都基于时间来做。有没有什么方法可以解决这个问题?谷歌搜索错误几乎没有结果。