summaryrefslogtreecommitdiffstats
path: root/src/GLDriver.hs
blob: 3d344efe0681cac22ee25598ffa607f4935d075e (plain)
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
{-# LANGUAGE ExistentialQuantification, DeriveDataTypeable #-}

module GLDriver ( Driver(..)
                , SomeDriver(..)
                , Event
                , SomeEvent(..)
                , fromEvent
                , QuitEvent(..)
                , Key(..)
                , KeyPressEvent(..)
                , KeyReleaseEvent(..)
                , MouseMotionEvent(..)
                , MousePressEvent(..)
                ) where

import Data.Typeable


class Driver a where
    initialized :: a -> Bool
    
    initGL :: a -> IO a
    deinitGL :: a -> IO ()
    
    swapBuffers :: a -> IO ()
    
    usleep :: a -> Integer -> IO ()
    
    nextEvent :: a -> IO (a, Maybe SomeEvent)

data SomeDriver = forall d. Driver d => SomeDriver d

instance Driver SomeDriver where
    initialized (SomeDriver d) = initialized d
    initGL (SomeDriver d) = initGL d >>= return . SomeDriver
    deinitGL (SomeDriver d) = deinitGL d
    swapBuffers (SomeDriver d) = swapBuffers d
    nextEvent (SomeDriver d) = nextEvent d >>= \(gl, ev) -> return (SomeDriver gl, ev)
    usleep (SomeDriver d) = usleep d


class (Typeable a, Show a) => Event a

data SomeEvent = forall a. Event a => SomeEvent a
instance Show SomeEvent where
    show (SomeEvent a) = show a

fromEvent :: Event a => SomeEvent -> Maybe a
fromEvent (SomeEvent a) = cast a


data QuitEvent = QuitEvent deriving (Typeable, Show)
instance Event QuitEvent


data Key = KeyLeft | KeyRight | KeyUp | KeyDown
         deriving (Eq, Ord, Show)

data KeyPressEvent = KeyPressEvent Key deriving (Typeable, Show)
instance Event KeyPressEvent

data KeyReleaseEvent = KeyReleaseEvent Key deriving (Typeable, Show)
instance Event KeyReleaseEvent


data MouseMotionEvent = MouseMotionEvent Float Float deriving (Typeable, Show)
instance Event MouseMotionEvent

data MousePressEvent = MousePressEvent Float Float deriving (Typeable, Show)
instance Event MousePressEvent