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 > Probably a silly mistake...

#39833 - Ultima2876 - Tue Apr 12, 2005 8:18 pm

Code:
void scansep(void)
   {
      if (hspeed[REG_VCOUNT] == 0)
         {
            hspeed[REG_VCOUNT] = random(2, 6);
         }
      hoffset[REG_VCOUNT] += hspeed[REG_VCOUNT];
      if (hoffset[REG_VCOUNT] > 256)
         {
            hoffset[REG_VCOUNT] = 0;
            hspeed[REG_VCOUNT] = 0;
         }
      REG_BG0HOFS = hoffset[REG_VCOUNT];
   }


This is my Hblank function. It works fine, apart from the fact that the hoffset seems to be resetting prematurely - the effect I'm trying to achieve is one of stars flying by at (pseudo)random speeds. Once a star goes a bit offscreen (to the left - hoffset > 256) the hoffset is reset and the speed of the "new" star is calculated.

All the REG_VCOUNT stuff is to have it so that every scanline retains its own properties (offset and speed). I have the hoffset and hspeed arrays set up with 256 elements each (I'm going to clear this up once my code is done - if I had too few elements it would cause problems but too many is easy to sort out later).

The result is that the starts scroll fine, every looks great, until stars start randomly disappearing (their hoffset is being reset to 0). I've been staring at this code for hours and really can't see what's wrong. Other relevant code bits:

RNG function:

Code:
s32 random(s32 min, s32 max)
      {
         seed *= 69069;
         return min + ((max - min + 1) * (seed >> 16)) / 0x10000;
      }


Variables:

Code:
u16 hoffset[256] = { 0, };
u8 hspeed[256] = { 0, };
u32 seed = 122;


If you need screenshots, just ask and I'll grab some. Thanks.

#39834 - poslundc - Tue Apr 12, 2005 8:28 pm

Don't really have time to look at your code indepth, but try pre-calculating your settings for each scanline, storing them in an array, and then just copying the values out during HBlank. This can often make the difference for a number of reasons.

Dan.

#39901 - Cearn - Wed Apr 13, 2005 10:26 am

The HBlank is only 272 cycles long; when I try the function no$gba gives me 338 for your function. It depends on compiler flags, code-base, section etc, but chances are the function takes just a leetle too long so REG_VCOUNT might have changed somewhere in the middle of the function, which would be bad. A quick remedy would be to have a temp variable read REG_VCOUNT at the start, but in the end poslundc' suggestion is probably preferable.

#39902 - Ultima2876 - Wed Apr 13, 2005 10:39 am

Excellent, I thought it'd be something like this. That explains it perfectly. Thanks so much, both of you.