wad.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // wad.c
  16. #include "quakedef.h"
  17. int wad_numlumps;
  18. lumpinfo_t *wad_lumps;
  19. byte *wad_base;
  20. void SwapPic (qpic_t *pic);
  21. /*
  22. ==================
  23. W_CleanupName
  24. Lowercases name and pads with spaces and a terminating 0 to the length of
  25. lumpinfo_t->name.
  26. Used so lumpname lookups can proceed rapidly by comparing 4 chars at a time
  27. Space padding is so names can be printed nicely in tables.
  28. Can safely be performed in place.
  29. ==================
  30. */
  31. void W_CleanupName (char *in, char *out)
  32. {
  33. int i;
  34. int c;
  35. for (i=0 ; i<16 ; i++ )
  36. {
  37. c = in[i];
  38. if (!c)
  39. break;
  40. if (c >= 'A' && c <= 'Z')
  41. c += ('a' - 'A');
  42. out[i] = c;
  43. }
  44. for ( ; i< 16 ; i++ )
  45. out[i] = 0;
  46. }
  47. /*
  48. ====================
  49. W_LoadWadFile
  50. ====================
  51. */
  52. void W_LoadWadFile (char *filename)
  53. {
  54. lumpinfo_t *lump_p;
  55. wadinfo_t *header;
  56. unsigned i;
  57. int infotableofs;
  58. wad_base = COM_LoadHunkFile (filename);
  59. if (!wad_base)
  60. Sys_Error ("W_LoadWadFile: couldn't load %s", filename);
  61. header = (wadinfo_t *)wad_base;
  62. if (header->identification[0] != 'W'
  63. || header->identification[1] != 'A'
  64. || header->identification[2] != 'D'
  65. || header->identification[3] != '2')
  66. Sys_Error ("Wad file %s doesn't have WAD2 id\n",filename);
  67. wad_numlumps = LittleLong(header->numlumps);
  68. infotableofs = LittleLong(header->infotableofs);
  69. wad_lumps = (lumpinfo_t *)(wad_base + infotableofs);
  70. for (i=0, lump_p = wad_lumps ; i<wad_numlumps ; i++,lump_p++)
  71. {
  72. lump_p->filepos = LittleLong(lump_p->filepos);
  73. lump_p->size = LittleLong(lump_p->size);
  74. W_CleanupName (lump_p->name, lump_p->name);
  75. if (lump_p->type == TYP_QPIC)
  76. SwapPic ( (qpic_t *)(wad_base + lump_p->filepos));
  77. }
  78. }
  79. /*
  80. =============
  81. W_GetLumpinfo
  82. =============
  83. */
  84. lumpinfo_t *W_GetLumpinfo (char *name)
  85. {
  86. int i;
  87. lumpinfo_t *lump_p;
  88. char clean[16];
  89. W_CleanupName (name, clean);
  90. for (lump_p=wad_lumps, i=0 ; i<wad_numlumps ; i++,lump_p++)
  91. {
  92. if (!strcmp(clean, lump_p->name))
  93. return lump_p;
  94. }
  95. Sys_Error ("W_GetLumpinfo: %s not found", name);
  96. return NULL;
  97. }
  98. void *W_GetLumpName (char *name)
  99. {
  100. lumpinfo_t *lump;
  101. lump = W_GetLumpinfo (name);
  102. return (void *)(wad_base + lump->filepos);
  103. }
  104. void *W_GetLumpNum (int num)
  105. {
  106. lumpinfo_t *lump;
  107. if (num < 0 || num > wad_numlumps)
  108. Sys_Error ("W_GetLumpNum: bad number: %i", num);
  109. lump = wad_lumps + num;
  110. return (void *)(wad_base + lump->filepos);
  111. }
  112. /*
  113. =============================================================================
  114. automatic byte swapping
  115. =============================================================================
  116. */
  117. void SwapPic (qpic_t *pic)
  118. {
  119. pic->width = LittleLong(pic->width);
  120. pic->height = LittleLong(pic->height);
  121. }