sceRtcGetCurrentTick u64 and u32 chaos :(

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

Moderators: cheriff, TyRaNiD

Post Reply
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

sceRtcGetCurrentTick u64 and u32 chaos :(

Post by Ghoti »

Hi folks,

I try to get the current tickcount to display the time you have played the current game. the problem is that it works with u64 and u32 variables. How can i convert them into an int which i can use for the sprintf (or a function i will create to display the seconds as a time.) What can i do?

Code: Select all

int iResult;
int iResultTime;
iResult = sceRtcGetCurrentTick(&iTimeNow);
		iResultTime = (iTimeNow - iTimeStart) / sceRtcGetTickResolution();
		sprintf(sCurrentTime, "%i", iResultTime); 
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

The strangest thing happens

Code: Select all

int GameMain(void) {
	int iResult;
	int iResultTime;

	//Load everything
	GameLoad();
	iResult = sceRtcGetCurrentTick(&iTimeStart);
	while(1) { 
		

		iResult = sceRtcGetCurrentTick(&iTimeNow);
		iResultTime = (iTimeNow - iTimeStart) / sceRtcGetTickResolution();
		sprintf(sCurrentTime, "%i", iResultTime); 
		// read controls
		GameControls();
		// if pause then don't do anything.
		if (iPause == 0){
			// Handle the quad
			GameHandleQuad();
			// Handle Game events
			GameEvents();
			// Handle the Line
			GameHandleLine();
		}
		// draw game
		GameRender();
return 0;
}
		//
If i don't put in the gettickcount codes to get the the time everything works fine but when i do as the code above there goes alot wrong with the game. Like that instead of 4 critters there are 5 critters. and with this the game crashes at a certain time. why is this code wrong?
Danny769
Posts: 55
Joined: Wed Feb 01, 2006 12:29 pm

Post by Danny769 »

That is odd, i dont see any error, unless im missing some thing small.
could it have some thing to do with any other part of your code?
User avatar
Saotome
Posts: 182
Joined: Sat Apr 03, 2004 3:45 am

Post by Saotome »

That code snippet doesn't help at all.
Is the type of iTimeStart and iTimeNow even u64 (like it should)?

From the SDK (svn):

Code: Select all

int sceRtcGetCurrentTick(u64 *tick);
infj
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

Hi,

well the two vars are declared as u64 and i don't get any compile error's with this but when i use the gettickcount(the two before the sprintf

Code: Select all

//iResult = sceRtcGetCurrentTick(&iTimeNow);
		//iResultTime = (iTimeNow - iTimeStart) / sceRtcGetTickResolution();
then when the following code runs it creates another critter:

Code: Select all

int GameEvents(void){
	if (iQuadPosY == 11){
		if (BlockStatus[1] == 0) {
			iField[iQuadPosX][iQuadPosY] = iCurrentQuad[1];
			BlockStatus[1] = 1;
		}
		if (BlockStatus[2] == 0) {
			iField[iQuadPosX + 1][iQuadPosY] = iCurrentQuad[2];
			BlockStatus[2] = 1;
		}
		if (BlockStatus[3] == 0) {
			iField[iQuadPosX + 1][iQuadPosY + 1] = iCurrentQuad[3];
			BlockStatus[3] = 1;
		}
		if (BlockStatus[4] == 0) {
			iField[iQuadPosX][iQuadPosY + 1] = iCurrentQuad[4];
			BlockStatus[4] = 1;
		}
		CheckSurroundingField(iQuadPosX, iQuadPosY + 1, iCurrentQuad[4]);
		CheckSurroundingField(iQuadPosX, iQuadPosY, iCurrentQuad[1]);
		CheckSurroundingField(iQuadPosX + 1, iQuadPosY + 1, iCurrentQuad[3]);
		CheckSurroundingField(iQuadPosX + 1, iQuadPosY, iCurrentQuad[2]);	
		iControllable = 1;
	}
	else {
	//if (iQuadPosYold + 1 == iQuadPosY){
		if (iControllable == 0) {
			if (iDropSpeed == 40){
				iQuadPosY += 1;
				iDropSpeed = 0;

			}
			else { iDropSpeed += 1;}
		}
		else {
			//if (iDropSpeed == 5){
				iQuadPosY += 1;
			//	iDropSpeed = 0;
				//iTest +=1;

			//}
			//else { iDropSpeed += 1;}
		}
	}



	return 0;
}
the above code checks if the position is 11 and when it is then the code save the temporary blocks and saves them in the field. The problem that occurs only when the tickcount code is added is that instead of 4 blocks that are saved in the field, 5 are saved in the field. When this occurs then in the following function:

Code: Select all

int GameHandleQuad(void){
	
	int iCheck = 0;
	// check block 4 en 1
	if (BlockStatus[4] != 1){
		if (iField[iQuadPosX][iQuadPosY + 1] > 0){
			if &#40;iQuadPosY <= 2&#41;&#123; GameOver&#40;&#41;; &#125;
			iField&#91;iQuadPosX&#93;&#91;iQuadPosY&#93; = iCurrentQuad&#91;4&#93;;
			iField&#91;iQuadPosX&#93;&#91;iQuadPosY - 1&#93; = iCurrentQuad&#91;1&#93;;
			BlockStatus&#91;1&#93; = 1;
			BlockStatus&#91;4&#93; = 1;
			iControllable = 1;
			iCheck += 1;
			CheckSurroundingField&#40;iQuadPosX, iQuadPosY, iCurrentQuad&#91;4&#93;&#41;;
			CheckSurroundingField&#40;iQuadPosX, iQuadPosY - 1, iCurrentQuad&#91;1&#93;&#41;;	
		&#125;	
		else &#123;BlockStatus&#91;4&#93; = 0; BlockStatus&#91;1&#93; = 0;&#125;
	&#125;
	

	// check block 3 en 2
	if &#40;BlockStatus&#91;3&#93; != 1&#41;&#123;
		if &#40;iField&#91;iQuadPosX + 1&#93;&#91;iQuadPosY + 1&#93; > 0&#41;&#123;
			if &#40;iQuadPosY <= 2&#41;&#123; GameOver&#40;&#41;; &#125;
			iField&#91;iQuadPosX + 1&#93;&#91;iQuadPosY&#93; = iCurrentQuad&#91;3&#93;;
			iField&#91;iQuadPosX + 1&#93;&#91;iQuadPosY - 1&#93; = iCurrentQuad&#91;2&#93;;
			BlockStatus&#91;3&#93; = 1;
			BlockStatus&#91;2&#93; = 1;
			iControllable = 1;
			iCheck += 2;
			CheckSurroundingField&#40;iQuadPosX + 1, iQuadPosY, iCurrentQuad&#91;3&#93;&#41;;
			CheckSurroundingField&#40;iQuadPosX + 1, iQuadPosY - 1, iCurrentQuad&#91;2&#93;&#41;;
			
		&#125;
		else &#123;BlockStatus&#91;2&#93; = 0; BlockStatus&#91;3&#93; = 0;&#125;
	&#125;

	if &#40;iCheck != 3 && iCheck != 0&#41;&#123;
		iQuadPosY -= 1;
	&#125;

	if &#40;BlockStatus&#91;3&#93; == 1&#41; &#123;
		if &#40;BlockStatus&#91;2&#93; == 1&#41; &#123;
			if &#40;BlockStatus&#91;1&#93; == 1&#41; &#123;
				if &#40;BlockStatus&#91;4&#93; == 1&#41; &#123;
					BlockStatus&#91;1&#93; = 0;
					BlockStatus&#91;2&#93; = 0;
					BlockStatus&#91;3&#93; = 0;
					BlockStatus&#91;4&#93; = 0;
					GameCreateQuad&#40;&#41;;
				&#125;
			&#125;
		&#125;
	&#125;

	return 0;


&#125;
then not every BlockStatus = 1 however it should be and without the code it is, so it should be the code with the ticks that cause the errors but i do not know why and what. Maybe it is because i calculate with u64 and u32 but save it into a integer?
charliex
Posts: 16
Joined: Thu Jan 26, 2006 4:03 pm

Post by charliex »

is sCurrentTime big enough to receive the whole string ?

you can leave it all as u64 if needed then cast as (int) at the sprintf.
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

hi well i declared my sCurrentTime as:

Code: Select all

char sCurrentTime&#91;100&#93;;
i tried (int) also but the same result. The strangest thing is that i don't see any double vars or so, so i don't know why my gettickcount interferes with my other code. It's a mysterie for me? don't know C very well but can it be something with declaring to much info so my memory fills up and saves over other info? or something in that way?

greets ghoti
User avatar
Raphael
Posts: 646
Joined: Tue Jan 17, 2006 4:54 pm
Location: Germany
Contact:

Post by Raphael »

Well, my compiler always tells me not to use sprintf but only use snprintf for security reasons. You could try that with snprintf(sCurrentTime, 100, "%i", iResultTime);
My guess there is that the 64bit division gives an overflow. You should also try to typecast everything in place, rather than to depend on the automatic typecasting, ie. use
iResultTime = (int)(iTimeNow - iTimeStart) / (int)sceRtcGetTickResolution();
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

:( that doesn't work also :(

isn't there a other way of calculating how many seconds have passed since you started the game?
charliex
Posts: 16
Joined: Thu Jan 26, 2006 4:03 pm

Post by charliex »

i've just been using clock() but that may just call the same functions internally.
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

hi back again.

Well i tried a bit around and i have used another function now which uses seconds (what i need)

the syntax =

Code: Select all

time_t sceKernelLibcTime  &#40;  time_t *  t   &#41; 
i use

Code: Select all

time_t iTimeNow;
sceKernelLibcTime&#40;&iTimeNow&#41;;
this works for the getting the seconds but i get the same errors but now i know which line makes the error. It is the function call. When I // it then the code runs fine without strange things but as soon as i put it back in then my blocks come out all wrong. So maybe i use the function incorrect ( i hope)?
Post Reply