HGamer3D-API 0.1.2 → 0.1.3
raw patch · 2 files changed
+249/−112 lines, 2 filesdep ~HGamer3D-Datadep ~HGamer3D-OIS-Bindingdep ~HGamer3D-Ogre-Binding
Dependency ranges changed: HGamer3D-Data, HGamer3D-OIS-Binding, HGamer3D-Ogre-Binding
Files
- HGamer3D-API.cabal +2/−2
- HGamer3D/APIs/One.hs +247/−110
HGamer3D-API.cabal view
@@ -1,5 +1,5 @@ Name: HGamer3D-API -Version: 0.1.2 +Version: 0.1.3 Synopsis: Library to enable 3D game development for Haskell - API Description: Library, to enable 3D game development for Haskell, @@ -24,7 +24,7 @@ Extra-source-files: Setup.hs Library - Build-Depends: base >= 3 && < 5, HGamer3D-Data == 0.1.0, HGamer3D-Ogre-Binding == 0.1.0, HGamer3D-OIS-Binding == 0.1.0 + Build-Depends: base >= 3 && < 5, HGamer3D-Data == 0.1.3, HGamer3D-Ogre-Binding == 0.1.3, HGamer3D-OIS-Binding == 0.1.3 Exposed-modules: HGamer3D.APIs.One Other-modules:
HGamer3D/APIs/One.hs view
@@ -43,9 +43,9 @@ -- Data types - basic systems - EngineSystem (EngineSystem, esRoot, esSceneManager, esResourceGroupManager, esTextureManager, esControllerManager), + InputSystem (InputSystem, isOisbSystem, isOisbSchema), ViewSystem (ViewSystem, vsCamera, vsRenderWindow, vsViewport), - InputSystem (InputSystem, isInputManager, isKeyboard, isMouse), + EngineSystem (EngineSystem, esRoot, esSceneManager, esResourceGroupManager, esTextureManager, esControllerManager), -- Data types - internal objects @@ -53,16 +53,16 @@ -- of the pure wrapping is used Light (Light), - Object (ManualObject, Mesh), + MeshTemplate (MeshSphere, MeshCube, MeshPlane, MeshResource, MeshManual), + Mesh (Mesh), Node (Node), Time (Time), Animation (Animation), - GraphicsObject (GraphicsObject, LightObject, CombinedObject), + GraphicsObject (MeshObject, LightObject, CombinedObject), -- Enums -------- - module HGamer3D.Bindings.OIS.EnumKeyCode, module HGamer3D.Bindings.Ogre.EnumTransformSpace, -- Functions @@ -80,9 +80,10 @@ createLight, -- Graphics Object Creation + meshTemplateFromManual, + createMesh, createLine, createCube, - createMesh, -- Graphics Object Functions @@ -101,9 +102,6 @@ setScale, combineGraphicsObjects, - -- helper function - getGraphicsObject, - -- Animation Functions getAnimation, startAnimation, @@ -114,7 +112,24 @@ getTime, -- Event Functions - isKeyDown, + Action (Action), + createTriggerAction, + bindKey, + isActionActive, + createAnalogAxisAction, + bindMouseAxis, + bindJoystickAxis, + bindMouseButton, + bindJoystickButton, + getActionAbsoluteValue, + getActionRelativeValue, + + isMouseAvailable, + isJoystickAvailable, + getAvailableAxisMouse, + getAvailableButtonsMouse, + getAvailableAxisJoystick, + getAvailableButtonsJoystick, -- run it renderLoop, @@ -139,6 +154,7 @@ import HGamer3D.Bindings.Ogre.TypeColourValue import HGamer3D.Bindings.Ogre.TypeQuaternion import HGamer3D.Bindings.Ogre.TypeVector3 +import HGamer3D.Bindings.Ogre.TypeSharedPtr import HGamer3D.Bindings.Ogre.EnumSceneType import HGamer3D.Bindings.Ogre.EnumTransformSpace @@ -160,18 +176,20 @@ import HGamer3D.Bindings.Ogre.ClassEntity import HGamer3D.Bindings.Ogre.ClassControllerManager -import HGamer3D.Bindings.OIS.Utils -import HGamer3D.Bindings.OIS.ClassInputManager -import HGamer3D.Bindings.OIS.EnumType -import HGamer3D.Bindings.OIS.EnumKeyCode -import HGamer3D.Bindings.OIS.ClassObject -import HGamer3D.Bindings.OIS.ClassKeyboard - import HGamer3D.Bindings.Ogre.ClassManualObject import HGamer3D.Bindings.Ogre.EnumOperationType +import HGamer3D.Bindings.Ogre.TypeHG3DClass +import HGamer3D.Bindings.Ogre.EnumPrefabType +import HGamer3D.Bindings.OIS.Utils +import HGamer3D.Bindings.OIS.ClassSystem +import HGamer3D.Bindings.OIS.ClassAction +import HGamer3D.Bindings.OIS.ClassActionSchema +import HGamer3D.Bindings.OIS.ClassAnalogAxisAction +import HGamer3D.Bindings.OIS.EnumActionType +import HGamer3D.Bindings.OIS.ClassDevice -import HGamer3D.Bindings.Ogre.TypeHG3DClass + import HGamer3D.Bindings.OIS.TypeHG3DClass import HGamer3D.Data.Vector3 @@ -186,9 +204,8 @@ -- |The encapsulation of the state of input devices, like keyboard, mouse, ... data InputSystem = InputSystem { - isMouse::HG3DClass, - isKeyboard::HG3DClass, - isInputManager::HG3DClass + isOisbSystem::HG3DClass, + isOisbSchema::HG3DClass } deriving (Show, Eq) -- |The encapsulation of the state of the view, camera, window and viewport @@ -208,18 +225,7 @@ } deriving (Show, Eq) --- common 3D objects, internal data objects (to get typesafety, later to be done with typed pointers) - --- | Encapsulation of a Light Object -data Light = Light HG3DClass --- | An Object is a basic graphical structure, without connection to a position -data Object = ManualObject HG3DClass -- ^The Ogre ManualObject, which is an Object, drawn by single statements - | Mesh HG3DClass -- ^A 3D Mesh - --- | A Node gives position, rotation and connection to other nodes and child parent relationships --- this is a Ogre abstration, which is used in the One API, to generate the 'GraphicsObject' below -data Node = Node HG3DClass - + -- | The time as abstracted type, to recognize it in the api data Time = Time Float @@ -227,12 +233,82 @@ -- get a better type checking in the API data Animation = Animation HG3DClass + +-- common 3D objects, internal data objects (to get typesafety, later to be done with typed pointers) + +-- | Encapsulation of a Light Object, has a name and an ogre object +data Light = Light String HG3DClass + +-- | MeshTemplate, a new template from with Meshes are created +data MeshTemplate = MeshSphere | MeshCube | MeshPlane | MeshResource String | MeshManual String SharedPtr + +-- | a displayed Mesh, in Ogre, this is an Entity +data Mesh = Mesh HG3DClass + + +-- | A Node gives position, rotation and connection to other nodes and child parent relationships +-- this is a Ogre abstration, which is used in the One API, to generate the 'GraphicsObject' below +data Node = Node HG3DClass + +-- helper not exported +getNewNode :: EngineSystem -> IO (Node) +getNewNode es = do + rootNode <- cSmGetRootSceneNode (esSceneManager es) + let vzero = Vector3 0.0 0.0 0.0 + let qident = Quaternion 1.0 0.0 0.0 0.0 + node <- cSnCreateChildSceneNode rootNode vzero qident + return (Node node) + -- | The graphics object -data GraphicsObject = GraphicsObject Object Node | LightObject Light | CombinedObject Node +data GraphicsObject = MeshObject Mesh Node | LightObject Light | CombinedObject Node --- +getNodeFromGo :: GraphicsObject -> HG3DClass +getNodeFromGo (MeshObject _ (Node node)) = node +getNodeFromGo (CombinedObject (Node node)) = node +meshTemplateFromManual :: EngineSystem -> String -> HG3DClass -> IO (MeshTemplate) +meshTemplateFromManual es name object = do + mesh <- cMnoConvertToMesh object name "General" + let mt = MeshManual name mesh + return (mt) +goFromMesh :: EngineSystem -> HG3DClass -> IO (GraphicsObject) +goFromMesh es entity = do + (Node node) <- getNewNode es + cSnAttachObject node entity + return (MeshObject (Mesh entity) (Node node)) + + +-- | Create one mesh, which can be placed into a scene +createMesh :: EngineSystem -> MeshTemplate -> IO (GraphicsObject) + +createMesh es MeshSphere = do + entity <- cSmCreateEntity4 (esSceneManager es) PtSphere + go <- goFromMesh es entity + return (go) + +createMesh es MeshCube = do + entity <- cSmCreateEntity4 (esSceneManager es) PtCube + go <- goFromMesh es entity + return (go) + +createMesh es MeshPlane = do + entity <- cSmCreateEntity4 (esSceneManager es) PtPlane + go <- goFromMesh es entity + return (go) + +createMesh es (MeshResource name) = do + entity <- cSmCreateEntity2 (esSceneManager es) name + go <- goFromMesh es entity + return (go) + +createMesh es (MeshManual name mesh) = do + entity <- cSmCreateEntity2 (esSceneManager es) name + go <- goFromMesh es entity + return (go) + + + -- basic initialize function -- | With this function, the graphics system is being initialized. The return @@ -248,8 +324,11 @@ fUAddResourceLocations "resources.cfg" renderWindow <-cRInitialise root True windowName "" - (inputManager, keyboard, mouse) <- fUInitializeOis renderWindow + fUInitializeOis renderWindow + oisbSystem <- cSyGetSingletonPtr + oisbSchema <- cSyGetDefaultActionSchemaAutoCreate oisbSystem + sceneManager <- cRCreateSceneManager root sceneManagerType "SceneManager" camera <- cSmCreateCamera sceneManager "SimpleCamera" @@ -277,7 +356,7 @@ return ( EngineSystem root sceneManager rgm tm cm, ViewSystem camera renderWindow viewport, - InputSystem mouse keyboard inputManager + InputSystem oisbSystem oisbSchema ) @@ -311,38 +390,10 @@ cSmSetAmbientLight (esSceneManager es) colour light <- cSmCreateLight (esSceneManager es) lightName cLSetPosition light x y z - return (LightObject (Light light)) + return (LightObject (Light lightName light)) --- helper functions, not exported -getNodeFromGraphicsObject :: GraphicsObject -> HG3DClass -getNodeFromGraphicsObject (GraphicsObject _ (Node node)) = node -getNodeFromGraphicsObject (CombinedObject (Node node)) = node - -getObjectFromGraphicsObject :: GraphicsObject -> HG3DClass -getObjectFromGraphicsObject (GraphicsObject (ManualObject object) (Node node)) = object -getObjectFromGraphicsObject (GraphicsObject (Mesh mesh) (Node node)) = mesh - -getNewNode :: EngineSystem -> IO (HG3DClass) -getNewNode es = do - rootNode <- cSmGetRootSceneNode (esSceneManager es) - let vzero = Vector3 0.0 0.0 0.0 - let qident = Quaternion 1.0 0.0 0.0 0.0 - node <- cSnCreateChildSceneNode rootNode vzero qident - return (node) - -getGraphicsObject :: EngineSystem -> Object -> IO (GraphicsObject) -getGraphicsObject es (ManualObject object) = do - node <- getNewNode es - cSnAttachObject node object - return (GraphicsObject (ManualObject object) (Node node) ) -getGraphicsObject es (Mesh mesh) = do - node <- getNewNode es - cSnAttachObject node mesh - return (GraphicsObject (Mesh mesh) (Node node) ) - - -- use "BaseWhiteNoLighting" Material -- |Creates a line by using the ManualObject functionality of Ogre @@ -352,7 +403,7 @@ -> ColourValue -- ^The color of the line -> Vector3 -- ^The start point of the line -> Vector3 -- ^The end point of the line - -> IO (GraphicsObject) -- ^Return value is a graphics object + -> IO (MeshTemplate) -- ^Return value is a graphics object createLine es lineName materialName colour vStart vEnd = do mo <- cSmCreateManualObject (esSceneManager es) lineName cMnoBegin mo materialName OtLineList "General" @@ -362,7 +413,7 @@ cMnoColour mo colour cMnoEnd mo - go <- getGraphicsObject es (ManualObject mo) + go <- meshTemplateFromManual es lineName mo return (go) -- |Creates a cube by using the ManualObject functionality of Ogre @@ -370,10 +421,8 @@ -> String -- ^The name of the cube object -> String -- ^The material used for the cube -> ColourValue -- ^The color of the line - -> Vector3 -- ^The initial position, where the cube is created - -- it is measured to the center of the cube - -> IO (GraphicsObject) -- ^Return value is a graphics object -createCube es cubeName materialName colour pos = do + -> IO (MeshTemplate) -- ^Return value is a graphics object +createCube es cubeName materialName colour = do mo <- cSmCreateManualObject (esSceneManager es) cubeName -- set Dynamic to false cMnoSetDynamic mo False @@ -420,22 +469,10 @@ cMnoEnd mo - go <- getGraphicsObject es (ManualObject mo) - setPosition go pos + go <- meshTemplateFromManual es cubeName mo return (go) --- |This function creates a graphics object, by loading a mesh -createMesh :: EngineSystem -- ^The engine system - -> String -- ^The name of the created mesh object - -> String -- ^The filename of the mesh, from the resources - -> IO (GraphicsObject) -- ^Return value is a graphics object -createMesh es meshName meshFile = do - entity <- cSmCreateEntity (esSceneManager es) meshName meshFile "General" - go <- getGraphicsObject es (Mesh entity) - return (go) - - -- |This function groups objects into a new object -- it is not perfoming any geometric operations, it just groups the -- input objects. Can be used, to move and rotate a group. @@ -444,9 +481,9 @@ -> IO (GraphicsObject) -- ^The return value is a new GraphicsObject combineGraphicsObjects es listObjects = do - node <- getNewNode es + (Node node) <- getNewNode es sequence_ (map ( \object -> do - let objectnode = getNodeFromGraphicsObject object + let objectnode = getNodeFromGo object parent <- cNGetParent objectnode -- currently no check here, if parent exists! -- no parent is only valid for root node and this is not accessible to the @@ -468,79 +505,79 @@ -- |Scales a GraphicsObject by multiplying a scale to the existing one scale :: GraphicsObject -> Vector3 -> IO () scale go sc = do - cNScale (getNodeFromGraphicsObject go) sc + cNScale (getNodeFromGo go) sc return () -- | Translate a GraphicsObject from its current position translate :: GraphicsObject -> Vector3 -> EnumTransformSpace -> IO () translate go v ets = do - cNTranslate (getNodeFromGraphicsObject go) v ets + cNTranslate (getNodeFromGo go) v ets return () -- roll roll :: GraphicsObject -> Radian -> EnumTransformSpace -> IO () roll go r ets = do - cNRoll (getNodeFromGraphicsObject go) r ets + cNRoll (getNodeFromGo go) r ets return () -- pitch pitch :: GraphicsObject -> Radian -> EnumTransformSpace -> IO () pitch go r ets = do - cNPitch (getNodeFromGraphicsObject go) r ets + cNPitch (getNodeFromGo go) r ets return () -- yaw yaw :: GraphicsObject -> Radian -> EnumTransformSpace -> IO () yaw go r ets = do - cNYaw (getNodeFromGraphicsObject go) r ets + cNYaw (getNodeFromGo go) r ets return () -- rotate rotate :: GraphicsObject -> Vector3 -> Radian -> EnumTransformSpace -> IO () rotate go v r ets = do - cNRotate (getNodeFromGraphicsObject go) v r ets + cNRotate (getNodeFromGo go) v r ets return () getPosition :: GraphicsObject -> IO (Vector3) getPosition go = do - pos <- cNGetPosition (getNodeFromGraphicsObject go) + pos <- cNGetPosition (getNodeFromGo go) return (pos) setPosition :: GraphicsObject -> Vector3 -> IO () setPosition go pos = do - cNSetPosition (getNodeFromGraphicsObject go) pos + cNSetPosition (getNodeFromGo go) pos return () setLightPos :: Light -> Float -> Float -> Float -> IO () -setLightPos (Light light) x y z = do +setLightPos (Light name light) x y z = do cLSetPosition light x y z getOrientation :: GraphicsObject -> IO (Quaternion) getOrientation go = do - quat <- cNGetOrientation (getNodeFromGraphicsObject go) + quat <- cNGetOrientation (getNodeFromGo go) return (quat) setOrientation :: GraphicsObject -> Quaternion -> IO () setOrientation go quat = do - cNSetOrientation (getNodeFromGraphicsObject go) quat + cNSetOrientation (getNodeFromGo go) quat return () getScale :: GraphicsObject -> IO (Vector3) getScale go = do - scale <- cNGetScale (getNodeFromGraphicsObject go) + scale <- cNGetScale (getNodeFromGo go) return (scale) setScale :: GraphicsObject -> Vector3 -> IO () setScale go scale = do - cNSetScale (getNodeFromGraphicsObject go) scale + cNSetScale (getNodeFromGo go) scale return () -- Animation Functions getAnimation :: GraphicsObject -> String -> IO (Animation) -getAnimation (GraphicsObject (Mesh mesh) _ ) animName = do +getAnimation (MeshObject (Mesh mesh) _ ) animName = do anim <- cEGetAnimationState mesh animName return (Animation anim) @@ -565,26 +602,126 @@ return (Time time) --- event functions +-- event functions, OIS stuff -isKeyDown :: InputSystem -> EnumKeyCode -> IO (Bool) -isKeyDown is keycode = do - isdown <- cOISKIsKeyDown (isKeyboard is) keycode - return (isdown) +data Action = Action HG3DClass +runIsFrame :: InputSystem -> Float -> IO () +runIsFrame is delta = do + cSyProcess (isOisbSystem is) delta + return () +createTriggerAction :: InputSystem -> String -> IO (Action) +createTriggerAction is actionName = do + let schema = isOisbSchema is + action <- cAsCreateTriggerAction schema actionName + return (Action action) + +createAnalogAxisAction :: InputSystem -> Float -> Float -> String -> IO (Action) +createAnalogAxisAction is minval maxval actionName = do + let schema = isOisbSchema is + action <- cAsCreateAnalogAxisAction schema actionName + cAaaSetMinimumValue action minval + cAaaSetMaximumValue action maxval + return (Action action) + +bindKey :: Action -> String -> IO () +bindKey (Action action) keyChar = do + cABind4 action ("Keyboard/" ++ keyChar) + return () +isMouseAvailable :: InputSystem -> IO (Bool) +isMouseAvailable is = do + avail <- cSyHasDevice (isOisbSystem is) "Mouse" + return (avail) + +bindMouseAxis :: Action -> String -> IO () +bindMouseAxis (Action action) axisName = do + cABind4 action ("Mouse/" ++ axisName) + return () + +bindMouseButton :: Action -> String -> IO () +bindMouseButton (Action action) buttonName = do + cABind4 action ("Mouse/" ++ buttonName) + return () + +isJoystickAvailable :: InputSystem -> String -> IO (Bool) +isJoystickAvailable is name = do + avail <- cSyHasDevice (isOisbSystem is) name + return (avail) + +bindJoystickAxis :: Action -> String -> String -> IO () +bindJoystickAxis (Action action) joystickName axisName = do + cABind4 action (joystickName ++ "/" ++ axisName) + return () + +bindJoystickButton :: Action -> String -> String -> IO () +bindJoystickButton (Action action) joystickName buttonName = do + cABind4 action (joystickName ++ "/" ++ buttonName) + return () + + +getAvailableAxisMouse :: InputSystem -> IO [String] +getAvailableAxisMouse is = do + dev <- cSyGetDevice (isOisbSystem is) "Mouse" + let axes = map (\x -> x ++ " Axis") ["X", "Y", "Z"] + bools <- mapM (cDHasState dev) axes + let avail = map fst $ filter snd (zip axes bools) + return (avail) + +getAvailableButtonsMouse :: InputSystem -> IO [String] +getAvailableButtonsMouse is = do + dev <- cSyGetDevice (isOisbSystem is) "Mouse" + let buttons = (map (\bt -> "Button " ++ (show bt)) [0..50]) ++ (map (\bt -> bt ++ " Button") ["Left", "Middle", "Right"]) + bools <- mapM (cDHasState dev) buttons + let avail = map fst $ filter snd (zip buttons bools) + return (avail) + +getAvailableAxisJoystick :: InputSystem -> String -> IO [String] +getAvailableAxisJoystick is name = do + dev <- cSyGetDevice (isOisbSystem is) name + let axes = (map (\x -> "Axis " ++ (show x)) [0..50]) ++ (map (\x -> "Slider " ++ (show x)) [0..50]) + bools <- mapM (cDHasState dev) axes + let avail = map fst $ filter snd (zip axes bools) + return (avail) + +getAvailableButtonsJoystick :: InputSystem -> String -> IO [String] +getAvailableButtonsJoystick is name = do + dev <- cSyGetDevice (isOisbSystem is) name + let buttons = (map (\bt -> "Button " ++ (show bt)) [0..50]) + bools <- mapM (cDHasState dev) buttons + let avail = map fst $ filter snd (zip buttons bools) + return (avail) + + +isActionActive :: Action -> IO (Bool) +isActionActive (Action action) = do + flag <- cAIsActive action + return (flag) + +getActionAbsoluteValue :: Action -> IO (Float) +getActionAbsoluteValue (Action action) = do + val <- cAaaGetAbsoluteValue action + return (val) + +getActionRelativeValue :: Action -> IO (Float) +getActionRelativeValue (Action action) = do + val <- cAaaGetRelativeValue action + return (val) + -- renderLoop function -renderLoop :: EngineSystem -> ViewSystem -> InputSystem -> (EngineSystem -> ViewSystem -> InputSystem -> IO ()) -> IO () -renderLoop es vs is stepFunc = do +renderLoop :: EngineSystem -> ViewSystem -> InputSystem -> Time -> (EngineSystem -> ViewSystem -> InputSystem -> IO ()) -> IO () +renderLoop es vs is (Time lastTime) stepFunc = do + (Time time) <- getTime es + let delta = time - lastTime + runIsFrame is delta fUMessagePump closed <- cRwIsClosed (vsRenderWindow vs) if (closed) then return () else do cRRenderOneFrame (esRoot es) - cOISOCapture (isKeyboard is) stepFunc es vs is - renderLoop es vs is stepFunc + renderLoop es vs is (Time time) stepFunc