126 lines
3.7 KiB
Haskell
126 lines
3.7 KiB
Haskell
module Main
|
|
( main
|
|
) where
|
|
import Loop
|
|
import Dodge.Data
|
|
import Dodge.StartNewGame
|
|
import Dodge.Initialisation
|
|
--import Dodge.LevelGen
|
|
import Dodge.Update
|
|
import Dodge.Event
|
|
import Dodge.Render
|
|
import Dodge.Config.KeyConfig
|
|
import Dodge.Config.Load
|
|
import Dodge.Config.Update
|
|
import Dodge.SoundLogic.LoadSound
|
|
import Picture
|
|
import Render
|
|
import Preload.Render
|
|
import Sound
|
|
import Preload
|
|
import Sound.Data
|
|
import Music
|
|
import Data.Preload
|
|
import Data.Preload.Render
|
|
|
|
import Control.Lens
|
|
import Control.Monad
|
|
import qualified Data.IntMap as IM
|
|
import Graphics.Rendering.OpenGL hiding (color, rotate, scale, translate)
|
|
import qualified SDL
|
|
import qualified SDL.Mixer as Mix
|
|
import Control.Parallel
|
|
import qualified Data.Map.Strict as M
|
|
|
|
main :: IO ()
|
|
main = do
|
|
-- load the config to get the window size and position
|
|
con <- loadDodgeConfig
|
|
let sizex = floor $ _windowX con
|
|
sizey = floor $ _windowY con
|
|
posx = _windowPosX con
|
|
posy = _windowPosY con
|
|
setupLoop
|
|
20
|
|
(sizex,sizey)
|
|
(Just (posx,posy))
|
|
theCleanup
|
|
(firstWorldLoad con)
|
|
theUpdateStep
|
|
(flip handleEvent)
|
|
|
|
theCleanup :: Universe -> IO ()
|
|
theCleanup uv = SDL.cursorVisible $= True >> cleanUpPreload (_preloadData uv)
|
|
|
|
firstWorldLoad :: Configuration -> IO Universe
|
|
firstWorldLoad theConfig = do
|
|
SDL.cursorVisible $= False
|
|
theKeyConfig <- loadKeyConfig
|
|
pdata <- doPreload >>= applyWorldConfig theConfig
|
|
-- w <- generateWorldFromSeed 0
|
|
return $ startNewGame $ Universe
|
|
{_uvWorld = initialWorld
|
|
,_config = theConfig
|
|
,_preloadData = pdata
|
|
,_menuLayers = []
|
|
,_savedWorlds = M.empty
|
|
,_keyConfig = theKeyConfig
|
|
}
|
|
|
|
theUpdateStep :: Universe -> IO Universe
|
|
theUpdateStep = doSideEffects <=< updateRenderSplit
|
|
|
|
updateRenderSplit :: Universe -> IO Universe
|
|
updateRenderSplit u = do
|
|
let preData = _preloadData u
|
|
updateUniverse u `par` void (doDrawing (_renderData preData) u)
|
|
return $ updateUniverse u
|
|
|
|
playSoundUnlessRewinding :: Universe -> IO (M.Map SoundOrigin Sound)
|
|
playSoundUnlessRewinding u
|
|
| _rewinding w = return M.empty
|
|
| otherwise = playSoundAndUpdate (_soundData $ _preloadData u) (_playingSounds w) (_toPlaySounds w)
|
|
where
|
|
w = _uvWorld u
|
|
|
|
doSideEffects :: Universe -> IO Universe
|
|
doSideEffects u = do
|
|
let w = _uvWorld u
|
|
let preData = _preloadData u
|
|
--newPlayingSounds <- playSoundAndUpdate (_soundData preData) (_playingSounds w) (_toPlaySounds w)
|
|
newPlayingSounds <- playSoundUnlessRewinding u
|
|
u' <- _sideEffects w u
|
|
endTicks <- SDL.ticks
|
|
let lastFrameTicks = _frameTimer preData
|
|
when (_debug_seconds_frame $ _config u) $ void $ renderFoldable
|
|
(_pictureShaders $ _renderData preData)
|
|
(setDepth (-1)
|
|
. translate (-0.5) (-0.8) . scale 0.0005 0.0005
|
|
. text $ "ms/frame " ++ show (endTicks - lastFrameTicks)
|
|
)
|
|
return $ u'
|
|
& preloadData . frameTimer .~ endTicks
|
|
& uvWorld . playingSounds .~ newPlayingSounds
|
|
& uvWorld . toPlaySounds .~ M.empty
|
|
& uvWorld . sideEffects .~ return
|
|
& uvWorld . rewinding .~ False -- this is probably not the best place for this
|
|
|
|
|
|
doPreload :: IO PreloadData
|
|
doPreload = do
|
|
rData <- preloadRender
|
|
lChunks <- loadSounds
|
|
lMusic <- loadMusic
|
|
Mix.playMusic Mix.Forever (lMusic IM.! 0)
|
|
return PreloadData
|
|
{ _renderData = rData
|
|
, _soundData = SoundData {_loadedChunks = lChunks}
|
|
, _musicData = MusicData {_loadedMusic = lMusic}
|
|
, _frameTimer = 0
|
|
}
|
|
|
|
--checkForGlErrors :: IO ()
|
|
--checkForGlErrors = do
|
|
-- errs <- errors
|
|
-- unless (null errs) $ putStrLn $ "GLerror during doLoop: " ++ unwords (map show errs)
|