Sign in to follow this  
Krynnish Conspiracy

Problem with animations

Recommended Posts

I am trying to create a function to store animations in structures pointing to each other. Opens BMPs, and stores them in malloc assigned areas for easy access. But the problem is that after the first BMP is loaded and placed, the second does not read. The program just seems to forget what it was doing. If I break the compiler in this part, it says that the program is reading the pixels from the BMP file. But it shouldn't take so long, so I figured something is wrong. If anyone can check out the code, I would be eternally thankful.
int ReadBMP(char *filename, BMP *bitamp, int UpdatePallete = 1)
/* Reads the bmp named filename to the struct Page pointed by bitamp.
   If UpdatePallete is TRUE, it updates the color pallete according to
   how it is defined in the BMP header.
*/
{
FILE *fp;
long i;
int x, j, skip;

if((fp = fopen(filename, "rb")) == NULL)
	return E_NULL;

//Read the header, minus the color pallete.
fread(bitamp, sizeof(BMP)-sizeof(Page), 1, fp);


//If TRUE, update the color pallete.
if(UpdatePallete)
	{
	for(j = 0; j < 256; j++)
		{
		Pall[j][2] = fgetc(fp) >> 2;
		Pall[j][1] = fgetc(fp) >> 2;
		Pall[j][0] = fgetc(fp) >> 2;
		fseek(fp, sizeof(byte), SEEK_CUR);
		SetPal(j, Pall[j][0], Pall[j][1], Pall[j][2]);
		}
	}
else
	{
	for(j = 0; j < 256; j++)
		fseek(fp, sizeof(byte)*4, SEEK_CUR);
	}

CreateVP(bitamp->width, bitamp->height, &bitamp->data);

skip =4-(bitamp->width%4);  //How many cells to skip.
for(i=(bitamp->height-1)*bitamp->width; i >= 0; i -= bitamp->width)
	{
	for(x = 0; x < bitamp->width; x++)
		bitamp->data.Data[(word)i+x] = (byte)fgetc(fp);
	//Skip the padding:
	if(skip)
		fseek(fp, skip*sizeof(byte), SEEK_CUR);
	}
fclose(fp);

return SUCCESS;

}

/////////////////

void Animate(Animation *anim, int num, char st[], char st2[], ...)
/* This function creates a chain of Animation structures pointing to each
   other. anim is the Animation-type structure which represents the FIRST
   in the chain. num is the number of animation anim will contain, st[]
   and st2[] are the first two file names of the BMPs to load, and any
   other optional parameters are also names of BMPs to load into the
   animation.
*/
{
va_list ap;
Animation *anim1, *anim2;
BMP *pic;
char *string;


//Loading the first 2 pictures and making them point to each other.
//These two are a must. More pictures are optional.
pic = (BMP *)malloc(sizeof(BMP));
ReadBMP(st, pic);
anim1 = (Animation *)malloc(sizeof(Animation));
if(pic == NULL)
	{
	SetTextMode();
	printf("Error: not enough memory");
	getch();
	exit(1);
	}
anim->prev = NULL;
anim->pic = pic;
anim->next = anim1;

pic = (BMP *)malloc(sizeof(BMP));
if(pic == NULL)
	{
	SetTextMode();
	printf("Error: not enough memory");
	getch();
	exit(1);
	}
ReadBMP(st2, pic);
anim1->pic = pic;
anim1->next = NULL;
anim1->prev = anim;

va_start(ap, st2);

for(num-=2;num>0;num--)
	{
	string = va_arg(ap, char *);
	pic = (BMP *)malloc(sizeof(BMP));
	if(pic == NULL)
		{
		SetTextMode();
		printf("Error: not enough memory");
		getch();
		exit(1);
		}
	ReadBMP(string, pic);
	anim2 = (Animation *)malloc(sizeof(Animation));
	anim1->next = anim2;
	anim2->prev = anim1;
	anim2->pic = pic;
	anim2->next = NULL;
	anim1 = anim2;
	}

va_end(ap);
}


BTW, I tried to check Michael Abrash's Black Book, but all the pdf files were downloaded as HTML instead of PDF, creating a mass of gibberish the Adobe Acrobat won't read, so it's uselss. [Edited by - Krynnish Conspiracy on March 27, 2005 4:03:28 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this