- Main Page
- Data Structures
- Files

- File List

# E:/DevIL/src-IL/src/il_mdl.c

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Sources
    00004 // Copyright (C) 2000-2002 by Denton Woods
    00005 // Last modified: 05/25/2001 <--Y2K Compliant! =]
    00006 //
    00007 // Filename: src-IL/src/il_mdl.c
    00008 //
    00009 // Description: Reads a Half-Life model file (.mdl).
    00010 //
    00011 //-----------------------------------------------------------------------------
    00012
    00013
    00014 #include "il_internal.h"
    00015 #ifndef IL_NO_MDL
    00016 #include "il_mdl.h"
    00017
    00018
    00019 ILboolean iLoadMdlInternal(void);
    00020 ILboolean iIsValidMdl(void);
    00021
    00023 ILboolean ilIsValidMdl(ILconst_string FileName)
    00024 {
    00025         ILHANDLE        MdlFile;
    00026         ILboolean       bMdl = IL_FALSE;
    00027
    00028         if (!iCheckExtension(FileName, IL_TEXT("mdl"))) {
    00029                 ilSetError(IL_INVALID_EXTENSION);
    00030                 return bMdl;
    00031         }
    00032
    00033         MdlFile = iopenr(FileName);
    00034         if (MdlFile == NULL) {
    00035                 ilSetError(IL_COULD_NOT_OPEN_FILE);
    00036                 return bMdl;
    00037         }
    00038
    00039         bMdl = ilIsValidMdlF(MdlFile);
    00040         icloser(MdlFile);
    00041
    00042         return bMdl;
    00043 }
    00044
    00045
    00047 ILboolean ilIsValidMdlF(ILHANDLE File)
    00048 {
    00049         ILuint          FirstPos;
    00050         ILboolean       bRet;
    00051
    00052         iSetInputFile(File);
    00053         FirstPos = itell();
    00054         bRet = iIsValidMdl();
    00055         iseek(FirstPos, IL_SEEK_SET);
    00056
    00057         return bRet;
    00058 }
    00059
    00060
    00062 ILboolean ilIsValidMdlL(const void *Lump, ILuint Size)
    00063 {
    00064         iSetInputLump(Lump, Size);
    00065         return iIsValidMdl();
    00066 }
    00067
    00068
    00069 // Internal function to get the header and check it.
    00070 ILboolean iIsValidMdl(void)
    00071 {
    00072         ILuint Id, Version;
    00073
    00074         Id = GetLittleUInt();
    00075         Version = GetLittleUInt();
    00076         iseek(-8, IL_SEEK_CUR);  // Restore to previous position.
    00077
    00078         // 0x54534449 == "IDST"
    00079         if (Id != 0x54534449 || Version != 10)
    00080                 return IL_FALSE;
    00081         return IL_TRUE;
    00082 }
    00083
    00084
    00086 ILboolean ilLoadMdl(ILconst_string FileName)
    00087 {
    00088         ILHANDLE        MdlFile;
    00089         ILboolean       bMdl = IL_FALSE;
    00090
    00091         MdlFile = iopenr(FileName);
    00092         if (MdlFile == NULL) {
    00093                 ilSetError(IL_COULD_NOT_OPEN_FILE);
    00094                 return bMdl;
    00095         }
    00096
    00097         bMdl = ilLoadMdlF(MdlFile);
    00098         icloser(MdlFile);
    00099
    00100         return bMdl;
    00101 }
    00102
    00103
    00105 ILboolean ilLoadMdlF(ILHANDLE File)
    00106 {
    00107         ILuint          FirstPos;
    00108         ILboolean       bRet;
    00109
    00110         iSetInputFile(File);
    00111         FirstPos = itell();
    00112         bRet = iLoadMdlInternal();
    00113         iseek(FirstPos, IL_SEEK_SET);
    00114
    00115         return bRet;
    00116 }
    00117
    00118
    00120 ILboolean ilLoadMdlL(const void *Lump, ILuint Size)
    00121 {
    00122         iSetInputLump(Lump, Size);
    00123         return iLoadMdlInternal();
    00124 }
    00125
    00126
    00127 ILboolean iLoadMdlInternal()
    00128 {
    00129         ILuint          Id, Version, NumTex, TexOff, TexDataOff, Position, ImageNum;
    00130         ILubyte         *TempPal;
    00131         TEX_HEAD        TexHead;
    00132         ILimage         *BaseImage=NULL;
    00133         ILboolean       BaseCreated = IL_FALSE;
    00134
    00135         if (iCurImage == NULL) {
    00136                 ilSetError(IL_ILLEGAL_OPERATION);
    00137                 return IL_FALSE;
    00138         }
    00139
    00140         Id = GetLittleUInt();
    00141         Version = GetLittleUInt();
    00142
    00143         // 0x54534449 == "IDST"
    00144         if (Id != 0x54534449 || Version != 10) {
    00145                 ilSetError(IL_INVALID_FILE_HEADER);
    00146                 return IL_FALSE;
    00147         }
    00148
    00149         // Skips the actual model header.
    00150         iseek(172, IL_SEEK_CUR);
    00151
    00152         NumTex = GetLittleUInt();
    00153         TexOff = GetLittleUInt();
    00154         TexDataOff = GetLittleUInt();
    00155
    00156         if (NumTex == 0 || TexOff == 0 || TexDataOff == 0) {
    00157                 ilSetError(IL_ILLEGAL_FILE_VALUE);
    00158                 return IL_FALSE;
    00159         }
    00160
    00161         iseek(TexOff, IL_SEEK_SET);
    00162
    00163         for (ImageNum = 0; ImageNum < NumTex; ImageNum++) {
    00164                 if (iread(TexHead.Name, 1, 64) != 64)
    00165                         return IL_FALSE;
    00166                 TexHead.Flags = GetLittleUInt();
    00167                 TexHead.Width = GetLittleUInt();
    00168                 TexHead.Height = GetLittleUInt();
    00169                 TexHead.Offset = GetLittleUInt();
    00170                 Position = itell();
    00171
    00172                 if (TexHead.Offset == 0) {
    00173                         ilSetError(IL_ILLEGAL_FILE_VALUE);
    00174                         return IL_FALSE;
    00175                 }
    00176
    00177                 if (!BaseCreated) {
    00178                         ilTexImage(TexHead.Width, TexHead.Height, 1, 1, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE, NULL);
    00179                         iCurImage->Origin = IL_ORIGIN_LOWER_LEFT;
    00180                         BaseCreated = IL_TRUE;
    00181                         BaseImage = iCurImage;
    00182                         //iCurImage->NumNext = NumTex - 1;  // Don't count the first image.
    00183                 }
    00184                 else {
    00185                         //iCurImage->Next = ilNewImage(TexHead.Width, TexHead.Height, 1, 1, 1);
    00186                         iCurImage = iCurImage->Next;
    00187                         iCurImage->Format = IL_COLOUR_INDEX;
    00188                         iCurImage->Type = IL_UNSIGNED_BYTE;
    00189                 }
    00190
    00191                 TempPal = (ILubyte*)ialloc(768);
    00192                 if (TempPal == NULL) {
    00193                         iCurImage = BaseImage;
    00194                         return IL_FALSE;
    00195                 }
    00196                 iCurImage->Pal.Palette = TempPal;
    00197                 iCurImage->Pal.PalSize = 768;
    00198                 iCurImage->Pal.PalType = IL_PAL_RGB24;
    00199
    00200                 iseek(TexHead.Offset, IL_SEEK_SET);
    00201                 if (iread(iCurImage->Data, TexHead.Width * TexHead.Height, 1) != 1)
    00202                         return IL_FALSE;
    00203                 if (iread(iCurImage->Pal.Palette, 1, 768) != 768)
    00204                         return IL_FALSE;
    00205
    00206                 if (ilGetBoolean(IL_CONV_PAL) == IL_TRUE) {
    00207                         ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
    00208                 }
    00209
    00210                 iseek(Position, IL_SEEK_SET);
    00211         }
    00212
    00213         iCurImage = BaseImage;
    00214
    00215         ilFixImage();
    00216
    00217         return IL_TRUE;
    00218 }
    00219
    00220 #endif//IL_NO_MDL