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

Below is the code for using SDL with Haskell to draw a diagonal line. I get the CYAN line when RGB should obviously 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 
+6
haskell sdl
source share
3 answers

Perhaps a less dirty hack (although it probably depends on the platform / implementation):

 import GHC.Word import Data.Bits fi a = fromIntegral a rgbColor::Word8β†’ Word8β†’ Word8β†’ Pixel rgbColor rgb = Pixel (shiftL (fi r) 24 .|. shiftL (fi g) 16 .|. shiftL (fi b) 8 .|. (fi 255)) 
+3
source share

I am seeing the same effect on Lucid with the ATI HD2400 and the 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 agree that this is an ugly hack, but it seems that the default pixel format is not RGB (?), But using the pixel format of a surface known as RGB helps. I have no experience with SDL, so I can’t say how to use it correctly.

+2
source share

I get the same problem on my system (both for strings and other primitives), and using the Pixel constructor directly instead of mapRGB seems to give the correct colors.

For example, if I import Graphics.UI.SDL.Color as SDLC , and then let white' = SDLC.Pixel maxBound , I get a white line, as expected. If SDLC.Pixel 4278190335 (or 255 * 2^24 + 255 , a reasonable value for red), I get a red line.

This is clearly not a real solution or answer, but it may offer some starting points.

Another weird thing: if I print both your white and mine, like this:

 print =<< SDL.getRGBA white (SDL.surfaceGetPixelFormat surf0) print =<< SDL.getRGBA white' (SDL.surfaceGetPixelFormat surf0) print white print white' 

I get this:

 (255,255,255,255) (255,255,255,255) Pixel 16777215 Pixel 4294967295 

So they look the same through getRGBA , but the actual Pixel values ​​are different.

+1
source share

All Articles