home *** CD-ROM | disk | FTP | other *** search
/ C++ Games Programming / CPPGAMES.ISO / thx / source / theatrix / media.cpp < prev    next >
C/C++ Source or Header  |  1995-04-26  |  4KB  |  149 lines

  1. #include <fstream.h>
  2. #include <string.h>
  3. #include <dir.h>
  4. #include <io.h>
  5. #include <values.h>
  6. #include <iomanip.h>
  7. #include "media.h"
  8. #include "theatrix.h"
  9.  
  10. short int Media::use_xms = 1;
  11. MediaLib Media::libraries[MAXFXLIBS];
  12. short int Media::libcount = 0;
  13.  
  14. MediaClip::MediaClip()
  15. {
  16.     w = h = 0;
  17.     size = 0;
  18.     buf = 0;
  19.     xoffset = 0;
  20. }
  21.  
  22. MediaLib::MediaLib()
  23. {
  24.     memset(name, 0, sizeof(name));
  25.     clipcount = 0;
  26.     clip = 0;
  27.     xms_handle = -1;
  28. }
  29.  
  30. MediaLib::~MediaLib()
  31. {
  32.     if (xms_handle != -1)    {
  33.         xms_free(xms_handle);
  34.         xms_handle = -1;
  35.     }
  36.     else
  37.         for (int i = 0; i < clipcount; i++)
  38.             delete [] clip[i].buf;
  39.     delete [] clip;
  40. }
  41.  
  42. Media::Media()
  43. {
  44.     bufsize = 0;
  45.     buffer = 0;
  46. }
  47.  
  48. Media::~Media()
  49. {
  50.     delete [] buffer;
  51. }
  52.  
  53. void Media::load_library(char *libfilename)
  54. {
  55.     // -- first make sure it's not already loaded
  56.     for (int i = 0; i < libcount; i++)
  57.         if (stricmp(libfilename, libraries[i].name) == 0)
  58.             return;
  59.     // --- make sure the file exists
  60.     if (access(libfilename, 0) != 0)
  61.         Theatrix::fatal(libfilename);
  62.     // --- make sure there is a library slot available
  63.     if (libcount == MAXFXLIBS)
  64.         Theatrix::fatal("Too many media libraries");
  65.  
  66.     MediaLib& lib = libraries[libcount++];
  67.     // --- see is xms is desired and available
  68.     if (xms_present() && use_xms)    {
  69.         struct ffblk ff;
  70.         findfirst(libfilename, &ff, 0);
  71.         unsigned xblk = (unsigned) ((ff.ff_fsize+1023)/1024);
  72.         if (xms_available() >= xblk)
  73.             lib.xms_handle = xms_allocate(xblk);
  74.     }
  75.  
  76.     ifstream libfile(libfilename,ios::binary);
  77.     libfile.read((char*)&lib.clipcount,sizeof(lib.clipcount));
  78.     lib.clip=new MediaClip[lib.clipcount];
  79.  
  80.     // ---- read and store all the clips
  81.     long xms_offset = 0;
  82.     long total = 0;
  83.     for (i = 0; i < lib.clipcount; i++)    {
  84.         MediaClip& clp = lib.clip[i];
  85.         if (hasdimensions())    {
  86.             // --- this is a graphic clip with height and width
  87.             libfile.read((char*)&clp.w,sizeof(short int));
  88.             libfile.read((char*)&clp.h,sizeof(short int));
  89.         }
  90.         // --- read the clip size
  91.         libfile.read((char*)&clp.size,sizeof(long));
  92.         total += clp.size;
  93.         // --- round it up to even number for xms
  94.         unsigned int sz = (unsigned int)((clp.size+1)&~1);
  95.         // ---- allocate a buffer to read the clip
  96.         clp.buf = new char[(size_t)sz];
  97.         memset(clp.buf, 0, sz);
  98.         // ---- read the clip
  99.         if (clp.size<=(long)MAXINT)
  100.             libfile.read(clp.buf,(int)(clp.size));
  101.         else    {
  102.             libfile.read(clp.buf,(int)MAXINT);
  103.             libfile.read(clp.buf+MAXINT,
  104.                         (int)(clp.size-(long)MAXINT));
  105.         }
  106.         if (lib.xms_handle != -1)    {
  107.             clp.xoffset = xms_offset;
  108.             copy_convtoxms(lib.xms_handle, clp.buf, xms_offset, sz);
  109.             xms_offset += sz;
  110.             // ---- remember the largest clip buffer size 
  111.             //      for the xms transfer buffer
  112.             if (bufsize < sz)
  113.                 bufsize = sz;
  114.             delete clp.buf;
  115.             clp.buf = 0;
  116.         }
  117.     }
  118.     strcpy(lib.name,libfilename);
  119.     cout << setw(12) << lib.name << ": "
  120.          << setw(3)  << lib.clipcount << " clips, "
  121.          << setw(6)  << total << " bytes";
  122.     if (lib.xms_handle != -1)
  123.         cout << " (XMS)";
  124.     cout << endl;
  125. }
  126.  
  127. MediaClip& Media::getclip(int library, int clp)
  128. {
  129.     MediaLib& lib = libraries[library];
  130.     MediaClip& cl = lib.clip[clp];
  131.     unsigned sz = (unsigned)((cl.size+1)&~1);
  132.     if (lib.xms_handle != -1)    {
  133.         if (buffer == 0)
  134.             buffer = new char[bufsize];
  135.         copy_xmstoconv(lib.xms_handle,buffer,cl.xoffset,sz);
  136.         cl.buf = buffer;
  137.     }
  138.     return cl;
  139. }
  140.  
  141. short int Media::library_number(char *fname)
  142. {
  143.     for (short int i = 0; i < libcount; i++)
  144.         if (stricmp(fname, libraries[i].name) == 0)
  145.             break;
  146.     return i;
  147. }
  148.  
  149.