Weird bug

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Mortimus
Posts: 7
Joined: Fri Jan 06, 2006 3:32 pm

Weird bug

Post by Mortimus »

When I run this code, the 2nd do while loop, wont show the month from the first do while, instead it just has it act exactly the same as the day variable, and change with each button press with the day. my print function prints the string at x, y using the font image i have.

Code: Select all

	do{
		ClearScreen(0x000000);
		print("What is your Birthday?", 20, 40);
		sceCtrlReadBufferPositive(&pad, 1);
		if&#40;i<10&#41;&#123;
			month&#91;0&#93; = 32;
			month&#91;1&#93; = i+48;
		&#125;else&#123;
			month&#91;0&#93; = 49;
			month&#91;1&#93; = i+38;
		&#125;
		//month = char&#40;i+49&#41;;
		if &#40;pad.Buttons & PSP_CTRL_DOWN&#41;&#123;
			if&#40;i<12&#41;&#123;
				i++;
			&#125;
		&#125;
		if &#40;pad.Buttons & PSP_CTRL_UP&#41;&#123;
			if&#40;i>1&#41;&#123;
				i--;
			&#125;
		&#125;
		while&#40;&#40;pad.Buttons & PSP_CTRL_DOWN&#41; || &#40;pad.Buttons & PSP_CTRL_UP&#41;&#41;&#123;
			sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
		&#125;
		print&#40;month, 20, 60&#41;;
		SwapBuffers&#40;&#41;;
	&#125;while&#40;!&#40;pad.Buttons & PSP_CTRL_CIRCLE&#41;&#41;;

	while&#40;pad.Buttons & PSP_CTRL_CIRCLE&#41;&#123;
		sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
	&#125; // make sure button is depressed

	do&#123;
		ClearScreen&#40;0x000000&#41;;
		print&#40;"What is your Birthday?", 20, 40&#41;;
		print&#40;month, 20, 60&#41;;
		sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
		if&#40;q<10&#41;&#123;
			day&#91;0&#93; = 32;
			day&#91;1&#93; = q+48;
		&#125;else&#123;
			day&#91;0&#93; = 48+&#40;q/10&#41;;
			day&#91;1&#93; = &#40;q%10&#41;+48;
		&#125;
		if &#40;pad.Buttons & PSP_CTRL_DOWN&#41;&#123;
			if&#40;q<31&#41;&#123;
				q++;
			&#125;
		&#125;
		if &#40;pad.Buttons & PSP_CTRL_UP&#41;&#123;
			if&#40;q>1&#41;&#123;
				q--;
			&#125;
		&#125;
		while&#40;&#40;pad.Buttons & PSP_CTRL_DOWN&#41; || &#40;pad.Buttons & PSP_CTRL_UP&#41;&#41;&#123;
			sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
		&#125;
		print&#40;day, 70, 60&#41;;
		SwapBuffers&#40;&#41;;
	&#125;while&#40;!&#40;pad.Buttons & PSP_CTRL_CIRCLE&#41;&#41;;
~ Mortimus ~
User avatar
Nige
Posts: 10
Joined: Thu Sep 29, 2005 10:30 am
Contact:

Post by Nige »

I've not run your code, but it'd be safer to use the C string functions, like:

sprintf(month, "%i", i)

instead of trying to directly manipulate a char array. This also ensures your string is zero-terminated (as long as 'month' is defined as char[3] or larger).

Personally I'd rework your loops so that the input side is split slightly from the display/calculation side, so you'd have

1. Display current month
2. Get input
3. Change month depending on input
4. Loop

instead of at the moment where you're reading inputs in the middle of displaying your screen information, but that's just a personal style I guess.
CPCPSP [http://nige.the-pub.org/cpcpsp/] an Amstrad CPC emulator for the PSP
Mortimus
Posts: 7
Joined: Fri Jan 06, 2006 3:32 pm

y i use my print

Post by Mortimus »

im using my print b/c it using a bitmap font, and displays over what I have, its not like the printf that displays debug info.
~ Mortimus ~
User avatar
nullp01nter
Posts: 26
Joined: Wed Jan 04, 2006 7:40 am
Location: Saxony/Germany

Post by nullp01nter »

im using my print b/c it using a bitmap font, and displays over what I have, its not like the printf that displays debug info.
This might be true, but what Nige suggested is not to prepare your string with that somehow ugly "if(i<10){month[0] = 32; month[1] = i+48; }else{month[0] = 49; month[1] = i+38;}" but with something more elegant. The recommended "sprintf" does not print to your screen, instead it outputs to the string buffer you specified. This way you are free to use all the printf features such as string output, integer, hex, floating point etc. and feed it to your own nifty text function. I don't know how your knowledge about printf is, but to clarify Nige's example: sprintf(month, "%i", i); fills your buffer "month" (which must be large enough) with the integer textual equivalent of what is inside your variable "i".

Thoralt
Mortimus
Posts: 7
Joined: Fri Jan 06, 2006 3:32 pm

oh thanx

Post by Mortimus »

oh thanx, i never have used printf or sprintf before programming for psp, i didn't know that.
~ Mortimus ~
Mortimus
Posts: 7
Joined: Fri Jan 06, 2006 3:32 pm

ok using that

Post by Mortimus »

ok using that method, i don't need that stupid if then for first and 2nd digit.
The first set works fine (the month), but then when it gets to the day, it messes up still, it shows where month should be as "%" and the day as "%" and it doesnt change with button input.
~ Mortimus ~
User avatar
nullp01nter
Posts: 26
Joined: Wed Jan 04, 2006 7:40 am
Location: Saxony/Germany

Post by nullp01nter »

To find out more about printf and what types of data you can output with it you may want to have a look at http://en.wikipedia.org/wiki/Printf - they have a quite good summary there.

Thoralt
Post Reply