some more optimization

This commit is contained in:
nek0 2018-10-26 02:03:44 +02:00
parent 56425c2396
commit 3f9841522c

View file

@ -18,7 +18,7 @@ import System.IO
import System.Console.ANSI as CA
import System.Console.Terminal.Size as TS
import Debug.Trace as T
-- import Debug.Trace as T
import qualified Map
import Types
@ -43,24 +43,27 @@ main = do
hSetBuffering stdin NoBuffering
hideCursor
state <- newMVar (State (V2 10 3) 0 False "")
c <- newMVar ' '
putStrLn "starting loop, press button"
c <- newMVar Nothing
whileM_ (not <$> stop <$> readMVar state) $ do
CA.clearScreen
takeMVar c
char <- getChar
char <- getKey
putMVar c char
modifyMVar_ state (\s -> if (not (stop s))
then
withMVar c (\char -> handleInput (Just char) s)
withMVar c (\char -> handleInput char s)
else
return s)
outputs <- withMVar state draw
withMVar state (\s -> when (not (stop s)) $ do
let output = L.intercalate "\n" outputs
let output = L.intercalate "\n"
( outputs
++ [show char]
++ [show (pos s) ++ " " ++ show (rot s)]
)
deepseq output (putStr output)
putStrLn ("\n" ++ [char] ++ "\n" ++ show (pos s) ++ " " ++ show (rot s)))
threadDelay msdelay
)
-- threadDelay msdelay
showCursor
handleInput :: Maybe Char -> State -> IO State
@ -86,7 +89,7 @@ handleInput (Just char) state = do
draw :: State -> IO [String]
draw state@(State pos@(V2 x y) rot _ _) = do
(Just (Window h w)) <- TS.size
let dims@(dw, dh) = (w, h) -- if w >= h then (w, w `div` 2) else (w, h)
let dims@(dw, dh) = (min 160 w, min 60 h) -- if w >= h then (w, w `div` 2) else (w, h)
output = map
(\mh -> map (\mw -> drawPixel dims (mw, mh)) [1 .. fromIntegral dw])
[1 .. fromIntegral dh - 2]
@ -96,7 +99,7 @@ draw state@(State pos@(V2 x y) rot _ _) = do
let rayLength = castRay (fromIntegral w) (fromIntegral h) ddw ddh
in getPixel (fromMaybe 11 rayLength)
castRay w h ddw ddh =
let iter = [0, 0.02 .. 10]
let iter = [0, 0.05 .. 10]
collPos a = (pos + (V2 0 (a * cos (- fov / 2 + ddh * fov / h))
`rotVec` (rot - fov / 2 + ddw * fov / w)))
in foldl (\acc a ->
@ -126,3 +129,29 @@ rotVec (V2 x y) rad = V2 nx ny
where
nx = x * cos rad + y * sin rad
ny = x * sin rad - y * cos rad
getKey :: IO (Maybe Char)
getKey = do
charMVar <- newEmptyMVar
tid <- forkIO (forkGetChar charMVar)
threadDelay (msdelay `div` 5)
empty <- isEmptyMVar charMVar
if (not empty)
then do
killThread tid
Just <$> takeMVar charMVar
else do
killThread tid
return Nothing
where
forkGetChar mvar = do
hSetEcho stdin False
cs <- (: []) <$> hGetChar stdin
-- cs <- whileM
-- (not <$> hIsEOF stdin)
-- (hGetChar stdin)
hSetEcho stdin True
-- putStrLn cs
when (not (null cs)) $ do
_ <- putMVar mvar (head cs)
return ()