]> git.xonotic.org Git - xonotic/netradiant.git/blobdiff - libs/picomodel/pm_ms3d.c
Merge commit 'd7763cb7e0d7f6c16cc45335f344385fa6f73ac5' into master-merge
[xonotic/netradiant.git] / libs / picomodel / pm_ms3d.c
index 08dd6f45e0b8752674317bc81eff90ca2d91304a..1472de549e0d128eb31c1152ea965a780acd0557 100644 (file)
 
    ----------------------------------------------------------------------------- */
 
-
-
-/* marker */
-#define PM_MS3D_C
-
 /* dependencies */
 #include "picointernal.h"
+#include "globaldefs.h"
 
 /* disable warnings */
-#ifdef _WIN32
+#if GDEF_COMPILER_MSVC
 #pragma warning( disable:4100 )                /* unref param */
 #endif
 
  #define DEBUG_PM_MS3D
  #define DEBUG_PM_MS3D_EX
 
-/* plain white */
-static picoColor_t white = { 255,255,255,255 };
-
 /* ms3d limits */
-#define MS3D_MAX_VERTS      8192
-#define MS3D_MAX_TRIS       16384
-#define MS3D_MAX_GROUPS     128
-#define MS3D_MAX_MATERIALS  128
-#define MS3D_MAX_JOINTS     128
-#define MS3D_MAX_KEYFRAMES  216
+const int MS3D_MAX_VERTS      = 8192;
+const int MS3D_MAX_TRIS       = 16384;
+const int MS3D_MAX_GROUPS     = 128;
+const int MS3D_MAX_MATERIALS  = 128;
+const int MS3D_MAX_JOINTS     = 128;
+const int MS3D_MAX_KEYFRAMES  = 216;
 
 /* ms3d flags */
-#define MS3D_SELECTED       1
-#define MS3D_HIDDEN         2
-#define MS3D_SELECTED2      4
-#define MS3D_DIRTY          8
+const int MS3D_SELECTED       = 1;
+const int MS3D_HIDDEN         = 2;
+const int MS3D_SELECTED2      = 4;
+const int MS3D_DIRTY          = 8;
 
 /* this freaky loader needs byte alignment */
 #pragma pack(push, 1)
@@ -164,19 +157,16 @@ TMsKeyframe;
  *     validates a milkshape3d model file.
  */
 static int _ms3d_canload( PM_PARAMS_CANLOAD ){
-       TMsHeader *hdr;
-
+       const TMsHeader *hdr;
 
-       /* to keep the compiler happy */
-       *fileName = *fileName;
 
        /* sanity check */
-       if ( bufSize < sizeof( TMsHeader ) ) {
+       if ( (size_t) bufSize < sizeof( TMsHeader ) ) {
                return PICO_PMV_ERROR_SIZE;
        }
 
        /* get ms3d header */
-       hdr = (TMsHeader *)buffer;
+       hdr = (const TMsHeader *)buffer;
 
        /* check ms3d magic */
        if ( strncmp( hdr->magic,"MS3D000000",10 ) != 0 ) {
@@ -206,7 +196,7 @@ static unsigned char *GetWord( unsigned char *bufptr, int *out ){
  */
 static picoModel_t *_ms3d_load( PM_PARAMS_LOAD ){
        picoModel_t    *model;
-       unsigned char  *bufptr;
+       unsigned char  *bufptr, *bufptr0;
        int shaderRefs[ MS3D_MAX_GROUPS ];
        int numGroups;
        int numMaterials;
@@ -228,8 +218,10 @@ static picoModel_t *_ms3d_load( PM_PARAMS_LOAD ){
        PicoSetModelName( model, fileName );
        PicoSetModelFileName( model, fileName );
 
+       bufptr0 = bufptr = (picoByte_t*) _pico_alloc( bufSize );
+       memcpy( bufptr, buffer, bufSize );
        /* skip header */
-       bufptr = (unsigned char *)buffer + sizeof( TMsHeader );
+       bufptr += sizeof( TMsHeader );
 
        /* get number of vertices */
        bufptr = GetWord( bufptr,&numVerts );
@@ -289,6 +281,7 @@ static picoModel_t *_ms3d_load( PM_PARAMS_LOAD ){
                        if ( triangle->vertexIndices[ k ] >= numVerts ) {
                                _pico_printf( PICO_ERROR,"Vertex %d index %d out of range (%d, max %d)",i,k,triangle->vertexIndices[k],numVerts - 1 );
                                PicoFreeModel( model );
+                               _pico_free( bufptr0 );
                                return NULL; /* yuck */
                        }
                }
@@ -321,6 +314,7 @@ static picoModel_t *_ms3d_load( PM_PARAMS_LOAD ){
                surface = PicoNewSurface( model );
                if ( surface == NULL ) {
                        PicoFreeModel( model );
+                       _pico_free( bufptr0 );
                        return NULL;
                }
                /* do surface setup */
@@ -354,7 +348,7 @@ static picoModel_t *_ms3d_load( PM_PARAMS_LOAD ){
                                PicoSetSurfaceXYZ( surface,vertexIndex,vertex->xyz );
 
                                /* store vertex color */
-                               PicoSetSurfaceColor( surface,0,vertexIndex,white );
+                               PicoSetSurfaceColor( surface, 0, vertexIndex, picoColor_white );
 
                                /* store vertex normal */
                                PicoSetSurfaceNormal( surface,vertexIndex,triangle->vertexNormals[ m ] );
@@ -413,6 +407,7 @@ static picoModel_t *_ms3d_load( PM_PARAMS_LOAD ){
                shader = PicoNewShader( model );
                if ( shader == NULL ) {
                        PicoFreeModel( model );
+                       _pico_free( bufptr0 );
                        return NULL;
                }
                /* scale shader colors */
@@ -476,6 +471,7 @@ static picoModel_t *_ms3d_load( PM_PARAMS_LOAD ){
 #endif
        }
        /* return allocated pico model */
+       _pico_free( bufptr0 );
        return model;
 //     return NULL;
 }