home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C++ Games Programming
/
CPPGAMES.ISO
/
thx
/
source
/
theatrix
/
media.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1995-04-26
|
4KB
|
149 lines
#include <fstream.h>
#include <string.h>
#include <dir.h>
#include <io.h>
#include <values.h>
#include <iomanip.h>
#include "media.h"
#include "theatrix.h"
short int Media::use_xms = 1;
MediaLib Media::libraries[MAXFXLIBS];
short int Media::libcount = 0;
MediaClip::MediaClip()
{
w = h = 0;
size = 0;
buf = 0;
xoffset = 0;
}
MediaLib::MediaLib()
{
memset(name, 0, sizeof(name));
clipcount = 0;
clip = 0;
xms_handle = -1;
}
MediaLib::~MediaLib()
{
if (xms_handle != -1) {
xms_free(xms_handle);
xms_handle = -1;
}
else
for (int i = 0; i < clipcount; i++)
delete [] clip[i].buf;
delete [] clip;
}
Media::Media()
{
bufsize = 0;
buffer = 0;
}
Media::~Media()
{
delete [] buffer;
}
void Media::load_library(char *libfilename)
{
// -- first make sure it's not already loaded
for (int i = 0; i < libcount; i++)
if (stricmp(libfilename, libraries[i].name) == 0)
return;
// --- make sure the file exists
if (access(libfilename, 0) != 0)
Theatrix::fatal(libfilename);
// --- make sure there is a library slot available
if (libcount == MAXFXLIBS)
Theatrix::fatal("Too many media libraries");
MediaLib& lib = libraries[libcount++];
// --- see is xms is desired and available
if (xms_present() && use_xms) {
struct ffblk ff;
findfirst(libfilename, &ff, 0);
unsigned xblk = (unsigned) ((ff.ff_fsize+1023)/1024);
if (xms_available() >= xblk)
lib.xms_handle = xms_allocate(xblk);
}
ifstream libfile(libfilename,ios::binary);
libfile.read((char*)&lib.clipcount,sizeof(lib.clipcount));
lib.clip=new MediaClip[lib.clipcount];
// ---- read and store all the clips
long xms_offset = 0;
long total = 0;
for (i = 0; i < lib.clipcount; i++) {
MediaClip& clp = lib.clip[i];
if (hasdimensions()) {
// --- this is a graphic clip with height and width
libfile.read((char*)&clp.w,sizeof(short int));
libfile.read((char*)&clp.h,sizeof(short int));
}
// --- read the clip size
libfile.read((char*)&clp.size,sizeof(long));
total += clp.size;
// --- round it up to even number for xms
unsigned int sz = (unsigned int)((clp.size+1)&~1);
// ---- allocate a buffer to read the clip
clp.buf = new char[(size_t)sz];
memset(clp.buf, 0, sz);
// ---- read the clip
if (clp.size<=(long)MAXINT)
libfile.read(clp.buf,(int)(clp.size));
else {
libfile.read(clp.buf,(int)MAXINT);
libfile.read(clp.buf+MAXINT,
(int)(clp.size-(long)MAXINT));
}
if (lib.xms_handle != -1) {
clp.xoffset = xms_offset;
copy_convtoxms(lib.xms_handle, clp.buf, xms_offset, sz);
xms_offset += sz;
// ---- remember the largest clip buffer size
// for the xms transfer buffer
if (bufsize < sz)
bufsize = sz;
delete clp.buf;
clp.buf = 0;
}
}
strcpy(lib.name,libfilename);
cout << setw(12) << lib.name << ": "
<< setw(3) << lib.clipcount << " clips, "
<< setw(6) << total << " bytes";
if (lib.xms_handle != -1)
cout << " (XMS)";
cout << endl;
}
MediaClip& Media::getclip(int library, int clp)
{
MediaLib& lib = libraries[library];
MediaClip& cl = lib.clip[clp];
unsigned sz = (unsigned)((cl.size+1)&~1);
if (lib.xms_handle != -1) {
if (buffer == 0)
buffer = new char[bufsize];
copy_xmstoconv(lib.xms_handle,buffer,cl.xoffset,sz);
cl.buf = buffer;
}
return cl;
}
short int Media::library_number(char *fname)
{
for (short int i = 0; i < libcount; i++)
if (stricmp(fname, libraries[i].name) == 0)
break;
return i;
}