From 546da85814945ed2188e670ddf9c2dfd409d6241 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 10 Apr 2010 14:17:08 +0200 Subject: Added player cursor --- src/WiimotePlayer.hs | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) (limited to 'src/WiimotePlayer.hs') diff --git a/src/WiimotePlayer.hs b/src/WiimotePlayer.hs index 035c975..75f923b 100644 --- a/src/WiimotePlayer.hs +++ b/src/WiimotePlayer.hs @@ -12,16 +12,21 @@ import Data.Maybe import Data.Ratio ((%)) import Data.Typeable import HWiid +import Graphics.Rendering.OpenGL.GL (GLfloat, Vector3(..)) +import Graphics.Rendering.OpenGL.GL.BeginEnd (unsafeRenderPrimitive, PrimitiveMode(..)) +import Graphics.Rendering.OpenGL.GL.CoordTrans (unsafePreservingMatrix, translate) +import Graphics.Rendering.OpenGL.GL.VertexSpec + import Player import Tank -data WiimotePlayer = WiimotePlayer Wiimote +data WiimotePlayer = WiimotePlayer Wiimote (Float, Float) deriving (Typeable, Show) instance Player WiimotePlayer where - playerUpdate (WiimotePlayer wiimote) tank = do + playerUpdate (WiimotePlayer wiimote oldaim) tank = do state <- hwiidGetState wiimote messages <- hwiidGetMesg wiimote @@ -39,23 +44,34 @@ instance Player WiimotePlayer where ny = ((fromIntegral . extNunchukStickY $ ext) - 0x80)/0x80 in if (nx*nx + ny*ny) < 0.4 then (x, y) else (x+nx, y+ny) - aim = handleIR state - aangle = if isJust aim - then - let aimx = fst $ fromJust aim - aimy = snd $ fromJust aim - ax = aimx - (fromRational . toRational . tankX $ tank) - ay = aimy - (fromRational . toRational . tankY $ tank) - in if (ax /= 0 || ay /= 0) then Just $ fromRational $ round ((atan2 ay ax)*1000000*180/pi)%1000000 else Nothing - else - Nothing + iraim = handleIR state + (aimx, aimy) = if isJust iraim then fromJust iraim else oldaim + ax = aimx - (fromRational . toRational . tankX $ tank) + ay = aimy - (fromRational . toRational . tankY $ tank) + aangle = if (ax /= 0 || ay /= 0) then Just $ fromRational $ round ((atan2 ay ax)*1000000*180/pi)%1000000 else Nothing move = (mx /= 0 || my /= 0) angle = atan2 my mx moveangle = if move then Just $ fromRational $ round ((angle - (sin $ 8*x)/8)*1000000*180/pi)%1000000 else Nothing when foo $ print $ state - return (WiimotePlayer wiimote, moveangle, move, aangle, shoot) - + return (WiimotePlayer wiimote (aimx, aimy), moveangle, move, aangle, shoot) + + renderPlayer (WiimotePlayer _ (x, y)) = unsafePreservingMatrix $ do + translate $ Vector3 x y (0 :: GLfloat) + + unsafeRenderPrimitive Quads $ do + texCoord $ TexCoord2 (0 :: GLfloat) (0 :: GLfloat) + vertex $ Vertex2 (-0.2 :: GLfloat) (-0.2 :: GLfloat) + + texCoord $ TexCoord2 (0 :: GLfloat) (1 :: GLfloat) + vertex $ Vertex2 (-0.2 :: GLfloat) (0.2 :: GLfloat) + + texCoord $ TexCoord2 (1 :: GLfloat) (1 :: GLfloat) + vertex $ Vertex2 (0.2 :: GLfloat) (0.2 :: GLfloat) + + texCoord $ TexCoord2 (1 :: GLfloat) (0 :: GLfloat) + vertex $ Vertex2 (0.2 :: GLfloat) (-0.2 :: GLfloat) + irXScale :: Float irXScale = 20 @@ -110,7 +126,7 @@ newWiimotePlayer = do wiimote <- hwiidOpen bdAddrAny (hwiidFlagMesgInterface .|. hwiidFlagNonblock) when (wiimote == nullWiimote) $ fail "Wiimote error" hwiidSetReportMode wiimote (hwiidReportButtons .|. hwiidReportAcc .|. hwiidReportIR .|. hwiidReportNunchuk) - return $ WiimotePlayer wiimote + return $ WiimotePlayer wiimote (0, 0) test :: (Bits a) => a -> a -> Bool test field bits = (field .&. bits) == bits -- cgit v1.2.3