Gameboy Development Forum

Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)

You are not logged in.

Ads

#1 2022-10-23 09:34:37

1Dr490n
New member
Registered: 2022-10-23
Posts: 2

0 > 0???

I'm trying to make my first GameBoy game. I have the variable pvx (player's x velocity). When I print pvx > 0, the result is 28673 (which I think is true), although pvx == 0. I have this issue, where comparisons don't work correctly and some other point in the code too. Anyone has an idea what the problem could be and how to fix it?

Edit: If I cast pvx to a char, everything works. But still, why??

Last edited by 1Dr490n (2022-10-23 09:36:08)

Offline

 

#2 2022-10-25 12:11:30

nitro2k01
Administrator
Registered: 2008-02-22
Posts: 242

Re: 0 > 0???

In c, zero is false and any non-zero value is true in an if statement, but a comparison like pvx > 0 should normally always be exactly 1 if true.

I couldn't tell you exactly why it happens, but note that 28673==0x7001 in hexadecimal. One possibility is that the code somehow set the lower half of a 16-byte register to 1 for true, and left the upper half untouched so it contains whatever junk was in that register previously. However, this still shouldn't happen. I tried reproducing it and couldn't.

Could you post some of your code?

Are you using GBDK-2020 or some older version. Upgrading to GBDK-2020 is probably recommended for both this reason (in case that fixes it) and other reasons.


Blog: Gameboy Genius
"A journey of a thousand miles begins with one small step"
Old Chinese Proverb

Offline

 

#3 2022-10-29 02:36:55

bbbbbr
Member
Registered: 2019-03-04
Posts: 124

Re: 0 > 0???

If you are using GBDK-2020 and printf (or anything else which takes a variable number of arguments), then the info below applies and may be related. In particular if you are using an 8 bit type and it is not explicitly cast then it may get re-cast to a different variable type (without conversion) when making a variadic function call.

https://gbdk-2020.github.io/gbdk-2020/d … 4cde306fea

Warning: to correctly pass parameters (such as chars, ints, etc) all of them should always be explicitly cast as when calling the function. See docs_chars_varargs for more details.


https://gbdk-2020.github.io/gbdk-2020/d … rs_varargs

Parameters (chars, ints, etc) to printf / sprintf should always be explicitly cast to avoid type related parameter passing issues.

For example, below will result in the likely unintended output:

Code:

sprintf(str_temp, "%u, %d, %x\n", UINT16_MAX, INT16_MIN, UINT16_MAX);
printf("%s",str_temp);
 
// Will output: "65535, 0, 8000"

Instead this will give the intended output:

Code:

sprintf(str_temp, "%u, %d, %x\n", (uint16_t)UINT16_MAX, (int16_t)INT16_MIN, (uint16_t)UINT16_MAX);
printf("%s",str_temp);
 
// Will output: "65535, -32768, FFFF"

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson