Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this Haskell SDL line cyan when it should be white?

Tags:

haskell

sdl

Below is some code to use SDL with Haskell to draw a diagonal line. I get a CYAN line when the RGB clearly should be white. This is on Ubuntu. Am I doing something wrong?

import qualified Graphics.UI.SDL as SDL
import qualified Graphics.UI.SDL.Primitives as SDLP

main = do
    SDL.init [SDL.InitEverything]
    SDL.setVideoMode 640 480 32 []
    SDL.setCaption "My Window" "My Test"
    surf0 <- SDL.getVideoSurface
    white <- SDL.mapRGB (SDL.surfaceGetPixelFormat surf0) 255 255 255
    SDLP.line surf0 0 0 640 480 white 
    SDL.flip surf0
    eventLoop
    SDL.quit
    print "done"
    where
    eventLoop = SDL.waitEventBlocking >>= checkEvent
    checkEvent (SDL.KeyUp _) = return ()
    checkEvent _ = eventLoop
like image 795
qrest Avatar asked Jul 20 '10 03:07

qrest


2 Answers

Maybe a less dirty hack (though probably platform/implementation dependant) :

import GHC.Word
import Data.Bits

fi a = fromIntegral a

rgbColor::Word8→ Word8→ Word8→ Pixel
rgbColor r g b = Pixel (shiftL (fi r) 24 .|. shiftL (fi g) 16 .|. shiftL (fi b) 8 .|. (fi 255))
like image 179
djfm Avatar answered Oct 15 '22 12:10

djfm


I observe the same effect on Lucid with ATI HD2400 and radeon driver (if that matters). But there is a workaround.

This example draws a white line:

import qualified Graphics.UI.SDL as SDL
import qualified Graphics.UI.SDL.Primitives as SDLP

import Control.Applicative ((<$>))

main = do
    SDL.init [SDL.InitEverything]
    sbase <- SDL.setVideoMode 640 480 24 []  -- draw here
    -- an ugly hack to get pixel format from an RGB surface:
    rgbPF <- SDL.surfaceGetPixelFormat <$> SDL.createRGBSurfaceEndian [] 1 1 24
    white <- SDL.mapRGB rgbPF (-1) (-1) (-1)
    SDLP.line sbase 0 0 (640-1) (480-1) white
    SDL.flip sbase
    eventLoop
    SDL.quit
  where
    eventLoop = SDL.waitEventBlocking >>= checkEvent
    checkEvent (SDL.KeyDown _) = return ()
    checkEvent _ = eventLoop

I accept that this is an ugly hack, but it seems that default surface' pixel format is not RGB (?), and using a pixel format of a surface known to be RGB helps. I don't have experience with SDL, so I cannot tell what the right way to use it is.

like image 25
sastanin Avatar answered Oct 15 '22 12:10

sastanin