Skip to content

Instantly share code, notes, and snippets.

@funrep
Created November 13, 2013 17:32
Show Gist options
  • Save funrep/7453026 to your computer and use it in GitHub Desktop.
Save funrep/7453026 to your computer and use it in GitHub Desktop.
import Data.Bits
import Data.Word
import Data.IORef
import qualified Graphics.UI.SDL as SDL
import qualified Graphics.UI.SDL.Primitives as SDL
main = do
SDL.init [SDL.InitEverything]
SDL.setVideoMode 640 480 32 []
var <- newIORef (0, 0)
let loop = do
event <- SDL.pollEvent
case event of
(SDL.KeyDown SDL.SDLK_DOWN) -> modifyIORef var (\(x, y) -> (x, y-1))
(SDL.KeyDown SDL.SDLK_UP) -> modifyIORef var (\(x, y) -> (x, y+1))
(SDL.KeyDown SDL.SDLK_LEFT) -> modifyIORef var (\(x, y) -> (x-1, y))
(SDL.KeyDown SDL.SDLK_RIGHT -> modifyIORef var (\(x, y) -> (x+1, y))
_ -> return ()
fmap renderBox var
loop
renderBox :: (Int, Int) -> IO ()
renderBox (x, y) = do
SDL.box screen (SDL.Rect x y 100 100) $ rgbColor 255 255 255
SDL.flip screen
rgbColor :: Word8 -> Word8 -> Word8 -> SDL.Pixel
rgbColor r g b = SDL.Pixel (shiftL (fi r) 24 .|.
shiftL (fi g) 16 .|.
shiftL (fi b) 8 .|.
255)
where fi = fromIntegral
@funrep
Copy link
Author

funrep commented Nov 13, 2013

foo.hs:12:5: parse error on input `event'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment