Created
November 13, 2013 17:32
-
-
Save funrep/7453026 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Author
funrep
commented
Nov 13, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment