Skip to content

Commit dc1bd55

Browse files
authored
Avoid qsort() in rmodels.c
1 parent ea82291 commit dc1bd55

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/rmodels.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5567,15 +5567,6 @@ static Model LoadVOX(const char *fileName)
55675567
unsigned char *m3d_loaderhook(char *fn, unsigned int *len) { return LoadFileData((const char *)fn, (int *)len); }
55685568
void m3d_freehook(void *data) { UnloadFileData((unsigned char *)data); }
55695569

5570-
// Comparison function for qsort
5571-
static int m3d_compare_faces(const void *a, const void *b)
5572-
{
5573-
m3df_t *fa = (m3df_t *)a;
5574-
m3df_t *fb = (m3df_t *)b;
5575-
5576-
return (fa->materialid - fb->materialid);
5577-
}
5578-
55795570
// Load M3D mesh data
55805571
static Model LoadM3D(const char *fileName)
55815572
{
@@ -5623,8 +5614,19 @@ static Model LoadM3D(const char *fileName)
56235614
// We always need a default material, so we add +1
56245615
model.materialCount++;
56255616

5626-
// Sort faces by material.
5627-
qsort(m3d->face, m3d->numface, sizeof(m3df_t), m3d_compare_faces);
5617+
// Sort faces by material (insertion)
5618+
for (i = 1; i < m3d->numface; i++)
5619+
{
5620+
m3df_t key = m3d->face[i];
5621+
j = i - 1;
5622+
5623+
while (j >= 0 && m3d->face[j].materialid > key.materialid)
5624+
{
5625+
m3d->face[j+1] = m3d->face[j];
5626+
j = j - 1;
5627+
}
5628+
m3d->face[j+1] = key;
5629+
}
56285630

56295631
model.meshes = (Mesh *)RL_CALLOC(model.meshCount, sizeof(Mesh));
56305632
model.meshMaterial = (int *)RL_CALLOC(model.meshCount, sizeof(int));

0 commit comments

Comments
 (0)