Skip to content

Commit ba9f39f

Browse files
committed
some changes, text API
1 parent 7aa4554 commit ba9f39f

File tree

6 files changed

+67
-41
lines changed

6 files changed

+67
-41
lines changed

Diff for: .travis.yml

+8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ matrix:
6767
compiler: ": #stack 8.0.2"
6868
addons: {apt: {packages: [libgmp-dev]}}
6969

70+
- env: BUILD=stack ARGS="--resolver lts-9"
71+
compiler: ": #stack 8.0.2 (2)"
72+
addons: {apt: {packages: [libgmp-dev]}}
73+
7074
# Nightly builds are allowed to fail
7175
- env: BUILD=stack ARGS="--resolver nightly"
7276
compiler: ": #stack nightly"
@@ -89,6 +93,10 @@ matrix:
8993
compiler: ": #stack 8.0.2 osx"
9094
os: osx
9195

96+
- env: BUILD=stack ARGS="--resolver lts-9"
97+
compiler: ": #stack 8.0.2 osx (2)"
98+
os: osx
99+
92100
- env: BUILD=stack ARGS="--resolver nightly"
93101
compiler: ": #stack nightly osx"
94102
os: osx

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Haskell library providing functions to use Cairo to draw on SDL textures and con
55
##### Install
66

77
This library depends on the new [SDL2 bindings](https://github.com/haskell-game/sdl2), available on
8-
Hackage as [sdl2 version 2.0.0 or greater](http://hackage.haskell.org/package/sdl2)
8+
Hackage as [sdl2 version 2.1.0 or greater](http://hackage.haskell.org/package/sdl2)
99
and [cairo bindings](https://hackage.haskell.org/package/cairo).
1010

1111
Just clone and install this repository:
@@ -15,7 +15,7 @@ cd sdl2-cairo
1515
cabal install
1616
```
1717

18-
It has been tested with GHC 7.10.1 on a Linux system without any problems.
18+
It should work with recent GHC versions (>= 7.8.4) without problems under Linux und OS X.
1919

2020
##### Documentation
2121

Diff for: src/SDL/Cairo.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ createCairoTexture r = createTexture r ARGB8888 TextureAccessStreaming
2626
createCairoTexture' :: Renderer -> Window -> IO Texture
2727
createCairoTexture' r w = do
2828
surf <- getWindowSurface w
29-
sz@(V2 w h) <- surfaceDimensions surf
29+
sz <- surfaceDimensions surf
3030
createCairoTexture r sz
3131

3232
-- |draw on SDL texture with Render monad from Cairo

Diff for: src/SDL/Cairo/Canvas.hs

+51-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
1+
{-# LANGUAGE CPP,GeneralizedNewtypeDeriving #-}
22
{-|
33
Module : SDL.Cairo.Canvas
44
Copyright : Copyright (c) 2015 Anton Pirogov
@@ -24,7 +24,7 @@ import SDL.Cairo.Canvas
2424
2525
main :: IO ()
2626
main = do
27-
initialize [InitEverything]
27+
initializeAll
2828
window <- createWindow "SDL2 Cairo Canvas" defaultWindow
2929
renderer <- createRenderer window (-1) defaultRenderer
3030
texture <- createCairoTexture' renderer window
@@ -52,7 +52,7 @@ module SDL.Cairo.Canvas (
5252
Color, Byte, gray, red, green, blue, rgb, (!@),
5353
stroke, fill, noStroke, noFill, strokeWeight, strokeJoin, strokeCap,
5454
-- * Coordinates
55-
Dim(..), toD, centered, corners,
55+
Dim(..), toD, Anchor(..), aligned, centered, corners,
5656
-- * Primitives
5757
background, point, line, triangle, rect, polygon, shape, ShapeMode(..),
5858
-- * Arcs and Curves
@@ -62,15 +62,18 @@ module SDL.Cairo.Canvas (
6262
-- * Images
6363
Image(imageSize), createImage, loadImagePNG, saveImagePNG, image, image', blend, grab,
6464
-- * Text
65-
Font(..), textFont, textSize, text, textC, textR,
65+
Font(..), textFont, textSize, text, text',
6666
-- * Math
6767
mapRange, radians, degrees,
6868
-- * Misc
6969
randomSeed, random, getTime, Time(..),
7070
module Graphics.Rendering.Cairo
7171
) where
7272

73+
#if __GLASGOW_HASKELL__ < 710
7374
import Control.Applicative
75+
#endif
76+
7477
import Data.Monoid
7578
import Control.Monad.State
7679
import Data.Word (Word8)
@@ -83,7 +86,6 @@ import System.Random (mkStdGen,setStdGen,randomRIO,Random)
8386

8487
import Linear.V2 (V2(..))
8588
import Linear.V4 (V4(..))
86-
import Linear.Affine (Point(..))
8789

8890
import SDL (Texture,TextureInfo(..),queryTexture)
8991
import qualified Graphics.Rendering.Cairo as C
@@ -113,12 +115,16 @@ getCanvasSize = gets csSize
113115
newtype Canvas a = Canvas { unCanvas :: StateT CanvasState IO a }
114116
deriving (Functor, Applicative, Monad, MonadIO, MonadState CanvasState)
115117

118+
-- instance Monad Canvas where
119+
-- f >>= g = undefined
120+
-- --TODO
121+
116122
-- |draw on a SDL texture using the 'Canvas' monad
117123
withCanvas :: Texture -> Canvas a -> IO a
118124
withCanvas t c = withCairoTexture' t $ \s -> do
119125
(TextureInfo _ _ w h) <- queryTexture t
120126
(ret, result) <- runStateT (unCanvas $ defaults >> c)
121-
CanvasState{ csSize = V2 (fromIntegral w) (fromIntegral h)
127+
CanvasState{ csSize = V2 (fromIntegral w) (fromIntegral h)
122128
, csSurface = s
123129
, csFG = Just $ gray 0
124130
, csBG = Just $ gray 255
@@ -181,14 +187,32 @@ strokeCap l = renderCairo $ C.setLineCap l
181187

182188
----
183189

184-
-- | position and size representation (X Y W H)
190+
-- | position (canonically, top-left corner) and size representation (X Y W H)
185191
data Dim = D Double Double Double Double deriving (Show,Eq)
186192

193+
-- | type indicating where the position coordinate is referring to
194+
data Anchor = NW | N | NE | E | SE | S | SW | W | Center | Baseline deriving (Show,Eq)
195+
187196
-- | create dimensions from position and size vector
188197
toD (V2 a b) (V2 c d) = D a b c d
189198

199+
-- | given dimensions with position coordinate not referring to the top-left corner,
200+
-- normalize to top-left corner coordinate
201+
aligned :: Anchor -> Dim -> Dim
202+
aligned NW dim = dim
203+
aligned NE (D x y w h) = D (x-w) y w h
204+
aligned SW (D x y w h) = D x (y-h) w h
205+
aligned SE (D x y w h) = D (x-w) (y-h) w h
206+
aligned Baseline dim = aligned SW dim
207+
aligned N (D x y w h) = D (x-w/2) y w h
208+
aligned W (D x y w h) = D x (y-h/2) w h
209+
aligned S (D x y w h) = D (x-w/2) (y-h) w h
210+
aligned E (D x y w h) = D (x-w) (y-h/2) w h
211+
aligned Center (D x y w h) = D (x-w/2) (y-h/2) w h
212+
190213
-- | takes dimensions with centered position, returns normalized (left corner)
191-
centered (D cx cy w h) = D (cx-w/2) (cy-h/2) w h
214+
centered = aligned Center
215+
192216
-- | takes dimensions with bottom-right corner instead of size, returns normalized (with size)
193217
corners (D xl yl xh yh) = D xl yl (xh-xl) (yh-yl)
194218

@@ -273,6 +297,7 @@ shape (ShapeRegular closed) ((V2 x y):ps) = drawShape $ do
273297
C.moveTo x y
274298
forM_ ps $ \(V2 x' y') -> C.lineTo x' y'
275299
when closed $ C.closePath
300+
shape (ShapeRegular _) _ = return ()
276301
shape ShapePoints ps = forM_ ps point
277302
shape ShapeLines (p1:p2:ps) = do
278303
line p1 p2
@@ -366,7 +391,7 @@ getTime = do
366391

367392
----
368393

369-
data Image = Image {imageSurface::C.Surface, imageSize::(V2 Int), imageFormat::Format}
394+
data Image = Image {imageSurface::C.Surface, imageSize::V2 Int, imageFormat::Format}
370395

371396
-- | create a new empty image of given size
372397
createImage :: V2 Int -> Canvas Image
@@ -399,20 +424,20 @@ image img@(Image _ (V2 w h) _) (V2 x y) =
399424

400425
-- | Render complete image inside given dimensions
401426
image' :: Image -> Dim -> Canvas ()
402-
image' img@(Image s (V2 ow oh) _) =
427+
image' img@(Image _ (V2 ow oh) _) =
403428
blend OperatorSource img (D 0 0 (fromIntegral ow) (fromIntegral oh))
404429

405430
-- | Copy given part of image to given part of screen, using given blending
406431
-- operator and resizing when necessary. Use 'OperatorSource' to copy without
407432
-- blending effects. (Processing: @copy(),blend()@)
408433
blend :: Operator -> Image -> Dim -> Dim -> Canvas ()
409-
blend op (Image s (V2 ow oh) _) sdim ddim = do
434+
blend op (Image s _ _) sdim ddim = do
410435
surf <- gets csSurface
411436
renderCairo $ copyFromToSurface op s sdim surf ddim
412437

413438
-- | get a copy of the image from current window (Processing: @get()@)
414439
grab :: Dim -> Canvas Image
415-
grab dim@(D x y w h) = do
440+
grab dim@(D _ _ w h) = do
416441
surf <- gets csSurface
417442
i@(Image s _ _) <- createImage (V2 (round w) (round h))
418443
renderCairo $ copyFromToSurface OperatorSource surf dim s (D 0 0 w h)
@@ -441,24 +466,21 @@ textSize s = gets csSurface >>= \cs -> do
441466

442467
-- | render text left-aligned (coordinate is top-left corner)
443468
text :: String -> V2 Double -> Canvas ()
444-
text str (V2 x y) = ifColor csFG $ \c -> do
445-
(C.TextExtents _ yb _ h _ _) <- C.textExtents str
446-
setColor c
469+
text str (V2 x y) = ifColor csFG $ \_ -> do
470+
(C.TextExtents _ yb _ _ _ _) <- C.textExtents str
471+
-- setColor c
447472
C.moveTo x (y-yb)
448473
C.showText str
449474

450-
-- | render text right-aligned (coordinate is top-right corner)
451-
textR :: String -> V2 Double -> Canvas ()
452-
textR str (V2 x y) = do
453-
(V2 w h) <- textSize str
454-
text str $ V2 (x-w) y
455-
456-
-- | render text centered (coordinate is central)
457-
textC :: String -> V2 Double -> Canvas ()
458-
textC str (V2 x y) = do
459-
(V2 w h) <- textSize str
460-
text str $ V2 (x-(w/2)) (y-(h/2))
461-
475+
-- | render text with specified alignment. returns x and y advancement
476+
text' :: String -> Anchor -> V2 Double -> Canvas () --(V2 Double)
477+
text' str a pos = ifColor csFG $ \_ -> do
478+
(C.TextExtents xb yb w h xa ya) <- C.textExtents str
479+
let (D x' y' _ _) = aligned a $ toD pos $ V2 w h
480+
-- setColor c
481+
C.moveTo x' y'
482+
C.showText str
483+
-- return $ V2 xa ya
462484

463485
-- helpers --
464486

@@ -480,7 +502,7 @@ ifColor cf m = get >>= \cs -> case cf cs of
480502

481503
-- |convert from 256-value RGBA to Double representation, set color
482504
setColor :: Color -> Render ()
483-
setColor c@(V4 r g b a) = C.setSourceRGBA (conv r) (conv g) (conv b) (conv a)
505+
setColor (V4 r g b a) = C.setSourceRGBA (conv r) (conv g) (conv b) (conv a)
484506
where conv = ((1.0/256)*).fromIntegral
485507

486508
-- | Add to garbage collection list
@@ -507,8 +529,6 @@ createScaledSurface s (V2 w h) = do
507529
-- | helper: returns new surface with only part of original content. does NOT cleanup!
508530
createTrimmedSurface :: C.Surface -> Dim -> Render C.Surface
509531
createTrimmedSurface s (D x y w h) = do
510-
ow <- C.imageSurfaceGetWidth s
511-
oh <- C.imageSurfaceGetHeight s
512532
s' <- liftIO $ C.createSimilarSurface s C.ContentColorAlpha (round w) (round h)
513533
C.renderWith s' $ do
514534
C.setSourceSurface s (-x) (-y)
@@ -521,7 +541,7 @@ copyFromToSurface :: Operator -> C.Surface -> Dim -> C.Surface -> Dim -> Render
521541
copyFromToSurface op src sdim@(D sx sy sw sh) dest (D x y w h) = do
522542
ow <- C.imageSurfaceGetWidth src
523543
oh <- C.imageSurfaceGetHeight src
524-
let needsTrim = sx/=0 || sy/=0 || round sw/=oh || round sh/=oh
544+
let needsTrim = sx/=0 || sy/=0 || round sw/=ow || round sh/=oh
525545
needsRescale = round sw/=round w || round sh/=round h
526546
s' <- if needsTrim then createTrimmedSurface src sdim else return src
527547
s'' <- if needsRescale then createScaledSurface s' (V2 w h) else return s'
@@ -542,4 +562,3 @@ setFont (Font face sz bold italic) = do
542562
(if italic then C.FontSlantItalic else C.FontSlantNormal)
543563
(if bold then C.FontWeightBold else C.FontWeightNormal)
544564
C.setFontSize sz
545-

Diff for: src/SDL/Cairo/Canvas/Interactive.hs

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Control.Concurrent (forkIO)
1515
import SDL
1616

1717
import SDL.Cairo (createCairoTexture')
18-
import SDL.Cairo.Canvas (Canvas, withCanvas)
18+
import SDL.Cairo.Canvas (Canvas, withCanvas, background, gray)
1919

2020
-- |for testing and debugging usage with ghci. Starts up an SDL window,
2121
-- forks a rendering loop and returns a function to draw in this window.
@@ -25,11 +25,10 @@ getInteractive = do
2525
w <- createWindow "SDL2 Cairo Canvas Interactive" defaultWindow
2626
r <- createRenderer w (-1) defaultRenderer
2727
t <- createCairoTexture' r w
28+
withCanvas t $ background $ gray 255
2829
forkIO $ forever $ do
2930
lockTexture t Nothing
3031
copy r t Nothing Nothing
3132
unlockTexture t
3233
present r
33-
return $ draw t
34-
where draw :: Texture -> Canvas () -> IO ()
35-
draw = withCanvas
34+
return $ withCanvas t

Diff for: stack.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# resolver:
1616
# name: custom-snapshot
1717
# location: "./custom-snapshot.yaml"
18-
resolver: lts-8.23
18+
resolver: lts-9.0
1919

2020
# User packages to be built.
2121
# Various formats can be used as shown in the example below.
@@ -63,4 +63,4 @@ extra-package-dbs: []
6363
# extra-lib-dirs: [/path/to/dir]
6464
#
6565
# Allow a newer minor version of GHC than the snapshot specifies
66-
# compiler-check: newer-minor
66+
# compiler-check: newer-minor

0 commit comments

Comments
 (0)