Welcome! Log In Create A New Profile

Advanced

declaring an array problem

Posted by g_man 
Re: declaring an array problem
November 11, 2010 07:03PM
fixed the < problem
Re: declaring an array problem
November 11, 2010 10:56PM
Hey g_man, it would really help if you uploaded your code somewhere so I can see the whole thing in context; the smallest discrepancy can make the biggest difference. For example, you typed:
int totalLevels = 5;
char buffer[totalLevels * sizeof(ftImage)];

that's a big difference from this:
const int totalLevels = 5;
char buffer[totalLevels * sizeof(ftImage)];

The former isn't even allowed technically, but is supported in newer standards. It's called a variable-length automatic array. See here:
[gcc.gnu.org]

It automatically dynamically allocates memory for you then cleans it up when the current scope is exited, but remember, the whole point of this exercise was to avoid using heap memory, so that const makes a big difference. Statically allocated memory has to have a size that is compile-time constant. Also, I assume you are putting everything inside a function just for testing purposes, but you should be aware that your fixed-length array will be allocated on the stack unless you precede it with the static keyword:
// inside function
static const int totalLevels = 5;
static char buffer[totalLevels * sizeof(ftImage)];

So, if indeed you are using static memory, then I have no idea why it's not working. As a last resort, try something like this:
const int totalLevels = 5;
ftImage lvl1txt(640,480);
ftImage lvl2txt(640,480);
ftImage lvl3txt(640,480);
ftImage lvl4txt(640,480);
ftImage lvl5txt(640,480);
ftImage *levelNumTxt[] = {&lvl1txt, &lvl2txt, &lvl3txt, &lvl4txt, &lvl5txt};

void Init()
{
  for (int i = 0; i < totalLevels; ++i)
  {
    levelNumTxt->setFont(hyperspd_ttf, hyperspd_ttf_size);
    ...
  }
}
Re: declaring an array problem
November 11, 2010 11:13PM
That didn't change anything. :(

Here is my entire code for that function:
void displayLevelList(GameWindow *gwd){
	
	ir_t ir;
	
	Image baseButtonaImg;
	Image baseButtonbImg;
	Sprite baseButton[30];
	LayerManager	manager(40);
	Image buttonPlusImg;
	Image buttonMinusImg;
	Sprite buttonPlus;
	Sprite buttonMinus;
	
	Rectangle *collisionRect;
	
	Image ptrImg;
	Sprite ptrSpr;
	
	const int totalLevels =5;
	int i;
	Sprite levelNumTxtSpr[12];
	
	ftImage lvl1txt(640,480);
	ftImage lvl2txt(640,480);
	ftImage lvl3txt(640,480);
	ftImage lvl4txt(640,480);
	ftImage lvl5txt(640,480);
	ftImage *levelNumTxt[] = {&lvl1txt, &lvl2txt, &lvl3txt, &lvl4txt, &lvl5txt};
	
	ptrImg.LoadImage(irimage1_png);
	ptrSpr.SetImage(&ptrImg);

	
	baseButtonaImg.LoadImage(lvlbtna_png);
	baseButtonbImg.LoadImage(lvlbtnb_png);
	
	
	for(i=0;i.SetImage(&baseButtonaImg);
 		if(i<12){
 			baseButton.SetPosition(100+(i%4)*114,100+floor(i/4)*133);
 			//manager.Append(&baseButton);
 		}	
 	}	
 	
 	buttonPlusImg.LoadImage(btnPlus_png);
 	buttonPlus.SetImage(&buttonPlusImg);
 	buttonPlus.SetPosition(550,243);
 	manager.Append(&buttonPlus);
 	
 	buttonMinusImg.LoadImage(btnMinus_png);
 	buttonMinus.SetImage(&buttonMinusImg);
 	buttonMinus.SetPosition(10,243);
 	manager.Append(&buttonMinus);
 	
 	for(i=0;i<totalLevels&&i<12;i++){
 		levelNumTxt->setFont(hyperspd_ttf, hyperspd_ttf_size);
		levelNumTxt->setSize(50);
		levelNumTxt->setColor(Color::Color(0,0,0));
		levelNumTxtSpr.SetPosition(100+floor(i/3)*114,100+(i%3)*133);
		levelNumTxtSpr.SetImage(levelNumTxt);
		levelNumTxt->printf("1");
		manager.Append(&levelNumTxtSpr);
	}
		
	while(1){
	
		WPAD_IR(0,&ir);
	
		ptrSpr.SetPosition(ir.x-8,ir.y);
		
		for(i=0;i.GetCollisionRectangle();
 			if((ir.x>collisionRect->x&&ir.x<(collisionRect->x+collisionRect->width))&&(ir.y>collisionRect->y&&ir.y<(collisionRect->y+collisionRect->height))){
				baseButton.SetImage(&baseButtonbImg);
			} else {
				baseButton.SetImage(&baseButtonaImg);
			}
		}
		
		for(i=0;i->printf("1");
			levelNumTxt->flush();
		}	

		manager.Draw(0,0);
		
		for(i=0;i->clear();
			levelNumTxt->reset();
		}

		gwd->Flush();
	}
}	
This code should display "1" 5 times, but it doesn't.
Thank you for your continued support
Re: declaring an array problem
November 12, 2010 12:11AM
Quote
calvinss4
next see if it works with two objects:
ftImage test1(640,480);
test1.setFont(hyperspd_ttf, hyperspd_ttf_size);
ftImage test2(640,480);
test2.setFont(hyperspd_ttf, hyperspd_ttf_size);

Quote
g_man
Ok I did that and it worked. Two different ftImages worked fine.

We seem to be going in circles here. So it works for 2 objects, but not 5?
- Try it again with just two objects. What happens?
- If that doesn't work, then try calling setFont on each object manually outside of the for loop. What happens?
- Either way, this is some really weird behavior man.
Re: declaring an array problem
November 12, 2010 03:07AM
Ok, I'm really mad at myself now. The reason nothing was showing up was because the text color was set to black, the same as the background.

On another note, thank you for helping me throughout this problem. I appreciate it.

EDIT: I was wondering if you could help me with one more unrelated problem. As you can see from this picture:

the numbers don't line up. I was wondering if someone knew how to find the width of the numbers.



Edited 1 time(s). Last edit at 11/12/2010 03:39AM by g_man.
Sorry, only registered users may post in this forum.

Click here to login