Skip to content

Commit 4da5ade

Browse files
committed
fix(#61): model animations marshalling issue
1 parent a6629dc commit 4da5ade

4 files changed

Lines changed: 41 additions & 60 deletions

File tree

h-raylib.cabal

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,15 @@ library
249249
if !flag(disable-lens)
250250
other-modules:
251251
Raylib.Util.Lenses.TH
252-
252+
253253
build-depends:
254-
, base >=4.0 && <4.22
254+
, base >=4.0 && <4.23
255255
, bytestring >=0.11.0 && <0.13
256256
, containers >=0.6.0 && <0.9
257257
, template-haskell >=2.16.0.0 && <2.24
258258
, text >=2.0 && <2.2
259-
260-
259+
260+
261261
if !flag(disable-lens)
262262
build-depends:
263263
, linear >=1.22 && <1.24
@@ -316,7 +316,7 @@ library
316316
Xext
317317

318318
cc-options:
319-
-Wno-implicit-function-declaration -Wno-unused-result -D_GLFW_X11
319+
-Wno-implicit-function-declaration -Wno-unused-result -Wno-discarded-qualifiers -D_GLFW_X11
320320

321321
elif (flag(platform-mac) || (flag(detect-platform) && os(osx)))
322322
frameworks: OpenGL Cocoa IOKit CoreVideo CoreAudio CoreFoundation
@@ -378,6 +378,6 @@ library
378378

379379
if flag(ghci)
380380
cpp-options: -DGHCI
381-
381+
382382
if flag(disable-lens)
383383
cpp-options: -DDISABLE_LENS

lib/rl_bindings.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,20 +2382,8 @@ TraceLogCallback_ customCallback;
23822382

23832383
void CustomCallback(int logLevel, const char *text, va_list args)
23842384
{
2385-
va_list args_copy;
2386-
va_copy(args_copy, args);
2387-
2388-
int len = vsnprintf(NULL, 0, text, args_copy);
2389-
va_end(args_copy);
2390-
2391-
if (len < 0) return;
2392-
2393-
char *formatted = malloc(len + 1);
2394-
2395-
vsnprintf(formatted, len + 1, text, args);
2385+
char *formatted = TextFormat(text, args);
23962386
customCallback(logLevel, formatted);
2397-
2398-
free(formatted);
23992387
}
24002388

24012389
RLBIND void SetTraceLogCallback_(TraceLogCallback_ a)

src/Raylib/Internal.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ module Raylib.Internal
6262

6363
-- * Miscellaneous
6464
c'rlGetShaderIdDefault,
65+
c'rlGetShaderLocsDefault,
6566
getPixelDataSize,
6667
)
6768
where
@@ -151,6 +152,7 @@ defaultWindowResources = do
151152

152153
$( genNative
153154
[ ("c'rlGetShaderIdDefault", "rlGetShaderIdDefault_", "rlgl_bindings.h", [t|IO CUInt|]),
155+
("c'rlGetShaderLocsDefault", "rlGetShaderLocsDefault_", "rlgl_bindings.h", [t|IO (Ptr CInt)|]),
154156
("c'rlUnloadShaderProgram", "rlUnloadShaderProgram_", "rlgl_bindings.h", [t|CUInt -> IO ()|]),
155157
("c'rlUnloadTexture", "rlUnloadTexture_", "rlgl_bindings.h", [t|CUInt -> IO ()|]),
156158
("c'rlUnloadFramebuffer", "rlUnloadFramebuffer_", "rlgl_bindings.h", [t|CUInt -> IO ()|]),

src/Raylib/Types/Core/Models.hs

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ import Foreign
9494
mallocForeignPtr,
9595
mallocForeignPtrArray,
9696
newArray,
97-
newForeignPtr,
9897
peekArray,
9998
plusPtr,
10099
pokeArray,
@@ -112,8 +111,8 @@ import Foreign.C
112111
castCharToCChar,
113112
peekCString,
114113
)
115-
import Raylib.Internal (Closeable (addToWindowResources, close), addShaderId, addTextureId, addVaoId, addVboIds, c'rlGetShaderIdDefault, c'rlUnloadShaderProgram, c'rlUnloadTexture, c'rlUnloadVertexArray, c'rlUnloadVertexBuffer)
116-
import Raylib.Internal.Foreign (Freeable (rlFreeDependents), c'free, freeMaybePtr, newMaybeArray, p'free, peekMaybeArray, peekStaticArray, pokeStaticArray, rightPad, rlFree, rlFreeMaybeArray)
114+
import Raylib.Internal (Closeable (addToWindowResources, close), addShaderId, addTextureId, addVaoId, addVboIds, c'rlGetShaderIdDefault, c'rlGetShaderLocsDefault, c'rlUnloadShaderProgram, c'rlUnloadTexture, c'rlUnloadVertexArray, c'rlUnloadVertexBuffer)
115+
import Raylib.Internal.Foreign (Freeable (rlFreeDependents), c'free, freeMaybePtr, newMaybeArray, peekMaybeArray, peekStaticArray, pokeStaticArray, rightPad, rlFree, rlFreeMaybeArray)
117116
import Raylib.Types.Core (Color, Matrix, Quaternion, Vector2, Vector3, Vector4, pattern Vector2, pattern Vector3, pattern Vector4)
118117
import Raylib.Types.Core.Textures (Texture (texture'id))
119118

@@ -133,7 +132,7 @@ data MaterialMapIndex
133132
| MaterialMapIrradiance
134133
| MaterialMapPrefilter
135134
| MaterialMapBrdf
136-
deriving (Eq, Show, Enum)
135+
deriving (Eq, Show, Read, Enum)
137136

138137
data DefaultShaderAttributeLocation
139138
= DefaultShaderAttribLocationPosition
@@ -145,7 +144,7 @@ data DefaultShaderAttributeLocation
145144
| DefaultShaderAttribLocationIndices
146145
| DefaultShaderAttribLocationBoneIds
147146
| DefaultShaderAttribLocationBoneWeights
148-
deriving (Eq, Show, Enum)
147+
deriving (Eq, Show, Read, Enum)
149148

150149
data ShaderLocationIndex
151150
= ShaderLocVertexPosition
@@ -177,7 +176,7 @@ data ShaderLocationIndex
177176
| ShaderLocVertexBoneIds
178177
| ShaderLocVertexBoneWeights
179178
| ShaderLocBoneMatrices
180-
deriving (Eq, Show, Enum)
179+
deriving (Eq, Show, Read, Enum)
181180

182181
data ShaderUniformDataType
183182
= ShaderUniformFloatType
@@ -189,7 +188,7 @@ data ShaderUniformDataType
189188
| ShaderUniformIVec3Type
190189
| ShaderUniformIVec4Type
191190
| ShaderUniformSampler2DType
192-
deriving (Eq, Show, Enum)
191+
deriving (Eq, Show, Read, Enum)
193192

194193
data ShaderUniformData
195194
= ShaderUniformFloat Float
@@ -201,7 +200,7 @@ data ShaderUniformData
201200
| ShaderUniformIVec3 (Int, Int, Int)
202201
| ShaderUniformIVec4 (Int, Int, Int, Int)
203202
| ShaderUniformSampler2D Texture
204-
deriving (Eq, Show)
203+
deriving (Eq, Show, Read)
205204

206205
data ShaderUniformDataV
207206
= ShaderUniformFloatV [Float]
@@ -213,7 +212,7 @@ data ShaderUniformDataV
213212
| ShaderUniformIVec3V [(Int, Int, Int)]
214213
| ShaderUniformIVec4V [(Int, Int, Int, Int)]
215214
| ShaderUniformSampler2DV [Texture]
216-
deriving (Eq, Show)
215+
deriving (Eq, Show, Read)
217216

218217
-- | Internal use
219218
unpackShaderUniformData :: ShaderUniformData -> IO (ShaderUniformDataType, ForeignPtr ())
@@ -321,7 +320,7 @@ data ShaderAttributeDataType
321320
| ShaderAttribVec2
322321
| ShaderAttribVec3
323322
| ShaderAttribVec4
324-
deriving (Eq, Show, Enum)
323+
deriving (Eq, Show, Read, Enum)
325324

326325
---------------------------------------
327326
-- models structures ------------------
@@ -331,7 +330,7 @@ data Mesh = Mesh
331330
{ mesh'vertexCount :: Int,
332331
mesh'triangleCount :: Int,
333332
mesh'vertices :: [Vector3],
334-
mesh'texcoords :: [Vector2],
333+
mesh'texcoords :: Maybe [Vector2],
335334
mesh'texcoords2 :: Maybe [Vector2],
336335
mesh'normals :: [Vector3],
337336
mesh'tangents :: Maybe [Vector4],
@@ -347,7 +346,7 @@ data Mesh = Mesh
347346
-- | Use `toEnum` on `DefaultShaderAttributeLocation` for indices
348347
mesh'vboId :: Maybe [Integer]
349348
}
350-
deriving (Eq, Show)
349+
deriving (Eq, Show, Read)
351350

352351
instance Storable Mesh where
353352
sizeOf _ = 120
@@ -356,7 +355,7 @@ instance Storable Mesh where
356355
vertexCount <- fromIntegral <$> peek (p'mesh'vertexCount _p)
357356
triangleCount <- fromIntegral <$> peek (p'mesh'triangleCount _p)
358357
vertices <- peekArray vertexCount =<< peek (p'mesh'vertices _p)
359-
texcoords <- peekArray vertexCount =<< peek (p'mesh'texcoords _p)
358+
texcoords <- peekMaybeArray vertexCount =<< peek (p'mesh'texcoords _p)
360359
texcoords2 <- peekMaybeArray vertexCount =<< peek (p'mesh'texcoords2 _p)
361360
normals <- peekArray vertexCount =<< peek (p'mesh'normals _p)
362361
tangents <- peekMaybeArray vertexCount =<< peek (p'mesh'tangents _p)
@@ -365,8 +364,8 @@ instance Storable Mesh where
365364
animVertices <- peekMaybeArray vertexCount =<< peek (p'mesh'animVertices _p)
366365
animNormals <- peekMaybeArray vertexCount =<< peek (p'mesh'animNormals _p)
367366
boneCount <- fromIntegral <$> peek (p'mesh'boneCount _p)
368-
boneIds <- (map fromIntegral <$>) <$> (peekMaybeArray boneCount =<< peek (p'mesh'boneIds _p))
369-
boneWeights <- (map realToFrac <$>) <$> (peekMaybeArray boneCount =<< peek (p'mesh'boneWeights _p))
367+
boneIds <- (map fromIntegral <$>) <$> (peekMaybeArray (4 * vertexCount) =<< peek (p'mesh'boneIds _p))
368+
boneWeights <- (map realToFrac <$>) <$> (peekMaybeArray (4 * vertexCount) =<< peek (p'mesh'boneWeights _p))
370369
boneMatrices <- peekMaybeArray boneCount =<< peek (p'mesh'boneMatrices _p)
371370
vaoId <- fromIntegral <$> peek (p'mesh'vaoId _p)
372371
vboId <- (map fromIntegral <$>) <$> (peekMaybeArray 9 =<< peek (p'mesh'vboId _p))
@@ -375,7 +374,7 @@ instance Storable Mesh where
375374
poke (p'mesh'vertexCount _p) (fromIntegral vertexCount)
376375
poke (p'mesh'triangleCount _p) (fromIntegral triangleCount)
377376
poke (p'mesh'vertices _p) =<< newArray vertices
378-
poke (p'mesh'texcoords _p) =<< newArray texcoords
377+
poke (p'mesh'texcoords _p) =<< newMaybeArray texcoords
379378
poke (p'mesh'texcoords2 _p) =<< newMaybeArray texcoords2
380379
poke (p'mesh'normals _p) =<< newArray normals
381380
poke (p'mesh'tangents _p) =<< newMaybeArray tangents
@@ -411,7 +410,7 @@ p'mesh'triangleCount = (`plusPtr` 4)
411410
p'mesh'vertices :: Ptr Mesh -> Ptr (Ptr Vector3)
412411
p'mesh'vertices = (`plusPtr` 8)
413412

414-
-- array (mesh'vertexCount)
413+
-- maybe array (mesh'vertexCount)
415414
p'mesh'texcoords :: Ptr Mesh -> Ptr (Ptr Vector2)
416415
p'mesh'texcoords = (`plusPtr` 16)
417416

@@ -496,7 +495,7 @@ data Shader = Shader
496495
{ shader'id :: Integer,
497496
shader'locs :: [Int]
498497
}
499-
deriving (Eq, Show)
498+
deriving (Eq, Show, Read)
500499

501500
instance Storable Shader where
502501
sizeOf _ = 16
@@ -507,13 +506,7 @@ instance Storable Shader where
507506
return $ Shader sId locs
508507
poke _p (Shader sId locs) = do
509508
poke (p'shader'id _p) (fromIntegral sId)
510-
defaultShaderId <- c'rlGetShaderIdDefault
511-
locsArr <- newArray (map fromIntegral locs)
512-
if sId == fromIntegral defaultShaderId
513-
then do
514-
locsPtr <- newForeignPtr p'free locsArr
515-
withForeignPtr locsPtr $ poke (p'shader'locs _p)
516-
else poke (p'shader'locs _p) locsArr
509+
poke (p'shader'locs _p) =<< newArray (map fromIntegral locs)
517510
return ()
518511

519512
instance Closeable Shader where
@@ -531,21 +524,19 @@ p'shader'locs :: Ptr Shader -> Ptr (Ptr CInt)
531524
p'shader'locs = (`plusPtr` 8)
532525

533526
instance Freeable Shader where
534-
rlFreeDependents val ptr = do
535-
defaultShaderId <- c'rlGetShaderIdDefault
527+
rlFreeDependents _ ptr = do
528+
defaultShaderLocs <- c'rlGetShaderLocsDefault
529+
locsPtr <- peek (p'shader'locs ptr)
536530
unless
537-
(shader'id val == fromIntegral defaultShaderId)
538-
( do
539-
locsPtr <- peek (p'shader'locs ptr)
540-
c'free $ castPtr locsPtr
541-
)
531+
(locsPtr == defaultShaderLocs)
532+
(c'free $ castPtr locsPtr)
542533

543534
data MaterialMap = MaterialMap
544535
{ materialMap'texture :: Texture,
545536
materialMap'color :: Color,
546537
materialMap'value :: Float
547538
}
548-
deriving (Eq, Show, Freeable)
539+
deriving (Eq, Show, Read, Freeable)
549540

550541
instance Storable MaterialMap where
551542
sizeOf _ = 28
@@ -575,7 +566,7 @@ data Material = Material
575566
material'maps :: Maybe [MaterialMap],
576567
material'params :: [Float]
577568
}
578-
deriving (Eq, Show)
569+
deriving (Eq, Show, Read)
579570

580571
instance Storable Material where
581572
sizeOf _ = 40
@@ -633,7 +624,7 @@ data Transform = Transform
633624
transform'rotation :: Quaternion,
634625
transform'scale :: Vector3
635626
}
636-
deriving (Eq, Show, Freeable)
627+
deriving (Eq, Show, Read, Freeable)
637628

638629
instance Storable Transform where
639630
sizeOf _ = 40
@@ -662,7 +653,7 @@ data BoneInfo = BoneInfo
662653
{ boneInfo'name :: String,
663654
boneInfo'parent :: Int
664655
}
665-
deriving (Eq, Show, Freeable)
656+
deriving (Eq, Show, Read, Freeable)
666657

667658
instance Storable BoneInfo where
668659
sizeOf _ = 36
@@ -692,7 +683,7 @@ data Model = Model
692683
model'bones :: Maybe [BoneInfo],
693684
model'bindPose :: Maybe [Transform]
694685
}
695-
deriving (Eq, Show)
686+
deriving (Eq, Show, Read)
696687

697688
instance Storable Model where
698689
sizeOf _ = 120
@@ -775,7 +766,7 @@ data ModelAnimation = ModelAnimation
775766
modelAnimation'framePoses :: [[Transform]],
776767
modelAnimation'name :: String
777768
}
778-
deriving (Eq, Show)
769+
deriving (Eq, Show, Read)
779770

780771
instance Storable ModelAnimation where
781772
sizeOf _ = 56
@@ -827,7 +818,7 @@ data Ray = Ray
827818
{ ray'position :: Vector3,
828819
ray'direction :: Vector3
829820
}
830-
deriving (Eq, Show, Freeable)
821+
deriving (Eq, Show, Read, Freeable)
831822

832823
instance Storable Ray where
833824
sizeOf _ = 24
@@ -853,7 +844,7 @@ data RayCollision = RayCollision
853844
rayCollision'point :: Vector3,
854845
rayCollision'normal :: Vector3
855846
}
856-
deriving (Eq, Show, Freeable)
847+
deriving (Eq, Show, Read, Freeable)
857848

858849
instance Storable RayCollision where
859850
sizeOf _ = 32
@@ -887,7 +878,7 @@ data BoundingBox = BoundingBox
887878
{ boundingBox'min :: Vector3,
888879
boundingBox'max :: Vector3
889880
}
890-
deriving (Eq, Show, Freeable)
881+
deriving (Eq, Show, Read, Freeable)
891882

892883
instance Storable BoundingBox where
893884
sizeOf _ = 24

0 commit comments

Comments
 (0)