Support custom memory management macros

Users can define their custom memory management macros.

NOTE: Most external libraries support custom macros in the same way, raylib should redefine those macros to raylib ones, to unify custom memory loading. That redefinition is only implemented as example for stb_image.h in [textures] module.
This commit is contained in:
Ray 2019-04-23 14:55:35 +02:00
parent 8ed71b9d5a
commit e67ebabb02
9 changed files with 364 additions and 338 deletions

View file

@ -651,7 +651,7 @@ Model LoadModel(const char *fileName)
TraceLog(LOG_WARNING, "[%s] No meshes can be loaded, default to cube mesh", fileName);
model.meshCount = 1;
model.meshes = (Mesh *)calloc(model.meshCount, sizeof(Mesh));
model.meshes = (Mesh *)RL_CALLOC(model.meshCount, sizeof(Mesh));
model.meshes[0] = GenMeshCube(1.0f, 1.0f, 1.0f);
}
else
@ -665,10 +665,10 @@ Model LoadModel(const char *fileName)
TraceLog(LOG_WARNING, "[%s] No materials can be loaded, default to white material", fileName);
model.materialCount = 1;
model.materials = (Material *)calloc(model.materialCount, sizeof(Material));
model.materials = (Material *)RL_CALLOC(model.materialCount, sizeof(Material));
model.materials[0] = LoadMaterialDefault();
model.meshMaterial = (int *)calloc(model.meshCount, sizeof(int));
model.meshMaterial = (int *)RL_CALLOC(model.meshCount, sizeof(int));
}
return model;
@ -685,14 +685,14 @@ Model LoadModelFromMesh(Mesh mesh)
model.transform = MatrixIdentity();
model.meshCount = 1;
model.meshes = (Mesh *)malloc(model.meshCount*sizeof(Mesh));
model.meshes = (Mesh *)RL_MALLOC(model.meshCount*sizeof(Mesh));
model.meshes[0] = mesh;
model.materialCount = 1;
model.materials = (Material *)malloc(model.materialCount*sizeof(Material));
model.materials = (Material *)RL_MALLOC(model.materialCount*sizeof(Material));
model.materials[0] = LoadMaterialDefault();
model.meshMaterial = (int *)malloc(model.meshCount*sizeof(int));
model.meshMaterial = (int *)RL_MALLOC(model.meshCount*sizeof(int));
model.meshMaterial[0] = 0; // First material index
return model;
@ -704,13 +704,13 @@ void UnloadModel(Model model)
for (int i = 0; i < model.meshCount; i++) UnloadMesh(&model.meshes[i]);
for (int i = 0; i < model.materialCount; i++) UnloadMaterial(model.materials[i]);
free(model.meshes);
free(model.materials);
free(model.meshMaterial);
RL_FREE(model.meshes);
RL_FREE(model.materials);
RL_FREE(model.meshMaterial);
// Unload animation data
free(model.bones);
free(model.bindPose);
RL_FREE(model.bones);
RL_FREE(model.bindPose);
TraceLog(LOG_INFO, "Unloaded model data from RAM and VRAM");
}
@ -866,7 +866,7 @@ void SetModelMeshMaterial(Model *model, int meshId, int materialId)
// Load model animations from file
ModelAnimation *LoadModelAnimations(const char *filename, int *animCount)
{
ModelAnimation *animations = (ModelAnimation *)malloc(1*sizeof(ModelAnimation));
ModelAnimation *animations = (ModelAnimation *)RL_MALLOC(1*sizeof(ModelAnimation));
int count = 1;
#define IQM_MAGIC "INTERQUAKEMODEL" // IQM file magic number
@ -935,12 +935,12 @@ ModelAnimation *LoadModelAnimations(const char *filename, int *animCount)
// bones
IQMPose *poses;
poses = malloc(sizeof(IQMPose)*iqm.num_poses);
poses = RL_MALLOC(sizeof(IQMPose)*iqm.num_poses);
fseek(iqmFile, iqm.ofs_poses, SEEK_SET);
fread(poses, sizeof(IQMPose)*iqm.num_poses, 1, iqmFile);
animation.boneCount = iqm.num_poses;
animation.bones = malloc(sizeof(BoneInfo)*iqm.num_poses);
animation.bones = RL_MALLOC(sizeof(BoneInfo)*iqm.num_poses);
for (int j = 0; j < iqm.num_poses; j++)
{
@ -957,12 +957,12 @@ ModelAnimation *LoadModelAnimations(const char *filename, int *animCount)
//animation.framerate = anim.framerate;
// frameposes
unsigned short *framedata = malloc(sizeof(unsigned short)*iqm.num_frames*iqm.num_framechannels);
unsigned short *framedata = RL_MALLOC(sizeof(unsigned short)*iqm.num_frames*iqm.num_framechannels);
fseek(iqmFile, iqm.ofs_frames, SEEK_SET);
fread(framedata, sizeof(unsigned short)*iqm.num_frames*iqm.num_framechannels, 1, iqmFile);
animation.framePoses = malloc(sizeof(Transform*)*anim.num_frames);
for (int j = 0; j < anim.num_frames; j++) animation.framePoses[j] = malloc(sizeof(Transform)*iqm.num_poses);
animation.framePoses = RL_MALLOC(sizeof(Transform*)*anim.num_frames);
for (int j = 0; j < anim.num_frames; j++) animation.framePoses[j] = RL_MALLOC(sizeof(Transform)*iqm.num_poses);
int dcounter = anim.first_frame*iqm.num_framechannels;
@ -1069,8 +1069,8 @@ ModelAnimation *LoadModelAnimations(const char *filename, int *animCount)
}
}
free(framedata);
free(poses);
RL_FREE(framedata);
RL_FREE(poses);
fclose(iqmFile);
@ -1145,10 +1145,10 @@ void UpdateModelAnimation(Model model, ModelAnimation anim, int frame)
// Unload animation data
void UnloadModelAnimation(ModelAnimation anim)
{
for (int i = 0; i < anim.frameCount; i++) free(anim.framePoses[i]);
for (int i = 0; i < anim.frameCount; i++) RL_FREE(anim.framePoses[i]);
free(anim.bones);
free(anim.framePoses);
RL_FREE(anim.bones);
RL_FREE(anim.framePoses);
}
// Check model animation skeleton match
@ -1177,7 +1177,7 @@ Mesh GenMeshPoly(int sides, float radius)
int vertexCount = sides*3;
// Vertices definition
Vector3 *vertices = (Vector3 *)malloc(vertexCount*sizeof(Vector3));
Vector3 *vertices = (Vector3 *)RL_MALLOC(vertexCount*sizeof(Vector3));
for (int i = 0, v = 0; i < 360; i += 360/sides, v += 3)
{
vertices[v] = (Vector3){ 0.0f, 0.0f, 0.0f };
@ -1186,18 +1186,18 @@ Mesh GenMeshPoly(int sides, float radius)
}
// Normals definition
Vector3 *normals = (Vector3 *)malloc(vertexCount*sizeof(Vector3));
Vector3 *normals = (Vector3 *)RL_MALLOC(vertexCount*sizeof(Vector3));
for (int n = 0; n < vertexCount; n++) normals[n] = (Vector3){ 0.0f, 1.0f, 0.0f }; // Vector3.up;
// TexCoords definition
Vector2 *texcoords = (Vector2 *)malloc(vertexCount*sizeof(Vector2));
Vector2 *texcoords = (Vector2 *)RL_MALLOC(vertexCount*sizeof(Vector2));
for (int n = 0; n < vertexCount; n++) texcoords[n] = (Vector2){ 0.0f, 0.0f };
mesh.vertexCount = vertexCount;
mesh.triangleCount = sides;
mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float));
mesh.texcoords = (float *)malloc(mesh.vertexCount*2*sizeof(float));
mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float));
mesh.vertices = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float));
mesh.texcoords = (float *)RL_MALLOC(mesh.vertexCount*2*sizeof(float));
mesh.normals = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float));
// Mesh vertices position array
for (int i = 0; i < mesh.vertexCount; i++)
@ -1222,9 +1222,9 @@ Mesh GenMeshPoly(int sides, float radius)
mesh.normals[3*i + 2] = normals[i].z;
}
free(vertices);
free(normals);
free(texcoords);
RL_FREE(vertices);
RL_FREE(normals);
RL_FREE(texcoords);
// Upload vertex data to GPU (static mesh)
rlLoadMesh(&mesh, false);
@ -1245,7 +1245,7 @@ Mesh GenMeshPlane(float width, float length, int resX, int resZ)
// Vertices definition
int vertexCount = resX*resZ; // vertices get reused for the faces
Vector3 *vertices = (Vector3 *)malloc(vertexCount*sizeof(Vector3));
Vector3 *vertices = (Vector3 *)RL_MALLOC(vertexCount*sizeof(Vector3));
for (int z = 0; z < resZ; z++)
{
// [-length/2, length/2]
@ -1259,11 +1259,11 @@ Mesh GenMeshPlane(float width, float length, int resX, int resZ)
}
// Normals definition
Vector3 *normals = (Vector3 *)malloc(vertexCount*sizeof(Vector3));
Vector3 *normals = (Vector3 *)RL_MALLOC(vertexCount*sizeof(Vector3));
for (int n = 0; n < vertexCount; n++) normals[n] = (Vector3){ 0.0f, 1.0f, 0.0f }; // Vector3.up;
// TexCoords definition
Vector2 *texcoords = (Vector2 *)malloc(vertexCount*sizeof(Vector2));
Vector2 *texcoords = (Vector2 *)RL_MALLOC(vertexCount*sizeof(Vector2));
for (int v = 0; v < resZ; v++)
{
for (int u = 0; u < resX; u++)
@ -1274,7 +1274,7 @@ Mesh GenMeshPlane(float width, float length, int resX, int resZ)
// Triangles definition (indices)
int numFaces = (resX - 1)*(resZ - 1);
int *triangles = (int *)malloc(numFaces*6*sizeof(int));
int *triangles = (int *)RL_MALLOC(numFaces*6*sizeof(int));
int t = 0;
for (int face = 0; face < numFaces; face++)
{
@ -1292,10 +1292,10 @@ Mesh GenMeshPlane(float width, float length, int resX, int resZ)
mesh.vertexCount = vertexCount;
mesh.triangleCount = numFaces*2;
mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float));
mesh.texcoords = (float *)malloc(mesh.vertexCount*2*sizeof(float));
mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float));
mesh.indices = (unsigned short *)malloc(mesh.triangleCount*3*sizeof(unsigned short));
mesh.vertices = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float));
mesh.texcoords = (float *)RL_MALLOC(mesh.vertexCount*2*sizeof(float));
mesh.normals = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float));
mesh.indices = (unsigned short *)RL_MALLOC(mesh.triangleCount*3*sizeof(unsigned short));
// Mesh vertices position array
for (int i = 0; i < mesh.vertexCount; i++)
@ -1323,10 +1323,10 @@ Mesh GenMeshPlane(float width, float length, int resX, int resZ)
// Mesh indices array initialization
for (int i = 0; i < mesh.triangleCount*3; i++) mesh.indices[i] = triangles[i];
free(vertices);
free(normals);
free(texcoords);
free(triangles);
RL_FREE(vertices);
RL_FREE(normals);
RL_FREE(texcoords);
RL_FREE(triangles);
#else // Use par_shapes library to generate plane mesh
@ -1335,9 +1335,9 @@ Mesh GenMeshPlane(float width, float length, int resX, int resZ)
par_shapes_rotate(plane, -PI/2.0f, (float[]){ 1, 0, 0 });
par_shapes_translate(plane, -width/2, 0.0f, length/2);
mesh.vertices = (float *)malloc(plane->ntriangles*3*3*sizeof(float));
mesh.texcoords = (float *)malloc(plane->ntriangles*3*2*sizeof(float));
mesh.normals = (float *)malloc(plane->ntriangles*3*3*sizeof(float));
mesh.vertices = (float *)RL_MALLOC(plane->ntriangles*3*3*sizeof(float));
mesh.texcoords = (float *)RL_MALLOC(plane->ntriangles*3*2*sizeof(float));
mesh.normals = (float *)RL_MALLOC(plane->ntriangles*3*3*sizeof(float));
mesh.vertexCount = plane->ntriangles*3;
mesh.triangleCount = plane->ntriangles;
@ -1453,16 +1453,16 @@ Mesh GenMeshCube(float width, float height, float length)
-1.0f, 0.0f, 0.0f
};
mesh.vertices = (float *)malloc(24*3*sizeof(float));
mesh.vertices = (float *)RL_MALLOC(24*3*sizeof(float));
memcpy(mesh.vertices, vertices, 24*3*sizeof(float));
mesh.texcoords = (float *)malloc(24*2*sizeof(float));
mesh.texcoords = (float *)RL_MALLOC(24*2*sizeof(float));
memcpy(mesh.texcoords, texcoords, 24*2*sizeof(float));
mesh.normals = (float *)malloc(24*3*sizeof(float));
mesh.normals = (float *)RL_MALLOC(24*3*sizeof(float));
memcpy(mesh.normals, normals, 24*3*sizeof(float));
mesh.indices = (unsigned short *)malloc(36*sizeof(unsigned short));
mesh.indices = (unsigned short *)RL_MALLOC(36*sizeof(unsigned short));
int k = 0;
@ -1500,9 +1500,9 @@ par_shapes_mesh* par_shapes_create_icosahedron(); // 20 sides polyhedron
par_shapes_translate(cube, -width/2, 0.0f, -length/2);
par_shapes_compute_normals(cube);
mesh.vertices = (float *)malloc(cube->ntriangles*3*3*sizeof(float));
mesh.texcoords = (float *)malloc(cube->ntriangles*3*2*sizeof(float));
mesh.normals = (float *)malloc(cube->ntriangles*3*3*sizeof(float));
mesh.vertices = (float *)RL_MALLOC(cube->ntriangles*3*3*sizeof(float));
mesh.texcoords = (float *)RL_MALLOC(cube->ntriangles*3*2*sizeof(float));
mesh.normals = (float *)RL_MALLOC(cube->ntriangles*3*3*sizeof(float));
mesh.vertexCount = cube->ntriangles*3;
mesh.triangleCount = cube->ntriangles;
@ -1539,9 +1539,9 @@ RLAPI Mesh GenMeshSphere(float radius, int rings, int slices)
par_shapes_scale(sphere, radius, radius, radius);
// NOTE: Soft normals are computed internally
mesh.vertices = (float *)malloc(sphere->ntriangles*3*3*sizeof(float));
mesh.texcoords = (float *)malloc(sphere->ntriangles*3*2*sizeof(float));
mesh.normals = (float *)malloc(sphere->ntriangles*3*3*sizeof(float));
mesh.vertices = (float *)RL_MALLOC(sphere->ntriangles*3*3*sizeof(float));
mesh.texcoords = (float *)RL_MALLOC(sphere->ntriangles*3*2*sizeof(float));
mesh.normals = (float *)RL_MALLOC(sphere->ntriangles*3*3*sizeof(float));
mesh.vertexCount = sphere->ntriangles*3;
mesh.triangleCount = sphere->ntriangles;
@ -1577,9 +1577,9 @@ RLAPI Mesh GenMeshHemiSphere(float radius, int rings, int slices)
par_shapes_scale(sphere, radius, radius, radius);
// NOTE: Soft normals are computed internally
mesh.vertices = (float *)malloc(sphere->ntriangles*3*3*sizeof(float));
mesh.texcoords = (float *)malloc(sphere->ntriangles*3*2*sizeof(float));
mesh.normals = (float *)malloc(sphere->ntriangles*3*3*sizeof(float));
mesh.vertices = (float *)RL_MALLOC(sphere->ntriangles*3*3*sizeof(float));
mesh.texcoords = (float *)RL_MALLOC(sphere->ntriangles*3*2*sizeof(float));
mesh.normals = (float *)RL_MALLOC(sphere->ntriangles*3*3*sizeof(float));
mesh.vertexCount = sphere->ntriangles*3;
mesh.triangleCount = sphere->ntriangles;
@ -1635,9 +1635,9 @@ Mesh GenMeshCylinder(float radius, float height, int slices)
par_shapes_merge_and_free(cylinder, capTop);
par_shapes_merge_and_free(cylinder, capBottom);
mesh.vertices = (float *)malloc(cylinder->ntriangles*3*3*sizeof(float));
mesh.texcoords = (float *)malloc(cylinder->ntriangles*3*2*sizeof(float));
mesh.normals = (float *)malloc(cylinder->ntriangles*3*3*sizeof(float));
mesh.vertices = (float *)RL_MALLOC(cylinder->ntriangles*3*3*sizeof(float));
mesh.texcoords = (float *)RL_MALLOC(cylinder->ntriangles*3*2*sizeof(float));
mesh.normals = (float *)RL_MALLOC(cylinder->ntriangles*3*3*sizeof(float));
mesh.vertexCount = cylinder->ntriangles*3;
mesh.triangleCount = cylinder->ntriangles;
@ -1677,9 +1677,9 @@ Mesh GenMeshTorus(float radius, float size, int radSeg, int sides)
par_shapes_mesh *torus = par_shapes_create_torus(radSeg, sides, radius);
par_shapes_scale(torus, size/2, size/2, size/2);
mesh.vertices = (float *)malloc(torus->ntriangles*3*3*sizeof(float));
mesh.texcoords = (float *)malloc(torus->ntriangles*3*2*sizeof(float));
mesh.normals = (float *)malloc(torus->ntriangles*3*3*sizeof(float));
mesh.vertices = (float *)RL_MALLOC(torus->ntriangles*3*3*sizeof(float));
mesh.texcoords = (float *)RL_MALLOC(torus->ntriangles*3*2*sizeof(float));
mesh.normals = (float *)RL_MALLOC(torus->ntriangles*3*3*sizeof(float));
mesh.vertexCount = torus->ntriangles*3;
mesh.triangleCount = torus->ntriangles;
@ -1717,9 +1717,9 @@ Mesh GenMeshKnot(float radius, float size, int radSeg, int sides)
par_shapes_mesh *knot = par_shapes_create_trefoil_knot(radSeg, sides, radius);
par_shapes_scale(knot, size, size, size);
mesh.vertices = (float *)malloc(knot->ntriangles*3*3*sizeof(float));
mesh.texcoords = (float *)malloc(knot->ntriangles*3*2*sizeof(float));
mesh.normals = (float *)malloc(knot->ntriangles*3*3*sizeof(float));
mesh.vertices = (float *)RL_MALLOC(knot->ntriangles*3*3*sizeof(float));
mesh.texcoords = (float *)RL_MALLOC(knot->ntriangles*3*2*sizeof(float));
mesh.normals = (float *)RL_MALLOC(knot->ntriangles*3*3*sizeof(float));
mesh.vertexCount = knot->ntriangles*3;
mesh.triangleCount = knot->ntriangles;
@ -1764,9 +1764,9 @@ Mesh GenMeshHeightmap(Image heightmap, Vector3 size)
mesh.vertexCount = mesh.triangleCount*3;
mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float));
mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float));
mesh.texcoords = (float *)malloc(mesh.vertexCount*2*sizeof(float));
mesh.vertices = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float));
mesh.normals = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float));
mesh.texcoords = (float *)RL_MALLOC(mesh.vertexCount*2*sizeof(float));
mesh.colors = NULL;
int vCounter = 0; // Used to count vertices float by float
@ -1848,7 +1848,7 @@ Mesh GenMeshHeightmap(Image heightmap, Vector3 size)
}
}
free(pixels);
RL_FREE(pixels);
// Upload vertex data to GPU (static mesh)
rlLoadMesh(&mesh, false);
@ -1878,9 +1878,9 @@ Mesh GenMeshCubicmap(Image cubicmap, Vector3 cubeSize)
float h = cubeSize.z;
float h2 = cubeSize.y;
Vector3 *mapVertices = (Vector3 *)malloc(maxTriangles*3*sizeof(Vector3));
Vector2 *mapTexcoords = (Vector2 *)malloc(maxTriangles*3*sizeof(Vector2));
Vector3 *mapNormals = (Vector3 *)malloc(maxTriangles*3*sizeof(Vector3));
Vector3 *mapVertices = (Vector3 *)RL_MALLOC(maxTriangles*3*sizeof(Vector3));
Vector2 *mapTexcoords = (Vector2 *)RL_MALLOC(maxTriangles*3*sizeof(Vector2));
Vector3 *mapNormals = (Vector3 *)RL_MALLOC(maxTriangles*3*sizeof(Vector3));
// Define the 6 normals of the cube, we will combine them accordingly later...
Vector3 n1 = { 1.0f, 0.0f, 0.0f };
@ -2167,9 +2167,9 @@ Mesh GenMeshCubicmap(Image cubicmap, Vector3 cubeSize)
mesh.vertexCount = vCounter;
mesh.triangleCount = vCounter/3;
mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float));
mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float));
mesh.texcoords = (float *)malloc(mesh.vertexCount*2*sizeof(float));
mesh.vertices = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float));
mesh.normals = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float));
mesh.texcoords = (float *)RL_MALLOC(mesh.vertexCount*2*sizeof(float));
mesh.colors = NULL;
int fCounter = 0;
@ -2204,11 +2204,11 @@ Mesh GenMeshCubicmap(Image cubicmap, Vector3 cubeSize)
fCounter += 2;
}
free(mapVertices);
free(mapNormals);
free(mapTexcoords);
RL_FREE(mapVertices);
RL_FREE(mapNormals);
RL_FREE(mapTexcoords);
free(cubicmapPixels); // Free image pixel data
RL_FREE(cubicmapPixels); // Free image pixel data
// Upload vertex data to GPU (static mesh)
rlLoadMesh(&mesh, false);
@ -2250,11 +2250,11 @@ BoundingBox MeshBoundingBox(Mesh mesh)
// Implementation base don: https://answers.unity.com/questions/7789/calculating-tangents-vector4.html
void MeshTangents(Mesh *mesh)
{
if (mesh->tangents == NULL) mesh->tangents = (float *)malloc(mesh->vertexCount*4*sizeof(float));
if (mesh->tangents == NULL) mesh->tangents = (float *)RL_MALLOC(mesh->vertexCount*4*sizeof(float));
else TraceLog(LOG_WARNING, "Mesh tangents already exist");
Vector3 *tan1 = (Vector3 *)malloc(mesh->vertexCount*sizeof(Vector3));
Vector3 *tan2 = (Vector3 *)malloc(mesh->vertexCount*sizeof(Vector3));
Vector3 *tan1 = (Vector3 *)RL_MALLOC(mesh->vertexCount*sizeof(Vector3));
Vector3 *tan2 = (Vector3 *)RL_MALLOC(mesh->vertexCount*sizeof(Vector3));
for (int i = 0; i < mesh->vertexCount; i += 3)
{
@ -2318,8 +2318,8 @@ void MeshTangents(Mesh *mesh)
#endif
}
free(tan1);
free(tan2);
RL_FREE(tan1);
RL_FREE(tan2);
// Load a new tangent attributes buffer
mesh->vboId[LOC_VERTEX_TANGENT] = rlLoadAttribBuffer(mesh->vaoId, LOC_VERTEX_TANGENT, mesh->tangents, mesh->vertexCount*4*sizeof(float), false);
@ -2746,7 +2746,7 @@ static Model LoadOBJ(const char *fileName)
long length = ftell(objFile); // Get file size
fseek(objFile, 0, SEEK_SET); // Reset file pointer
data = (char *)malloc(length);
data = (char *)RL_MALLOC(length);
fread(data, length, 1, objFile);
dataLength = length;
@ -2763,12 +2763,12 @@ static Model LoadOBJ(const char *fileName)
// Init model meshes array
model.meshCount = meshCount;
model.meshes = (Mesh *)malloc(model.meshCount*sizeof(Mesh));
model.meshes = (Mesh *)RL_MALLOC(model.meshCount*sizeof(Mesh));
// Init model materials array
model.materialCount = materialCount;
model.materials = (Material *)malloc(model.materialCount*sizeof(Material));
model.meshMaterial = (int *)calloc(model.meshCount, sizeof(int));
model.materials = (Material *)RL_MALLOC(model.materialCount*sizeof(Material));
model.meshMaterial = (int *)RL_CALLOC(model.meshCount, sizeof(int));
/*
// Multiple meshes data reference
@ -2787,9 +2787,9 @@ static Model LoadOBJ(const char *fileName)
memset(&mesh, 0, sizeof(Mesh));
mesh.vertexCount = attrib.num_faces*3;
mesh.triangleCount = attrib.num_faces;
mesh.vertices = (float *)malloc(mesh.vertexCount*3*sizeof(float));
mesh.texcoords = (float *)malloc(mesh.vertexCount*2*sizeof(float));
mesh.normals = (float *)malloc(mesh.vertexCount*3*sizeof(float));
mesh.vertices = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float));
mesh.texcoords = (float *)RL_MALLOC(mesh.vertexCount*2*sizeof(float));
mesh.normals = (float *)RL_MALLOC(mesh.vertexCount*3*sizeof(float));
int vCount = 0;
int vtCount = 0;
@ -2934,17 +2934,26 @@ static Model LoadIQM(const char *fileName)
typedef struct IQMTriangle {
unsigned int vertex[3];
} IQMTriangle;
// NOTE: Adjacency unused by default
typedef struct IQMAdjacency {
unsigned int triangle[3];
} IQMAdjacency;
typedef struct IQMJoint {
unsigned int name;
int parent;
float translate[3], rotate[4], scale[3];
} IQMJoint;
typedef struct IQMVertexArray {
unsigned int type;
unsigned int flags;
unsigned int format;
unsigned int size;
unsigned int offset;
} IQMVertexArray;
// NOTE: Below IQM structures are not used but listed for reference
/*
typedef struct IQMAdjacency {
unsigned int triangle[3];
} IQMAdjacency;
typedef struct IQMPose {
int parent;
@ -2960,19 +2969,11 @@ static Model LoadIQM(const char *fileName)
unsigned int flags;
} IQMAnim;
typedef struct IQMVertexArray {
unsigned int type;
unsigned int flags;
unsigned int format;
unsigned int size;
unsigned int offset;
} IQMVertexArray;
// NOTE: Bounds unused by default
typedef struct IQMBounds {
float bbmin[3], bbmax[3];
float xyradius, radius;
} IQMBounds;
*/
//-----------------------------------------------------------------------------------
// IQM vertex data types
@ -3028,12 +3029,12 @@ static Model LoadIQM(const char *fileName)
}
// Meshes data processing
imesh = malloc(sizeof(IQMMesh)*iqm.num_meshes);
imesh = RL_MALLOC(sizeof(IQMMesh)*iqm.num_meshes);
fseek(iqmFile, iqm.ofs_meshes, SEEK_SET);
fread(imesh, sizeof(IQMMesh)*iqm.num_meshes, 1, iqmFile);
model.meshCount = iqm.num_meshes;
model.meshes = malloc(model.meshCount*sizeof(Mesh));
model.meshes = RL_MALLOC(model.meshCount*sizeof(Mesh));
char name[MESH_NAME_LENGTH];
@ -3043,24 +3044,24 @@ static Model LoadIQM(const char *fileName)
fread(name, sizeof(char)*MESH_NAME_LENGTH, 1, iqmFile); // Mesh name not used...
model.meshes[i].vertexCount = imesh[i].num_vertexes;
model.meshes[i].vertices = malloc(sizeof(float)*model.meshes[i].vertexCount*3); // Default vertex positions
model.meshes[i].normals = malloc(sizeof(float)*model.meshes[i].vertexCount*3); // Default vertex normals
model.meshes[i].texcoords = malloc(sizeof(float)*model.meshes[i].vertexCount*2); // Default vertex texcoords
model.meshes[i].vertices = RL_MALLOC(sizeof(float)*model.meshes[i].vertexCount*3); // Default vertex positions
model.meshes[i].normals = RL_MALLOC(sizeof(float)*model.meshes[i].vertexCount*3); // Default vertex normals
model.meshes[i].texcoords = RL_MALLOC(sizeof(float)*model.meshes[i].vertexCount*2); // Default vertex texcoords
model.meshes[i].boneIds = malloc(sizeof(int)*model.meshes[i].vertexCount*4); // Up-to 4 bones supported!
model.meshes[i].boneWeights = malloc(sizeof(float)*model.meshes[i].vertexCount*4); // Up-to 4 bones supported!
model.meshes[i].boneIds = RL_MALLOC(sizeof(int)*model.meshes[i].vertexCount*4); // Up-to 4 bones supported!
model.meshes[i].boneWeights = RL_MALLOC(sizeof(float)*model.meshes[i].vertexCount*4); // Up-to 4 bones supported!
model.meshes[i].triangleCount = imesh[i].num_triangles;
model.meshes[i].indices = malloc(sizeof(unsigned short)*model.meshes[i].triangleCount*3);
model.meshes[i].indices = RL_MALLOC(sizeof(unsigned short)*model.meshes[i].triangleCount*3);
// Animated verted data, what we actually process for rendering
// NOTE: Animated vertex should be re-uploaded to GPU (if not using GPU skinning)
model.meshes[i].animVertices = malloc(sizeof(float)*model.meshes[i].vertexCount*3);
model.meshes[i].animNormals = malloc(sizeof(float)*model.meshes[i].vertexCount*3);
model.meshes[i].animVertices = RL_MALLOC(sizeof(float)*model.meshes[i].vertexCount*3);
model.meshes[i].animNormals = RL_MALLOC(sizeof(float)*model.meshes[i].vertexCount*3);
}
// Triangles data processing
tri = malloc(sizeof(IQMTriangle)*iqm.num_triangles);
tri = RL_MALLOC(sizeof(IQMTriangle)*iqm.num_triangles);
fseek(iqmFile, iqm.ofs_triangles, SEEK_SET);
fread(tri, sizeof(IQMTriangle)*iqm.num_triangles, 1, iqmFile);
@ -3079,7 +3080,7 @@ static Model LoadIQM(const char *fileName)
}
// Vertex arrays data processing
va = malloc(sizeof(IQMVertexArray)*iqm.num_vertexarrays);
va = RL_MALLOC(sizeof(IQMVertexArray)*iqm.num_vertexarrays);
fseek(iqmFile, iqm.ofs_vertexarrays, SEEK_SET);
fread(va, sizeof(IQMVertexArray)*iqm.num_vertexarrays, 1, iqmFile);
@ -3089,7 +3090,7 @@ static Model LoadIQM(const char *fileName)
{
case IQM_POSITION:
{
vertex = malloc(sizeof(float)*iqm.num_vertexes*3);
vertex = RL_MALLOC(sizeof(float)*iqm.num_vertexes*3);
fseek(iqmFile, va[i].offset, SEEK_SET);
fread(vertex, sizeof(float)*iqm.num_vertexes*3, 1, iqmFile);
@ -3106,7 +3107,7 @@ static Model LoadIQM(const char *fileName)
} break;
case IQM_NORMAL:
{
normal = malloc(sizeof(float)*iqm.num_vertexes*3);
normal = RL_MALLOC(sizeof(float)*iqm.num_vertexes*3);
fseek(iqmFile, va[i].offset, SEEK_SET);
fread(normal, sizeof(float)*iqm.num_vertexes*3, 1, iqmFile);
@ -3123,7 +3124,7 @@ static Model LoadIQM(const char *fileName)
} break;
case IQM_TEXCOORD:
{
text = malloc(sizeof(float)*iqm.num_vertexes*2);
text = RL_MALLOC(sizeof(float)*iqm.num_vertexes*2);
fseek(iqmFile, va[i].offset, SEEK_SET);
fread(text, sizeof(float)*iqm.num_vertexes*2, 1, iqmFile);
@ -3139,7 +3140,7 @@ static Model LoadIQM(const char *fileName)
} break;
case IQM_BLENDINDEXES:
{
blendi = malloc(sizeof(char)*iqm.num_vertexes*4);
blendi = RL_MALLOC(sizeof(char)*iqm.num_vertexes*4);
fseek(iqmFile, va[i].offset, SEEK_SET);
fread(blendi, sizeof(char)*iqm.num_vertexes*4, 1, iqmFile);
@ -3155,7 +3156,7 @@ static Model LoadIQM(const char *fileName)
} break;
case IQM_BLENDWEIGHTS:
{
blendw = malloc(sizeof(unsigned char)*iqm.num_vertexes*4);
blendw = RL_MALLOC(sizeof(unsigned char)*iqm.num_vertexes*4);
fseek(iqmFile,va[i].offset,SEEK_SET);
fread(blendw,sizeof(unsigned char)*iqm.num_vertexes*4,1,iqmFile);
@ -3173,13 +3174,13 @@ static Model LoadIQM(const char *fileName)
}
// Bones (joints) data processing
ijoint = malloc(sizeof(IQMJoint)*iqm.num_joints);
ijoint = RL_MALLOC(sizeof(IQMJoint)*iqm.num_joints);
fseek(iqmFile, iqm.ofs_joints, SEEK_SET);
fread(ijoint, sizeof(IQMJoint)*iqm.num_joints, 1, iqmFile);
model.boneCount = iqm.num_joints;
model.bones = malloc(sizeof(BoneInfo)*iqm.num_joints);
model.bindPose = malloc(sizeof(Transform)*iqm.num_joints);
model.bones = RL_MALLOC(sizeof(BoneInfo)*iqm.num_joints);
model.bindPose = RL_MALLOC(sizeof(Transform)*iqm.num_joints);
for (int i = 0; i < iqm.num_joints; i++)
{
@ -3216,15 +3217,15 @@ static Model LoadIQM(const char *fileName)
}
fclose(iqmFile);
free(imesh);
free(tri);
free(va);
free(vertex);
free(normal);
free(text);
free(blendi);
free(blendw);
free(ijoint);
RL_FREE(imesh);
RL_FREE(tri);
RL_FREE(va);
RL_FREE(vertex);
RL_FREE(normal);
RL_FREE(text);
RL_FREE(blendi);
RL_FREE(blendw);
RL_FREE(ijoint);
return model;
}
@ -3249,7 +3250,7 @@ static Model LoadGLTF(const char *fileName)
int size = ftell(gltfFile);
fseek(gltfFile, 0, SEEK_SET);
void *buffer = malloc(size);
void *buffer = RL_MALLOC(size);
fread(buffer, size, 1, gltfFile);
fclose(gltfFile);
@ -3259,7 +3260,7 @@ static Model LoadGLTF(const char *fileName)
cgltf_data *data;
cgltf_result result = cgltf_parse(&options, buffer, size, &data);
free(buffer);
RL_FREE(buffer);
if (result == cgltf_result_success)
{
@ -3270,7 +3271,7 @@ static Model LoadGLTF(const char *fileName)
// Process glTF data and map to model
model.meshCount = data->meshes_count;
model.meshes = malloc(model.meshCount*sizeof(Mesh));
model.meshes = RL_MALLOC(model.meshCount*sizeof(Mesh));
for (int i = 0; i < model.meshCount; i++)
{
@ -3282,11 +3283,11 @@ static Model LoadGLTF(const char *fileName)
model.meshes[i].triangleCount = data->meshes[i].primitives_count;
// data.meshes[i].weights not used (array of weights to be applied to the Morph Targets)
model.meshes[i].vertices = malloc(sizeof(float)*model.meshes[i].vertexCount*3); // Default vertex positions
model.meshes[i].normals = malloc(sizeof(float)*model.meshes[i].vertexCount*3); // Default vertex normals
model.meshes[i].texcoords = malloc(sizeof(float)*model.meshes[i].vertexCount*2); // Default vertex texcoords
model.meshes[i].vertices = RL_MALLOC(sizeof(float)*model.meshes[i].vertexCount*3); // Default vertex positions
model.meshes[i].normals = RL_MALLOC(sizeof(float)*model.meshes[i].vertexCount*3); // Default vertex normals
model.meshes[i].texcoords = RL_MALLOC(sizeof(float)*model.meshes[i].vertexCount*2); // Default vertex texcoords
model.meshes[i].indices = malloc(sizeof(unsigned short)*model.meshes[i].triangleCount*3);
model.meshes[i].indices = RL_MALLOC(sizeof(unsigned short)*model.meshes[i].triangleCount*3);
}
}