memory gets allocated and written.

No garantuee for correctness....
This commit is contained in:
Nicole Dresselhaus 2014-04-26 16:52:32 +02:00
parent a81418bf40
commit 2e22e77d75
2 changed files with 20 additions and 20 deletions

View File

@ -200,28 +200,30 @@ parseIQM :: String -> IO IQM
parseIQM a = parseIQM a =
do do
f <- B.readFile a f <- B.readFile a
putStrLn "Before Parse:" -- Parse Headers/Offsets
putStrLn $ show f let result = parse doIQMparse f
putStrLn "Real Parse:" raw <- case result of
r <- return $ parse doIQMparse f
raw <- case r of
Done _ x -> return x Done _ x -> return x
y -> error $ show y y -> error $ show y
let ret = raw -- Fill Vertex-Arrays with data of Offsets
return ret let va = vertexArrays raw
va' <- mapM (readInVAO f) va
return $ raw {
vertexArrays = va'
}
readInVAO :: IQMVertexArray -> ByteString -> IO IQMVertexArray readInVAO :: ByteString -> IQMVertexArray -> IO IQMVertexArray
readInVAO (IQMVertexArray type' a format num offset ptr) d = readInVAO d (IQMVertexArray type' a format num offset ptr) =
do do
let let
byteLen = (fromIntegral num)*(vaSize format) byteLen = fromIntegral num * vaSize format
data' = skipDrop (fromIntegral offset) byteLen d data' = skipDrop (fromIntegral offset) byteLen d
when (not (ptr == nullPtr)) $ error $ "Error reading Vertex-Array: Double Read of " ++ show type' unless (ptr == nullPtr) $ error $ "Error reading Vertex-Array: Double Read of " ++ show type'
p <- mallocBytes byteLen p <- mallocBytes byteLen
putStrLn $ concat ["Allocating ", show byteLen, " Bytes at ", show p]
unsafeUseAsCString data' (\s -> copyBytes p s byteLen) unsafeUseAsCString data' (\s -> copyBytes p s byteLen)
p' <- unsafeCoerce p return $ IQMVertexArray type' a format num offset $ castPtr p
return (IQMVertexArray type' a format num offset p')
doIQMparse :: Parser IQM doIQMparse :: Parser IQM
doIQMparse = doIQMparse =
@ -235,8 +237,6 @@ doIQMparse =
meshes' <- readMeshes $ fromIntegral $ num_meshes h --read meshes meshes' <- readMeshes $ fromIntegral $ num_meshes h --read meshes
skipToCounter $ ofs_vertexarrays h --skip 0-n bytes to get to Vertex-Arrays skipToCounter $ ofs_vertexarrays h --skip 0-n bytes to get to Vertex-Arrays
vaf <- readVAFs $ fromIntegral $ num_vertexarrays h --read Vertex-Arrays vaf <- readVAFs $ fromIntegral $ num_vertexarrays h --read Vertex-Arrays
_ <- lift takeByteString
return IQM return IQM
{ header = h { header = h
, texts = filter (not.null) (split (unsafeCoerce '\0') text) , texts = filter (not.null) (split (unsafeCoerce '\0') text)

View File

@ -1,4 +1,4 @@
{-# LANGUAGE ExistentialQuantification, RankNTypes, CPP, BangPatterns #-} -- {-# LANGUAGE ExistentialQuantification, RankNTypes, CPP, BangPatterns #-}
-- | Word32 or Word64 - depending on implementation. Format just specifies "uint". -- | Word32 or Word64 - depending on implementation. Format just specifies "uint".
-- 4-Byte in the documentation indicates Word32 - but not specified! -- 4-Byte in the documentation indicates Word32 - but not specified!
module Importer.IQM.Types where module Importer.IQM.Types where
@ -13,7 +13,6 @@ import Graphics.Rendering.OpenGL.Raw.Types
import Prelude as P import Prelude as P
import Foreign.Storable import Foreign.Storable
import Foreign.C.Types import Foreign.C.Types
import Foreign.Marshal.Array
-- | Mesh-Indices to distinguish the meshes referenced -- | Mesh-Indices to distinguish the meshes referenced
newtype Mesh = Mesh Word32 deriving (Show, Eq) newtype Mesh = Mesh Word32 deriving (Show, Eq)
@ -148,7 +147,7 @@ vaSize IQMshort = sizeOf (undefined :: CShort)
vaSize IQMushort = sizeOf (undefined :: CUShort) vaSize IQMushort = sizeOf (undefined :: CUShort)
vaSize IQMint = sizeOf (undefined :: CInt) vaSize IQMint = sizeOf (undefined :: CInt)
vaSize IQMuint = sizeOf (undefined :: CUInt) vaSize IQMuint = sizeOf (undefined :: CUInt)
vaSize IQMhalf = sizeOf (undefined :: Word16) --TODO: Find 16-Bit-Float-Datatype vaSize IQMhalf = sizeOf (undefined :: Word16) --TODO: Find 16-Bit-Float-Datatype FIXME!
vaSize IQMfloat = sizeOf (undefined :: CFloat) vaSize IQMfloat = sizeOf (undefined :: CFloat)
vaSize IQMdouble = sizeOf (undefined :: CDouble) vaSize IQMdouble = sizeOf (undefined :: CDouble)
@ -187,10 +186,11 @@ data IQMVertexArray = IQMVertexArray
IQMData IQMData
deriving (Eq) deriving (Eq)
instance Show IQMVertexArray where instance Show IQMVertexArray where
show (IQMVertexArray t fl fo nc off _) = "IQMVertexArray (Type: " ++ show t ++ show (IQMVertexArray t fl fo nc off dat) = "IQMVertexArray (Type: " ++ show t ++
", Flags: " ++ show fl ++ ", Flags: " ++ show fl ++
", Format: " ++ show fo ++ ", Format: " ++ show fo ++
", NumComponents: " ++ show nc ++ ", NumComponents: " ++ show nc ++
", Offset: " ++ show off ++ ", Offset: " ++ show off ++
", Data at: " ++ show dat ++
")" ")"