-- This source file is part of HGamer3D -- (A project to enable 3D game development in Haskell) -- For the latest info, see http://www.althainz.de/HGamer3D.html -- -- (c) 2011 Peter Althainz -- -- Licensed under the Apache License, Version 2.0 (the "License"); -- you may not use this file except in compliance with the License. -- You may obtain a copy of the License at -- -- http://www.apache.org/licenses/LICENSE-2.0 -- -- Unless required by applicable law or agreed to in writing, software -- distributed under the License is distributed on an "AS IS" BASIS, -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -- See the License for the specific language governing permissions and -- limitations under the License. -- Engine3D.hs -- -- Basic functionality of HGamer3D, Base API module HGamer3D.APIs.Base.Common ( MHGamer3D, Position3D (..), Scale3D (..), translate3D, Direction3D (..), Orientation3D (..), CommonSystem (..), EngineSystem (..), getUniqueName, initCommon, TimeMS (..), getTimeMS, runMHGamer3D ) where import HGamer3D.Data.Colour import HGamer3D.Data.Vector import HGamer3D.Data.Angle import HGamer3D.Data.HG3DClass import qualified Data.Text as T import System.Directory import Control.Monad import Control.Monad.Trans import Control.Monad.Reader import Control.Monad.State import Control.Concurrent import System.Win32.Time -- identification dll -- hg3ddllname :: String hg3ddllname = "HGamer3D-Version-0.1.7-DontDelete.txt" data CommonSystem = CommonSystem { csHG3DPath::String } data EngineSystem = EngineSystem { esRoot::HG3DClass, esSceneManager::HG3DClass, esResourceGroupManager::HG3DClass, esTextureManager::HG3DClass, esControllerManager::HG3DClass, esLogManager::HG3DClass, esCamera::HG3DClass, esRenderWindow::HG3DClass, esViewport::HG3DClass } type MHGamer3D a = (ReaderT (CommonSystem, EngineSystem)) (StateT [Integer] IO) a class Position3D t where position3D :: t -> MHGamer3D Vec3 positionTo3D :: t -> Vec3 -> MHGamer3D () translate3D :: Position3D t => t -> Vec3 -> MHGamer3D () translate3D t v = do p <- position3D t positionTo3D t ( v &+ p ) return () class Scale3D t where scale3D :: t -> MHGamer3D Vec3 scaleTo3D :: t -> Vec3 -> MHGamer3D () class Direction3D t where direction3D :: t -> MHGamer3D Vec3 directionTo3D :: t -> Vec3 -> MHGamer3D () class Orientation3D t where orientation3D :: t -> MHGamer3D UnitQuaternion orientationTo3D :: t -> UnitQuaternion -> MHGamer3D () -- unique identifiers, by appending a unique integer to a prefix -- getUniqueName :: String -> StateT [Integer] IO String getUniqueName prefix = do (x:xs) <- get put xs return (prefix ++ (show x)) -- function, which finds installation path of HG3D -- getHG3DPath :: IO String getHG3DPath = do env <- findExecutable hg3ddllname let path = case env of Just path -> fst (T.breakOn (T.pack $ "\\bin\\" ++ hg3ddllname) (T.pack path)) Nothing -> T.pack "" return (T.unpack path) initCommon :: IO CommonSystem initCommon = do csHG3DPath <- getHG3DPath return (CommonSystem csHG3DPath) -- Milliseconds Timescale data TimeMS = TimeMS Int -- time in milliseconds instance Show TimeMS where show (TimeMS s) = (show s) ++ " Milliseconds" getTimeMS :: MHGamer3D (TimeMS) getTimeMS = do fr <- liftIO $ queryPerformanceFrequency wt <- liftIO $ queryPerformanceCounter return (TimeMS $ fromIntegral (wt * 1000 `div` fr)) runMHGamer3D :: (CommonSystem, EngineSystem) -> [Integer] -> MHGamer3D a -> IO (a, [Integer]) runMHGamer3D (cs, es) s action = do (actionResult, newState) <- runStateT (runReaderT action (cs, es) ) s return (actionResult, newState)