summaryrefslogtreecommitdiffstats
path: root/HTanks.hs
diff options
context:
space:
mode:
Diffstat (limited to 'HTanks.hs')
-rw-r--r--HTanks.hs39
1 files changed, 32 insertions, 7 deletions
diff --git a/HTanks.hs b/HTanks.hs
index 7f611f5..b600a3d 100644
--- a/HTanks.hs
+++ b/HTanks.hs
@@ -2,6 +2,7 @@
import Game
import Level
+import Render
import Tank
import GLDriver
@@ -10,6 +11,7 @@ import GLX
import Control.Concurrent (threadDelay)
import Control.Monad.State
import Data.Maybe
+import System.Time
main :: IO ()
@@ -18,16 +20,39 @@ main = do
let gameState = GameState {level = testLevel, tanks = [Tank 0.5 0.5 0]}
- runGame gameState $ mainLoop gl
-
- deinitGL gl
+ when (initialized gl) $ do
+ time <- getClockTime
+ runGame gameState $ mainLoop gl time
+
+ deinitGL gl
+
+minFrameTime :: Integer
+minFrameTime = 10000
-mainLoop :: Driver a => a -> Game ()
-mainLoop gl = do
+mainLoop :: Driver a => a -> ClockTime -> Game ()
+mainLoop gl time = do
run <- liftIO $ handleEvents gl
- liftIO $ threadDelay 10000
- when run $ mainLoop 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
+
+
handleEvents :: Driver a => a -> IO Bool
handleEvents gl = do
event <- nextEvent gl