This repository has been archived on 2025-03-03. You can view files and clone it, but cannot push or open issues or pull requests.
htanks/HTanks.hs

69 lines
1.4 KiB
Haskell
Raw Normal View History

2010-02-22 22:25:06 +01:00
{-# LANGUAGE PatternGuards #-}
2010-02-22 16:50:42 +01:00
import Game
import Level
import Render
2010-02-22 16:50:42 +01:00
import Tank
import GLDriver
import GLX
2010-02-22 18:27:18 +01:00
import Control.Concurrent (threadDelay)
import Control.Monad.State
import Data.Maybe
import System.Time
2010-02-22 18:27:18 +01:00
2010-02-22 16:50:42 +01:00
main :: IO ()
main = do
2010-02-22 18:27:18 +01:00
gl <- initGL glxDriver
2010-02-22 16:50:42 +01:00
let gameState = GameState {level = testLevel, tanks = [Tank 0.5 0.5 0]}
when (initialized gl) $ do
time <- getClockTime
runGame gameState $ mainLoop gl time
deinitGL gl
minFrameTime :: Integer
minFrameTime = 10000
2010-02-22 16:50:42 +01:00
mainLoop :: Driver a => a -> ClockTime -> Game ()
mainLoop gl time = do
2010-02-22 18:27:18 +01:00
run <- liftIO $ handleEvents gl
render
liftIO $ swapBuffers gl
newTime <- liftIO getClockTime
let td = timeDiff newTime time
when (td < minFrameTime) $
liftIO $ threadDelay $ fromIntegral (minFrameTime - td)
newTime <- liftIO getClockTime
liftIO $ print $ timeDiff newTime time
when run $ mainLoop gl newTime
timeDiff :: ClockTime -> ClockTime -> Integer
timeDiff (TOD s1 ps1) (TOD s2 ps2) = (s1-s2)*1000000 + (ps1-ps2)`div`1000000
2010-02-22 18:27:18 +01:00
handleEvents :: Driver a => a -> IO Bool
handleEvents gl = do
event <- nextEvent gl
if (isJust event)
then
handleEvent $ fromJust event
else
return True
handleEvent :: SomeEvent -> IO Bool
2010-02-22 22:25:06 +01:00
handleEvent ev
| Just QuitEvent <- fromEvent ev = return False
| otherwise = return True