Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix demo dependencies #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Combinators for drawing 2D shapes and images in Haskell (using OpenGL)

* Summary: this module is a functional wrapper around OpenGL, so you don't have to go into the deep, dark world of imperative stateful programming just to draw stuff. It supports 2D only (for now), with support drawing geometry, images, and text.

* [Online documentation](http://hackage.haskell.org/package/graphics-drawingcombinators-1.4.4.1/docs/Graphics-DrawingCombinators.html)

* [Hackage entry](http://hackage.haskell.org/package/graphics-drawingcombinators)
63 changes: 63 additions & 0 deletions demo/example_b.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import Control.Monad
import Data.Monoid
import Graphics.DrawingCombinators ((%%))
import qualified Graphics.DrawingCombinators as Draw
import qualified Graphics.UI.GLFW as GLFW
import Data.IORef
import Data.Maybe

import System.Environment(getArgs)

windowWidth, windowHeight :: Int
windowWidth = 800
windowHeight = 600

initGL :: IO GLFW.Window
initGL = do
isInited <- GLFW.init
unless isInited $ error "Cannot init GLFW"

maybeW <- GLFW.createWindow windowWidth windowHeight "DrawingCombinators demo" Nothing Nothing
let window = fromMaybe (error "No window was created") maybeW
GLFW.makeContextCurrent $ Just window

return window

unitText :: Draw.Font -> String -> Draw.Image Any
unitText font str = (Draw.translate (-1,0) %% Draw.scale (2/w) (2/w) %% Draw.text font str)
`mappend`
Draw.tint (Draw.Color 1 0 0 1) (Draw.line (-1,0) (1,0))
where
w = Draw.textWidth font str

quadrants :: (Monoid a) => Draw.Image a -> Draw.Image a
quadrants img = mconcat [
(Draw.translate (-0.5,0.5) %%),
(Draw.translate (0.5,0.5) `Draw.compose` Draw.rotate (-pi/2) %%),
(Draw.translate (0.5,-0.5) `Draw.compose` Draw.rotate pi %%),
(Draw.translate (-0.5,-0.5) `Draw.compose` Draw.rotate (pi/2) %%)] (Draw.scale 0.5 0.5 %% img)

circleText :: Draw.Font -> String -> Draw.Image Any
circleText font str = unitText font str `mappend` Draw.tint (Draw.Color 0 0 1 0.5) Draw.circle

main :: IO ()
main = do
args <- getArgs
font <- case args of
[fontName] -> Draw.openFont fontName
_ -> error "Usage: drawingcombinators-example some_font.ttf"

window <- initGL

done <- newIORef False
GLFW.setWindowCloseCallback window (Just $ const $ writeIORef done True)
mainLoop window font done 0.0

where
mainLoop window font done rotation = do
Draw.clearRender $ Draw.rotate rotation %% quadrants (circleText font "Hello, world!")
GLFW.swapBuffers window
GLFW.pollEvents
doneValue <- readIORef done
when (not doneValue) $
mainLoop window font done (rotation - 0.01)
14 changes: 12 additions & 2 deletions graphics-drawingcombinators.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Author: Luke Palmer
Homepage: http://github.com/luqui/graphics-drawingcombinators
Maintainer: [email protected]
Build-Type: Simple
Extra-Source-Files: demo/example.hs
Extra-Source-Files: demo/example.hs, demo/example-b.hs
cabal-Version: >= 1.8

source-repository head
Expand Down Expand Up @@ -47,5 +47,15 @@ Executable example
else
Buildable: True
Build-depends: base >= 3 && < 5,
GLFW-b, OpenGL >= 2.2 && < 2.7, graphics-drawingcombinators
GLFW, OpenGL >= 2.2 && < 2.9, graphics-drawingcombinators
ghc-options: -Wall

Executable example_b
Main-is: demo/example_b.hs
if !flag(examples)
Buildable: False
else
Buildable: True
Build-depends: base >= 3 && < 5,
GLFW-b >= 1.4, OpenGL >= 2.2 && < 2.9, graphics-drawingcombinators
ghc-options: -Wall