1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
|
module GTest where
import Graphics
import GraphicsColor
-- This import decl reveals a Hugs bug
-- import GraphicsBrush(RGB(RGB))
import GraphicsPicture
-- import Region
import GraphicsRegion
import List(nub)
import Array
import Concurrent
----------------------------------------------------------------
myPar :: [IO ()] -> IO ()
myPar ms = foldr par (return ()) ms
where
par m1 m2 = do
v1 <- newEmptyMVar
v2 <- newEmptyMVar
forkIO (m1 >> putMVar v1 ())
forkIO (m2 >> putMVar v2 ())
takeMVar v1
takeMVar v2
return ()
----------------------------------------------------------------
-- abbreviation for runGraphics - almost all tests that you run
-- will be of the form "rg w<n>" for some small positive n
rg = runGraphics
main = runGraphics $ myPar [w1,w2,w3,w4,w5,w6 p1,w7,w8,w9,w10]
w1 = do
w <- openWindow "My First Graphics Program" (400,400)
draw w $ text (150,200) "Hello Graphics Window 1"
getLBP w
draw w $ text (150,250) "Hit a key"
getKey w
clearWindow w
draw w $ text (150,200) "Thank you"
draw w $ text (150,250) "Hit another key"
getKey w
closeWindow w
w2 = do
w <- openWindow "My Second Graphics Program" (400,400)
draw w $ text (150,200) "Hello Graphics Window 2"
getRBP w
draw w $ text (150,250) "Hit a key"
getKey w
clearWindow w
draw w $ text (150,200) "Thank you"
draw w $ text (150,250) "Hit another key"
getKey w
closeWindow w
----------------------------------------------------------------
-- Region demo
----------------------------------------------------------------
r1 = unions
[ Shape (Rect (000,000) (300,100))
, Shape (Ellipse (100,000) (200,500))
]
r2 = Shape (Rect (050,050) (250,150))
r3 = r1 `Intersect` r2
r4 = pentangle 0
pentangle :: Angle -> MyRegion
pentangle phi = poly phi (4*pi/5) 5
triangle :: Angle -> MyRegion
triangle phi = poly phi (2*pi/3) 3
-- draw a regular polygon with "n" sides "angle" apart
poly :: Angle -> Angle -> Int -> MyRegion
poly init delta n = Shape (Polygon vertices)
where
thetas = take n [init, init+delta ..]
vertices = [ (round x, round y)
| theta <- thetas
, let x = 200*(1+cos theta)
, let y = 200*(1+sin theta)
]
----------------------------------------------------------------
w3 = do
w <- openWindow "Regions demo" (300,500)
mapM_ (\ r -> do
setPicture w (drawRegion r)
getLBP w)
[r1,r2,r3,r4]
clearWindow w
draw w $ text (150,250) "Hit a key"
getKey w
closeWindow w
w4 = do
wa <- openWindow "Multiwindow demo 1" (300,500)
wb <- openWindow "Multiwindow demo 2" (300,500)
draw wa $ text (100,100) "Hello World 1"
draw wb $ text (100,100) "Hello World 2"
ka <- getKey wa
draw wb $ text (100,200) ("Got key " ++ [ka])
kb <- getKey wb
draw wa $ text (100,200) ("Got key " ++ [kb])
closeWindow wa
closeWindow wb
----------------------------------------------------------------
-- Picture demo
----------------------------------------------------------------
type Pic color = [(color, MyRegion)]
p1 = [ (Red,r3), (Green, r2), (Blue, r1)]
w5 = do
w <- openWindow "Picture demo" (300,500)
setPicture w (drawPic p1)
getLBP w
clearWindow w
draw w $ text (150,250) "Hit a key"
getKey w
closeWindow w
drawPic :: Pic Color -> Picture
drawPic p = overMany [ withColor c $ drawRegion r | (c,r) <- p ]
----------------------------------------------------------------
-- Faster picture drawing
----------------------------------------------------------------
-- This version is more efficient because it allocates colors just once.
--
-- The cost of this performance gain is that we have to expose the
-- allocation and deallocation of brushes to the programmer, which
-- makes it possible for them to deallocate too early, to deallocate
-- too late (eg never), or to deallocate too often.
--
-- Notice that you must not deallocate brushes (and fonts, etc)
-- until _after_ you clear the screen - otherwise the redraw routine
-- might be called and you'll have a dangling reference to the brush.
w6 p = do
w <- openWindow "Faster Picture Demo" (300,500)
-- get the "palette"
let colors = nub [ c | (c,r) <- p ]
brushes <- mapM (\c -> createBrush (colorTable ! c)) colors
let palette = array (minBound,maxBound) (zip colors brushes)
let pic = [ (palette!c, r) | (c,r) <- p ]
setPicture w (drawPic2 pic)
getLBP w
clearWindow w
mapM_ deleteBrush brushes -- delete brushes after clearWindow
draw w $ text (150,250) "Hit a key"
getKey w
closeWindow w
drawPic2 :: Pic Brush -> Picture
drawPic2 p = overMany [ withBrush b (drawRegion r) | (b,r) <- p ]
----------------------------------------------------------------
-- Animation (sort of)
--
-- Simple animations made of lists of pictures
----------------------------------------------------------------
type Frame = Pic Color
type Anim = [Frame]
a1 :: Anim
a1 = [ [(Red, pentangle phi)
,(Green, triangle (-phi))
]
| phi <- [0, pi/20 .. 2*pi]
]
-- draw an animation (using user-input to step through animation)
w7 = do
w <- openWindow "Animation demo" (400,400)
mapM_ (drawFrame w) a1
where
-- draw a frame and wait for left button press
drawFrame :: Window -> Frame -> IO ()
drawFrame w p = do
setPicture w (drawPic p)
getLBP w
return ()
----------------------------------------------------------------
-- Timer demo
----------------------------------------------------------------
-- draw an animation (using timer to step through animation)
w8 = do
w <- openWindowEx "Timer demo" Nothing (Just (400,400))
drawBufferedPicture (Just 50)
mapM_ (drawFrame w) (cycle a1)
where
-- draw a frame and wait for a tick
drawFrame :: Window -> Frame -> IO ()
drawFrame w p = do
setPicture w (drawPic p)
getTick w
----------------------------------------------------------------
-- Text demo
----------------------------------------------------------------
-- half-inch wide, red text on a transparent background at a 45 degree angle
-- quarter-inch wide, red text on a green background at a -45 degree angle
w9 = do
w <- openWindow "Font demo" (500,500)
font1 <- createFont (50,50) (pi/4) False False "Arial"
font2 <- createFont (25,50) (-pi/4) True True "Times New Roman"
draw w
$ withTextColor (RGB 255 0 0)
$ withFont font1
$ withBkMode Transparent
$ text (050,450) "Font Test 1"
draw w
$ withTextColor (RGB 0 0 255)
$ withFont font2
$ withBkColor (RGB 0 255 0)
$ text (050,050) "Font Test 2"
getLBP w
deleteFont font1
deleteFont font2
closeWindow w
----------------------------------------------------------------
-- Error catching demo
----------------------------------------------------------------
-- This program demonstrates that the system doesn't get left in
-- an inconsistent state even if your program hits an error.
w10 = do
w <- openWindow "Error recovery demo" (300,300)
draw w
$ text (10,150) "Click me to test error recovery"
getLBP w
draw w $ error "foo1"
--error "foo2"
getLBP w
clearWindow w
draw w
$ text (10,150) "Shouldn't have made it this far"
getLBP w
closeWindow w
----------------------------------------------------------------
-- Bitmap demo
----------------------------------------------------------------
w11 = do
w <- openWindow "Bitmap demo" (300,500)
setPicture w $
text (150,200) "Test"
`over`
drawPic p1
savePicture "bitmaps/Foo.bmp" (300,500) $
text (150,200) "Test"
`over`
drawPic p1
getLBP w
closeWindow w
w11b = do
(bmp,_) <- loadBitmap "bitmaps/Foo.bmp"
w <- openWindow "Bitmap demo" (300,500)
setPicture w $ bitmap (50,50) bmp
getKey w
closeWindow w
w11c = do
(bmp,_) <- loadBitmap "bitmaps/Foo.bmp"
w <- openWindow "Bitmap demo" (300,500)
setPicture w $ stretchBitmap (0,400) (400,0) bmp
getKey w
closeWindow w
w12 = do
w <- openWindow "Bitmap demo" (400,400)
mapM_ (drawFrame w) (zip [100..] a1)
where
-- draw a frame and wait for left button press
drawFrame :: Window -> (Int,Frame) -> IO ()
drawFrame w (i,p) = do
setPicture w $ drawPic p
savePicture name (400,400) $ drawPic p
getLBP w
return ()
where
name = "bitmaps/" ++ "Foo" ++ show i ++ ".bmp"
----------------------------------------------------------------
-- Examples from the documentation
----------------------------------------------------------------
demos :: IO ()
demos = runGraphics $ myPar
[ helloWorld
, eg cp
, fontDemo
, lineDemo 0
, lineDemo 1
, lineDemo 20
, keyTest
, timerDemo
, ellipseTest
]
helloWorld :: IO ()
helloWorld = do
w <- openWindow "Hello World Window" (300, 300)
draw w $ text (100, 100) "Hello"
draw w $ text (100, 200) "World"
getKey w
closeWindow w
cp :: Picture
cp =
mkBrush (colorTable!Red) $ \ red ->
mkBrush (colorTable!Blue) $ \ blue ->
over
(withBrush red $ polygon [(200,200),(400,200),(300,400)])
(withBrush blue $ polygon [(100,100),(500,100),(500,500),(100,500)])
eg :: Picture -> IO ()
eg p = do
w <- openWindow "Hello World Window" (600,600)
draw w p
getKey w
closeWindow w
fontDemo :: IO ()
fontDemo = do
w <- openWindow "Font Demo Window" (500,500)
draw w $
withTextColor (RGB 255 0 0) $
mkFont (50,100) (pi/4) False True "Arial" $ \ font ->
withFont font $
withBkColor (RGB 0 255 0) $
withBkMode Opaque $
text (050,450) "Font Demo"
getKey w
closeWindow w
-- Note that "width" must be 1 or less for the penstyle to matter
lineDemo :: Int -> IO ()
lineDemo width = do
w <- openWindow ("Line Demo Window " ++ show width) (500,500)
draw w $
let
color = colorTable ! Red
in
mkPen Solid width color $ \ pen1 ->
mkPen Dash width color $ \ pen2 ->
mkPen Dot width color $ \ pen3 ->
mkPen DashDotDot width color $ \ pen4 ->
overMany
[ withPen pen1 $ line (100,100) (400,100)
, withPen pen2 $ line (100,200) (400,200)
, withPen pen3 $ line (100,300) (400,300)
, withPen pen4 $ line (100,400) (400,400)
]
getKey w
closeWindow w
-- Just what keys can we see?
keyTest :: IO ()
keyTest= do
w <- openWindow "Keypress Demo Window" (500,500)
c <- getKey w
print (fromEnum c)
closeWindow w
-- Tick counter
timerDemo = do
w <- openWindowEx
"Timer demo" -- title
(Just (500,500)) -- initial position of window
Nothing -- (Just (100,100)) -- initial size of window
drawFun -- draw function - see below
(Just 50) -- tick rate
let
loop x = do
setPicture w $ text (0,50) $ show x
getTick w -- wait for next tick on window
loop (x+1)
loop 0
where
-- The possible choices of "drawFun" are
--
-- o drawBufferedPicture - use a double buffer to reduce animation flicker
-- o drawPicture - draw directly to screen (for speed)
useDoubleBuffering = True
drawFun = if useDoubleBuffering then
drawBufferedPicture
else
drawPicture
ellipseTest :: IO ()
ellipseTest = do
w <- openWindow "Ellipse Test" (300, 300)
draw w $ ellipse (0,0) (200, 100)
getKey w
closeWindow w
----------------------------------------------------------------
-- Region code - prototype of the code in SOE
----------------------------------------------------------------
{-
module Region(
MyRegion(Empty, Union, Intersect, Shape),
unions,
Shape(Rect, Ellipse, Polygon),
drawRegion
) where
import GraphicsRegion
-}
data MyRegion
= Empty
| Union MyRegion MyRegion
| Intersect MyRegion MyRegion
| Shape Shape
unions :: [MyRegion] -> MyRegion
unions = foldr Union Empty
--intersects :: [MyRegion] -> MyRegion
--intersects = foldr Intersect Full
drawRegion :: MyRegion -> Picture
drawRegion r =
regionToRGN r $ \ rgn ->
region rgn
regionToRGN :: MyRegion -> (Region -> Picture) -> Picture
regionToRGN Empty c = mkEmpty c
regionToRGN (Shape s) c = shapeToRGN s c
regionToRGN (r1 `Union` r2) c =
regionToRGN r1 $ \ rgn1 ->
regionToRGN r2 $ \ rgn2 ->
orRegion rgn1 rgn2 $ \ rgn ->
c rgn
regionToRGN (r1 `Intersect` r2) c =
regionToRGN r1 $ \ rgn1 ->
regionToRGN r2 $ \ rgn2 ->
andRegion rgn1 rgn2 $ \ rgn ->
c rgn
data Shape
= Rect Point Point
| Ellipse Point Point
| Polygon [Point]
shapeToRGN :: Shape -> (Region -> Picture) -> Picture
shapeToRGN (Rect p1 p2) = mkRectangle p1 p2
shapeToRGN (Ellipse p1 p2) = mkEllipse p1 p2
shapeToRGN (Polygon ps) = mkPolygon ps
----------------------------------------------------------------
-- End
----------------------------------------------------------------
|