gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > weird problem with loop

#23225 - Darmstadium - Wed Jul 07, 2004 10:19 pm

i'm having a very very weird problem. First I will explain a little about what's going on in my demo. In it you move along the level and there are various objects that your character can collide with, that is he can't walk through them. There is a struct called mobj, which holds information about an object that you can collide with and the sprite that is the object's visual representation. In this struct are the x and y coords of the sprite and the number of the sprite (0-127). Anyway, the collsion system isn't really important in my problem. The point is I need to update the actual sprite's coords with the ones in it's mobj struct. I have an array of 128 mobj structs called mobjs. I try to loop through them all like this:
Code:

for (int loop = 0; loop < 128; loop++)
{
     sprites[mobjs[loop].sprite_number].attribute0 &= 0xFE00;
     sprites[mobjs[loop].sprite_number].attribute0 |= mobjs[loop].y;
     sprites[mobjs[loop].sprite_number].attribute1 &= 0xFF00;
     sprites[mobjs[loop].sprite_number].attribute1 |= mobjs[loop].x;
}


But that doesn't work. In my demo, I have a mobj for testing and a sprite, but the sprite's coords are totally unaffected. The test mobj is mobjs[0]. I tried replacing the loop in my code with this
Code:

sprites[mobjs[0].sprite_number].attribute0 &= 0xFE00;
sprites[mobjs[0].sprite_number].attribute0 |= mobjs[0].y;
sprites[mobjs[0].sprite_number].attribute1 &= 0xFF00;
sprites[mobjs[0].sprite_number].attribute1 |= mobjs[0].x;

and the coordinates are updated correctly. I have no idea how this could be possible.

Thanks for helping!

#23226 - poslundc - Wed Jul 07, 2004 10:38 pm

Trace your program, and you'll see the problem. Here's a hint:

Iteration 0:

loop == 0
mobjs[0].sprite_number == 0
effective code (sample line):
sprites[0].attribute0 |= mobjs[0].y

Iteration 1:

loop == 1
mobjs[1].sprite_number == 0 (assuming this is an unused sprite)
effective code (sample line):
sprites[0].attribute0 |= mobjs[1].y


... See the problem?

Dan.

#23227 - Darmstadium - Wed Jul 07, 2004 10:44 pm

ah, of course! thanks man

#23516 - ProblemBaby - Wed Jul 14, 2004 4:40 pm

another problem I see is if you have big values in y, x you will got problems too. because you AND them wrong

Code:

     sprites[mobjs[loop].sprite_number].attribute0 &= 0xFE00;
     sprites[mobjs[loop].sprite_number].attribute0 |= mobjs[loop].y;
     sprites[mobjs[loop].sprite_number].attribute1 &= 0xFF00;
     sprites[mobjs[loop].sprite_number].attribute1 |= mobjs[loop].x;


this should be changed to

Code:

     sprites[mobjs[loop].sprite_number].attribute0 &= 0xFF00;
     sprites[mobjs[loop].sprite_number].attribute0 |= mobjs[loop].y;
     sprites[mobjs[loop].sprite_number].attribute1 &= 0xFE00;
     sprites[mobjs[loop].sprite_number].attribute1 |= mobjs[loop].x;


or am I wrong?

#23520 - Abscissa - Wed Jul 14, 2004 5:04 pm

ProblemBaby wrote:

or am I wrong?


You're correct. The Y coords take 8-bits, but the X coords need 9-bits (since the screen is wider than it is tall). ANDing with 0xFF00 knocks off the 8 high bits, leaving the low 8 for Y. ANDing with 0xFE00 only knocks off the high 7 bits, leaving 9 for X.