Forgotten array

Started by
6 comments, last by Muzlack 22 years, 2 months ago
Strange problem... THis is linked with another problem I posted earlier. A 2 dimensional array is forgotten in a method. Let me show you the code.
      
typedef struct tagSPRITE {
	int x,y;
	int width,height;
	RECT frames[100];
	int counter;
	int updatecounter;
	int state;
	int curframe;
	int curanim;
	int *anims[20]; //THIS ONE

	int curanimframes;
	int indexinanim;
	int runthroughs;
	LPDIRECTDRAWSURFACE7 surf;
} SPRITE, *SPRITEPTR;
  
An here is the method I use to assign the animation.
  
void MakeAnimation(SPRITEPTR sprite, int animindex, int numframes, int *anim) {
	sprite->anims[animindex]=(int *)malloc((numframes+1)*sizeof(int));
	for(int i=0; ianims[animindex]=anim[z];
	}
	sprite->anims[animindex]=anim;
	sprite->curanimframes=numframes;
	sprite->indexinanim=0;
}
  
Now, here, I use this method.
  
enemy MakeBoss2 () {
	enemy me;
	CreateSprite(&me.ship,100,100,"bosses.bmp"); //load from enemy.bmp

	me.bullet.curframe=0;
	CFFromCell(&me.ship,0,2,0); //4-5 are from 1,0 in enemy.bmp

	CFFromCell(&me.ship,1,3,0);
	CFFromCell(&me.ship,2,2,1);
	CFFromCell(&me.ship,3,3,1);
	CFFromCell(&me.ship,4,0,3);
	CFFromCell(&me.ship,5,1,3);
	CFFromCell(&me.ship,6,2,3);
	CFFromCell(&me.ship,7,3,3);
	int explodea2[] = {4,5,6,7};
	CreateSprite(&me.bullet,2,17,"fire.bmp");
	CFFromCell(&me.bullet,0,0,0);
	me.isthere=true;
	me.curstate=attack;
	me.oldstate=attack;
	me.state.states[attack]=.30;
	me.state.states[avoid]=.35;
	me.state.states[randommove]=.4;
	me.state.states[useold]=.9;
	me.state.states[still]=1.0;		
	me.damage=2;
	me.bulletspeed=18;
	me.howmuchscore=1000;
	me.isbullet=false;
	me.explodeframes=3;
	MakeAnimation(&me.ship,0,1,fly);
	MakeAnimation(&me.ship,1,1,zapa);
	MakeAnimation(&me.ship,2,3,explodea2);
	me.ship.updatecounter=2;
	ChangeAnim(&me.ship,0,1);
	me.engspeed=4;
	Load_VOC("explode.voc",&me.explodesnd);
	Load_VOC("l1.voc", &me.shootsnd);
	me.shootsnd.lpdsbuffer->SetFrequency(22050);
	me.curhp=me.fullhp=20;
	Load_VOC("mis.voc", &me.shootsnd);

	return (me);
}
  
Now, from writing to an output file, I have determined that the makeanimations DO work here. It returns the enemy ship. Then:
  
if(enemies[i].ship.runthroughs==1) {
				FILE* fp=fopen("debug.txT", "w"); fprintf(fp,"%d",enemies[i].ship.anims[2][0]);fclose(fp);
				enemies[i].isthere=false;
				score+=enemies[i].howmuchscore;
				kills++;
				UpdateKills();
			}
  
Here, I print this information again, and the Animation has been forgotten. it is -8590000 or something like that. I know that the enemy has already been initialized when this code comes around... Does anyone know what is going on?!?!? Thanks in advance! Sponge Factory --Muzlack Edited by - Muzlack on February 23, 2002 7:54:39 PM Edited by - Muzlack on February 23, 2002 7:55:56 PM
--Muzlack
Advertisement
sprite->anims[animindex]=(int *)malloc((numframes+1)*sizeof(int));...sprite->anims[animindex]=anim; 


Memory leak. And you are missing code in your for loop.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
So what do I do to repair this?

Sponge Factory
--Muzlack
--Muzlack
Ooops... I tried 2 ways to accomplish this, and forgot to edit some of this out. both ways don''t work.
--Muzlack
malloc() gave you a pointer, you happily throw it away to store anim. I think you wanted to copy the contents of of *anim (e.g. an array) onto the memory you allocated. To do that have a look at memcpy().
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Sorry, my c book doesn''t cover any of this stuff, and I got the basis of what I need to know for games down, but this still doesn''t work. Here''s my code:
void MakeAnimation(SPRITEPTR sprite, int animindex, int numframes, int *anim) {
sprite->anims[animindex]=(int *)malloc((numframes+1)*sizeof(int));
memcpy(sprite->anims[animindex],anim,sizeof(int)*(numframes+1));
sprite->curanimframes=numframes;
sprite->indexinanim=0;
}
No, this blinks, we''re gettin there.
--Muzlack
Wait a sec... Now it works, thanks!

Sponge Factory
--Muzlack
--Muzlack
I may have another problem that is probably a memory leak that I just realized. I have no clue how to fix it, but how I load my bitmaps is I use the dsutil DDLoadBitmap() function. But now, after a certain amount of kills (probably from loading and reloading) The enemy firing bitmap stops loading correctly. I have tested to see if the coordinates on the bitmap are still correct, and they are, I''m guessing that there is no image to draw when it tries to draw the image. I don''t have a clue whats going on here, and I know no one here has time to sift through 1300 lines of code to help me with my problem. Is there a guess on what I should do to fix the problem? Oh yeah, the new code has nothing to do with this problem.

Sponge Factory
--Muzlack
--Muzlack

This topic is closed to new replies.

Advertisement