Haskell linux pixel drawing library

I want to draw individual pixels on the screen in a window or something to display in real time in haskell.

I'm just starting with haskell (but not with functional programming, not with graphics), so I'm trying to create some rudimentary graphical objects with it.

I tried using SDL, but the following code gives me a blank screen:

import Prelude
import Graphics.UI.SDL as SDL

createColor screen r g b = SDL.mapRGB (SDL.surfaceGetPixelFormat screen) r g b

drawGrad screen = SDL.setColors screen [SDL.Color x y 255 | x <- [0..255], y <- [0..255]] 800

main = do
  SDL.init [InitEverything]
  setVideoMode 256 256 32 []
  screen <- getVideoSurface
  drawGrad screen
  SDL.flip screen
  quitHandler

quitHandler :: IO ()
quitHandler = do
  e <- waitEvent
  case e of
    Quit -> return ()
    otherwise -> quitHandler
+3
source share
2 answers

It seems to me that you are not really drawing anything. Are you sure setColors does what you think it does? If I need memory, to adjust the color palette for an 8-bit surface, then you explicitly set the 32-bit video mode.

, , , , , : 8- 8- 256 , ; , RGB-, 8 (32- 8- -, 8 padding , ).

, , , :

import Ix

-- Given coordinates, create a 1x1 rectangle containing that pixel
getPix x y = SDL.Rect (fromIntegral x) (fromIntegral y) 1 1

-- Draw a pixel on a surface with color determined by position
drawPixel surf (x, y) = do
    let pixRect = Just (getPix x y)
    pixColor <- createColor surf x y 255
    SDL.fillRect surf pixRect pixColor

-- Apply drawPixel to each coordinate on the screen
drawGrad screen = mapM_ (drawPixel screen) $ range ((0,0),(255,255))

: , Win32. Haskell, . . Happy Fun IO Monad.

, , SDL, Haskell. Graphics.UI.SDL , , API C Haskell. SDL (, PyGame), SDL C .

+2

, , , Haskell SDL, , , , LazyFoo SDL 31

+1

Source: https://habr.com/ru/post/1724893/


All Articles