root/lang/haskell/nario/Util.hs @ 20026

Revision 20026, 2.8 kB (checked in by mokehehe, 5 years ago)

マップ情報をファイルから読み込むよう変更

Line 
1module Util where
2
3import System.Time (getClockTime, diffClockTimes, noTimeDiff, tdMin, tdSec, tdPicosec)
4import Data.IORef (newIORef, readIORef, writeIORef, modifyIORef)
5import Control.Concurrent (threadDelay)
6import Data.Maybe (fromJust)
7import Multimedia.SDL
8
9import Const
10
11
12-- ユーティリティ関数
13
14-- x に d を加算した結果が x0~x1 の範囲内を超えないようにする
15-- もとから範囲外だったときはそれ以上遠ざからないように
16rangeadd x d x0 x1
17        | d > 0 && x < x1       = min (x + d) x1
18        | d < 0 && x > x0       = max (x + d) x0
19        | otherwise     = x
20
21
22-- 値を0に近づける
23friction x d
24        | x > d         = x - d
25        | x < -d        = x + d
26        | otherwise     = 0
27
28
29
30-- キーボード処理
31
32data PadBtn =
33        PadU | PadD | PadL | PadR | PadA | PadB
34        deriving (Eq, Show, Enum)
35
36data KeyState =
37        Pushed | Pushing | Released | Releasing
38        deriving (Eq, Show)
39
40isPressed Pushed  = True
41isPressed Pushing = True
42isPressed _       = False
43
44type KeyProc = PadBtn -> KeyState
45
46keyProc bef cur gk
47        | not bp && not cp = Releasing
48        | not bp && cp     = Pushed
49        | bp     && not cp = Released
50        | bp     && cp     = Pushing
51        where
52                bp = any (flip elem bef) phykeys
53                cp = any (flip elem cur) phykeys
54                phykeys = mapPhyKey gk
55
56mapPhyKey PadU = [SDLK_UP, SDLK_i]
57mapPhyKey PadD = [SDLK_DOWN, SDLK_k]
58mapPhyKey PadL = [SDLK_LEFT, SDLK_j]
59mapPhyKey PadR = [SDLK_RIGHT, SDLK_l]
60mapPhyKey PadA = [SDLK_SPACE]
61mapPhyKey PadB = [SDLK_LSHIFT, SDLK_RSHIFT]
62
63-- 時間調節
64
65elapseTime :: Integer -> IO (IO (Int,Bool))
66elapseTime fps = do
67  let frametime = picosec `div` fps
68  tm <- getClockTime
69  st <- newIORef ((0,0,noTimeDiff), (1,tm))
70  return $ do
71    ((bef,cur,fdt), (cnt,bt)) <- readIORef st
72    ct       <- getClockTime
73    let dt   = diffClockTimes ct bt
74        ndt  = diffClockTimes ct tm
75        adj  = frametime*cnt - toPsec dt
76        nc   = if cnt==fps then (1,ct) else (cnt+1,bt)
77        (nbef,ncur) = if tdSec fdt /= tdSec ndt then (cur,0) else (bef,cur)
78    if adj < 0 then do
79        writeIORef st ((nbef,ncur,ndt), nc)
80        return (bef, False)
81      else do
82        writeIORef st ((nbef,ncur+1,ndt), nc)
83        threadDelay $ fromInteger $ min 16666 $ adj `div` 1000000
84        return (bef, True)
85  where
86    toPsec dt = toInteger (tdMin dt * 60 + tdSec dt) * picosec + tdPicosec dt
87    picosec = 1000000000000
88
89
90
91-- 画像リソース
92type ImageResource = [(ImageType, Surface)]
93
94
95-- 画像リソース読み込み
96loadImageResource :: IO ImageResource
97loadImageResource = mapM load images
98        where
99                load imgtype = do
100                        sur <- loadBMP $ ("data/img/" ++) $ imageFn imgtype
101--                      colorKey <- mapRGB (surfacePixelFormat sur) $ Color r g b a
102                        let colorKey = 0xff00ff
103                        setColorKey sur [SRCCOLORKEY] colorKey
104                        return (imgtype, sur)
105                r = 255
106                g = 0
107                b = 255
108                a = 255
109
110
111getImageSurface :: ImageResource -> ImageType -> Surface
112getImageSurface imgres t = fromJust $ lookup t imgres
Note: See TracBrowser for help on using the browser.