Hey guys I'm new here. I have been programming for a number of years and my dream is to be a game programmer. I have most of my engine done (graphics, sound and input core are done). My afore mentioned custom file type is for containing a sprite (with all it's animations and attributes like cell height, width, number of frames, etc). I am able to write the file just fine, but when it comes to reading it, I am at my wits end. Someone out there help me =D. First I will show you the functions in question.
Code:
HRESULT Sprite::WriteSprFile(char *newSprFileName)
{
FILE *fp;
if((fp = fopen(newSprFileName, "wb"))==NULL)
{
out3<<"File open failed!"<<endl;
return E_FAIL;
}
out3<<"File created"<<endl;
Bitmap bitmap;
bitmap.Load(bitmapFileName);
SpriteHeader sh;
AnimationStruct as[200];
sh.cellHeight = cellHeight;
sh.cellWidth = cellWidth;
sh.nAnimations = nAnimations;
sh.nFrames = nFrames;
sh.bitDepth = bitmap.returnBitDepth();
sh.imageSize = bitmap.returnSizeInBytes();
out3<<"Sprite Header filled"<<endl;
fwrite(&sh, sizeof(SpriteHeader), 1, fp);
out3<<"Sprite Header written"<<endl;
out3<<"Writing "<<nAnimations<<" animations to file"<<endl;
for(int i=0; i<nAnimations; i++)
{
out3<<"Now writing animation: "<<animations[i]->animationName<<endl;
memcpy(&as[i].animationName, animations[i]->animationName, 20);
as[i].endFrame = animations[i]->endFrame;
as[i].loopType = animations[i]->loopType;
as[i].speed = animations[i]->speed;
as[i].startFrame = animations[i]->startFrame;
}
fwrite(&as, sizeof(AnimationStruct), nAnimations, fp);
out3<<"Size of image: "<<sh.imageSize<<" bytes"<<endl;
fwrite(bitmap.returnBuffer(), sizeof(UCHAR), sh.imageSize, fp);
out3<<"Entire image written to file successfully"<<endl;
bitmap.Unload();
fclose(fp);
return 1;
}
This is the function that writes the file from a Sprite that is already preinitialized with all the proper values. It is initialized with a call to an overloaded function that takes a sprite descriptor file (.spi which I wrote, and works already) and a bitmap image. The function above takes the sprite loaded in this manner and then writes it out to a file (.spr) that contains all the sprite information and the image data (which is loaded using a bitmap class I wrote earlier). In anycase initializing the sprite with the bitmap and .spi file works. And here is the function that reads the .spr file:
Code:
HRESULT Sprite::LoadFromSpr(char *fname)
{
FILE *fp;
if((fp = fopen(fname, "rb"))==NULL)
return E_FAIL;
out3<<"File opened"<<endl;
SpriteHeader sh;
AnimationStruct as[200];
UINT imageSize;
UCHAR *buffer;
ZeroMemory(&sh, sizeof(SpriteHeader));
fread(&sh, sizeof(SpriteHeader), 1, fp);
out3<<"Sprite Header read in"<<endl;
out3<<"Animations: "<<sh.nAnimations<<endl<<"Frames: "<<sh.nFrames<<endl<<"Cell Height: "<<sh.cellHeight<<endl<<"Cell Width: "<<sh.cellWidth<<endl<<"Bit Depth: "<<sh.bitDepth<<endl<<"Image Size: "<<sh.imageSize<<endl;
nAnimations = sh.nAnimations;
nFrames = sh.nFrames;
cellHeight = sh.cellHeight;
cellWidth = sh.cellWidth;
ZeroMemory(&as, sizeof(AnimationStruct)*nAnimations);
fread(&as, sizeof(AnimationStruct), nAnimations, fp);
out3<<"Loaded animations: "<<endl;
for(int i=0; i<sh.nAnimations; i++)
{
Animation *anim = new Animation(as[i].animationName, as[i].startFrame, as[i].endFrame, ((float)as[i].speed)/10, as[i].loopType);
out3<<"Animation: "<<as[i].animationName<<endl;
animations.addToEnd(anim);
}
buffer = (UCHAR *)malloc(sh.imageSize);
out3<<"Buffer memory allocated!"<<endl;
fseek(fp, -(int)sh.imageSize, SEEK_SET);
fread(buffer, sizeof(UCHAR), sh.imageSize, fp);
out3<<"Image buffered! File load complete!"<<endl;
out3<<"File Contents: "<<buffer<<endl;
fclose(fp);
image.Initialize(buffer, cellWidth*nFrames, cellHeight, sh.bitDepth);
return 1;
}
This function works as far as reading in the sprite header an all of the animation data, but it won't read the image data in properly. However I have tested just writing the image data to the spr file (without the animation data), and then reading only the image data and then it works fine. But somehow it's failing to read the proper amount of data or something...I really don't know. And a few notes: don't worry about the last line before return 1, that is just how it initializes it's direct draw surface (and that works fine). Someone please tell me what I'm doing wrong here. And for reference if you need it, here are the two structs that hold the information being read/written to the file:
Code:
typedef struct SpriteHeader
{
unsigned int nFrames;
unsigned int nAnimations;
unsigned int cellWidth;
unsigned int cellHeight;
unsigned int bitDepth;
unsigned int imageSize;
}SpriteHeader;
typedef struct AnimationStruct
{
char animationName[20];
unsigned int startFrame;
unsigned int endFrame;
unsigned float speed;
unsigned int loopType;
}AnimationStruct;
Anyone who can help me will make my week...after this I can move on with the project and tackle other more important things.