]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - libs/picomodel/pm_md2.c
Merge commit '515673c08f8718a237e90c2130a1f5294f966d6a'
[xonotic/netradiant.git] / libs / picomodel / pm_md2.c
index 8a603a5d0fa09b596993b0e6013f8278c838b757..19b5689ccd070895411c970f70efd3146c2a359f 100644 (file)
@@ -300,20 +300,17 @@ float     md2_normals[ MD2_NUMVERTEXNORMALS ][ 3 ] =
 
 static int _md2_canload( PM_PARAMS_CANLOAD )
 {
-       md2_t   *md2;
-
-       /* to keep the compiler happy */
-       *fileName = *fileName;
+       const md2_t     *md2;
 
        /* sanity check */
-       if( bufSize < ( sizeof( *md2 ) * 2) )
+       if( (size_t) bufSize < ( sizeof( *md2 ) * 2) )
                return PICO_PMV_ERROR_SIZE;
        
        /* set as md2 */
-       md2 = (md2_t*) buffer;
+       md2 = (const md2_t*) buffer;
        
        /* check md2 magic */
-       if( *((int*) md2->magic) != *((int*) MD2_MAGIC) ) 
+       if( *((const int*) md2->magic) != *((const int*) MD2_MAGIC) ) 
                return PICO_PMV_ERROR_IDENT;
        
        /* check md2 version */
@@ -332,7 +329,6 @@ static int _md2_canload( PM_PARAMS_CANLOAD )
 static picoModel_t *_md2_load( PM_PARAMS_LOAD )
 {
        int                             i, j, dups, dup_index;
-       short                   tot_numVerts;
        index_LUT_t             *p_index_LUT, *p_index_LUT2, *p_index_LUT3;
        index_DUP_LUT_t *p_index_LUT_DUPS;
        md2Triangle_t   *p_md2Triangle;
@@ -344,7 +340,7 @@ static picoModel_t *_md2_load( PM_PARAMS_LOAD )
        md2Triangle_t   *triangle;
        md2XyzNormal_t  *vertex;
 
-       picoByte_t      *bb;
+       picoByte_t      *bb, *bb0;
        picoModel_t             *picoModel;
        picoSurface_t   *picoSurface;
        picoShader_t    *picoShader;
@@ -353,18 +349,17 @@ static picoModel_t *_md2_load( PM_PARAMS_LOAD )
        picoColor_t             color;
        
 
-       // md2 loading
-       _pico_printf( PICO_NORMAL, "Loading \"%s\"", fileName );
-
        /* set as md2 */
-       bb = (picoByte_t*) buffer;
-       md2     = (md2_t*) buffer;
+       bb0 = bb = (picoByte_t*) _pico_alloc(bufSize);
+       memcpy(bb, buffer, bufSize);
+       md2     = (md2_t*) bb;
 
        /* check ident and version */
-       if( *((int*) md2->magic) != *((int*) MD2_MAGIC) || _pico_little_long( md2->version ) != MD2_VERSION )
+       if( *((const int*) md2->magic) != *((const int*) MD2_MAGIC) || _pico_little_long( md2->version ) != MD2_VERSION )
        {
                /* not an md2 file (todo: set error) */
                _pico_printf( PICO_ERROR, "%s is not an MD2 File!", fileName );
+               _pico_free(bb0);
                return NULL;
        }
        
@@ -393,12 +388,14 @@ static picoModel_t *_md2_load( PM_PARAMS_LOAD )
        if( md2->numFrames < 1 )
        {
                _pico_printf( PICO_ERROR, "%s has 0 frames!", fileName );
+               _pico_free(bb0);
                return NULL;
        }
        
        if( frameNum < 0 || frameNum >= md2->numFrames )
        {
                _pico_printf( PICO_ERROR, "Invalid or out-of-range MD2 frame specified" );
+               _pico_free(bb0);
                return NULL;
        }
 
@@ -432,7 +429,7 @@ static picoModel_t *_md2_load( PM_PARAMS_LOAD )
        }
 
        // set Skin Name
-       strncpy(skinname, (bb + md2->ofsSkins), MD2_MAX_SKINNAME );
+       strncpy(skinname, (const char *) (bb + md2->ofsSkins), MD2_MAX_SKINNAME );
 
        // Print out md2 values
        _pico_printf(PICO_VERBOSE,"Skins: %d  Verts: %d  STs: %d  Triangles: %d  Frames: %d\nSkin Name \"%s\"\n", md2->numSkins, md2->numXYZ, md2->numST, md2->numTris, md2->numFrames, &skinname );
@@ -446,6 +443,7 @@ static picoModel_t *_md2_load( PM_PARAMS_LOAD )
        if( picoModel == NULL )
        {
                _pico_printf( PICO_ERROR, "Unable to allocate a new model" );
+               _pico_free(bb0);
                return NULL;
        }
 
@@ -461,6 +459,7 @@ static picoModel_t *_md2_load( PM_PARAMS_LOAD )
        {
                _pico_printf( PICO_ERROR, "Unable to allocate a new model surface" );
                PicoFreeModel( picoModel );
+               _pico_free(bb0);
                return NULL;
        }
 
@@ -472,6 +471,7 @@ static picoModel_t *_md2_load( PM_PARAMS_LOAD )
        {
                _pico_printf( PICO_ERROR, "Unable to allocate a new model shader" );
                PicoFreeModel( picoModel );
+               _pico_free(bb0);
                return NULL;
        }
 
@@ -490,7 +490,6 @@ static picoModel_t *_md2_load( PM_PARAMS_LOAD )
        }
 
        // Fill in Look Up Table, and allocate/fill Linked List from vert array as needed for dup STs per Vert.
-       tot_numVerts = md2->numXYZ;
        dups = 0;
        for(i=0; i<md2->numTris; i++)
        {
@@ -647,6 +646,7 @@ static picoModel_t *_md2_load( PM_PARAMS_LOAD )
        _pico_free(p_index_LUT_DUPS);
 
        /* return the new pico model */
+       _pico_free(bb0);
        return picoModel;
 
 }