Advertisement
KeinMitleid

Doom WAD Reader

Nov 27th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.36 KB | None | 0 0
  1. // WAD reference: http://doom.wikia.com/wiki/WAD
  2. // DOOM WAD code: code.metager.de/source/xref/idsoftware/doom/w_wad.c
  3.  
  4. import std.c.windows.windows;
  5. import std.stdio;
  6.  
  7. struct wadinfo_t
  8. {
  9.     char[4] identification;
  10.     int     numlumps;
  11.     int     infotableofs;
  12. }
  13.  
  14. struct filelump_t
  15. {
  16.     int     filepos;
  17.     int     size;
  18.     char[8] name;
  19. }
  20.  
  21. // Integers little-endianize themselves?
  22. void main()
  23. {
  24.     // Get file.
  25.     // rawRead insists on using arrays, thus the "[1])[0]" hack.
  26.     auto wad = File("doom.wad", "r");
  27.     auto info = wad.rawRead(new wadinfo_t[1])[0];
  28.  
  29.     // Get lumps.
  30.     // Keeping the name member in filelump_t keeps the code elegant.
  31.     // Associative arrays may not be a good solution if lump order is a concern.
  32.     wad.seek(info.infotableofs);
  33.     filelump_t[char[8]] lumps;
  34.     {
  35.     auto temp = wad.rawRead(new filelump_t[info.numlumps]);
  36.     foreach (lump; temp) lumps[lump.name] = lump;
  37.     }
  38.  
  39.     // Get ENDOOM lump.
  40.     wad.seek(lumps["ENDOOM"].filepos);
  41.     auto endoom = wad.rawRead(new char[lumps["ENDOOM"].size]);
  42.  
  43.     // Output ENDOOM.
  44.     // printf() is used because it works with Windows functions.
  45.     auto console = GetStdHandle(STD_OUTPUT_HANDLE);
  46.     for (int i = 0; i < endoom.length; i += 2)
  47.     {
  48.         SetConsoleTextAttribute(console, endoom[i + 1]);
  49.         printf("%.*s", endoom[i] ~ "");
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement