Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-11-2007, 06:18 AM
deusprogrammer's Avatar   
deusprogrammer deusprogrammer is offline
Newbie
 
Join Date: Mar 2007
Posts: 4
Rep Power: 0
deusprogrammer is on a distinguished road
Send a message via AIM to deusprogrammer Send a message via MSN to deusprogrammer Send a message via Yahoo to deusprogrammer
Default Creating a Custom File Type...Need Help

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.

Last edited by deusprogrammer; 03-11-2007 at 06:43 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 03-12-2007, 12:43 AM
deusprogrammer's Avatar   
deusprogrammer deusprogrammer is offline
Newbie
 
Join Date: Mar 2007
Posts: 4
Rep Power: 0
deusprogrammer is on a distinguished road
Send a message via AIM to deusprogrammer Send a message via MSN to deusprogrammer Send a message via Yahoo to deusprogrammer
Default

Please help me ;_; . This problem is driving me mad.
__________________

SUPER MARIO SUNSHINE!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-13-2007, 03:41 AM
deusprogrammer's Avatar   
deusprogrammer deusprogrammer is offline
Newbie
 
Join Date: Mar 2007
Posts: 4
Rep Power: 0
deusprogrammer is on a distinguished road
Send a message via AIM to deusprogrammer Send a message via MSN to deusprogrammer Send a message via Yahoo to deusprogrammer
Default Is there a doctor in the house?

Okay, is there any information I can give you that will get me a response from anyone? I refuse to believe there is no one here with an answer to my problem =(.
__________________

SUPER MARIO SUNSHINE!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Project: ionFiles - Joomla Simple File Download Jordan Community Projects 331 11-26-2008 12:35 PM
Project: ionFiles - Joomla Simple File Download - Mirror 2 Jordan ionFiles 6 11-06-2008 01:24 PM
Windows XP Tricks & Tips!!!!..new ones. pranky Tutorials 9 08-23-2008 04:22 PM
text file manipulations in vb6.0 Ronin_paes Visual Basic Programming 3 06-11-2007 05:54 AM
Creating a Custom Cursor ahsan16 Tutorials 2 01-13-2007 06:03 PM


All times are GMT -5. The time now is 05:44 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads