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.

Beginners > The pernproject "Scrolling both ways the not so easy wa

#15171 - alek - Sun Jan 18, 2004 3:05 pm

I've been looking at the code for scrolling backgrounds larger than 512x512 in Mode0 found in the pernproject example "Scrolling both ways the not so easy way" and I have not been able to figure out a few things. I've been looking at this code a long time and the map viewer in VisualBoy Advance. I sent a email to dovoto but he hasn't answered so I hope you guys can give me a hand. So the questions I have are:

1. Why do you have to AND the x and y vule with 31?

Code:

for(loopx = 0; loopx < 31; loopx++)
{
//this is were the data from our map editor is copied to video memory
bgMap[((loopx+x)&31) + (y & 31) * 32] = data[(loopx+x) + (y) * dataw];
}


2. How does the oldy thing work?

Code:

static int oldy = -1;
//do we need to copy in new data?   
if(y == oldy)
return(0);
oldy = y;

#15176 - LOst? - Sun Jan 18, 2004 4:50 pm

alek wrote:
I've been looking at the code for scrolling backgrounds larger than 512x512 in Mode0 found in the pernproject example "Scrolling both ways the not so easy way" and I have not been able to figure out a few things. I've been looking at this code a long time and the map viewer in VisualBoy Advance. I sent a email to dovoto but he hasn't answered so I hope you guys can give me a hand. So the questions I have are:

1. Why do you have to AND the x and y vule with 31?

Code:

for(loopx = 0; loopx < 31; loopx++)
{
//this is were the data from our map editor is copied to video memory
bgMap[((loopx+x)&31) + (y & 31) * 32] = data[(loopx+x) + (y) * dataw];
}



ANDing the x anf y value with 31 will make x and y loop between 0 and 31.

So if x is 32, then x = 0. And if x is 33, then x = 1.

#15179 - poslundc - Sun Jan 18, 2004 4:59 pm

alek wrote:
2. How does the oldy thing work?


I haven't looked at the Pern code, but my guess is that it's checking to see if the value for your y-position has changed from one frame to the next, so that it will only update if it needs to.

This works because each frame oldy will contain the value of y from the previous frame. So if the current value of y is the same as the value of y from the previous frame, then no update is necessary. (Otherwise, y has changed from the last frame, so we need to update the map accordingly.)

Dan.

#15180 - YopYop - Sun Jan 18, 2004 5:08 pm

(x & 31) is equal to x%32
you can replace %nb by &(nb-1) if nb is a 2 powered number


static int oldy = -1; // static means that oldy is initialized to -1 only in the first function call (it's like a global variable for the compilo)

//do we need to copy in new data?
if(y == oldy) //for all function call he verify y
return(0); //if y has not change he have nothing to do
oldy = y; //but if it chang he update oldy

//here he may copy in the new data

I hope it will help you

#15183 - alek - Sun Jan 18, 2004 5:43 pm

So thats what static does. Thanks for the help.

Lostr:
Quote:
ANDing the x anf y value with 31 will make x and y loop between 0 and 31.

So if x is 32, then x = 0. And if x is 33, then x = 1.

I hade figured that much out, why do you have to do it?

#15184 - LOst? - Sun Jan 18, 2004 6:10 pm

alek wrote:
So thats what static does. Thanks for the help.

Lostr:
Quote:
ANDing the x anf y value with 31 will make x and y loop between 0 and 31.

So if x is 32, then x = 0. And if x is 33, then x = 1.

I hade figured that much out, why do you have to do it?


You don't need to copy more than 32 tiles on the bg. The rest of the bg will not be visible.

#15185 - alek - Sun Jan 18, 2004 6:17 pm

Thanks, now I can go on with my little project =)

#15189 - poslundc - Sun Jan 18, 2004 7:01 pm

alek wrote:
So thats what static does. Thanks for the help.


Actually, it depends on where the static keyword is used.

If a variable inside a function is declared as static, it means that the variable's value is retained from one function call to the next. It's like using a global variable, except the scope of the variable is still limited to the function.

If a global variable or function is declared as static, it is an indicator to the compiler that the variable/function is for internal use only (ie. only accessible by functions in the same C file) and should not be made available to the linker.

It's one of the wackier little quirks of C syntax that has evolved over the years...

Dan.

#15193 - alek - Sun Jan 18, 2004 8:39 pm

Appriciate all the help, it has really helped alot.