OpenGL-2.9.2.0: A binding for the OpenGL graphics system

Copyright | (c) Sven Panne 2013
License | BSD3
Maintainer | Sven Panne <svenpanne@gmail.com>
Stability | stable
Portability | portable
Safe Haskell | Safe-Inferred
Language | Haskell98

Graphics.Rendering.OpenGL.GL.StateVar

Contents

- Readable State Variables
- Writable State Variables
- General State Variables
- Utility Functions

Description

State variables are references in the IO monad, like `IORef`s or parts of the OpenGL state. Note that state variables are not neccessarily writable or readable, they may come in read-only or write-only flavours, too. As a very simple example for a state variable, consider an explicitly allocated memory buffer. This buffer can easily be converted into a `StateVar`:

    makeStateVarFromPtr :: Storable a => Ptr a -> StateVar a
    makeStateVarFromPtr p = makeStateVar (peek p) (poke p)

The example below puts 11 into a state variable (i.e. into the buffer), increments the contents of the state variable by 22, and finally prints the resulting content:

      do p <- malloc :: IO (Ptr Int)
         let v = makeStateVarFromPtr p
         v $= 11
         v $~ (+ 22)
         x <- get v
         print x

`IORef`s are state variables, too, so an example with them looks extremely similiar:

      do v <- newIORef (0 :: Int)
         v $= 11
         v $~ (+ 22)
         x <- get v
         print x

Synopsis

- class HasGetter g where
  - get :: g a -> IO a
- data GettableStateVar a
- makeGettableStateVar :: IO a -> GettableStateVar a
- class HasSetter s where
  - ($=) :: s a -> a -> IO ()
- data SettableStateVar a
- makeSettableStateVar :: (a -> IO ()) -> SettableStateVar a
- data StateVar a
- makeStateVar :: IO a -> (a -> IO ()) -> StateVar a
- ($~) :: (HasGetter v, HasSetter v) => v a -> (a -> a) -> IO ()
- ($=!) :: HasSetter s => s a -> a -> IO ()
- ($~!) :: (HasGetter v, HasSetter v) => v a -> (a -> a) -> IO ()

# Readable State Variables

class HasGetter g where

The class of all readable state variables.

Methods

get :: g a -> IO a

Read the value of a state variable.

Instances

HasGetter IORef |
HasGetter StateVar |
HasGetter GettableStateVar |

data GettableStateVar a

A concrete implementation of a read-only state variable, carrying an IO action to read the value.

Instances

HasGetter GettableStateVar |

makeGettableStateVar :: IO a -> GettableStateVar a

Construct a `GettableStateVar` from an IO action.

# Writable State Variables

class HasSetter s where

The class of all writable state variables.

Methods

($=) :: s a -> a -> IO () infixr 2

Write a new value into a state variable.

Instances

HasSetter IORef |
HasSetter StateVar |
HasSetter SettableStateVar |

data SettableStateVar a

A concrete implementation of a write-only state variable, carrying an IO action to write the new value.

Instances

HasSetter SettableStateVar |

makeSettableStateVar :: (a -> IO ()) -> SettableStateVar a

Construct a `SettableStateVar` from an IO action.

# General State Variables

data StateVar a

A concrete implementation of a readable and writable state variable, carrying one IO action to read the value and another IO action to write the new value.

Instances

HasSetter StateVar |
HasGetter StateVar |

makeStateVar :: IO a -> (a -> IO ()) -> StateVar a

Construct a `StateVar` from two IO actions, one for reading and one for writing.

# Utility Functions

($~) :: (HasGetter v, HasSetter v) => v a -> (a -> a) -> IO ()

A modificator convenience function, transforming the contents of a state variable with a given funtion.

($=!) :: HasSetter s => s a -> a -> IO ()

A variant of `$=` which is strict in the value to be set.

($~!) :: (HasGetter v, HasSetter v) => v a -> (a -> a) -> IO ()

A variant of `$~` which is strict in the transformed value.